1. 程式人生 > >Dubbo + Zookeeper入門初探

Dubbo + Zookeeper入門初探

2018年2月15日,阿里巴巴的dubbo進入了Apache孵化器,社群的加入,希望dubbo能變得更好…

最近在學習一個分散式專案,使用到了dubbo,之前沒有使用過,體驗一下,分散式專案地址:點選這裡

使用dubbo官網的一張圖來介紹下dubbo(本人才開始學習,如有錯誤,歡迎指正):

  • Registry:註冊中心,相當於房產中介,服務提供者和使用者都需要在這裡註冊/使用服務,我使用zookeeper實現。

  • Monitor:監控中心,相當於房產局,它可以統計服務提供者和服務使用者的一些資訊,及他們之間的關係,我使用dubbo admin實現。

  • Provider:服務提供者,相當於房東

    ,提供服務。

  • Consumer:服務消費者,想當於租戶,使用服務。

下面我通俗的解釋下dubbo的整個流程,我將服務比喻成房子

start:dubbo一啟動,房東想好自己準備要租出去的房子

register:房東將房子拿到房產中介那邊進行登記,並留下自己的聯絡方式

subscribe:租戶告訴房產中介自己想租一個什麼樣的房子

notify:房產中介回覆給租戶符合條件的房子的房東的聯絡方式

invoke:租戶拿著聯絡方式去找房東租房子

count:房產局全程監控著房東和租戶之間的交易

其中:

  • start、register、subscribe在dubbo服務一啟動就完成了

  • notify、count是非同步執行的

  • invoke是同步執行的

一、搭建java和tomcat環境

二、搭建zookeeper

我使用的是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.cfgzoo.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 

配置下dataDirdataLogDir的路徑,為之前建立的兩個資料夾的路徑,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監控中心

版本要求:

請使用dubbo-admin-2.5.6.war及以上版本,否則會不支援JDK1.8!

下載連結:點選這裡

小心踩坑:

如果你的zookeeperdubbo-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>

還需要在相關配置檔案加上dubbobean的頭部約束,將下面的新增到bean頭部即可:

xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

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

4.1 服務提供方程式碼

對於服務提供方,如果我們想要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為超時時間

4.2 服務使用方程式碼

服務使用方,我使用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;
    }
}

五、測試

伺服器上分別啟動zookpertomcat

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的輸出:

[email protected]:/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:

檢視所有註冊的服務:

檢視包括消費者和提供者的所有應用名:

消費者、提供者詳細資訊:

相關推薦

Dubbo + Zookeeper入門初探

2018年2月15日,阿里巴巴的dubbo進入了Apache孵化器,社群的加入,希望dubbo能變得更好… 最近在學習一個分散式專案,使用到了dubbo,之前沒有使用過,體驗一下,分散式專案地址:點選這裡 使用dubbo官網的一張圖來介紹下

Dubbo,Zookeeper入門

