SpringBoot整合Dubbo服务提供者和消费者
前言
Github:https://github.com/HealerJean
Dubbo官方:http://dubbo.apache.org/
1、服务提供者和消费者
1.1、父工程:
项目名称:
hlj-dubbo
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.healerjean.proj</groupId>
<artifactId>hlj-parent</artifactId>
<version>${project.healerjean.version}</version>
<name>>hlj-parent</name>
<description>父工程</description>
<packaging>pom</packaging>
<modules>
<module>hlj-server-provider-2001</module>
<module>hlj-server-consumer-3001</module>
<module>hlj-api</module>
<module>hlj-common</module>
</modules>
<properties>
<project.healerjean.version>1.0.0-SNAPSHOT</project.healerjean.version>
<java.version>1.8</java.version>
<lombok.version>1.18.4</lombok.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!--swagger 版本-->
<swagger.version>2.7.0</swagger.version>
<!--要激活的resoure目录-->
<profiles.active>src/profiles/local</profiles.active>
<!--commons-lang3-->
<commons-lang3.version>3.7</commons-lang3.version>
<!-- zookeeper 版本-->
<zookeeper.version>3.4.10</zookeeper.version>
<!-- dubbo 版本-->
<dubbo.version>2.7.7</dubbo.version>
<curator.version>4.2.0</curator.version>
<zookeeper.version>3.4.9</zookeeper.version>
<disruptor.version>3.3.6</disruptor.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- Dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${curator.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>${curator.version}</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--lombok版本太低了,不支持java10 升级版本:>= 1.18.0-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
<!--StringUtils-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<profiles>
<profile>
<!-- 本地开发环境 -->
<id>local</id>
<properties>
<profiles.active>src/profiles/local</profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 开发环境 -->
<id>dev</id>
<properties>
<profiles.active>src/profiles/dev</profiles.active>
</properties>
<activation>
<property>
<name>dev</name>
<value>true</value>
</property>
</activation>
</profile>
<profile>
<!-- 生产环境 -->
<id>product</id>
<properties>
<profiles.active>src/profiles/product</profiles.active>
</properties>
<activation>
<property>
<name>product</name>
<value>true</value>
</property>
</activation>
</profile>
</profiles>
<build>
<!-- 定义资源目录 -->
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>${profiles.active}</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
1.2、API 工程
项目名称:
hlj-api
1.2.1、pom依赖
<?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>
<parent>
<groupId>com.healerjean.proj</groupId>
<artifactId>hlj-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>hlj-api</artifactId>
<version>${project.healerjean.version}</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- dubbo-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--StringUtils-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.10.3</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.2.2、dubbo
接口:
注意下面的包路径,将来 提供者和消费者都要扫描
com.healerjean.proj.service
package com.healerjean.proj.service;
import com.healerjean.proj.dto.UserDTO;
public interface ProviderDubboService {
UserDTO connect(String name);
}
1.3、服务提供者_2001
项目名称:
hlj-server-provider-2001
1.3.1、pom依赖
<!-- dubbo-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</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>
</dependency>
<?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>
<parent>
<groupId>com.healerjean.proj</groupId>
<artifactId>hlj-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>hlj-server-provider-2001</artifactId>
<version>${project.healerjean.version}</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions><!-- 去掉默认配置 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--hlj-api-->
<dependency>
<groupId>com.healerjean.proj</groupId>
<artifactId>hlj-api</artifactId>
<version>${project.healerjean.version}</version>
</dependency>
<!-- dubbo-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</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>
</dependency>
<!-- aop 切面 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--StringUtils-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- 引入log4j2依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- Log4j2 异步支持 -->
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.3.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.3.2、dubbo.properties
####################################
## Dubbo 服务提供者配置
####################################
dubbo.application.name=hlj-server-provider
dubbo.protocols.name=dubbo
dubbo.protocols.port=20880
dubbo.application.qosEnable=true
dubbo.application.qos-port=40880
dubbo.application.qosAcceptForeignIp=false
dubbo.registry.address=zookeeper://127.0.0.1:2181
# 配置Dubbo缓存文件,这个文件会缓存:注册中心的列表,服务提供者列表,有了这项配置后,当应用重启过程中,Dubbo注册中心不可用时则应用会从这个缓存文件读取服务提供者列表的信息,进一步保证应用可靠性。
dubbo.registry.file=/home/work/temp/dubbu_tmp/public-info
1.3.3、application.properties
spring.application.name=hlj-server-provider
server.port=2001
spring.main.allow-bean-definition-overriding=true
1.3.5、dubbo接口实现类
注意这里的
@Service
是dubbo
提供的
package com.healerjean.proj.service;
import com.healerjean.proj.dto.UserDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
@Slf4j
@DubboService
public class ProviderDubboServiceImpl implements ProviderDubboService {
@Override
public UserDTO connect(String name) {
log.info("消费者:ProviderDubboServiceImpl 【{} 】连接成功", name);
return new UserDTO().setName(name).setId(1L).setDescription("连接成功");
}
}
1.3.4、启动类
@EnableDubbo(scanBasePackages = "com.healerjean.proj.service")
:扫描接口的路径
package com.healerjean.proj;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
@EnableDubbo(scanBasePackages = "com.healerjean.proj.service")
@PropertySource("classpath:dubbo.properties")
@SpringBootApplication
public class ServerProvider_2001_Application {
public static void main(String[] args) {
SpringApplication.run(ServerProvider_2001_Application.class, args);
}
}
1.4、服务消费者_3001
项目名称:
hlj-server-consumer-3001
1.4.1、pom依赖
<?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>
<parent>
<groupId>com.healerjean.proj</groupId>
<artifactId>hlj-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>hlj-server-consumer-3001</artifactId>
<version>${project.healerjean.version}</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--hlj-api-->
<dependency>
<groupId>com.healerjean.proj</groupId>
<artifactId>hlj-api</artifactId>
<version>${project.healerjean.version}</version>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions><!-- 去掉默认配置 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- dubbo-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</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>
<version>${zookeeper.version}</version>
</dependency>
<!--hlj-common-->
<dependency>
<groupId>com.healerjean.proj</groupId>
<artifactId>hlj-common</artifactId>
<version>${project.healerjean.version}</version>
</dependency>
<!-- aop 切面 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!-- dubbo-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</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>
</dependency>
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--StringUtils-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- 引入log4j2依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- Log4j2 异步支持 -->
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.3.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.4.2、dubbo.properties
####################################
### dubbo zookeeper
####################################
dubbo.application.name=hlj-server-consumer
dubbo.protocols.name=dubbo
dubbo.protocols.port=20890
dubbo.application.qosEnable=true
dubbo.application.qosPort=40890
dubbo.application.qosAcceptForeignIp=false
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.consumer.timeout=3000
1.4.3、application.properties
spring.application.name=hlj-server-consumer
server.port=3001
spring.main.allow-bean-definition-overriding=true
1.4.4、启动类
@EnableDubbo(scanBasePackages = "com.healerjean.proj.service")
package com.healerjean.proj;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
@EnableDubbo(scanBasePackages = "com.healerjean.proj.service")
@PropertySource("classpath:dubbo.properties")
@SpringBootApplication
public class ServerConsumer_3001_Application {
public static void main(String[] args) {
SpringApplication.run(ServerConsumer_3001_Application.class, args);
}
}
1.4.5、dubbo调用
package com.healerjean.proj.controller;
import com.healerjean.proj.dto.UserDTO;
import com.healerjean.proj.service.ProviderDubboService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Api(description = "服务消费者_3001_控制器")
@RestController
@RequestMapping("api/consumer")
@Slf4j
public class ConsumerController extends BaseController {
@DubboReference
private ProviderDubboService providerDubboService;
@ApiOperation(value = "connect",
notes = "connect",
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
response = String.class)
@GetMapping(value = "connect")
public UserDTO connectProvider(String name) {
log.info("服务消费者控制器--------connect--------");
return providerDubboService.connect(name);
}
}
1.4.6、访问测试
http://custom.network.com:3001/api/consumer/connect?name=HealerJean
接口返回:
{
"id": 1,
"name": "HealerJean",
"description": "连接成功"
}
1.5、服务提供者_2002
复制一份2001 的代码,改变dubbo配置文件,如下
1.5.1、dubbo.properties
####################################
## Dubbo 服务提供者配置
####################################
dubbo.application.name=hlj-server-provider
dubbo.protocols.name=dubbo
dubbo.protocols.port=20881
dubbo.application.qosEnable=true
dubbo.application.qos-port=40881
dubbo.application.qosAcceptForeignIp=false
dubbo.registry.address=zookeeper://127.0.0.1:2181
# 配置Dubbo缓存文件,这个文件会缓存:注册中心的列表,服务提供者列表,有了这项配置后,当应用重启过程中,Dubbo注册中心不可用时则应用会从这个缓存文件读取服务提供者列表的信息,进一步保证应用可靠性。
dubbo.registry.file=/home/work/temp/dubbu_tmp/public-info
1.5.2、application.properties
spring.application.name=hlj-server-provider
server.port=2002
spring.main.allow-bean-definition-overriding=true
1.5.3、启动测试
服务提供者2001
服务提供者2002
服务消费者3001
1.5.3.1、访问接口
http://custom.network.com:3001/api/consumer/connect?name=HealerJean
会发现一会打到了服务提供者2001,一会打到了服务提供者2002 ,负载成功
2、dubbo注解的使用
2.1、@DubboService
服务提供者,接口实现需要配置该注解
@DubboService(version = "0.1", group = "inter_one")
属性 | 值 | 说明 |
---|---|---|
version | String | 服务版本(默认 0.0.0),建议使用两位数字版本,如:1.0,通常在接口不兼容时版本号才需要升级,也可以当做group使用,但是不建议 |
group | String | 服务分组,当一个接口有多个实现,可以用分组区分 |
public interface ProviderDubboService {
String connect(String name);
}
@Slf4j
@DubboService(version = "0.1", group = "inter_one")
public class ProviderDubboServiceImpl implements ProviderDubboService {
@Override
public String connect(String name) {
log.info("消费者:ProviderDubboServiceImpl 【{} 】连接成功", name);
int i = 1 / 0;
return name + ":连接成功";
}
}
@Slf4j
@DubboService(version = "0.1", group = "inter_other")
public class ProviderDubboOtherServiceImpl implements ProviderDubboService {
@Override
public String connect(String name) {
log.info("消费者 ProviderDubboOtherServiceImpl:【{} 】连接成功", name);
return name + ":连接成功";
}
}
2.2、@DubboReference
属性 | 值 | 说明 |
---|---|---|
version | String | 提供者接口的版本 |
group | String | 提供者的分组。当一个接口有多个实现的时候,可以用分组区别 |
mock | String | (非业务异常) 服务接口调用失败Mock实现类名,该Mock类必须有一个无参构造函数,与Local的区别在于,Local总是被执行,而Mock只在出现非业务异常(比如超时,网络异常等)时执行,Local在远程调用之前执行,Mock在远程调用后执行。 |
@RestController
@RequestMapping("api/consumer")
@Slf4j
public class ConsumerController extends BaseController {
// moke (非业务异常) 服务接口调用失败Mock实现类名,该Mock类必须有一个无参构造函数,与Local的区别在于,Local总是被执行,而Mock只在出现非业务异常(比如超时,网络异常等)时执行,Local在远程调用之前执行,Mock在远程调用后执行。
@DubboReference(version = "0.1", group = "inter_one", mock = "com.healerjean.proj.service.impl.Apple")
private ProviderDubboService providerDubboService;
@GetMapping(value = "connect")
public String connectProvider(String name) {
String connect = providerDubboService.connect(name);
return connect;
}
}
@Slf4j
public class Apple implements Fruits {
@Override
public void name() {
log.info("Fruits--------苹果");
}
}