Dubbo實戰(三)多註冊中心配置
阿新 • • 發佈:2019-01-23
本文將展示如何在Dubbo中進行多註冊中心配置。
開發環境
- JDK 1.7
- Maven 3.3.9
- Spring 4.2.7.RELEASE
Spring配置
<?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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"
default-lazy-init="false">
<context:component-scan base-package="com.ricky.dubbo" />
<context:annotation-config />
<import resource="spring-dubbo.xml"/>
</beans>
待暴露的服務
package com.ricky.dubbo.provider.impl;
import org.springframework.stereotype.Service;
import com.ricky.dubbo.api.DemoService;
import com.ricky.dubbo.api.model.User;
@Service("demoService")
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello " + name;
}
@Override
public User findUserById(long id) {
User user = new User();
user.setId(id);
user.setName("Ricky");
user.setAge(26);
return user;
}
}
package com.ricky.dubbo.provider.impl;
import org.springframework.stereotype.Service;
import com.ricky.dubbo.api.HelloService;
@Service("helloService")
public class HelloServiceImpl implements HelloService {
@Override
public String sayHi(String msg) {
return "Hello "+msg;
}
}
1、多註冊中心註冊
比如:有些服務只在A機房部署,而B機房的其它應用又需要引用此服務,那麼可以將服務同時註冊到兩個註冊中心。
spring-dubbo-multi-registry.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-app" />
<!-- 多註冊中心配置 -->
<dubbo:registry id="qd_registry" address="zookeeper://127.0.0.1:2181" />
<dubbo:registry id="wh_registry" address="zookeeper://127.0.0.1:2183" default="false" />
<!-- 用dubbo協議在20880埠暴露服務 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 宣告需要暴露的服務介面 -->
<dubbo:service interface="com.ricky.dubbo.api.HelloService" ref="helloService" registry="qd_registry,wh_registry" />
</beans>
2、不同服務使用不同註冊中心
比如:CRM有些服務是專門為國際站設計的,有些服務是專門為中文站設計的。
<?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-app" />
<!-- 多註冊中心配置 -->
<dubbo:registry id="qd_registry" address="zookeeper://127.0.0.1:2181" />
<dubbo:registry id="wh_registry" address="zookeeper://127.0.0.1:2183" default="false" />
<!-- 用dubbo協議在20880埠暴露服務 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 宣告需要暴露的服務介面 -->
<dubbo:service interface="com.ricky.dubbo.api.DemoService" ref="demoService" registry="qd_registry" />
<dubbo:service interface="com.ricky.dubbo.api.HelloService" ref="helloService" registry="wh_registry" />
</beans>