springboot+dubbo分组+zookeeper集群
provider
修改注册中心服务地址为多个
<dubbo:registry protocol="zookeeper" address="192.168.73.211:2181,192.168.73.212:2181,192.168.73.213:2181" />
同一接口有多个实现使用group区分
同一接口出现版本不兼容升级使用version过度
<dubbo:service interface="com.example.demo.service.DemoService" ref="demoService" group="afternoon" version="1.0.0"/>
provider.xml完整配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 提供者名称 -->
<dubbo:application name="dubbo-provider" />
<!-- zookeeper注册中心的服务地址 -->
<dubbo:registry protocol="zookeeper" address="192.168.73.211:2181,192.168.73.212:2181,192.168.73.213:2181" />
<!-- 用dubbo协议在20880端口暴露服务,协议通信端口 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 用户服务接口 -->
<dubbo:service interface="com.example.demo.service.DemoService" ref="demoService" group="afternoon" version="1.0.0"/>
<dubbo:service interface="com.example.demo.service.DemoService" ref="demoService2" group="morning" version="2.0.0"/>
<!-- 用户服务接口实现 -->
<bean id="demoService" class="com.example.demo.service.impl.DemoServiceImpl"/>
<bean id="demoService2" class="com.example.demo.service.impl.DemoServiceImpl2"/>
</beans>
consumer
修改注册中心地址为多个
<dubbo:registry protocol="zookeeper" address="192.168.73.211:2181,192.168.73.212:2181,192.168.73.213:2181" />
consumer.xml完整代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 服务名 -->`在这里插入代码片`
<dubbo:application name="demo-consumer" />
<!-- zookeeper注册中心暴露服务地址 -->
<dubbo:registry protocol="zookeeper" address="192.168.73.211:2181,192.168.73.212:2181,192.168.73.213:2181" />
<!-- 用户服务接口 -->
<dubbo:reference id="demoService" interface="com.example.demo.service.DemoService" />
<dubbo:annotation package="com.example.demo.controller" />
<!-- 把标记了@Controller注解的类转换为bean -->
<context:component-scan base-package="com.example.demo.controller" />
</beans>
注意:
@Reference引用服务加上group、version
//用dubbo的@Reference注解去连接提供者提供服务
@Reference(group = "morning",version = "2.0.0")
private DemoService demoService;