在分佈式系統中,國內經常使用 Zookeeper + Dubbo 組合,而 SpringBoot 推薦使用 Spring 提供的分佈式一站式解決方案 Spring + SpringBoot + SpringCloud。html
圖 2:Dubbo 架構圖java
參考【Docker 中安裝 Zookeeper】。git
一、新建 Provider 工程,引入 Web 場景啓動器和 Dubbo 及 Zookeeper 相關依賴:github
<?xml version="1.0" encoding="UTF-8"?> <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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.20.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>zze.springboot.dubbo</groupId> <artifactId>provider-ticket</artifactId> <version>0.0.1-SNAPSHOT</version> <name>provider-ticket</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--引入 Dubbo 整合 SpringBoot 的場景啓動器--> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.1.0</version> </dependency> <!--引入 Zookeeper 客戶端工具依賴--> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
二、配置 Provider 工程註冊到 Zookeeper:web
dubbo: application: name: provider-ticket # 指定服務應用名稱 registry: address: zookeeper://192.168.202.136:2181 # Zookeeper 服務地址 scan: base-packages: zze.springboot.dubbo.ticket.service # 掃描指定包下服務註冊到 Zookeeper
三、編寫要註冊的服務接口及其實現類:spring
package zze.springboot.dubbo.ticket.service; public interface TicketService { public String getTicket(); }
package zze.springboot.dubbo.ticket.service.impl; import com.alibaba.dubbo.config.annotation.Service; import org.springframework.stereotype.Component; import zze.springboot.dubbo.ticket.service.TicketService; @Component // 註冊到 IoC 容器 @Service // 使用 Dubbo 的 @Service 註解標註這是一個要發佈的服務 public class TicketServiceImpl implements TicketService { @Override public String getTicket() { return "《大話西遊》"; } }
四、啓動工程,看到 Dubbo 服務註冊信息以下:apache
一、新建 Consumer 工程,引入 Web 場景啓動器和 Dubbo 及 Zookeeper 相關依賴:springboot
<?xml version="1.0" encoding="UTF-8"?> <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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.20.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>zze.springboot.dubbo</groupId> <artifactId>consumer-user</artifactId> <version>0.0.1-SNAPSHOT</version> <name>consumer-user</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--引入 Dubbo 整合 SpringBoot 的場景啓動器--> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.1.0</version> </dependency> <!--引入 Zookeeper 客戶端工具依賴--> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
二、配置 Zookeeper 地址:架構
dubbo: application: name: consumer-user # 配置消費者服務應用名稱 registry: address: zookeeper://192.168.202.136:2181 # Zookeeper 服務地址
三、拷貝 Provider 工程中服務接口到 Consumer 工程:app
package zze.springboot.dubbo.ticket.service; public interface TicketService { public String getTicket(); }
四、編寫 Service 類,遠程引用 Provider 提供的 Service 實現:
package zze.springboot.dubbo.user.service; import com.alibaba.dubbo.config.annotation.Reference; import org.springframework.stereotype.Service; import zze.springboot.dubbo.ticket.service.TicketService; @Service public class UserService { @Reference // 遠程引用 Provider 提供的實現 private TicketService ticketService; public String buyTicket(){ return ticketService.getTicket(); } }
五、測試:
package zze.springboot.dubbo.user; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import zze.springboot.dubbo.user.service.UserService; @RunWith(SpringRunner.class) @SpringBootTest public class ConsumerUserApplicationTests { @Autowired private UserService userService; @Test public void contextLoads() { String s = userService.buyTicket(); System.out.println(s); /* 《大話西遊》 */ } }