Dubbo 使用入門demo

上一篇介紹了Dubbo簡介和實(shí)現(xiàn)原理。 Dubbo 簡介

本文主要介紹采用Dubbo與Zookeeper、Spring框架的整合。
整合流程:

  1. 安裝Zookeeper,啟動(dòng);
  2. 創(chuàng)建MAVEN項(xiàng)目,構(gòu)建Dubbo+Zookeeper+Spring實(shí)現(xiàn)的簡單demo;
  3. 安裝Dubbo-admin,實(shí)現(xiàn)監(jiān)控。

一、Zookeeper安裝
本demo中的Dubbo注冊中心采用的是Zookeeper.
具體的安裝方法,可參考文章:Windows環(huán)境下Zookeeper安裝和使用

二、新建Spring Maven工程
項(xiàng)目所需環(huán)境:

  1. jdk1.8
  2. apache-tomcat-8.5.31
  3. 開發(fā)工具(IntelliJ IDEA 2016.3)
  4. apache maven 3.3.9
    項(xiàng)目結(jié)構(gòu):
    dubbo-api : 存放公共接口;
    dubbo-consumer、dubbo-consumer2 : 調(diào)用遠(yuǎn)程服務(wù);
    dubbo-provider : 提供遠(yuǎn)程服務(wù)。


    image.png

    代碼構(gòu)建過程:

  1. 構(gòu)建MAVEN項(xiàng)目,導(dǎo)入所需要的jar包依賴(spring, dubbo, zookeeper等jar)。
  2. 創(chuàng)建dubbo-api的MAVEN項(xiàng)目(有獨(dú)立的pom.xml,用來打包供提供者消費(fèi)者使用)。
    在項(xiàng)目中定義服務(wù)接口:該接口需單獨(dú)打包,在服務(wù)提供方和消費(fèi)方共享。


    image.png

    接口:DemoService

package com.alibaba.dubbo.demo;

import java.util.List;
public interface DemoService {
    List<String> getPermissions(Long id);
}

pom.xml無需修改。

3)創(chuàng)建dubbo-provider的MAVEN項(xiàng)目(有獨(dú)立的pom.xml,用來打包供消費(fèi)者使用)。 實(shí)現(xiàn)公共接口,此實(shí)現(xiàn)對消費(fèi)者隱藏


image.png

DemoServiceImpl實(shí)現(xiàn)類:

package com.alibaba.dubbo.demo.impl;

import com.alibaba.dubbo.demo.DemoService;

import java.util.ArrayList;
import java.util.List;

public class DemoServiceImpl implements DemoService {
    public List<String> getPermissions(Long id) {
        List<String> demo = new ArrayList<String>();
        demo.add(String.format("Permission_%d", id - 1));
        demo.add(String.format("Permission_%d", id));
        demo.add(String.format("Permission_%d", id + 1));
        return demo;
    }
}

pom.xml中需加入公共接口所在的依賴 :

     <dependency>
            <groupId>DubboDemo</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

在main/rescourse下新增 provider.xml配置聲明暴露服務(wù)

<?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/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!--定義了提供方應(yīng)用信息,用于計(jì)算依賴關(guān)系;在 dubbo-admin 或 dubbo-monitor 會(huì)顯示這個(gè)名字,方便辨識(shí)-->
    <dubbo:application name="demotest-provider" owner="programmer" organization="dubbox"/>
    <!--使用 zookeeper 注冊中心暴露服務(wù),注意要先開啟 zookeeper-->
    <dubbo:registry address="zookeeper://localhost:2181"/>
    <!-- 用dubbo協(xié)議在20880端口暴露服務(wù) -->
    <dubbo:protocol name="dubbo" port="20880" />
    <!--使用 dubbo 協(xié)議實(shí)現(xiàn)定義好的 api.PermissionService 接口-->
    <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" protocol="dubbo" />
    <!--具體實(shí)現(xiàn)該接口的 bean-->
    <bean id="demoService" class="com.alibaba.dubbo.demo.impl.DemoServiceImpl"/>
</beans>

在impl包下,新增啟動(dòng)服務(wù)的main方法

package com.alibaba.dubbo.demo.impl;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class Provider {
        public static void main(String[] args) throws IOException {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
            System.out.println(context.getDisplayName() + ": here");
            context.start();
            System.out.println("服務(wù)已經(jīng)啟動(dòng)...");
            System.in.read();
        }
    }
  1. 創(chuàng)建dubbo-consumer的MAVEN項(xiàng)目(可以有多個(gè)consumer,但是需要配置好)。 調(diào)用所需要的遠(yuǎn)程服務(wù)。


    image.png

在main/rescourse下新增 consumer .xml配置引用遠(yuǎn)程服務(wù)

<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <dubbo:application name="demotest-consumer" owner="programmer" organization="dubbox"/>
    <!--向 zookeeper 訂閱 provider 的地址,由 zookeeper 定時(shí)推送-->
    <dubbo:registry address="zookeeper://localhost:2181"/>
    <!--使用 dubbo 協(xié)議調(diào)用定義好的 api.PermissionService 接口-->
    <dubbo:reference id="permissionService" interface="com.alibaba.dubbo.demo.DemoService"/>
</beans>

在impl包下,新增調(diào)用遠(yuǎn)程服務(wù)的main方法

package com.alibaba.dubbo.consumer;

import com.alibaba.dubbo.demo.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Consumer {
    public static void main(String[] args) {
        //測試常規(guī)服務(wù)
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("consumer.xml");
        context.start();
        System.out.println("consumer start");
        DemoService demoService = context.getBean(DemoService.class);
        System.out.println("consumer");
        System.out.println(demoService.getPermissions(1L));
    }
}

5)運(yùn)行項(xiàng)目
先確保Zookeeper已成功啟動(dòng)
進(jìn)入dubbo-provider工程下,右鍵運(yùn)行Provider.class,啟動(dòng)provider服務(wù)。


image.png

再進(jìn)入dubbo-consumer工程下,右鍵運(yùn)行Consumer.class,啟動(dòng)consumer模塊。


image.png

三、部署dubbo-admin管理控制臺(tái)
請參考文章:dubbo-admin 監(jiān)控搭建

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容