Spring boot 集成Dubbox

前言

因爲工作原因,需要在項目中集成dubbo,所以去查詢dubbo相關文檔,發現dubbo目前已經不更新了,所以把目光投向了dubbox,dubbox是噹噹網基於dubbo二次開發的一個項目,dubbox,因爲公司項目中一個是基於spring mvc 3.0的,一個是基於spring boot的,而spring boot相對來說文檔少一點,所以此文記錄下spring boot下如何繼承dubbox

一、安裝zookeeper

1、zookeeper簡介

ZooKeeper是一個分佈式的,開放源碼的分佈式應用程序協調服務,是Google的Chubby一個開源的實現,是Hadoop和Hbase的重要組件。它是一個爲分佈式應用提供一致性服務的軟件,提供的功能包括:配置維護、域名服務、分佈式同步、組服務等。
ZooKeeper的目標就是封裝好複雜易出錯的關鍵服務,將簡單易用的接口和性能高效、功能穩定的系統提供給用戶

而dubbo就是依賴zookeeper的一個分佈式框架,當然二次開發的dubbox肯定也會依賴於zookeeper,所以我們需要先安裝zookeeper

2、下載zookeeper

zookeeper官網地址http://zookeeper.apache.org/
下載地址http://apache.fayea.com/zookeeper/

3、安裝zookeeper

因爲我是在Windows環境配置的,所以就簡單說一下windows下面的配置吧,首先解壓壓縮包,然後進入conf文件夾,複製一下zoo_sample.cfg創建副本,然後重命名爲zoo.cfg,因爲zookeeper只識別zoo.cfg,而默認是沒有這個文件的,zoo_sample.cfg是默認的配置文件,但是因爲文件名的原因,所以zookeeper無法識別,當然直接重命名zoo_sample.cfg也是可以的,只是看自己喜歡咯

4、啓動zookeeper

Windows環境下直接運行bin目錄下的zkServer.cmd就可以,如果是Linux環境,則在bin目錄下運行

./zkServer.sh start

命令,就可以啓動zookeeper

5、添加dubbox依賴

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.8.4</version>
        </dependency>
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

注意,這裏的dubbo的2.8.4版本是我自己編譯的,這個在maven倉庫是沒有的,因爲dubbo已經不更新了,而2.8.4是噹噹網的dubbox,所以如果你要使用,要麼就編譯dubbox,要麼就使用舊版本的dubbo


6、添加服務提供者的接口
接口類:

package wang.raye.dubbo.interfaces;

public interface DubboInterface {

    public String hello(String name);
}

接口實現類:

package wang.raye.dubbodemo1;

import org.springframework.stereotype.Service;

import wang.raye.dubbo.DubboInterface;
@Service
public class DubboImpl implements DubboInterface {

    public String hello(String name) {
        return "hello "+name+"  this is dubbodemo1";
    }

}

7、添加dubbo配置xml
xml文件放在sources文件夾,名字可以隨便命名,我這裏是dubbo.xml

<?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:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
    default-lazy-init="false" >
   <!-- 提供方應用名稱信息,這個相當於起一個名字,我們dubbo管理頁面比較清晰是哪個應用暴露出來的  -->     
   <dubbo:application name="dubbo-provider1"></dubbo:application> 
   <!--使用註解方式暴露接口
   <dubbo:annotation package="wang.raye.dubbodemo1" />  -->
   <!-- 使用zookeeper註冊中心暴露服務地址 -->  
   <dubbo:registry address="zookeeper://192.168.1.126:2181" check="false" subscribe="false" register=""></dubbo:registry>
  <!-- 要暴露的服務接口-->
  <dubbo:service interface="wang.raye.dubbo.interfaces.DubboInterface" ref="dubboImpl" /> 
</beans>

這裏每個節點都有解釋了,相信不過過多解釋


8、配置消費者

類中引用遠程提供者

package wang.raye.dubbodemo3.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import wang.raye.dubbo.DubboInterface;

@Controller
public class DubboControll {

    @Autowired
    private DubboInterface interface1;
    @RequestMapping("/hello")
    @ResponseBody
    public String hello(String name){
        return interface1.hello(name);
    }



}

消費者的xml配置

<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="dubbo-custom-dsp"/>   
    <dubbo:registry address="zookeeper://192.168.1.126:2181" />  
    <!-- 生成遠程服務代理,可以和本地bean一樣使用demoService -->  
    <dubbo:reference id="dubboService" interface="wang.raye.dubbo.interfaces.DubboInterface" />  
</beans>

這裏也是每個接點都有註釋,應該不用多說,而且比較本文不是講究dubbo配置的,主要是講spring boot集成dubbox


9、引用dubbo配置xml
在Spring boot的Application類添加註解

@ImportResource({"classpath:dubbo.xml"})

因爲我的xml名字是dubbo.xml,所以當你用的時候需要換成自己的xml名字,==注意:消費者項目和服務提供者的項目的Application類都需要配置此註解==

結尾

至此,spring boot集成duubox就說完了,當然這樣說肯定很空洞,所以我吧我測試的項目上傳到了github,大家可以參考看看,當然要測試的話需要修改dubbo.xml的dubbo:registry 節點中的zookeeper地址。

demo地址,爲了顯示出dubbo的優勢,所以我創建了2個服務提供者,頻繁調用的話,會自動選擇這2箇中的任何一個,連負載均衡都免了,應該是zookeeper自己做了,所以感覺還是挺不錯的

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