前言
本次学习一下 springboot 整合 dubbo、zookeeper并做记录,前提需要先搭建起来 dubbo 和 zookeeper 。
代码已上传到 GitHub https://github.com/fengfanli/springboot-dubbo-demo,记得 mark一下哦
一、项目最后目录结构
项目是采用maven的 模块化搭建的。不了解模块化工程可以看一下这个博客
SpringBoot创建maven多模块项目,看这一篇文章即可
对目录进行分析:
分为三个模块:
- api(接口)(该模块必须要有,这里放置数据和接口)
- provider(业务接口提供者,实现接口,实现api模块中的 接口,让 customer 消费)
- customer(订阅消费服务,调用提供者中的接口)
二、创建根项目dubbo-demo
1. dubbo相关依赖
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>2.7.5</version>
</dependency>
2. 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>api</module>
<module>customer</module>
<module>provider</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.feng.dubbo</groupId>
<artifactId>dubbo-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-demo</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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>2.7.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- 打包跳过单元测试 -->
<!--<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>-->
</plugins>
</build>
</project>
三、 创建 api模块
1. api模块结构:
2. User实体类
@Data
@NoArgsConstructor
public class User implements Serializable {
private Integer id;
private String name;
private String sex;
private String age;
public User(Integer id, String name, String sex, String age) {
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
}
}
3. UserService 接口类
public interface UserService {
List<User> findAll();
String sayHello(String name);
}
- 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>dubbo-demo</artifactId>
<groupId>com.feng.dubbo</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.feng.dubbo</groupId>
<artifactId>api</artifactId>
<build>
<finalName>${artifactId}</finalName>
<plugins>
<!-- 配置Jar打包插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
四、 创建 provider 服务提供者 模块
1. provider模块目录
2. UserServiceImpl 接口实现类
这个地方注意一下 @Service() 为 dubbo的注解 import org.apache.dubbo.config.annotation.Service;
//@Service(version = "1.0.0", interfaceClass = UserService.class)
@Service()
public class UserServiceImpl implements UserService {
@Override
public List<User> findAll() {
LinkedList users = new LinkedList();
User user = new User(1, "小明", "男", "20");
User user1 = new User(2, "小紅", "女", "22");
users.add(user);
users.add(user1);
return users;
}
@Override
public String sayHello(String name) {
return "hello,"+name;
}
}
3. application.yml
这里的 address 为 zookeeper注册与服务中心 的 ip,端口默认为 2181,也可以修改。
dubbo:
application:
name: dubbo-provider
registry:
address: zookeeper://127.0.0.1:2181 # zookeeper 注册中心地址
protocol:
name: dubbo
port: 20880 # dubbo 协议端口,默认为20880
server:
port: 81
4. Provider 启动类
@SpringBootApplication
@EnableDubbo(scanBasePackages = "com")
public class Provider {
public static void main(String[] args) {
SpringApplication.run(Provider.class, args);
}
}
5. 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>dubbo-demo</artifactId>
<groupId>com.feng.dubbo</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.feng.dubbo</groupId>
<artifactId>provider</artifactId>
<dependencies>
<dependency>
<groupId>com.feng.dubbo</groupId>
<artifactId>api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<finalName>${artifactId}</finalName>
<plugins>
<!-- 配置Jar打包插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
五、创建 customer 消费者模块
1. 目录结构
2. TestController 控制器
@RestController
@RequestMapping("/user")
public class TestController {
@Reference
private UserService userService;
@RequestMapping("/findAll")
public List<User> findAll(){
return userService.findAll();
}
@RequestMapping("sayHello")
public String sayHello(String name){
return userService.sayHello(name);
}
@RequestMapping("hello/{name}")
public String hello(@PathVariable String name){
return "hello"+name;
}
}
3. application.yml 配置文件
dubbo:
application:
name: dubbo-customer
registry:
address: zookeeper://127.0.0.1:2181 # zookeeper 注册中心地址
server:
port: 80
4. Customer启动类
@SpringBootApplication
@EnableDubbo(scanBasePackages = "com")
public class Customer {
public static void main(String[] args) {
SpringApplication.run(Customer.class, args);
}
}
5. 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>dubbo-demo</artifactId>
<groupId>com.feng.dubbo</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.feng.dubbo</groupId>
<artifactId>customer</artifactId>
<dependencies>
<dependency>
<groupId>com.feng.dubbo</groupId>
<artifactId>api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
项目到此 搭建完成
六、运行、监控
1. 运行项目
- 首先运行zookeeper,双击
bin/zkServer.cmd
- 启动 provider 模块的
Provider.Main
方法 - 最后启动Customer 模块的
Customer.Main
方法,此时服务已被注册到 zookeeper ,且消费者已从 zookeeper 订阅所有被注册的服务。
2. 查看监控
- 启动dubbo的控制管理界面,这里采用上一个博客的2.5.8版本的
dubbo-admin
管理模块 - 浏览器登录如下:
http://localhost:8090/dubbo-admin-2.5.8/
- 查看提供者和消费者,都已经注册到zookeeper上了。
七、注意的点
- 提供者者模块中的
@Service()
注解为dubbo的注解