Dubbo常用配置及源码解析-多版本支持

1、多版本的支持
如何发布服务,需要将需要暴露的服务接口发布出去供客户端调用,需要在java同级目录新建一个resources目录,然后将resoureces目录标记成Test Resoureces Root,然后在esources目录下新建MATE-INF.spring目录,在该目录下添加配置文件dubbo-server.xml文件
  dubbo的服务端配置文件如下
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--提供方信息,用于计算依赖关系-->
<dubbo:application name="dubbo-server" owner="mic"/>

<!--注册中心 暴露服务地址-->
<dubbo:registry address="zookeeper://192.168.126.129:2181"/>

<!--用dubbo协议在20880 端口暴露服务-->
<dubbo:protocol port="20880" name="dubbo"/>

<!--声明需要暴露的服务接口,指定协议为dubbo,设置版本号1.1.1-->
<dubbo:service interface="com.gupaoedu.dubbo.IGpHello"  ref="gpHelloService" protocol="dubbo" version="1.1.1"/>
<!--声明需要暴露的服务接口,指定协议为dubbo,设置版本号1.1.2-->
<dubbo:service interface="com.gupaoedu.dubbo.IDemoService"    ref="demoService" protocol="dubbo" version="1.1.2"/>

<!--和本地服务一样实现服务-->
<bean id="gpHelloService" class="com.gupaoedu.dubbo.GpHelloImpl"/>

<bean id="demoService" class="com.gupaoedu.dubbo.DemoService"/>
</beans>

服务端的接口以及实现类

public interface IGpHello {String sayHello(String msg);}
public interface IDemoService {String protocolDemo(String msg);}
public class GpHelloImpl implements IGpHello{
@Override
public String sayHello(String msg) {return "Hello:"+msg;}
}
public class GpHelloImpl2 implements IGpHello{
@Override
public String sayHello(String msg) {return "Hello,i'm server 2:"+msg;}
}

编写Main方法,用spring容器来启动服务

public class Main {
public static void main(String[] args) throws IOException {
//默认情况下会使用spring容器来启动服务
com.alibaba.dubbo.container.Main.main(new String[]{"spring","log4j"});}
}
服务启动过程会进行服务注册,启动监听端口,启动服务之后
在客户端通过远程调用访问服务端发布的服务,相应的客户端配置文件 dubbo-client.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--提供方信息-->
<dubbo:application name="dubbo-client" owner="mic"/>

<!--注册中心-->
<dubbo:registry id="zokeeper" address="zookeeper://192.168.126.129:2181?register=false" file="d:/dubbo-server"/>

<!--声明需要暴露的服务接口,指定版本号-->
<dubbo:reference id="gpHelloService" interface="com.gupaoedu.dubbo.IGpHello" registry="zookeeper" version="1.1.1"/>
</beans>

其实我们可以在zookeeper的客户端可以发现,事实上已经发布了服务方已经发布了两个不同版本的服务,具体如下

1 dubbo%3A%2F%2F192.168.126.1%3A20880%2Fcom.gupaoedu.dubbo.IDemoService%3Fanyhost%3Dtrue%26application%3Ddubbo-server%26dubbo%3D2.5.3%26interface%3Dcom.gupaoedu.dubbo.IDemoService%26methods%3DprotocolDemo%26owner%3Dmic%26pid%3D22548%26revision%3D1.1.2%26side%3Dprovider%26timestamp%3D1530450331827%26version%3D1.1.2
2 dubbo%3A%2F%2F192.168.126.1%3A20880%2Fcom.gupaoedu.dubbo.IGpHello%3Fanyhost%3Dtrue%26application%3Ddubbo-server%26dubbo%3D2.5.3%26interface%3Dcom.gupaoedu.dubbo.IGpHello%26methods%3DsayHello%26owner%3Dmic%26pid%3D22548%26revision%3D1.1.1%26side%3Dprovider%26timestamp%3D1530450325703%26version%3D1.1.1

我们知道,这两个版本正是我们在服务发布方设置的不同版本号,同样的,在消费端,我们可以通过设置指定的版本号获取相应的版本服务,消费的代码如下

public class App{
         public static void main( String[] args ) throws IOException, InterruptedException {
                   ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("dubbo-client.xml");
                    context.start();
                   IGpHello demoService = (IGpHello)context.getBean("gpHelloService");//获取远程服务代理
                  String hello = demoService.sayHello("world");//执行远程调用方法
                  System.out.println(hello);//显示调用结果
         }
}

在控制台,我们可以看到同样的服务发布地址url。

  

  

  

  

  

  

  

猜你喜欢

转载自www.cnblogs.com/yxh1008/p/9251693.html