Dubbo实战(三)多注册中心配置

来源:互联网 发布:淘宝无法收藏宝贝 编辑:程序博客网 时间:2024/05/01 21:39

本文将展示如何在Dubbo中进行多注册中心配置。

转载:http://blog.csdn.net/top_code/article/details/51935533

开发环境

  • 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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

待暴露的服务

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;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
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
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
原创粉丝点击