标签:容器 a20 resource 编写 context void rup throws org
<?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"});} }
<?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。
标签:容器 a20 resource 编写 context void rup throws org
原文地址:https://www.cnblogs.com/yxh1008/p/9251693.html