為啥整合 Dubbo 實現 SOA Dubbo 不單單隻是高性能的 RPC 調用框架,更是 SOA 服務治理的一種方案。 ...
一、為啥整合 Dubbo 實現 SOA
Dubbo 不單單隻是高性能的 RPC 調用框架,更是 SOA 服務治理的一種方案。
核心:
1. 遠程通信,向本地調用一樣調用遠程方法。
2. 集群容錯
3. 服務自動發現和註冊,可平滑添加或者刪除服務提供者。
我們常常使用 Springboot 暴露 HTTP 服務,並走 JSON 模式。但慢慢量大了,一種 SOA 的治理方案。這樣可以暴露出 Dubbo 服務介面,提供給 Dubbo 消費者進行 RPC 調用。下麵我們詳解下如何集成 Dubbo。
二、windows10 搭建zookeeper 和啟動zookeeper 服務
運行環境:JDK 7 或 8,Maven 3.0+
技術棧:SpringBoot 1.5+、Dubbo 2.5+、ZooKeeper 3.3+
1.ZooKeeper 服務註冊中心
ZooKeeper 是一個分散式的,開放源碼的分散式應用程式協調服務。它是一個為分散式應用提供一致性服務的軟體,提供的功能包括:配置維護、功能變數名稱服務、分散式同步、組服務等。
Step1:下載最新版本且穩定的zookeeper,在http://apache.fayea.com/zookeeper/里就可以下載
step2:解壓下載的文件,zookeeper不需要安裝的,解壓即可。 然後把解壓後文件放到你要的位置,如:D:\zookeeper\zookeeper-3.3.6
Step3:進入D:\zookeeper\zookeeper-3.3.6\conf目錄,將裡面的zoo_sample.cfg文件,做一個備份,然後改名為zoo.cfg,因為zookeeper啟動後,只認識zoo.cfg中的所有設置和配置
Step4:打開zoo.cfg,輸入以下內容(如何打開,大家挑自己喜歡的編輯軟體,我用的是Notepad++)。
# The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. dataDir=D:\\zookeeper\\data dataLogDir=D:\\zookeeper\\log # the port at which the clients will connect clientPort=2181
之所以上面用“\\”,下麵用“/”,只是為了告訴大家,只有這2中斜杠的用法是正確的,其他都是錯誤的。
Step5:進入D:\zookeeper,文件夾下創建2個子目錄,目錄名分別為data、logs
Step6: 使用cmd,進入D:\zookeeper\zookeeper-3.3.6\bin,運行zkServer.cmd,啟動服務,如下圖(port:2181是zookeeper的專用監聽埠)。
三、springboot-dubbo-server 和 springboot-dubbo-client 工程配置詳解
1.詳解 springboot-dubbo-server Dubbo 服務提供者工程
springboot-dubbo-server 工程目錄結構
2、springboot-dubbo-server pom.xml配置文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zzg</groupId> <artifactId>springboot-dubbo-server</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- Spring Boot 啟動父依賴 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <properties> <dubbo-spring-boot>1.0.0</dubbo-spring-boot> </properties> <dependencies> <!-- Spring Boot Dubbo 依賴 --> <dependency> <groupId>io.dubbo.springboot</groupId> <artifactId>spring-boot-starter-dubbo</artifactId> <version>${dubbo-spring-boot}</version> </dependency> <!-- Spring Boot Web 依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </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>4.12</version> </dependency> </dependencies> </project>
3、springbooot-server-dubbo 資源配置文件 application.properties
## Dubbo \u670D\u52A1\u63D0\u4F9B\u8005\u914D\u7F6E spring.dubbo.application.name=provider spring.dubbo.registry.address=zookeeper://127.0.0.1:2181 spring.dubbo.protocol.name=dubbo spring.dubbo.protocol.port=20880 spring.dubbo.scan=com.spring.springboot.dubbo //dubbo 資源掃描文件
4、springboot-server-dubbo 功能源代碼
package com.spring.springboot.domain; import java.io.Serializable; public class City implements Serializable { private static final long serialVersionUID = -1L; /** * 城市編號 */ private Long id; /** * 省份編號 */ private Long provinceId; /** * 城市名稱 */ private String cityName; /** * 描述 */ private String description; public City() { } public City(Long id, Long provinceId, String cityName, String description) { this.id = id; this.provinceId = provinceId; this.cityName = cityName; this.description = description; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Long getProvinceId() { return provinceId; } public void setProvinceId(Long provinceId) { this.provinceId = provinceId; } public String getCityName() { return cityName; } public void setCityName(String cityName) { this.cityName = cityName; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
package com.spring.springboot.dubbo; import com.spring.springboot.domain.City; /** * 城市業務 Dubbo 服務層 * * Created by zzg on 01/04/2017. */ public interface CityDubboService { /** * 根據城市名稱,查詢城市信息 * @param cityName */ City findCityByName(String cityName); }
package com.spring.springboot.dubbo.impl; import com.alibaba.dubbo.config.annotation.Service; import com.spring.springboot.domain.City; import com.spring.springboot.dubbo.CityDubboService; /** * 城市業務 Dubbo 服務層實現層 * * Created by zzg on 01/04/2017. */ // 註冊為 Dubbo 服務 @Service(version = "1.0.0") public class CityDubboServiceImpl implements CityDubboService { public City findCityByName(String cityName) { // TODO Auto-generated method stub return new City(1L,2L,"南縣","是我的故鄉"); } }
package com.spring.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Spring Boot 應用啟動類 * * Created by zzg on 01/04/2017. */ // Spring Boot 應用的標識 @SpringBootApplication public class ServerApplication { public static void main(String[] args) { // 程式啟動入口 // 啟動嵌入式的 Tomcat 並初始化 Spring 環境及其各 Spring 組件 SpringApplication.run(ServerApplication.class,args); } }
2.詳解 springboot-dubbo-client Dubbo 服務消費者工程
1.springboot-dubbo-client 工程目錄結構
2、springboot-dubbo-client pom.xml配置文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zzg</groupId> <artifactId>springboot-dubbo-client</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- Spring Boot 啟動父依賴 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <properties> <dubbo-spring-boot>1.0.0</dubbo-spring-boot> </properties> <dependencies> <!-- Spring Boot Dubbo 依賴 --> <dependency> <groupId>io.dubbo.springboot</groupId> <artifactId>spring-boot-starter-dubbo</artifactId> <version>${dubbo-spring-boot}</version> </dependency> <!-- Spring Boot Web 依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </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>4.12</version> </dependency> </dependencies> </project>
3、springboot-dubbo-client 資源配置文件application.properties
## \u907F\u514D\u548C server \u5DE5\u7A0B\u7AEF\u53E3\u51B2\u7A81 server.port=8081 //防止服務提供者與服務消費者埠衝突 ## Dubbo \u670D\u52A1\u6D88\u8D39\u8005\u914D\u7F6E spring.dubbo.application.name=consumer spring.dubbo.registry.address=zookeeper://127.0.0.1:2181 spring.dubbo.scan=com.spring.springboot.dubbo //dubbo 服務掃描
4、springboot-server-client 功能源代碼
package com.spring.springboot.domain; import java.io.Serializable; public class City implements Serializable { /** * */ private static final long serialVersionUID = 1L; /** * 城市編號 */ private Long id; /** * 省份編號 */ private Long provinceId; /** * 城市名稱 */ private String cityName; /** * 描述 */ private String description; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public Long getProvinceId() { return provinceId; } public void setProvinceId(Long provinceId) { this.provinceId = provinceId; } public String getCityName() { return cityName; } public void setCityName(String cityName) { this.cityName = cityName; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Override public String toString() { return "City{" + "id=" + id + ", provinceId=" + provinceId + ", cityName='" + cityName + '\'' + ", description='" + description + '\'' + '}'; } }
package com.spring.springboot.dubbo; import org.springframework.stereotype.Component; import com.alibaba.dubbo.config.annotation.Reference; import com.spring.springboot.domain.City; /** * 城市 Dubbo 服務消費者 * * Created by zzg on 01/04/2017. */ @Component public class CityDubboConsumerService { @Reference(version = "1.0.0") CityDubboService cityDubboService; public CityDubboService getCityDubboService() { return cityDubboService; } public void setCityDubboService(CityDubboService cityDubboService) { this.cityDubboService = cityDubboService; } public void printCity() { String cityName = "南縣"; if(cityDubboService == null){ System.out.println("cityDubboService 服務對象 為空"); }else{ System.out.println("cityDubboService 服務對象 不 為空"); } City city = cityDubboService.findCityByName(cityName); System.out.println(city.toString()); } }
package com.spring.springboot.dubbo; import com.spring.springboot.domain.City; /** * 城市業務 Dubbo 服務層 * * Created by zzg on 01/04/2017. */ public interface CityDubboService { /** * 根據城市名稱,查詢城市信息 * @param cityName */ City findCityByName(String cityName); }
package com.spring.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import com.spring.springboot.dubbo.CityDubboConsumerService; /** * Spring Boot 應用啟動類 * * Created by zzg on 01/04/2017. */ // Spring Boot 應用的標識 @SpringBootApplication public class ClientApplication { public static void main(String[] args) { // TODO Auto-generated method stub // 程式啟動入口 // 啟動嵌入式的 Tomcat 並初始化 Spring 環境及其各 Spring 組件 ConfigurableApplicationContext run = SpringApplication.run(ClientApplication.class, args); CityDubboConsumerService cityService = run.getBean(CityDubboConsumerService.class); cityService.printCity(); } }
運行截圖: