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

本文将展示如何在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>

参考资料

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值