Springmvc整合dubbo 使用学习(dubbo开发中使用到的一些服务配置方式)

353 阅读3分钟

通过之前的学习了解了dubbo的常规的使用,下面我们看看特殊情况或者说真实环境下使用dubbo的一些配置实例。

一、一个接口有多个实现时可以使用group来区分

1、服务提供者配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://code.alibabatech.com/schema/dubbo
  8. http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  9. ">
  10. <!-- 提供方应用信息,用于计算依赖关系,这个和client没必要一致 -->
  11. <dubbo:application name="hello-world-app-my" />
  12. <!-- 使用zookeeper广播注册中心暴露服务地址 -->
  13. <dubbo:registry protocol="zookeeper" address="192.168.0.107:2181"/>
  14. <!-- 用dubbo协议在20880端口暴露服务 -->
  15. <dubbo:protocol name="dubbo" port="20880" />
  16. <!-- 声明需要暴露的服务接口 -->
  17. <dubbo:service group="1" interface="com.test.dubboser.ServiceDemo"
  18. ref="demoService" />
  19. <dubbo:service group="2" interface="com.test.dubboser.ServiceDemo"
  20. ref="demoService2" />
  21. <!--和本地bean一样实现服务 -->
  22. <bean id="demoService" class="com.test.dubboser.ServiceImp"/>
  23. <bean id="demoService2" class="com.test.dubboser.ServiceImp2"/>
  24. </beans>


其他的配置都是一样,而暴露的服务接口通过group来区分两个实现类

2、服务消费者配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://code.alibabatech.com/schema/dubbo
  8. http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  9. ">
  10. <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
  11. <dubbo:application name="consumer-of-helloworld-app-my" />
  12. <!-- 使用zookeeper广播注册中心暴露发现服务地址 -->
  13. <dubbo:registry protocol="zookeeper" address="192.168.0.107:2181" />
  14. <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
  15. <dubbo:reference id="demoServicemy" group="1" interface="com.test.dubboser.ServiceDemo" />
  16. <dubbo:reference id="demoServicemy2" group="2" interface="com.test.dubboser.ServiceDemo" />
  17. </beans>

这里同样使用group来区分

二、当一个接口实现出现不兼容升级时可以用版本号过渡,版本号不同的服务互相间不引用

1、服务提供者配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://code.alibabatech.com/schema/dubbo
  8. http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  9. ">
  10. <!-- 提供方应用信息,用于计算依赖关系,这个和client没必要一致 -->
  11. <dubbo:application name="hello-world-app-my" />
  12. <!-- 使用zookeeper广播注册中心暴露服务地址 -->
  13. <dubbo:registry protocol="zookeeper" address="192.168.0.107:2181"/>
  14. <!-- 用dubbo协议在20880端口暴露服务 -->
  15. <dubbo:protocol name="dubbo" port="20880" />
  16. <!-- 声明需要暴露的服务接口 -->
  17. <dubbo:service version="1" interface="com.test.dubboser.ServiceDemo"
  18. ref="demoService" />
  19. <dubbo:service version="2" interface="com.test.dubboser.ServiceDemo"
  20. ref="demoService2" />
  21. <!--和本地bean一样实现服务 -->
  22. <bean id="demoService" class="com.test.dubboser.ServiceImp"/>
  23. <bean id="demoService2" class="com.test.dubboser.ServiceImp2"/>
  24. </beans>

添加version 来区分

2、服务消费者配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://code.alibabatech.com/schema/dubbo
  8. http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  9. ">
  10. <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
  11. <dubbo:application name="consumer-of-helloworld-app-my" />
  12. <!-- 使用zookeeper广播注册中心暴露发现服务地址 -->
  13. <dubbo:registry protocol="zookeeper" address="192.168.0.107:2181" />
  14. <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
  15. <dubbo:reference id="demoServicemy" version="1" interface="com.test.dubboser.ServiceDemo" />
  16. <dubbo:reference id="demoServicemy2" version="2" interface="com.test.dubboser.ServiceDemo" />
  17. </beans>


服务消费者这边也要使用version 来区分

经典介绍:

源码结构


愿意了解框架技术或者源码的朋友直接求求交流分享技术:2042849237

分布式的一些解决方案,有愿意了解的朋友可以找我们团队探讨

更多详细源码参考来源:minglisoft.cn/technology

dubbo+springmvc+mybatis+ehcache+redis J2ee分布式架构, restful, kafka, shiro