SpringBoot 整合 Dubbo
1、搭建 zookpeer 注册中心:
下载地址:https://zookeeper.apache.org/releases.html
(1)解压下载的 tar.gz 包(版本为 zookpeer-3.4.14版本):
(2)创建 data 目录,用于保存 zookpeer 的数据及日志:
(3)在 conf 目录下,复制一份 zoo_sample.cfg 文件,并重命名为 zoo.cfg(这是 zookpeer 默认读取的配置文件):
(4)修改 zoo.cfg 文件:
将 dataDir 的路径修改为刚刚创建的目录。
(5)启动 zookpeer :
在 bin 目录下,执行 zkServer.cmd 命令启动。(默认会占用 2181 端口)
2、搭建 Dubbo 管理控制台(基于 dubbo 2.7.3 版本):
(1)在 github 下载 dubbo admin 的源码:
git clone https://github.com/apache/dubbo-admin.git
下载后的源码结构如下:
由于 dubbo 2.7.x 版本采用的是前后端分离架构,所以分别进行打包。
(2)打包 dubbo-admin-server 模块(管理控制台后端接口):
进入 dubbo-admin-server 目录,并执行 mvn clean package -Dmaven.test.skip=true 命令:
打包完成后,在target目录下使用 java -jar dubbo-admin-server-0.1.jar 启动后端接口(默认会占用 8080 端口):
(3)打包 dubbo-admin-ui 模块(管理控制台前端页面):
由于该模块是采用 vue 前端工程方式构建,所以需要预先安装 node 环境,安装成功后,可使用 node -v 查看版本信息:
执行 npm install cnpm -g --registry=https://registry.npm.taobao.org 命令,将 node 包管理器切换为 cnpm
(因为npm安装插件都是从国外服务器下载,受网络影响大,可能出现异常):
执行 npm config get registry,查看 cnpm 是否配置成功:
然后在 dubbo-admin-ui 目录下执行 cnpm install 命令,安装依赖模块:
依赖模块安装完成后,执行 cnpm run dev 启动前端模块,待启动完成后通过 http://localhost:8081 即可访问控制台页面:
默认登陆用户名和密码都为为 root;如上,已成功看到 dubbo 管理控制台页面。
3、搭建 SpringBoot 多模块项目:
(1)代码结构如下:
sp-dubbo:父模块
service-consumer:消费者模块
service-provider:提供者模块
service-interface:公共接口
(2)sp-dubbo 父模块的 pom.xml 配置:
<?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> <groupId>sp-dubbo</groupId> <artifactId>sp-dubbo</artifactId> <packaging>pom</packaging> <version>1.0</version> <name>sp-dubbo</name> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <lombok.version>1.16.20</lombok.version> <spring-boot.version>2.1.6.RELEASE</spring-boot.version> <dubbo.version>2.7.3</dubbo.version> <dubbo-spring-boot-starter.version>2.7.3</dubbo-spring-boot-starter.version> </properties> <!-- 定义包信息,统一管理 --> <dependencyManagement> <dependencies> <!--使用 spring-boot-dependencies 管理包依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- Apache Dubbo --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-bom</artifactId> <version>${dubbo.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> <!-- Dubbo Spring Boot Starter --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo-spring-boot-starter.version}</version> </dependency> </dependencies> </dependencyManagement> <!-- 聚合子模块 --> <modules> <module>service-consumer</module> <module>service-provider</module> <module>service-interface</module> </modules> </project>
(3)service-interface 模块配置:
pom.xml 依赖:
<?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"> <parent> <artifactId>sp-dubbo</artifactId> <groupId>sp-dubbo</groupId> <version>1.0</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>service-interface</groupId> <artifactId>service-interface</artifactId> <version>1.0</version> <name>service-interface</name> <dependencies> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> </project>
User 类:
package inf.entity; import lombok.Data; import lombok.experimental.Accessors; import java.io.Serializable; @Data @Accessors(chain = true) public class User implements Serializable { private int userId; private String userName; private String address; }
IUserService 接口:
package inf.service; import inf.entity.User; import java.util.List; public interface IUserService { List<User> getUserList(); }
(3)service-provider 模块配置:
pom.xml 依赖:
<?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"> <parent> <artifactId>sp-dubbo</artifactId> <groupId>sp-dubbo</groupId> <version>1.0</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>service-provider</groupId> <artifactId>service-provider</artifactId> <version>1.0</version> <name>service-provider</name> <dependencies> <!-- 引入公共接口模块 --> <dependency> <groupId>service-interface</groupId> <artifactId>service-interface</artifactId> <version>1.0</version> </dependency> <!-- dubbo --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <exclusions> <exclusion> <artifactId>log4j</artifactId> <groupId>log4j</groupId> </exclusion> <exclusion> <artifactId>slf4j-log4j12</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <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> </dependencies> </project>
application.yml 配置:
# 启动端口
server:
port: 9001
dubbo:
# 提供者应用名称
application:
name: service-provider
# 使用dubbo协议暴露服务,端口为 20880
protocol:
name: dubbo
port: 20880
# 指定注册中心地址
registry:
address: zookeeper://127.0.0.1:2181
# 指定元数据中心
metadata-report:
address: zookeeper://127.0.0.1:2181
UserServiceImpl 类:
package provider.service; import inf.entity.User; import inf.service.IUserService; import org.apache.dubbo.config.annotation.Service; import java.util.ArrayList; import java.util.List; /** * 这里使用的是 dubbo 的 @Service 注解,用于暴露服务,并非 Spring的 @Service 注解 */ @Service(version = "1.0") public class UserServiceImpl implements IUserService { public List<User> getUserList() { User user = new User() .setUserId(1) .setUserName("Tom") .setAddress("上海"); List<User> userList = new ArrayList<>(); userList.add(user); return userList; } }
StartProvider9001 启动类:
package provider; import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableDubbo // 开启基于注解的 dubbo 功能 @SpringBootApplication public class StartProvider9001 { public static void main( String[] args ) { SpringApplication.run(StartProvider9001.class, args); } }
(4)service-consumer 模块配置:
pom.xml 依赖:
<?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"> <parent> <artifactId>sp-dubbo</artifactId> <groupId>sp-dubbo</groupId> <version>1.0</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>service-consumer</groupId> <artifactId>service-consumer</artifactId> <version>1.0</version> <name>service-consumer</name> <dependencies> <!-- 引入公共接口模块 --> <dependency> <groupId>service-interface</groupId> <artifactId>service-interface</artifactId> <version>1.0</version> </dependency> <!-- dubbo --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <exclusions> <exclusion> <artifactId>log4j</artifactId> <groupId>log4j</groupId> </exclusion> <exclusion> <artifactId>slf4j-log4j12</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <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> </dependencies> </project>
application.yml 配置:
# 启动端口
server:
port: 9000
dubbo:
application:
name: service-consumer
registry:
address: zookeeper://127.0.0.1:2181
metadata-report:
address: zookeeper://127.0.0.1:2181
UserController 类:
package consumer.controller; import inf.entity.User; import inf.service.IUserService; import org.apache.dubbo.config.annotation.Reference; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class UserController { // @Reference注解用于引用暴露的服务,version参数则对应服务的版本号 @Reference(version = "1.0") IUserService userService; @RequestMapping("/getUserList") public List<User> getUserList() { return userService.getUserList(); } }
StartConsumer9000 启动类:
package consumer; import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @EnableDubbo // 开启基于注解的 dubbo 功能 @SpringBootApplication public class StartConsumer9000 { public static void main( String[] args ) { SpringApplication.run(StartConsumer9000.class, args); } }
4、测试功能是否生效:
分别启动 provider 和 consumer 两个模块,然后访问:http://localhost:9000/getUserList:
如上,可成功获取到数据。再看下 dubbo 管理控制台是否有相应的服务:
如上,在 dubbo 管理控制台已成功获取到 consumer 和 provider 的服务接口信息。
参考:
http://dubbo.apache.org/zh-cn/docs/user/quick-start.html
https://github.com/apache/dubbo-admin
https://github.com/apache/dubbo-spring-boot-project