2018年2月15日,阿里巴巴的dubbo進入了Apache孵化器,社區的加入,但願dubbo能變得更好…前端
最近在學習一個分佈式項目,使用到了dubbo
,以前沒有使用過,體驗一下,分佈式項目地址:點擊這裏java
使用dubbo官網的一張圖來介紹下dubbo
(本人才開始學習,若有錯誤,歡迎指正):git
Registry:註冊中心,至關於房產中介,服務提供者和使用者都須要在這裏註冊/使用服務,我使用zookeeper
實現。github
Monitor:監控中心,至關於房產局,它能夠統計服務提供者和服務使用者的一些信息,及他們之間的關係,我使用dubbo admin
實現。web
Provider:服務提供者,至關於房東,提供服務。spring
Consumer:服務消費者,想當於租戶,使用服務。apache
下面我通俗的解釋下dubbo的整個流程,我將服務比喻成房子:bootstrap
start:dubbo一啓動,房東想好本身準備要租出去的房子ubuntu
register:房東將房子拿到房產中介那邊進行登記,並留下本身的聯繫方式vim
subscribe:租戶告訴房產中介本身想租一個什麼樣的房子
notify:房產中介回覆給租戶符合條件的房子的房東的聯繫方式
invoke:租戶拿着聯繫方式去找房東租房子
count:房產局全程監控着房東和租戶之間的交易
其中:
start、register、subscribe在dubbo服務一啓動就完成了
notify、count是異步執行的
invoke是同步執行的
這一步比較簡單,直接跳過,不會的能夠看下這篇文章:Linux搭建JavaWeb開發環境(Java、Tomcat、MySQL)
我使用的是zookeeper-3.5.2-alpha
,點我下載。
下載後將其解壓:
wxs@ubuntu:~$ sudo tar zxf zookeeper-3.5.2-alpha.tar.gz wxs@ubuntu:~$ sudo mv zookeeper-3.5.2-alpha /usr wxs@ubuntu:~$ cd /usr/zookeeper-3.5.2-alpha wxs@ubuntu:/usr/zookeeper-3.5.2-alpha$ ls bin ivysettings.xml recipes build.xml ivy.xml src CHANGES.txt lib zookeeper-3.5.2-alpha.jar conf LICENSE.txt zookeeper-3.5.2-alpha.jar.asc contrib NOTICE.txt zookeeper-3.5.2-alpha.jar.md5 dist-maven README_packaging.txt zookeeper-3.5.2-alpha.jar.sha1 docs
在zookper文件夾下創建logs
文件夾和data
文件夾用於存放日誌和數據:
wxs@ubuntu:/usr/zookeeper-3.5.2-alpha$ sudo mkdir data wxs@ubuntu:/usr/zookeeper-3.5.2-alpha$ sudo mkdir logs
進入conf
目錄,複製一份zoo_sample.cfg
爲zoo.cfg
,對其進行修改:
wxs@ubuntu:/usr/zookeeper-3.5.2-alpha$ cd conf/ wxs@ubuntu:/usr/zookeeper-3.5.2-alpha/conf$ cp zoo_sample.cfg zoo.cfg wxs@ubuntu:/usr/zookeeper-3.5.2-alpha/conf$ vim zoo.cfg
配置下dataDir
和dataLogDir
的路徑,爲以前建立的兩個文件夾的路徑,clientPort
使用默認的2181端口便可:
我使用的時單機模式,沒有配集羣,這樣就能夠了。
進入到bin
目錄,啓動服務便可:
wxs@ubuntu:/usr/zookeeper-3.5.2-alpha/bin$ ./zkServer.sh start ZooKeeper JMX enabled by default Using config: /usr/zookeeper-3.5.2-alpha/bin/../conf/zoo.cfg Starting zookeeper ... STARTED wxs@ubuntu:/usr/zookeeper-3.5.2-alpha/bin$ ./zkServer.sh status ZooKeeper JMX enabled by default Using config: /usr/zookeeper-3.5.2-alpha/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Mode: standalone
當心踩坑:
執行./zkServer.sh start
時不要加sudo
,若是root用戶配置文件沒有配JAVA_HOME
會出現找不到JAVA_HOME
!
wxs@ubuntu:/usr/zookeeper-3.5.2-alpha/bin$ sudo ./zkServer.sh start Error: JAVA_HOME is not set and java could not be found in PATH.
相關命令:
啓動服務:start 中止服務: stop 重啓服務; restart 查看狀態:status
版本要求:
請使用dubbo-admin-2.5.6.war
及以上版本,不然會不支持JDK1.8!
下載連接:點擊這裏
當心踩坑:
若是你的zookeeper
和dubbo-admin
在一臺服務器上,dubbo-admin
不用修改任何內容!
若是不在一臺服務器上,將war包解壓後,修改項目/WEF-INF/dubbo.properties
文件,將zookeeper地址改成其所在服務器的地址(這裏同時能修改root用戶和guest用戶的密碼)。
這裏牽扯到項目代碼,若是看不懂,能夠下載文章開頭的項目源碼,或者直接使用官方提供的dubbo-demo
,更爲簡單。
首先給服務提供方和服務使用方導入依賴包:
<properties> <dubbo.version>2.6.1</dubbo.version> <zookeeper.version>3.5.2-alpha</zookeeper.version> <curator.version>4.0.1</curator.version> </properties> <!-- dubbo包 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <!-- 排除dubbo自帶的spring和netty,使用項目的,若是自己項目沒有,無需排除 --> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> <exclusion> <groupId>org.jboss.netty</groupId> <artifactId>netty</artifactId> </exclusion> </exclusions> </dependency> <!-- zookeeper包 --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <type>pom</type> </dependency> <!-- curator(zookeeper的客戶端)包 --> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-client</artifactId> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> </dependency>
還須要在相關配置文件加上dubbo
的bean
的頭部約束,將下面的添加到bean頭部便可:
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd
對於服務提供方,若是咱們想要TbItemService
對外提供服務:
package jit.wxs.service.impl; import jit.wxs.pojo.TbItem; import jit.wxs.mapper.TbItemMapper; import jit.wxs.service.TbItemService; import com.baomidou.mybatisplus.service.impl.ServiceImpl; import org.springframework.stereotype.Service; /** * <p> * 商品表 服務實現類 * </p> * * @author jitwxs * @since 2018-03-21 */ @Service public class TbItemServiceImpl extends ServiceImpl<TbItemMapper, TbItem> implements TbItemService { }
須要修改spring關於service的配置文件,加入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" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 掃描service層註解 --> <context:component-scan base-package="jit.wxs.service"/> <!-- dubbo發佈服務 --> <!-- 提供方應用信息,用於計算依賴關係 --> <dubbo:application name="e3-manager" /> <!-- 配置zookeeper的地址,集羣地址用逗號隔開 --> <dubbo:registry protocol="zookeeper" address="192.168.30.145:2181" /> <!-- 用dubbo協議在20880端口暴露服務 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 聲明須要暴露的服務接口 ref:爲注入的對應接口的bean timneout:超時時間,單位ms,開發模式能夠設長一點方便debug --> <dubbo:service interface="jit.wxs.service.TbItemService" ref="tbItemServiceImpl" timeout="600000"/> </beans>
dubbo:application
:提供方的應用名
dubbo:registry
:註冊中心的類型和地址
dubbo:protocol
:這個服務要暴露在哪一個端口上(使用方根據這個端口使用服務)
dubbo:service
:設置暴露的服務的接口,ref爲該接口的bean,timeout爲超時時間
服務使用方,我使用Spring MVC
來實現,修改Spring MVC的配置文件,加入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" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 掃描組件 --> <context:component-scan base-package="jit.wxs.web"/> <!-- 註解驅動 --> <mvc:annotation-driven /> <!-- 全局異常類 --> <!--<bean class="cn.edu.jit.exception.GlobalExceptionResolver"/>--> <!-- 視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 引用dubbo服務 --> <!-- 使用方應用信息,用於計算依賴關係 --> <dubbo:application name="e3-manager-web"/> <!-- 指定zookeeper的地址,集羣用逗號分隔 --> <dubbo:registry protocol="zookeeper" address="192.168.30.145:2181"/> <!-- 申明要訪問的接口,並建立代理對象,注入bean,名爲id的值 --> <dubbo:reference interface="jit.wxs.service.TbItemService" id="tbItemService" /> </beans>
dubbo:application
: 使用方的應用名
dubbo:registry
:註冊中心的類型和地址
dubbo:reference
:要使用的服務的接口,並將返回的注入bean,名稱爲id設的值
若是配置沒有問題的話,如今使用方已經可以使用提供方提供的服務了,直接將tbItemService
注入進來便可:
package jit.wxs.web; import jit.wxs.pojo.TbItem; import jit.wxs.service.TbItemService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * <p> * 商品表 前端控制器 * </p> * * @author jitwxs * @since 2018-03-21 */ @RestController @RequestMapping("/items") public class TbItemController { @Autowired private TbItemService tbItemService; @GetMapping("/{id}") public TbItem getItemById(@PathVariable Long id) { TbItem item = null; if(id != null) { item = tbItemService.selectById(id); } return item; } }
服務器上分別啓動zookper
和tomcat
:
wxs@ubuntu:/usr/zookeeper-3.5.2-alpha/bin$ ./zkServer.sh start ZooKeeper JMX enabled by default Using config: /usr/zookeeper-3.5.2-alpha/bin/../conf/zoo.cfg Starting zookeeper ... STARTED wxs@ubuntu:/usr/zookeeper-3.5.2-alpha/bin$ ./zkServer.sh status ZooKeeper JMX enabled by default Using config: /usr/zookeeper-3.5.2-alpha/bin/../conf/zoo.cfg Client port found: 2181. Client address: localhost. Mode: standalone
wxs@ubuntu:/usr/apache-tomcat-8.5.28/bin$ ./startup.sh Using CATALINA_BASE: /usr/apache-tomcat-8.5.28 Using CATALINA_HOME: /usr/apache-tomcat-8.5.28 Using CATALINA_TMPDIR: /usr/apache-tomcat-8.5.28/temp Using JRE_HOME: /usr/jdk1.8.0_161/jre Using CLASSPATH: /usr/apache-tomcat-8.5.28/bin/bootstrap.jar:/usr/apache-tomcat-8.5.28/bin/tomcat-juli.jar Tomcat started.
使用下面命令能夠持續顯示tomcat
的輸出:
wxs@ubuntu:/usr/apache-tomcat-8.5.28/bin$ tail -f ../logs/catalina.out
分別啓動服務提供方的項目和服務使用方的項目:
測試下/items/{id}
這個API:
成功!下面再訪問下監控中心,由於監控中心和zookeeper
在一臺服務器上,個人tomcat部署在8888端口,即訪問192.168.30.145:8888/dubbo-admin
便可,用戶名密碼默認爲root:
查看全部註冊的服務:
查看包括消費者和提供者的全部應用名:
消費者、提供者詳細信息:
本文轉自 做者:Jitwxs 來源:CSDN 原文:https://blog.csdn.net/yuanlaijike/article/details/79654183