同步 clip info try zab 協議 提供服務 分布式 emp Zookeeper 功能:分布式應用程序協調服務,集群管理者,監視集群各個節點狀態-->提交反饋-->進行下一步合理操作; 機制:目錄方式,當目錄節點發生變化(數據

Dubbo+Zookeeper入門例項

 現在的公司介面呼叫是通過dubbo來實現的,所以這兩天就瞭解了一下dubbo是如何進行通訊的,寫了一個最簡單的例子。自上得來終覺淺,絕知此事要躬行。     前言.dubbo介紹Dubbo是一個分散式服務框架,Dubbo的架構如圖所示:節點角色說明:Provider: 暴露

Dubbo+zookeeper入門示例搭建

-安裝zookeeper 1.在官網上下載zookeeper安裝檔案,解壓,重新命名zookeeper-3.4.5\conf目錄下的zoo_sample.cfg為zoo.cfg 2.在zookeeper-3.4.5\bin目錄下,雙擊zk

dubbo入門學習 四 註冊中心 zookeeper入門

一、Dubbo支援的註冊中心 1. Zookeeper   1.1 優點:支援網路叢集   1.2 缺點:穩定性受限於Zookeeper 2. Redis   2.1 優點:效能高.   2.2 缺點:對伺服器環境要求較高. 3. Multicast   3.1 優點:面中心化,不需要額外安裝

dubbo+zookeeper 分散式應用的快速入門

參考地址:https://blog.csdn.net/hua1586981/article/details/79195111  網際網路的快速迭代,隨之而來的客戶需求的不斷變更,要求開發人員,具有敏捷的反應能力,快速應對需求變化。怎麼快速應對變化,當然一個高可用,低耦合的分散式框架少不了

Dubbo新手入門例項HelloWorld(zookeeper)

最近剛接觸dubbo,新手入門遇到好多麻煩,網上搜來的入門demo也是各種問題,百般周折自己終於倒騰出來了,與大家共享~1.建立服務方專案dubbo-server,在pom.xml中構建專案依賴<project xmlns="http://maven.apache.or

Maven+SpringMVC+Dubbo+zookeeper 簡單的入門demo配置

參考:http://blog.csdn.net/aixiaoyang168/article/details/51362675dubbo是一個分散式服務框架,致力於提供高效能和透明化的RPC遠端服務呼叫方案,是阿里巴巴SOA服務化治理方案的核心框架,每天為2,000+個服務提供

Dubbo + zookeeper搭建分散式服務入門(帶原始碼)

dubbo + zookeeper 搭建分散式服務入門 dubbo是阿里開源的高效能RPC框架,框架圖如下: 可以分為4個部分,註冊中心,消費者,提供者和監控中心,這也是一般分散式服務的常見架構。 本文作為dubbo入門例子,採用zookeeper作為註冊

Dubbo zookeeper 初探

轉: 參考: Dubbo的簡介 http://doc.okbase.net/congcong68/archive/112508.html http://blog.csdn.net/congcong68/article/details/41113239 htt

springmvc+mybatis+dubbo+zookeeper分布式架構

中心 熱插拔 信息化 在線 cli zookeeper zookeep soft bat 平臺簡介 Jeesz是一個分布式的框架,提供項目模塊化、服務化、熱插拔的思想,高度封裝安全性的Java EE快速開發平臺。 Jeesz本身集成Dubbo

JEESZ 分布式架構--dubbo+zookeeper+springmvc+mybatis+shiro+redis

spring mvc+my batis kafka dubbo+zookeerper restful redis分布式緩存 平臺簡介 Jeesz是一個分布式的框架,提供項目模塊化、服務化、熱插拔的思想,高度封裝安全性的Java EE快速開發平臺。 Jeesz本身集成

分布式服務:Dubbo+Zookeeper+Proxy+Restful

spring mvc+my batis kafka dubbo+zookeerper restful redis分布式緩存 分布式分布式服務:Dubbo+Zookeeper+Proxy+Restful分布式消息中間件:KafKa+Flume+Zookeeper分布式緩存:Redis 分布

分布式服務--spring mvc +mybatis + Dubbo+Zookeeper+Proxy+Restful

spring mvc+my batis kafka dubbo+zookeerper restful redis分布式緩存 雲服務子系統:後臺管理系統、Restfu服務系統、Dubbo服務/管控/監控中心Zookeeper註冊中心、報表分析系統、日誌記錄系統、定時調度系統搜索引擎系統、分布式文

dubbo+zookeeper+springmvc+mybatis+shiro+redis分布式大型互聯網企業架構!

分布式、微服務、雲架構 dubbo+zookeeper springmvc+mybatis shiro+redis java分布式大型互聯網企業架構 spring 平臺簡介 Jeesz是一個分布式的框架,提供項目模塊化、服務化、熱插拔的思想,高度封裝安全性的Java EE

精華【分布式、微服務、雲架構、dubbo+zookeeper+springmvc+mybatis+shiro+redis】分布式大型互聯網企業架構!

net ios 系統數據庫 權限分配 容器 移動 activit str 重復 平臺簡介 Jeesz是一個分布式的框架,提供項目模塊化、服務化、熱插拔的思想,高度封裝安全性的Java EE快速開發平臺。 Jeesz本身集成Dubbo服務管控、

精華分布式、微服務、雲架構dubbo+zookeeper+springmvc+mybatis+shiro+redis分布式大型互聯網企業架構!

分布式、微服務、雲架構 spring springmvc dubbo+zookeeper spring mvc+mybatis redis分布式緩存 平臺簡介 Jeesz是一個分布式的框架,提供項目模塊化、服務化、熱插拔的思想,高度封裝安全性的Java EE快速開發平臺。

從頭開始搭建一個dubbo+zookeeper平臺

sa首先,看下一般網站架構隨著業務的發展,邏輯越來越復雜,數據量越來越大,交互越來越多之後的常規方案演進歷程。 其次,當服務越來越多之後,我們需要做哪些服務治理? 最後,是dubbo的架構圖 註冊中心的選擇 dubbo支持多種類型的註冊中心: Multicas

精華分布式、微服務、雲架構dubbo+zookeeper+springmvc+mybatis+shiro+redis分布式大型互聯網企業架構

分布式、微服務、雲架構 spring springmvc spring mvc+mybatis dubbo+zookeeper redis分布式緩存 平臺簡介 Jeesz是一個分布式的框架,提供項目模塊化、服務化、熱插拔的思想,高度封裝安全性的Java EE快速開發平臺。

精華分布式微服務雲架構dubbo+zookeeper+springmvc+mybatis+shiro+redis分布式大型互聯網企業架構

分布式、微服務、雲架構 spring springmvc dubbo+zookeeper spring mvc+mybatis redis分布式緩存 平臺簡介 Jeesz是一個分布式的框架,提供項目模塊化、服務化、熱插拔的思想,高度封裝安全性的Java EE快速開發平臺。