在dubbo中,服務被註冊在註冊中心中,咱們把提供服務的server成爲服務提供方,調用服務的server稱爲服務調用方,二者經過RPC進行調用,並使用了dubbo協議(使用的協議能夠通過配置進行修改)協調工做。這裏的demo是dubbo源碼中提供的demo。java
一、這裏使用zookeeper做爲註冊中心,因此須要準備zookeeper的可用環境。dubbo官網中推薦zookeeper做爲註冊中心,提供的源碼內部並無使用zookeeper,multicast廣播(並無怎麼去了解)。關於zookeeper安裝使用能夠參考https://my.oschina.net/xianggao/blog/538867。git
二、 dubbo託管GitHub地址 https://github.com/alibaba/dubbogithub
通常是服務的實現。每每在架構層面抽象成服務層。web
package com.alibaba.dubbo.demo; public interface DemoService { String sayHello(String name); }
public class DemoServiceImpl implements DemoService { public String sayHello(String name) { System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress()); return "Hello " + name + ", response form provider: " + RpcContext.getContext().getLocalAddress(); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 提供方應用名稱,用於計算依賴關係 --> <dubbo:application name="demo-provider"/> <!-- 使用zookeeper註冊中心暴露服務地址 --> <dubbo:registry address="zookeeper://192.168.199.144:2181"/> <!-- 使用dubbo協議,在20880端口暴露服務 --> <dubbo:protocol name="dubbo" port="20880"/> <!-- service實現類做爲本地的一個bean --> <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/> <!-- 聲明須要暴露的服務接口 --> <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/> </beans>
服務的配置,dubbo經過xml文件掃描,在spring的啓動階段把服務註冊到註冊中心。這裏的demoService是服務的實現者。當服務提供方獲取到消費方的rpc請求後,將會調用該實現類的對應的方法運行,並把結果經過PRC返回給調用方。spring
public class Provider { public static void main(String[] args) throws Exception { //Prevent to get IPV6 address,this way only work in debug mode //But you can pass use -Djava.net.preferIPv4Stack=true,then it work well whether in debug mode or not System.setProperty("java.net.preferIPv4Stack", "true"); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-provider.xml"}); context.start(); System.in.read(); // press any key to exit } }
當啓動提供方應用成功後,經過日誌能夠看到服務已經在服務中心進行註冊,並獲取到以下信息:架構
經過zookeeper的客戶端鏈接ZK的服務,查看樹的ZNode狀況app
[root@localhost bin]# ./zkCli.sh -server 192.168.199.144:2181ide
zk樹中,爲dubbo的服務com.alibaba.dubbo.demo.DemoService創建了一個節點,並分別創建consumers, routers, providers, configurators四個節點。這四個節點分別記錄了消費方,服務路由信息,提供方,配置的一些信息。咱們查看providers能夠看到192.168.10.245:20880這臺機器再爲咱們服務。this
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 指定web應用名字 --> <dubbo:application name="demo-consumer"/> <!-- 聲明服務註冊中心 --> <dubbo:registry address="zookeeper://192.168.199.144:2181"/> <!-- 引用服務,demoService僅用於根據接口生成動態代理,默認使用javassist生成代理對象 --> <dubbo:reference id="demoService" check="false" interface="com.alibaba.dubbo.demo.DemoService"/> </beans>
運行消費者住程序能夠看到結果Consumerspa
public class Consumer { public static void main(String[] args) { //Prevent to get IPV6 address,this way only work in debug mode //But you can pass use -Djava.net.preferIPv4Stack=true,then it work well whether in debug mode or not System.setProperty("java.net.preferIPv4Stack", "true"); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"}); context.start(); DemoService demoService = (DemoService) context.getBean("demoService"); // get remote service proxy while (true) { try { Thread.sleep(1000); String hello = demoService.sayHello("world:飛哥"); // call remote method System.out.println(hello); // get result } catch (Throwable throwable) { throwable.printStackTrace(); } } } }