SpringBoot集成Dubbo
Dubbo教程(二) | SpringBoot集成Dubbo
一、Dubbo Spring Boot 版本关系
在使用 Dubbo 和 Spring Boot 进行微服务开发时,版本的选择是非常重要的。
不同版本之间可能会存在兼容性问题,导致服务无法正常运行。
因此,了解 Dubbo Spring Boot 版本关系是非常必要的。
目前网上有两个方式可以看匹配关系
方式一:Dubbo 官方提供
方式二: github 官方提供(已经过时了,就不要看了)
Dubbo 官方提供的 对应关系:
https://cn.dubbo.apache.org/zh-cn/overview/mannual/java-sdk/versions/
Dubbo分支 | 最新版本 | JDK | Spring Boot | 详细说明 |
---|---|---|---|---|
3.3.x (当前文档) | 3.3.0 | 8, 17, 21 | 2.x、3.x | 生产可用(推荐,长期维护)! 最新Triple协议升级,内置Metrics、Tracing、GraalVM支持等 |
3.2.x | 3.2.10 | 8, 17 | 2.x、3.x | 生产可用(长期维护)! |
3.1.x | 3.1.11 | 8, 17 | 2.x、3.x | 仅修复安全漏洞! |
3.0.x | 3.0.15 | 8 | 2.x | 停止维护! |
2.7.x | 2.7.23 | 8 | 2.x | 停止维护! |
2.6.x | 2.6.20 | 6, 7 | - | 停止维护! |
2.5.x | 2.5.10 | 6, 7 | - | 停止维护! |
github 官方提供的 对应关系(该版本说明很久没有更新了):
https://cn.dubbo.apache.org/zh-cn/overview/mannual/java-sdk/versions/
旧版本
如果您仍然使用版本低于 2.7.0 的旧版 Dubbo,请使用以下 Spring Boot 启动器:
Dubbo Spring Boot | Dubbo | Spring Boot |
---|---|---|
0.2.1.发布 | 2.6.5+ | 2.x |
0.1.2.发布 | 2.6.5+ | 1.x |
去看manven 官网 仓库里面你就会发现。工件已经迁移了
二、引入maven
Dubbo 起步依赖
<!--dubbo集成springboot依赖-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.15</version>
</dependency>
123456
ZooKeeper
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.7.0</version>
</dependency>
12345
ZooKeeper API 管理依赖(ZooKeeper的高级操作工具包)
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.1.0</version>
</dependency>
12345
三、项目结构
对目录进行分析:
分为三个模块:
- api 接口 :该模块必须要有,这里放置数据和接口)
- provider 生成者:业务接口提供者,实现接口,实现api模块中的 接口,让 customer 消费)
- customer 消费者:调用提供者中的接口
父模块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>
<groupId>com.example</groupId>
<artifactId>springboot-dubbo-demo</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
<modules>
<module>api</module>
<module>provider</module>
<module>customer</module>
</modules>
<name>springboot-dubbo-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.6.13</spring-boot.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>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!--dubbo集成springboot依赖-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.15</version>
</dependency>
<!-- Zookeeper 客户端依赖 -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.7.0</version>
</dependency>
<!-- ZooKeeper的高级操作工具包-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
aip模块 构建
aip 模块 目录结构 如下:
实体类
package com.example.api.model;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@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;
}
}
123456789101112131415161718192021222324
接口
package com.example.api.service;
import com.example.api.model.User;
import java.util.List;
public interface UserService {
List<User> findAll();
String sayHello(String name);
}
12345678910111213
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>springboot-dubbo-demo</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>api</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
12345678910111213141516171819
provider模块 构建
provider模块目录结构 如下:
业务实现类
关键注解:
@DubboService:主要用于标记一个服务提供者,使其能够被 Dubbo 框架识别并注册到服务注册中心。
package com.example.provider.service.impl;
import com.example.api.model.User;
import com.example.api.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import java.util.LinkedList;
import java.util.List;
@DubboService
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;
}
}
12345678910111213141516171819202122232425262728
启动类
关键注解:
@DubboComponentScan 扫描 Dubbo相关注解
package com.example.provider;
import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@DubboComponentScan(basePackages = "com.example.provider.*")
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
123456789101112131415
application.yml
server:
port: 8001
#生成者模块
dubbo:
application:
name: dubbo-provider
registry:
address: zookeeper://127.0.0.1:2181 # zookeeper 注册中心地址
protocol:
name: dubbo
port: 20880 # dubbo 协议端口,默认为20880
1234567891011
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>springboot-dubbo-demo</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>provider</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.example.provider.ProviderApplication</mainClass>
<!-- <skip>true</skip>-->
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
customer模块 构建
customer模块目录结构 如下:
控制层
关键注解:
@DubboReference :主要作用是在服务消费者端引用远程服务提供者的服务
package com.example.customer.controller;
import com.example.api.model.User;
import com.example.api.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/user")
public class TestController {
@DubboReference
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;
}
}
12345678910111213141516171819202122232425262728293031323334
启动类
关键注解:
@DubboComponentScan 扫描 Dubbo相关注解
package com.example.customer;
import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@DubboComponentScan(basePackages = "com.example.customer.*")
public class CustomerApplication {
public static void main(String[] args) {
SpringApplication.run(CustomerApplication.class, args);
}
}
123456789101112131415
application.yml
server:
port: 8002
#消费者模块
dubbo:
application:
name: dubbo-customer
registry:
address: zookeeper://127.0.0.1:2181 # zookeeper 注册中心地址
12345678
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>springboot-dubbo-demo</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>customer</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.example.customer.CustomerApplication</mainClass>
<!-- <skip>true</skip>-->
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
参考文章
【1】SpringBoot集成Dubbo实现RPC远程过程调用
【2】springboot 整合 dubbo 教程(注解方式) + 新版 dubbo admin 使用教程
【3】dubbo学习三:springboot整合dubbo+zookeeper,并使用dubbo管理界面监控服务是否注册到zookeeper上。