环境准备
本文基于 dubbo 2.7 与 spring boot 的整合。笔者提前准备的环境:
- jdk 1.8
- maven 3.3
- zookeeper 3.4.13
项目整体结构
- dubbo-spring-boot-demo
- dubbo-spring-boot-api
- dubbo-spring-boot-provider
- dubbo-spring-boot-consumer
该示例包含三个模块,其中 dubbo-spring-boot-api
定义接口,dubbo-spring-boot-provider
为服务提供者,dubbo-spring-boot-consumer
为服务消费者。
定义依赖
在 dubbo-spring-boot-demo
定义所有依赖。
<?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>com.joyxj</groupId>
<artifactId>dubbo-spring-boot-demo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>dubbo-spring-boot-api</module>
<module>dubbo-spring-boot-provider</module>
<module>dubbo-spring-boot-consumer</module>
</modules>
<properties>
<java.version>1.8</java.version>
<target.version>1.8</target.version>
<dubbo_version>2.7.0</dubbo_version>
<curator_version>4.0.1</curator_version>
<zookeeper_version>3.4.13</zookeeper_version>
<platform_bom_version>Cairo-SR7</platform_bom_version>
</properties>
<dependencyManagement>
<dependencies>
<!--引入platform bom 进行包管理-->
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>${platform_bom_version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo_version}</version>
</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.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper_version}</version>
<exclusions>
<exclusion>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>${curator_version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo_version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${target.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
接口定义
在 dubbo-spring-boot-api
模块定义接口。
package com.joyxj.dubbo.spring.boot.api;
/**
* Hello Service 接口
*
* @author xiaoj
* @version 1.0
* @date 2019-06-26
*/
public interface HelloService {
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-spring-boot-demo</artifactId> <groupId>com.joyxj</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>dubbo-spring-boot-provider</artifactId> <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.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> </dependency> <dependency> <groupId>com.joyxj</groupId> <artifactId>dubbo-spring-boot-api</artifactId> <version>${parent.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.yml
文件配置server: port: 8081 dubbo: protocol: port: 20881 # 端口 name: dubbo # 协议名称 id: dubbo #编号 registry: address: zookeeper://192.168.71.128:2181 # 注册中心 config-center: address: zookeeper://192.168.71.128:2181 # 配置中心 application: name: dubbo-spring-boot-provider #应用名称 qos-enable: false # 不启动运维
实现
UserService
接口`
java
package com.joyxj.dubbo.spring.boot.provider.impl;
import com.joyxj.dubbo.spring.boot.api.HelloService;
import org.apache.dubbo.config.annotation.Service;
/**
* Hello Service 实现类
*
* @author xiaoj
* @version 1.0
* @date 2019-06-26
*/
@Service(group = "dubbo-spring-boot-demo",version = "1.0.0")
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "Hello," + name;
}
}
```
这里使用注解的方式声明一个服务提供者。
启动类
package com.joyxj.dubbo.spring.boot.provider; import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 服务提供者启动类 * * @author xiaoj * @version 1.0 * @date 2019-06-26 */ @SpringBootApplication @EnableDubbo(scanBasePackages = "com.joyxj.dubbo.spring.boot.provider") @EnableDubboConfig public class DubboSpringBootProviderApplication { public static void main(String[] args) { SpringApplication.run(DubboSpringBootProviderApplication.class,args); } }
启动服务
执行
DubboSpringBootProviderApplication
的main
方法即可。
服务消费者
服务消费者与服务提供者的配置类似。
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-spring-boot-demo</artifactId> <groupId>com.joyxj</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>dubbo-spring-boot-consumer</artifactId> <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.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </dependency> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-recipes</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> </dependency> <dependency> <groupId>com.joyxj</groupId> <artifactId>dubbo-spring-boot-api</artifactId> <version>${parent.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.yml
配置server: port: 8082 dubbo: protocol: port: 20882 # 端口 name: dubbo # 协议名称 id: dubbo #编号 registry: address: zookeeper://192.168.71.128:2181 # 注册中心 config-center: address: zookeeper://192.168.71.128:2181 # 配置中心 application: name: dubbo-spring-boot-consumer #应用名称,与提供者不一致 qos-enable: false # 不启动运维
调用服务
这里提供一个 restful 接口服务去调用 dubbo 服务。也是生产的实践,dubbo 服务用于内部服务间的调用,而不对外提供。
package com.joyxj.dubbo.spring.boot.consumer.controller; import com.joyxj.dubbo.spring.boot.api.HelloService; import org.apache.dubbo.config.annotation.Reference; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * Hello Service Controller * * @author xiaoj * @version 1.0 * @date 2019-06-26 */ @RestController public class HelloServiceController { @Reference(group = "dubbo-spring-boot-demo",version = "1.0.0") private HelloService helloService; @RequestMapping(value = "hello/{name}") public String sayHello(@PathVariable("name") String name) { return helloService.sayHello(name); } }
启动类
`
java
package com.joyxj.dubbo.spring.boot.consumer;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 启动类
*
* @author xiaoj
* @version 1.0
* @date 2019-06-26
*/
@SpringBootApplication
@EnableDubboConfig
@EnableDubbo(scanBasePackages = "com.joyxj.dubbo.spring.boot.consumer")
public class DubboSpringBootConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboSpringBootConsumerApplication.class,args);
}
}
```
测试
浏览器输入
http://localhost:8082/hello/xiaoj
,返回Hello,xiaoj
源码地址
文章源码地址:dubbo-spring-boot-demo