Springboot 集成 Dubbo(以注解形式)
网址
Dubbo 官方文档:http://dubbo.apache.org/zh-cn/docs/user/quick-start.html
Zookeeper 安装
单机安装,见官网
案例一
使用专用接口定义
项目总体结构如下
springboot-dubbo ----dubbo-api ----pom ----src ----main ----java ----top.simba1949.service.DubboService.java ----dubbo-provider ----pom ----src ----main ----java ----top.simba1949 ----Application.java ----service.impl.DubboServiceImpl.java ----resources ----application.yml ----dubbo-consumer ----pom ----src ----main ----java ----top.simba1949 ----Application.java ----controller.DubboController ----resources ----application.yml
父项目
用于管理项目依赖和版本控制
Springboot 集成 Dubbo 的依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo-spring-boot-starter.version}</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> </dependency>
pom.xml
<properties> <lombok.version>1.18.4</lombok.version> <dubbo.version>2.6.4 </dubbo.version> <dubbo-spring-boot-starter.version>0.2.0</dubbo-spring-boot-starter.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>io.spring.platform</groupId> <artifactId>platform-bom</artifactId> <version>Cairo-SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <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.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <!-- 表示依赖不会传递 --> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo-spring-boot-starter.version}</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> </dependency> </dependencies> <build> <plugins> <!--编译插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <!-- 配置使用的 jdk 版本 --> <target>1.8</target> <source>1.8</source> </configuration> </plugin> <!--springboot-maven打包插件--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!--资源拷贝插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <!--实现热部署插件--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <!-- 如果没有该配置,devtools不会生效 --> </configuration> </plugin> </plugins> <!--IDEA是不会编译src的java目录的xml文件,如果需要读取,则需要手动指定哪些配置文件需要读取--> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*</include> </includes> </resource> </resources> </build>
接口定义 module
DubboService.java
package top.simba1949.service; /** * @author SIMBA1949 * @date 2019/1/21 22:34 */ public interface DubboService { /** * HELLO * @param hello * @return */ String str(String hello); }
服务提供者 module
服务提供者依赖接口定义 module
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-01</artifactId> <groupId>top.simba1949</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>dubbo-01-producer</artifactId> <dependencies> <dependency> <groupId>top.simba1949</groupId> <artifactId>dubbo-01-service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
application.yml 配置文件
server: port: 8091 # dubbo 配置 dubbo: application: id: dubbo-producer name: dubbo-producer # dubbo 注册中心地址 registry: address: zookeeper://192.168.239.147:2181 # 服务提供者的协议名和端口 protocol: server: true name: dubbo port: 20800
实现类 DubboServiceImpl.java 实现具体的业务逻辑
package top.simba1949.service.impl; import com.alibaba.dubbo.config.annotation.Service; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import top.simba1949.service.DubboService; /** * com.alibaba.dubbo.config.annotation.Service 是 Alibaba 包下的注解,表明这是 dubbo 服务提供者 * @author SIMBA1949 * @date 2019/1/21 22:34 */ @Slf4j @Component @Service public class DubboServiceImpl implements DubboService { @Override public String str(String hello) { log.info("服务提供者:" + hello); return "服务提供者道:" + hello; } }
启动类 Application.java
package top.simba1949; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @EnableDubbo 用于开启 dubbo 注解的扫描和自动配置 * @author SIMBA1949 * @date 2019/1/21 22:32 */ @EnableDubbo @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
服务消费者 module
服务消费者依赖接口定义 module
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-01</artifactId> <groupId>top.simba1949</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>dubbo-01-consumer</artifactId> <dependencies> <dependency> <groupId>top.simba1949</groupId> <artifactId>dubbo-01-service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
application.yml
server: port: 8081 dubbo: application: id: dubbo-consumer name: dubbo-consumer registry: address: zookeeper://192.168.239.147:2181 protocol: name: dubbo port: 20800 consumer: check: false
调用服务的具体的类 DubboController.java
package top.simba1949.controller; import com.alibaba.dubbo.config.annotation.Reference; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import top.simba1949.service.DubboService; /** * @Reference 会自动注入 dubbo 服务 * @author SIMBA1949 * @date 2019/1/21 22:45 */ @RestController @RequestMapping @Slf4j public class DubboController { @Reference private DubboService dubboService; @GetMapping("hello") public String str(String hello){ log.info("消费者消费前:" + hello); String str = dubboService.str(hello); log.info("消费者消费后:" + str); return str; } }
启动类
package top.simba1949; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author SIMBA1949 * @date 2019/1/21 22:43 */ @SpringBootApplication @EnableDubbo public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
案例二
服务提供者和服务消费者,使用相同的接口和实体类,但是属于不同的文件
服务提供者
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"> <modelVersion>4.0.0</modelVersion> <groupId>top.simba1949</groupId> <artifactId>dubbo-02-provider</artifactId> <version>1.0-SNAPSHOT</version> <properties> <lombok.version>1.18.4</lombok.version> <dubbo.version>2.6.4 </dubbo.version> <dubbo-spring-boot-starter.version>0.2.0</dubbo-spring-boot-starter.version> <fastjson.version>1.2.55</fastjson.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>io.spring.platform</groupId> <artifactId>platform-bom</artifactId> <version>Cairo-SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <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.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <!-- 表示依赖不会传递 --> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo-spring-boot-starter.version}</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> </dependency> </dependencies> <build> <plugins> <!--编译插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <!-- 配置使用的 jdk 版本 --> <target>1.8</target> <source>1.8</source> </configuration> </plugin> <!--springboot-maven打包插件--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!--资源拷贝插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <!--实现热部署插件--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <!-- 如果没有该配置,devtools不会生效 --> </configuration> </plugin> </plugins> <!--IDEA是不会编译src的java目录的xml文件,如果需要读取,则需要手动指定哪些配置文件需要读取--> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*</include> </includes> </resource> </resources> </build> </project>
application.yml
server: port: 8071 # DUBBO CONFIGURE dubbo: application: id: springboot-dubbo-provider name: springboot-dubbo-provider registry: address: zookeeper://192.168.239.147:2181 protocol: server: true name: dubbo port: 20801
实体类
package top.simba1949.common; import lombok.Data; import java.io.Serializable; import java.util.Date; /** * @author SIMBA1949 * @date 2019/1/21 23:45 */ @Data public class UserDto implements Serializable { private static final long serialVersionUID = 2223107801863147437L; private Long id; private String username; private Date birthday; private Boolean flag; }
接口类
package top.simba1949.service; import top.simba1949.common.UserDto; /** * @author SIMBA1949 * @date 2019/1/21 23:45 */ public interface UserService { /** * USER * @param userStr * @return */ UserDto user(String userStr); }
接口实现类
package top.simba1949.service.impl; import com.alibaba.dubbo.config.annotation.Service; import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import top.simba1949.common.UserDto; import top.simba1949.service.UserService; /** * @author SIMBA1949 * @date 2019/1/21 23:45 */ @Component @Slf4j @Service public class UserServiceImpl implements UserService { @Override public UserDto user(String userStr) { log.info("服务提供者获取的数据为:" + userStr); UserDto userDto = JSON.parseObject(userStr, UserDto.class); userDto.setUsername("服务提供者:" + userDto.getUsername()); return userDto; } }
启动类
server: port: 8071 # DUBBO CONFIGURE dubbo: application: id: springboot-dubbo-provider name: springboot-dubbo-provider registry: address: zookeeper://192.168.239.147:2181 protocol: server: true name: dubbo port: 20801
服务消费者
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"> <modelVersion>4.0.0</modelVersion> <groupId>top.simba1949</groupId> <artifactId>dubbo-02-consumer</artifactId> <version>1.0-SNAPSHOT</version> <properties> <lombok.version>1.18.4</lombok.version> <dubbo.version>2.6.4 </dubbo.version> <dubbo-spring-boot-starter.version>0.2.0</dubbo-spring-boot-starter.version> <fastjson.version>1.2.55</fastjson.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>io.spring.platform</groupId> <artifactId>platform-bom</artifactId> <version>Cairo-SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <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.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <!-- 表示依赖不会传递 --> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo-spring-boot-starter.version}</version> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> </dependency> </dependencies> <build> <plugins> <!--编译插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <!-- 配置使用的 jdk 版本 --> <target>1.8</target> <source>1.8</source> </configuration> </plugin> <!--springboot-maven打包插件--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!--资源拷贝插件--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <configuration> <encoding>UTF-8</encoding> </configuration> </plugin> <!--实现热部署插件--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <!-- 如果没有该配置,devtools不会生效 --> </configuration> </plugin> </plugins> <!--IDEA是不会编译src的java目录的xml文件,如果需要读取,则需要手动指定哪些配置文件需要读取--> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*</include> </includes> </resource> </resources> </build> </project>
配置文件
server: port: 8079 # DUBBO CONFIGURE dubbo: application: id: springboot-dubbo-consumer name: springboot-dubbo-consumer registry: address: zookeeper://192.168.239.147:2181 protocol: name: dubbo port: 20801 consumer: check: false
实体类
package top.simba1949.common; import lombok.Data; import java.io.Serializable; import java.util.Date; /** * @author SIMBA1949 * @date 2019/1/21 23:45 */ @Data public class UserDto implements Serializable { private static final long serialVersionUID = 2223107801863147437L; private Long id; private String username; private Date birthday; private Boolean flag; }
接口类
package top.simba1949.service; import top.simba1949.common.UserDto; /** * @author SIMBA1949 * @date 2019/1/21 23:45 */ public interface UserService { /** * USER * @param userStr * @return */ UserDto user(String userStr); }
调用dubbo服务java类
package top.simba1949.controller; import com.alibaba.dubbo.config.annotation.Reference; import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import top.simba1949.common.UserDto; import top.simba1949.service.UserService; /** * @author SIMBA1949 * @date 2019/1/21 23:58 */ @RestController @RequestMapping @Slf4j public class UserController { @Reference private UserService userService; @PostMapping("user") public UserDto user(@RequestBody UserDto userDto){ log.info("消费者消费之前的数据:" + userDto); String userStr = JSON.toJSONString(userDto); UserDto user = userService.user(userStr); log.info("消费者消费之后的数据:" + user); return user; } }
启动类
package top.simba1949; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author SIMBA1949 * @date 2019/1/21 23:43 */ @EnableDubbo @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
Dubbo Admin 管理的安装
Dubbo Admin 安装