dubbo--多版本支持

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fangxinde/article/details/87259618

        服务端提供接口的实现存在不兼容升级时,可由dubbo的版本号操作进行过渡,为了缓解服务器压力或为了测试新接口,可对部分用户进行升级接口,过段时间后,再对剩余的用户进行升级接口。

服务提供方:

向外暴露的接口:

public interface IOrderServices {
    DoOrderResponse doOrder(DoOrderRequest request);
}

接口不同版本的实现类:

版本1实现的接口:

@Service(value = "orderService")
public class OrderServiceImpl implements IOrderServices{

    @Override
    public DoOrderResponse doOrder(DoOrderRequest request) {
        System.out.println("曾经来过版本1:"+request);
        DoOrderResponse response = new DoOrderResponse();
        response.setData("版本测试");
        response.setCode("0001");
        response.setMemo("处理成功,版本1");
        return response;
    }
}

 版本2实现的接口:

@Service(value = "orderService2")
public class OrderServiceImpl2 implements IOrderServices{

    @Override
    public DoOrderResponse doOrder(DoOrderRequest request) {
        System.out.println("曾经来过版本2:"+request);
        DoOrderResponse response = new DoOrderResponse();
        response.setData("版本测试");
        response.setCode("0002");
        response.setMemo("处理成功,版本2");
        return response;
    }
}

 服务提供方的文件配置:

<?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:context="http://www.springframework.org/schema/context"
       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://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://code.alibabatech.com/schema/dubbo
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd" default-autowire="byName">
    <context:annotation-config></context:annotation-config>
    <context:component-scan base-package="com.gupao.vip.mic.dubbo.order"/>
    <!--name:当前项目在整个分布式架构里面的唯一名称,计算依赖关系的标签 owner表示当前application是由谁维护-->
    <dubbo:application name="order-provider" owner="mic"/>
    <!--能对服务调用进行监控-->
    <!--<dubbo:monitor protocol="registry"/>-->
    <!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
    <dubbo:registry protocol="zookeeper" address="192.168.70.63:2181,192.168.70.64:2181,192.168.70.65:2181,192.168.70.66:2181"/>
    <!--指定hessian协议-->
    <dubbo:protocol name="hessian" port="8090" server="jetty"/>
    <!--服务发布的配置,需要暴露的服务接口-->
    <!--版本1接口-->
    <dubbo:service interface="com.gupao.vip.mic.dubbo.order.IOrderServices" ref="orderService" protocol="hessian" version="1.0"/>
    <!--版本2接口-->
    <dubbo:service interface="com.gupao.vip.mic.dubbo.order.IOrderServices" ref="orderService2" protocol="hessian" version="2.0"/>
</beans>

消费方:根据版本号获取不同接口实现业务。

<?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">
    <!--name:当前项目在整个分布式架构里面的唯一名称,计算依赖关系的标签 owner表示当前application是由谁维护-->
    <dubbo:application name="order-user" owner="mic"/>
    <!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
    <dubbo:registry address="zookeeper://192.168.70.66:2181?backup=192.168.70.65:2181,192.168.70.64:2181,192.168.70.63:2181"/>
    <!--生成一个远程服务的调用代理:根据版本号调用对应的接口服务-->
    <dubbo:reference id="orderService2.0"
                     interface="com.gupao.vip.mic.dubbo.order.IOrderServices"
                     protocol="hessian"
                      version="2.0"/>
    <dubbo:reference id="orderService1.0"
                     interface="com.gupao.vip.mic.dubbo.order.IOrderServices"
                     protocol="hessian"
                     version="1.0"/>
    <dubbo:reference id="orderQueryServices" interface="com.gupao.vip.mic.dubbo.order.IOrderQueryService" protocol="dubbo" version="1.0"/>

</beans>
package com.gupao.vip.mic.dubbo.user;

public class App 
{
    public static void main( String[] args ) throws IOException {
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("order-consumer.xml");
        DoOrderRequest request = new DoOrderRequest();
        request.setName("fangxinde");
        //测试版本01
        IOrderServices services01=  (IOrderServices)context.getBean("orderService1.0");
        DoOrderResponse response = services01.doOrder(request);
        System.out.println(response);
        //测试版本02
        IOrderServices services02=  (IOrderServices)context.getBean("orderService2.0");
         response = services02.doOrder(request);
        System.out.println(response);
        System.in.read();
    }
}

运行结果:
DoOrderResponse{data=版本测试, code='0001', memo='处理成功,版本1'}
DoOrderResponse{data=版本测试, code='0002', memo='处理成功,版本2'}

猜你喜欢

转载自blog.csdn.net/fangxinde/article/details/87259618