Dubbo入門練習demo(IDEA版)

Dubbo是阿里巴巴開源的基於 Java 的高性能 RPC 分佈式服務框架,現已成爲 Apache 基金會孵化項目。

因爲是阿里開源項目,國內很多互聯網公司都在用,已經經過很多線上考驗。內部使用了 Netty、Zookeeper,保證了高性能高可用性。

使用 Dubbo 可以將核心業務抽取出來,作爲獨立的服務,逐漸形成穩定的服務中心,可用於提高業務複用靈活擴展,使前端應用能更快速的響應多變的市場需求。

一、創建基本結構

1、創建maven項目,命名 dubbodemo。

2、創建子模塊 api(公共接口)、consumer(服務消費者)、provider(服務提供者)。

、添加依賴

1、在 dubbodemo 的 pom.xml 中添加 dubbo 和 zookeeper 客戶端依賴。

<dependencies>
    <!-- dubbo當前最新版 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>dubbo</artifactId>
        <version>2.6.0</version>
    </dependency>
    <!-- zookeeper客戶端 -->
    <dependency>
        <groupId>com.101tec</groupId>
        <artifactId>zkclient</artifactId>
        <version>0.10</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>utf-8</encoding>
            </configuration>
        </plugin>
    </plugins>
</build>

 三、定義接口

1、寫接口

api 項目爲其他兩個項目提供接口,保證服務生產者和服務消費者使用的接口統一,我們在 api 項目中創建包com.myx.demo.dubbo.api,並創建接口DemoService。

2、引用接口
在服務生產者和服務消費者項目中引用接口,保證兩端接口一致。分別在兩個項目的 pom.xml 中添加如下代碼:


    <dependencies>
        <!-- API接口引用 -->
        <dependency>
            <groupId>com.myx</groupId>
            <artifactId>api</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

3、其他依賴

緊急情況下可能需要直接手動調用接口,而生產環境只有服務提供者,消費者暫時不方便操作時,可通過telnet進行觸發。

服務提供方引入fastjson依賴:

<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>fastjson</artifactId>
	<version>1.2.51</version>
</dependency>

四、添加實現類以及XML配置

1、在服務提供者包中添加實現類

2、服務提供者配置

將實現類註冊到註冊中心(只有這樣,其他應用纔有可能請求到),在 服務提供者包中 src / main / resources 下創建 dubbo-provider.xml 文件,用於配置dubbo服務端註冊,代碼如下:

<?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應用程序命名-->
    <dubbo:application name="dubbo-demo-provider"/>

    <!--dubbo註冊地址-->
    <dubbo:registry address="zookeeper://localhost:2181"/>

    <!--dubbo協議地址-->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--接口聲明-->
    <dubbo:service interface="com.myx.demo.dubbo.api.DemoService" ref="demoService"/>
    <bean id="demoService" class="com.myx.demo.dubbo.provider.DemoServiceImpl"/>
</beans>

3、服務消費者配置

添加服務消費者包中配置文件 dubbo-consumer.xml 放於 src / main / resources 下,內容與服務端相似,只不過不需要暴露接口,而是引用接口,代碼如下: 

<?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應用程序命名-->
    <dubbo:application name="dubbo-demo-provider"/>

    <!--dubbo註冊地址-->
    <dubbo:registry address="zookeeper://localhost:2181"/>

    <!--接口引用-->
    <dubbo:reference interface="com.myx.demo.dubbo.api.DemoService" id="demoService"/>
</beans>

 

五、註冊中心

因爲要對服務進行註冊,所以需要添加註冊中心。此處添加註冊中心zookeeper,官網下載 zookeeper 後解壓,將conf文件夾下的 zoo_sample.cfg 重命名爲 zoo.cfg,之後在命令提示符下切換至bin目錄下,執行 .\zkServer.cmd。

看到如下提示後表示啓動成功:

2019-10-01 23:12:59,007 [myid:] - INFO  [main:NIOServerCnxnFactory@89] - binding to port 0.0.0.0/0.0.0.0:2181

六、 測試

1、按照上述要求啓動 zookeeper

2、啓動服務端

在服務端下添加 Provider.java 

 

3、客戶端測試

在 src / main / java 下創建包 com.myx.demo.dubbo.api,並創建文件 consumer.java,main 方法代碼如下: 

4、結果顯示

執行後在控制檯查看結果,如下所示,用戶名後成功添加字符串,表示服務提供者和服務消費者都測試成功。

參考文章:https://www.jianshu.com/p/b492ef5d4b98

代碼:https://github.com/MiaoPlus/dubbodemo.git

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章