标签:work 启动 ice blog turn service 代理 led 客户端
版本:
重要的网址:
首先从https://github.com/alibaba/dubbo下载dubbo源码到本地,我们的第一个dubbo项目就是dubbo源码中的dubbo-demo子模块。
代码结构如下:
其中:
一 dubbo-demo-api
1 package com.alibaba.dubbo.demo; 2 3 public interface DemoService { 4 String sayHello(String name); 5 }
只提供了一个接口。
二 dubbo-demo-provider
1 配置文件
src/main/resources/META-INF/spring/dubbo-demo-provider.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 4 xmlns="http://www.springframework.org/schema/beans" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 6 http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 7 8 <!-- 提供方应用信息,用于计算依赖关系 --> 9 <dubbo:application name="demo-provider"/> 10 11 <!-- 使用zookeeper注册中心,并使用curator客户端 --> 12 <dubbo:registry protocol="zookeeper" address="10.211.55.5:2181" client="curator"/> 13 14 <!-- 使用dubbo协议在20880端口暴露服务 --> 15 <dubbo:protocol name="dubbo" port="20880"/> 16 17 <!-- 和本地bean一样实现服务 --> 18 <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/> 19 20 <!-- 声明需要暴露的服务接口 --> 21 <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/> 22 </beans>
配置项:
2 provider提供服务接口实现
1 package com.alibaba.dubbo.demo.provider; 2 3 import com.alibaba.dubbo.demo.DemoService; 4 import com.alibaba.dubbo.rpc.RpcContext; 5 6 import java.text.SimpleDateFormat; 7 import java.util.Date; 8 9 public class DemoServiceImpl implements DemoService { 10 public String sayHello(String name) { 11 System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress()); 12 return "Hello " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress(); 13 } 14 }
3 provider启动类
1 package com.alibaba.dubbo.demo.provider; 2 3 import org.springframework.context.support.ClassPathXmlApplicationContext; 4 5 public class Provider { 6 public static void main(String[] args) throws Exception { 7 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-provider.xml"}); 8 context.start(); 9 10 System.in.read(); // 按任意键退出 11 } 12 }
三 dubbo-demo-consumer
1 配置文件
src/main/resources/META-INF/spring/dubbo-demo-consumer.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 4 xmlns="http://www.springframework.org/schema/beans" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 6 http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> 7 8 <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 --> 9 <dubbo:application name="demo-consumer"/> 10 11 <!-- 使用zookeeper注册中心,并使用curator客户端 --> 12 <dubbo:registry protocol="zookeeper" address="10.211.55.5:2181" client="curator"/> 13 14 <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> 15 <dubbo:reference id="demoService" check="false" interface="com.alibaba.dubbo.demo.DemoService"/> 16 </beans>
配置项:
2 consumer启动类
1 package com.alibaba.dubbo.demo.consumer; 2 3 import com.alibaba.dubbo.demo.DemoService; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 public class Consumer { 7 public static void main(String[] args) { 8 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"}); 9 context.start(); 10 11 DemoService demoService = (DemoService) context.getBean("demoService"); // 获取远程服务代理 12 String hello = demoService.sayHello("world"); // 执行远程方法 13 14 System.out.println(hello); // 显示调用结果 15 } 16 }
启动服务并且调用远程服务。
四 启动服务
1 启动provider
启动成功后,会发现在zookeeper上创建了节点:
/dubbo
--/com.alibaba.dubbo.demo.DemoService
----/providers
------/dubbo://xx.xx.xx.xx:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=1393&side=provider×tamp=1506831679007
注意:
2 启动consumer
启动成功后,会发现在zookeeper上创建了节点:
/dubbo
--/com.alibaba.dubbo.demo.DemoService
----/consumers
------/consumer://xx.xx.xx.xx/com.alibaba.dubbo.demo.DemoService?application=demo-consumer&category=consumers&check=false&dubbo=2.0.0&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=1434&side=consumer×tamp=1506832498078
之后,看到provider和consumer双方的互动输出,则表示rpc成功!
第一个dubbo项目就结束了,dubbo-demo项目也可以查看http://dubbo.io/的例子。
标签:work 启动 ice blog turn service 代理 led 客户端
原文地址:http://www.cnblogs.com/java-zhao/p/7616962.html