Dubbo服务分组

来源:互联网 发布:淘宝店铺装修添加宝贝 编辑:程序博客网 时间:2024/06/06 15:46

Dubbo提供服务分组,用于当一个接口有多种实现时,可用使用group分组。实现代码如下:

package com.yncp.dubbo.service;public interface IDubboGroupService {      public String print();  }
package com.yncp.dubbo.service.impl;import com.yncp.dubbo.service.IDubboGroupService;public class DubboGroup1ServiceImpl implements IDubboGroupService {    @Override    public String print() {         return "feedback";    }}
package com.yncp.dubbo.service.impl;import com.yncp.dubbo.service.IDubboGroupService;public class DubboGroup2ServiceImpl implements IDubboGroupService {     @Override    public String print() {          return "member";    }}

applicationContext.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">    <!-- 配置Bean -->    <bean id="dubboGroupService1" class="com.yncp.dubbo.service.impl.DubboGroup1ServiceImpl"/>    <bean id="dubboGroupService2" class="com.yncp.dubbo.service.impl.DubboGroup2ServiceImpl"/>    <!-- 引入配置文件 -->    <import resource="classpath:dubbo.xml"/> </beans>

dubbo.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">     <!-- 指定web服务名字 -->    <dubbo:application name="DubboGroup"/>    <!-- 声明服务注册中心 -->    <dubbo:registry  protocol="zookeeper" address="127.0.0.1:2181"/>    <!-- 指定传输层通信协议 -->    <dubbo:protocol name="dubbo" port="20880"/>    <dubbo:protocol name="rmi" port="1099"/>        <!-- 暴露你的服务地址 -->    <dubbo:service         ref="dubboGroupService1"         interface="com.yncp.dubbo.service.IDubboGroupService"        protocol="dubbo,rmi"        group="feedback"    />     <dubbo:service         ref="dubboGroupService2"         interface="com.yncp.dubbo.service.IDubboGroupService"        protocol="dubbo,rmi"        group="member"    /> </beans>

调用端代码实现如下:

package com.yncp.dubbo.service;public interface IDubboGroupService {    public String print();}

调用端dubbo.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">    <!-- 指定web服务名字 -->    <dubbo:application name="DubboGroup"/>    <!-- 声明服务注册中心 -->    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>    <!-- 指定传输层通信协议 -->    <dubbo:protocol name="dubbo" port="20881"/>    <dubbo:protocol name="rmi" port="1010"/>    <!-- 暴露你的服务地址 -->    <dubbo:reference         id="groupservice1"         interface="com.yncp.dubbo.service.IDubboGroupService"        protocol="dubbo"        group="feedback"    />     <dubbo:reference         id="groupservice2"         interface="com.yncp.dubbo.service.IDubboGroupService"        protocol="dubbo"        group="member"    />    <dubbo:reference         id="groupservice3"         interface="com.yncp.dubbo.service.IDubboGroupService"        protocol="dubbo"        group="*"    /> </beans>

调用代码如下:

package com.yncp.dubbo.service;import java.io.IOException;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class DubboStart {      public static void main(String[] args) throws IOException {        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");        //访问feedback接口        IDubboGroupService groupservice1=(IDubboGroupService) ctx.getBean("groupservice1");        System.out.println(groupservice1.print());        //访问member接口        IDubboGroupService groupservice2=(IDubboGroupService) ctx.getBean("groupservice2");        System.out.println(groupservice2.print());              //访问随机接口        IDubboGroupService groupservice3=(IDubboGroupService) ctx.getBean("groupservice3");        System.out.println(groupservice3.print());       }}
0 1
原创粉丝点击