当springboot和dubbo走到了一起,那么如何让它两更快更容易的集成在一起呢?dubbo-spring-boot-starter帮助我们做到了这点,它实现了dubbo在springboot项目中自动配置(@EnableAutoConfiguration)。
dubbo-spring-boot-starter模块
dubbo-spring-boot-starter主要由以下部分内容组成:
- dubbo-spring-boot-parent 模块主要管理 Dubbo Spring Boot 工程的 Maven 依赖;
- dubbo-spring-boot-autoconfigure 模块提供 Spring Boot’s @EnableAutoConfiguration 的实现 - DubboAutoConfiguration, 它简化了 Dubbo 核心组件的装配;
- dubbo-spring-boot-actuator 提供 Production-Ready 特性:健康检查、控制断点、外部化配置等;
dubbo-starter方式集成dubbo案例
现在通过一个案例来看springboot使用dubbo-spring-boot-starter来集成dubbo是多么简单的事儿。
这里通过dubbo直连的方式,不引入注册中心。
首先在项目POM文件中中引入如下信息(制定dubbo的版本号以及dubbo的依赖管理):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| <properties> <dubbo.version>2.7.0</dubbo.version> </properties>
<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> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-bom</artifactId> <version>${dubbo.version}</version> <type>pom</type> <scope>import</scope> </dependency>
<dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> <exclusion> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </dependencyManagement>
|
然后定义一个公共的server-api:
1 2 3 4
| public interface HelloService {
String hello(String name); }
|
springboot-dubbo-provider
在服务提供者中引入如下依赖:
1 2 3 4 5 6 7 8 9 10 11 12 13
| <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>
|
在application.yml文件中设置dubbo的简单配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| spring: application: name: springboot-dubbo-provider
dubbo:
protocol: name: dubbo port: 28080 registry: address: N/A scan: base-packages: com.lazycece.sbac.dubbo.provider.service provider: group: demo
|
HelloSerice服务提供信息如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.lazycece.sbac.dubbo.provider.service;
import com.lazycece.sbac.dubbo.api.HelloService; import org.apache.dubbo.config.annotation.Service; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;
@Component @Service(version = "1.0.0", timeout = 3000) public class HelloServiceImpl implements HelloService {
@Value("${dubbo.application.name}") private String serviceName;
@Override public String hello(String name) { return String.format("Hello, %s ! the service is %s", name, serviceName); } }
|
springboot-dubbo-consumer
在服务消费者中引入如下依赖:
1 2 3 4 5 6 7 8 9 10 11 12 13
| <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
|
在application.yml文件中设置dubbo的简单配置:
1 2 3 4 5 6
| spring: application: name: springboot-dubbo-consumer dubbo: consumer: group: demo
|
HelloSerice服务消费案例代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.lazycece.sbac.dubbo.consumer.controller;
import com.lazycece.sbac.dubbo.api.HelloService; import org.apache.dubbo.config.annotation.Reference; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController;
@RestController public class HelloController {
@Reference(version = "1.0.0", url = "dubbo://127.0.0.1:28080") private HelloService helloService;
@GetMapping("/hello/{name}") public String hello(@PathVariable String name) { return helloService.hello(name); } }
|
结果验证
在浏览器访问如下地址:http://localhost:8080/hello/lazycece
输出信息为: Hello, lazycece ! the service is springboot-dubbo-provider
案例代码
案例代码地址:https://github.com/lazycece/springboot-actual-combat/tree/master/springboot-ac-dubbo