dubbo 简单入门注解方式

目录

 

注解配置

服务提供方

Service注解暴露服务

javaconfig形式配置公共模块

指定dubbo扫描路径

服务消费方

Reference注解引用服务

javaconfig形式配置公共模块

指定dubbo扫描路径

扫描二维码关注公众号,回复: 2776723 查看本文章

注解配置

服务提供方

Service注解暴露服务

  • import com.alibaba.dubbo.config.annotation.Service;
     
    @Service(timeout = 5000)
    public class AnnotateServiceImpl implements AnnotateService { 
        // ...
    }
    

javaconfig形式配置公共模块

@Configuration
public class DubboConfiguration {

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

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

指定dubbo扫描路径

@SpringBootApplication
@DubboComponentScan(basePackages = "com.alibaba.dubbo.test.service.impl")
public class ProviderTestApp {
    // ...
}

服务消费方

Reference注解引用服务

public class AnnotationConsumeService {

    @com.alibaba.dubbo.config.annotation.Reference
    public AnnotateService annotateService;
    
    // ...
}

javaconfig形式配置公共模块

@Configuration
public class DubboConfiguration {

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

    @Bean
    public ConsumerConfig consumerConfig() {
        ConsumerConfig consumerConfig = new ConsumerConfig();
        consumerConfig.setTimeout(3000);
        return consumerConfig;
    }

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

指定dubbo扫描路径

@SpringBootApplication
@DubboComponentScan(basePackages = "com.alibaba.dubbo.test.service")
public class ConsumerTestApp {
    // ...
}

猜你喜欢

转载自blog.csdn.net/su717854075/article/details/81222543
今日推荐