項目中dubbo的使用


導語:Dubbo是阿里巴巴的一個分布式服務的開源框架,致力於提供高性能和透明化的RPC遠程服務調用方案,是阿里巴巴SOA服務化治理方案的核心框架,每天為2,000+個服務提供3,000,000,000+次訪問量支持,並被廣泛應用於阿里巴巴集團的各成員站點。

  參考網站:http://dubbo.io/

 

一、Dubbo-admin管理web工程配置

復制代碼
部署dubbo-admin
    1、校驗jdk安裝:參考jdk安裝
java –version 
2、安裝tomcat:參考tomcat安裝            
3、yum install –y unzip
rm –rf /usr/local/tomcat/webapp/ROOT/*
unzip dubbo-admin-2.5.4.war –d /usr/local/tomcat/webapp/ROOT
4、配置 hosts : vi /etc/hosts
       a、【root@localhost】 
            127.0.0.1  localhost
            127.0.0.1  root
       b、【root@lgp】 //lgp只是一個例子
            127.0.0.1  lgp
5、啟動服務
        ./usr/local/tomcat/bin/startup.sh
復制代碼

 

二、Dubbo+Zookeeper部署

   1、Zookeeper部署

    a、先安裝單台

復制代碼
    yum -y install openssh-clients    :安裝scp,用於不同機器上復制文件
    mkdir /home/lgp  :創建包用於存放 zookeeper
    cp –rf zookeeper-3.4.6.tar.gz /home/lgp
    cd /home/lgp
    tar -xzvf zookeeper-3.4.6.tar.gz
    cd zookeeper-3.4.6/conf/
    mv zoo_sample.cfg   zoo.cfg
    vi zoo.cfg    :參考詳情
復制代碼

server.X=A:B:C  # 其中X是一個數字, 表示這是第幾號server. A是該server所在的IP地址. B配置該server和集群中的leader交換消息所使用的端口. C配置選舉leader時所使用的端口. 這里的x是一個數字,與myid文件中的id是一致的。

 

復制代碼
    cd ..
    mkdir data
    mkdir logs
    touch /data/myid
    echo 1 > /home/lgp/ zookeeper-3.4.6/data/myid
   vi /etc/hosts
復制代碼

   b、集群

復制代碼
復制:對應的機器創建 /home/lgp 
      yum -y install openssh-clients
    
      修改對應 myid ,etc/hosts

scp /home/lgp/zookeeper-3.4.6 192.168.1.81:/home/lgp  //跨機器復制

啟動服務:
    service iptables stop:分別關閉對應機器的 iptables
    cd /home/lgp/zookeeper-3.4.6/bin
    ./bin/zkServer.sh start :開啟zookeeper
    ./bin/zkServer.sh status :狀態查詢
    ./bin/zkServer.sh stop :關閉zookeeper
復制代碼

  2、整合Zookeeper

cd /usr/local/tomcat/webapp/ROOT/WEB-INF
vi dubbo.properties

  pwd:/usr/local/apache-tomcat-7.0.61/webapps/ROOT/WEB-INF   dubbo.properties

 

三、Dubbo項目整合SpringMVC

  1、下載jar包

復制代碼
maven導入所需jar包:dubbo、zookeeper、zkclient
    <dependency>                  <groupId>com.alibaba</groupId>                  <artifactId>dubbo</artifactId>                  <version>2.5.3</version>   </dependency> <!-- Zookeeper 用於分布式服務管理 -->              <dependency>                  <groupId>org.apache.zookeeper</groupId>                  <artifactId>zookeeper</artifactId>                 <version>3.4.5</version>              </dependency>             <dependency>                  <groupId>com.101tec</groupId>                  <artifactId>zkclient</artifactId>                  <version>0.3</version>             </dependency>     <!-- Zookeeper 用於分布式服務管理 end -->
復制代碼

  2、eclipse配置dubbo.xsd,解決標簽不識別的問題

    百度雲盤:https://pan.baidu.com/s/1nuIKgMd

路徑:http://code.alibabatech.com/schema/dubbo/dubbo.xsd

 

  3、在.properties配置zookeeper的注冊地址

dubbo.registry.address=192.168.1.72:2181\,192.168.1.73:2182\,192.168.1.74:2183

 

  4、service提供服務:spring-dubbo-provider.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: 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="guduo-service-user" />     <!-- 使用zookeeper注冊中心暴露服務地址 --> <dubbo:registry protocol="zookeeper" address="${dubbo.registry.address}" />   <!—本機 -->     <dubbo:protocol name="dubbo" port="20823" />     <!-- 監控中心配置,protocol="registry",表示從注冊中心發現監控中心地址 -->     <dubbo:monitor protocol="registry"/>     <!-- 當ProtocolConfig和ServiceConfig某屬性沒有配置時,采用此缺省值 -->     <dubbo:provider timeout="30000" threadpool="fixed" threads="100" accepts="1000" />     <!-- 服務接口 -->     <dubbo:service retries="0" interface="com.guduo.service.system.UserReportService" ref="userReportService" /> <dubbo:service retries="0" interface="com.guduo.service.system.UserSuggestionService" ref="userSuggestionService" /> </beans>  
復制代碼

  spring-context.xml引入spring-dubbo-provider.xml

 

  5、consumer注冊服務:dubbo-consumer.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: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="guduo-web-publish" />     <!-- 使用zookeeper注冊中心暴露服務地址 -->     <!-- 注冊中心地址 -->     <dubbo:registry protocol="zookeeper" address="${dubbo.registry.address}" />     <!-- 用戶服務接口 -->     <dubbo:reference interface="com.guduo.service.user.PmsActionService" id="pmsActionService" check="false"/>     <dubbo:reference interface="com.guduo.service.user.PmsMenuService" id="pmsMenuService" check="false"/> </beans>  
復制代碼

 

四、開發階段,啟動Dubbo服務

  建議放在"src/test"目錄下面

復制代碼
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; /**  *   * @描述: 啟動Dubbo服務用的MainClass.  * @作者: 劉廣平  * @創建時間: 2016-9-5,下午9:47:55 .  * @版本: 1.0 .  */ public class DubboProvider {     private static final Log log = LogFactory.getLog(DubboProvider.class);     public static void main(String[] args) {         try {             ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-context.xml");             context.start();         } catch (Exception e) {             log.error("== DubboProvider context start error:",e);         }         synchronized (DubboProvider.class) {             while (true) {                 try {                     DubboProvider.class.wait();                 } catch (InterruptedException e) {                     log.error("== synchronized error:",e);                 }             }         }     } }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM