dubbo - integrate spring boot, simple annotation based instance

Keywords: PHP Dubbo Spring Zookeeper github

I. Guide Package:

<dependencies>
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.5.7</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

Among them:

<dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>0.2.0</version>
</dependency>

It is the integration package of spring and dubbo. Do not import this package, configure registryconfig.setclient ("cursor")

@Bean
    public RegistryConfig registryConfig(){
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("zookeeper://127.0.0.1:2181");
        registryConfig.setClient("curator");
        return registryConfig;
    }

An error will occur:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/curator/framework/CuratorFrameworkFactory

2. Remote object: use @ service annotation (com.alibaba.dubbo.config.annotation.Service). The remote interface does not need any annotation.

How to export remote objects

Use the annotation @ DubboComponentScan(basePackages = "com.dr.service") on the configuration class to automatically scan the remote objects under the package (use @ Service annotation). Currently, the client only knows not to use it.

The cursor is wrong
@Configuration
@DubboComponentScan(basePackages = "com.dr.service")
public class DubboConfig {

    @Bean
    public ApplicationConfig applicationConfig(){
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("annotate-dubbo");
        return applicationConfig;
    }

    @Bean
    public RegistryConfig registryConfig(){
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("zookeeper://127.0.0.1:2181");
        registryConfig.setClient("curator");
        return registryConfig;
    }

4. Start. Note that this instance uses zookeeper as the registry, so the zookeeper service must be started first.

@SpringBootApplication
@DubboComponentScan(basePackages="com.dr.service")
public class ProviderApp {

    public static void main(String[] args)throws IOException {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(DubboConfig.class);
        ctx.start();
        System.out.println("Provider start...");

        System.in.read();
    }
}

Consumer end:

Focus on getting remote objects

I. how to obtain remote objects

Use annotation on remote interface: @ Reference,com.alibaba.dubbo.config.annotation.Reference package

Note: @ ComponentScan is used on the Configuration class (with @ Configuration). Its function is to import spring bean s, while @ DubboComponentScan is used to export remote objects on the provider side.

gitHub address: git@github.com:dengrongrong/dubbo-spring-boot.git

Posted by ricoche on Sun, 20 Oct 2019 09:25:06 -0700