SpringBoot框架中使用注解式Dubbo

SpringBoot框架中使用注解式Dubbo

新建三个模块

dubbo-demo-api 通用接口

dubbo-demo-service 服务提供方

dubbo-demo-web 服务消费方

注解配置

需要 2.5.7 及以上版本支持

服务提供方

Service注解暴露服务

  
  import com.alibaba.dubbo.config.annotation.Service;
   
  @Component
  @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;
      }
  }

application.properties形式配置

  
  dubbo.registry.address=zookeeper://192.168.144.130:2181
  dubbo.application.name=wstore-web-sso

指定dubbo扫描路径

注意 @DubboComponentScan(basePackages = "com.wang.test.service.impl") 填写接口实现类的包名

  
  @SpringBootApplication
  @DubboComponentScan(basePackages = "com.wang.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;
      }
  }

application.properties形式配置

  
  dubbo.registry.address=zookeeper://192.168.144.130:2181
  dubbo.application.name=wstore-web-sso

指定dubbo扫描路径

注意 @DubboComponentScan(basePackages = "com.wang.test.service")填写公用接口的包名

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

dubbo-demo-web 中测试能否消费服务

  
  //这里的Service是org.springframework.stereotype.Service
  @Service
  public class TestService {
      
      //服务消费方注解
      @Reference
      UserService userService;
      
      public void test01(){
          WstoreResultMsg user = userService.login(null);
      }
  }

controller中调用

  
  @Controller
  public class LoginController {
  
      @Autowired
      TestService testService;
  
      @GetMapping("/test")
      @ResponseBody
      public String test(){
          testService.test01();
          return "OK";
      }
  }
  

运行项目,浏览器输入localhost:8080/test测试,返回ok即可

总结

1、服务消费方和提供方注解一定正确,注意不同的@Service
2、服务提供方暴露服务时,不要忘记加上Spring容器的@Component注解
3、@DubboComponentScan的包名不要写错

更多坑欢迎访问我的 github主页

你可能感兴趣的:(SpringBoot框架中使用注解式Dubbo)