dubbo入門(1)——dubbo-demo

在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

1. 服務提供方

通常是服務的實現。每每在架構層面抽象成服務層。web

1.1 工程結構

1.2 定義服務接口 DemoService

package com.alibaba.dubbo.demo;

public interface DemoService {
    String sayHello(String name);
}

1.3 服務提供者實現接口DemoServiceImpl

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();
    }

}

1.4 服務配置文件

<?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

1.5 服務註冊

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
    }

}

當啓動提供方應用成功後,經過日誌能夠看到服務已經在服務中心進行註冊,並獲取到以下信息:架構

  • 暴露的服務IP和端口是192.168.10.245:20880
  • 服務註冊中心192.168.199.199:2181
  • 服務所在的應用名是demo-provider
  • 所暴露的服務的接口和方法是com.alibaba.dubbo.demo.DemoService

經過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

2 服務消費者 Consumer

2.1 消費者配置文件dubbo-demo-consumer.xml

<?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();
            }


        }

    }
}

2.2運行結果以下:

相關文章
相關標籤/搜索