本節將簡單整合SpringBoot+Duboo
1、Zookeeper的安裝:
1 從官網下載你喜歡的zookeeper的版本:
http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz
2 解壓到你指定的目錄,我的是xx/software/zookeeper3 修改/conf 文件夾下的zoo_sample.cfg,改名爲zoo.cfg,並且修改裏面的內容爲:
tickTime=2000
dataDir=xx/software/data
dataLogDir=xx/software/log
clientPort=2181
也就是說你要創建兩個目錄data和log,來保存數據文件和日誌文件4 回到zookeeper目錄,進入bin目錄,./zkServer.sh 啓動zookeeper
5 可以使用./zkCli.sh 來進入zookeeper的環境,並且進行簡單的操作。不過本人推薦使用dubbo管控臺來操作。
2、dubbo管控臺:
1 從 https://pan.baidu.com/s/1rJf4-Y4-2RLq1Gy4NQsdZQ 密碼:2vfz 下載dubbo的管控臺。
2 然後在一個tomcat中解壓,解壓後修改裏面一個配置文件dubbo.proerties,修改IP爲你的zookeeper服務器的地址。
dubbo.registry.address=zookeeper://xxx.xxx.xxx.xxx:2181
dubbo.admin.root.password=root
dubbo.admin.guest.password=guest
後面兩個參數可以根據你的興趣來修改。3 重新啓動dubbo管控臺,默認賬號root,密碼root,就進入dubbo管控臺主頁,並且此時已經連接上你的zookeeper。
3、SpringBoot搭建後臺框架:1 要使用Dubbo,最基本需要兩個項目,一個是用來提供服務(ServiceProvider),一個用來消費服務(ServiceConsumer)。也可以將服務的接口專門提出來寫一個項目(ServiceAPI),然後讓ServiceProvider去調用ServiceAPI所定義的服務。本項目使用的是SpringBoot做爲搭建框架。
2 在IDEA中,創建一個maven項目,命名爲dubbo-parent,作爲所有項目的父項目,用來管理版本信息,pom文件的部分內容爲:
<modules>
<module>architecture-dubbo-api</module>
<module>architecture-dubbo-consumer</module>
<module>architecture-dubbo-provider</module>
</modules>
<properties>
<!-- 父類統一管理版本信息 -->
<junit.version>4.12</junit.version>
<dubbo-spring-boot>1.0.0</dubbo-spring-boot>
<zookeeper.version>3.4.10</zookeeper.version>
<dubbo.version>2.5.6</dubbo.version>
</properties>
3 在以上項目右鍵創建一個Module,命名爲dubbo-service-provider,用來暴露服務,此項目的pom文件部分內容爲: <!-- 使用另外一種方式,不繼承parent,使用scope=import導入的方式使用Spring Boot -->
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Spring Boot Dubbo 依賴 -->
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>${dubbo-spring-boot}</version>
</dependency>
<!-- Spring Boot Test 依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
</dependencies>
4 application.yml文件爲:
# Dubbo 服務提供者配置
spring:
dubbo:
application:
name: provider
registry:
address: zookeeper://xxx.xxx.xxx.xxx:2181 #zookeeper的註冊中心
protocol:
name: dubbo
port: 20880
scan: com.laowang.service.impl #這裏是你要暴露服務的包的完整路徑
# Dubbo 服務提供者配置
spring:
dubbo:
application:
name: provider
registry:
address: zookeeper://xxx.xxx.xxx.xxx:2181 #zookeeper的註冊中心
protocol:
name: dubbo
port: 20880
scan: com.laowang.service.impl #這裏是你要暴露服務的包的完整路徑
5 在父項目右鍵創建一個Module,命名爲dubbo-service-consumer,用來消費服務,此項目的pom文件部分內容爲:
<!-- 使用另外一種方式,不繼承parent,使用scope=import導入的方式使用Spring Boot -->
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- Spring Boot Dubbo 依賴 -->
<dependency>
<groupId>io.dubbo.springboot</groupId>
<artifactId>spring-boot-starter-dubbo</artifactId>
<version>${dubbo-spring-boot}</version>
</dependency>
<!-- 依賴服務暴露項目 -->
<dependency>
<groupId>com.laowang</groupId>
<artifactId>architecture-dubbo-provider</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- springboot web 依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
6 application.yml爲:#Dubbo 服務消費者配置
server:
port: 8081
spring:
dubbo:
application: #應用配置,用於配置當前應用信息,不管該應用是提供者還是消費者。
name: consumer
registry: #註冊中心配置,用於配置連接註冊中心相關信息。
address: zookeeper://xxx.xxx.xxx.xxx:2181
protocol: #協議配置,用於配置提供服務的協議信息,協議由提供方指定,消費方被動接受。
name: dubbo
port: 20880
scan: com.laowang.dubbo #服務暴露與發現消費所在的package
PS:請大家注意yml語法,這裏編輯器無法正常顯示格式。很鬱悶。
7 先啓動provider項目,然後可以觀察dubbo管控臺是否有服務已經註冊到zookeeper上:
8 然後啓動consumer項目,訪問剛剛的服務即可,這樣,一個簡單的SpringBoot+Dubbo框架就搭好了。
目前的項目結構是這樣的:
parent:
provider:
consumer: