# 【Dubbo】使用 Dubbo 整合 SpringBoot 搭建一个服务
# 文章目录
# 开发前提
由于 dubbo 的注册中心用的是 zookeeper,所以首先需要安装 zookeeper 。
# 构建 Springboot 项目
第一步:选择新建 project 或者 module,在界面中选择 maven 点击 next:
第二步:填上项目的基本信息点击 Finish:
第三步:右击项目 new -> Module:
第四步:在界面中选择 maven 点击 next:
第五步:填上项目的基本信息点击 Finish:
第六步:重复第三,四,五步,分别创建项目需要的 dubbo-api,dubbo-provider,dubbo-customer 几个模块,如下:
第七步:导入父工程依赖:
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 <?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.demo</groupId> <artifactId>dubbo-demo</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>dubbo-provider</module> <module>dubbo-customer</module> <module>dubbo-api</module> </modules> <properties> <java.version>1.8</java.version> <source.level>1.8</source.level> <target.level>1.8</target.level> <lombok.version>1.18.16</lombok.version> <skip_maven_deploy>true</skip_maven_deploy> <spring-boot-dependencies.version>2.4.1</spring-boot-dependencies.version> <spring-cloud-dependencies.version>Dalston.SR4</spring-cloud-dependencies.version> <junit.version>4.12</junit.version> <dubbo.version>3.0.2.1</dubbo.version> <spring-dubbo.version>2.0.0</spring-dubbo.version> <lombok.version>1.18.16</lombok.version> </properties> <dependencyManagement> <dependencies> <!-- 统一jar版本管理,避免使用 spring-boot-parent --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot-dependencies.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-bom</artifactId> <version>${dubbo.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!--dubbo 和 springboot 整合的包--> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> <scope>compile</scope> </dependency> </dependencies> </dependencyManagement> </project>
# 开发 api 模块
user 实体类:
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.demo.api.entity;import lombok.Data;import java.io.Serializable;@Data public class User implements Serializable { private Long id; private String username; private String password; }
创建本次测试的接口:
1 2 3 4 5 6 7 8 package com.demo.api.service;import com.demo.api.entity.User;public interface IUserService { User selectUserById (Long id) ; }
# 开发生产者模块
# 第一步:导入依赖
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 <?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-demo</artifactId> <groupId>com.demo</groupId> <version>1.0 -SNAPSHOT</version> </parent> <modelVersion>4.0 .0 </modelVersion> <artifactId>dubbo-provider</artifactId> <dependencies> <dependency> <groupId>com.demo</groupId> <artifactId>dubbo-api</artifactId> <version>1.0 -SNAPSHOT</version> </dependency> <!--dubbo 与 spring-boot 整合包--> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> </dependency> <!--springboot 启动核心包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!--springboot rest --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-registry-zookeeper</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3 .0 </version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3 .1 </version> </dependency> <!--mysql 的驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>** *.xml</include> </includes> <filtering>false </filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>** *.xml</include> </includes> <filtering>false </filtering> </resource> </resources> </build> </project>
# 第二步:添加配置
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 server: port: 8081 spring: application: name: dubbo-samples-privider-springCloud datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://8.142.127.37:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true username: root password: 123456 mybatis-plus: type-aliases-package: com.demo.provider.entity mapper-locations: com/demo/provider/mapper/*.xml dubbo: application: name: ${spring.application.name} registry: address: zookeeper://43.139.86.193:2181 timeout: 2000 protocol: name: dubbo port: 20890 scan: base-packages: com.demo.provider.service.impl
# 第三步:编写启动类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 package com.demo.provider;import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@EnableDubbo @SpringBootApplication @MapperScan("com.demo.provider.mapper") public class ProviderApp { public static void main (String[] args) { SpringApplication.run(ProviderApp.class,args); System.out.println("生产者启动完毕" ); } }
# 第四步:添加 mapper 接口
1 2 3 4 5 6 7 8 9 10 11 12 13 package com.demo.provider.mapper;import com.demo.api.entity.User;public interface UserMapper { User selectUserById (Long id) ; }
xml:
1 2 3 4 5 6 7 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.demo.provider.mapper.UserMapper" > <select id="selectUserById" resultType="com.demo.api.entity.User" > select * from user where id = #{id} </select> </mapper>
# 第五步:实现接口:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 package com.demo.provider.service.impl;import com.demo.api.entity.User;import com.demo.api.service.IUserService;import com.demo.provider.mapper.UserMapper;import lombok.AllArgsConstructor;import org.apache.dubbo.config.annotation.DubboService;@DubboService @AllArgsConstructor public class UserServiceImpl implements IUserService { private final UserMapper userMapper; public User selectUserById (Long id) { User user = userMapper.selectUserById(id); return user; } }
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 package com.demo.provider.controller; import com.demo.api.entity.User;import com.demo.api.service.IUserService;import lombok.AllArgsConstructor;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController @RequestMapping("/provider") @AllArgsConstructor public class UserController { private final IUserService userService; @RequestMapping("/selectUserById/{id}") public User selectUserById (@PathVariable("id") Long id) { return userService.selectUserById(id); } }
# 开发消费者模块
# 第一步:导入依赖
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 <?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-demo</artifactId> <groupId>com.demo</groupId> <version>1.0 -SNAPSHOT</version> </parent> <modelVersion>4.0 .0 </modelVersion> <artifactId>dubbo-customer</artifactId> <dependencies> <!--dubbo-samples-springcloud-api 项目 依赖--> <dependency> <groupId>com.demo</groupId> <artifactId>dubbo-api</artifactId> <version>1.0 -SNAPSHOT</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> </dependency> <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.apache.dubbo</groupId> <artifactId>dubbo-registry-zookeeper</artifactId> </dependency> </dependencies> </project>
# 第二步:添加配置
1 2 3 4 5 6 7 8 9 10 11 12 13 server: port: 8082 spring: application: name: dubbo-samples-consumer-springCloud dubbo: registry: address: zookeeper: timeout: 2000 protocol: name: dubbo
# 第三步:编写启动类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package com.demo.customer;import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication @EnableDubbo public class ConsumerApplication { public static void main (String[] args) { SpringApplication.run(ConsumerApplication.class, args); System.out.println("消费者启动完毕!" ); } }
# 第四步:编写调用生产者接口
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 package com.demo.customer.controller;import com.demo.api.entity.User;import com.demo.api.service.IUserService;import lombok.extern.slf4j.Slf4j;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;@RestController @RequestMapping("/consumer") @Slf4j public class ConsumerUserController { @DubboReference( protocol = "dubbo", loadbalance = "random") private IUserService userService; @RequestMapping("/selectUserById/{id}") public User getUser (@PathVariable("id") Long id) { User user = userService.selectUserById(id); log.info("response from provider: {}" , user); return user; } }
整个项目结构如下:
# 测试
分别启动生产者和消费者,在浏览器分别调用以下接口:
1 2 http://localhost:8081/provider/selectUserById/1` `http://localhost:8082/consumer/selectUserById/1
结果:
# 关于我
Brath 是一个热爱技术的 Java 程序猿,公众号「InterviewCoder」定期分享有趣有料的精品原创文章!
非常感谢各位人才能看到这里,原创不易,文章如果有帮助可以关注、点赞、分享或评论,这都是对我的莫大支持!