1 Dubbo与SpringBoot的整合
基于Zookeeper实现Dubbo与Spring Boot的集成整合,依赖导入
<properties> <dubbo-version>2.7.8</dubbo-version> <spring-boot.version>2.3.0.RELEASE</spring-boot.version> </properties> <dependencyManagement> <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- Apache Dubbo --> <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> <dependencies> <!-- Dubbo Spring Boot Starter --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo-version}</version> </dependency> <!-- Dubbo核心组件 --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> </dependency> <!--Spring Boot 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>${spring-boot.version}</version> </dependency> <!-- Zookeeper客户端框架 --> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.0.1</version> </dependency> <!-- Zookeeper dependencies --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper</artifactId> <version>${dubbo-version}</version> <type>pom</type> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
Dubbo采用2.7.8版本, Spring Boot采用的是2.3.0.RELEASE版本。
如果依赖下载出现问题, 可以指定具体的仓库:
<repositories> <repository> <id>apache.snapshots.https</id> <name>Apache Development Snapshot Repository</name> <url>https://repository.apache.org/content/repositories/snapshots</url> <releases> <enabled>false</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories>
2 公用RPC接口工程
为便于客户端与服务端的RPC接口引用, 这里对RPC接口做统一封装。
定义了一个订单服务接口, 用于测试验证。
public interface OrderService { /** * 获取订单详情 * @param orderId * @return */ String getOrder(Long orderId); }
3. 服务端工程
- 工程结构
- POM依赖
<dependencies> <!-- Dubbo Spring Boot Starter --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo-version}</version> </dependency> <!-- Dubbo 核心依赖 --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> </dependency> <!-- 公用RPC接口依赖 --> <dependency> <groupId>com.oldlu</groupId> <artifactId>dubbo-spring-interface</artifactId> <version>${project.version}</version> </dependency> </dependencies>
- RPC服务接口
@DubboService(version = "${dubbo.spring.provider.version}") public class OrderServiceImpl implements OrderService { /** * 服务端口 */ @Value("${server.port}") private String serverPort; @Value("${dubbo.spring.provider.version}") private String serviceVersion; /** * 获取订单详情 * @param orderId * @return */ public String getOrder(Long orderId) { String result = "get order detail ,orderId="+orderId +",serverPort="+serverPort +",serviceVersion="+serviceVersion; System.out.println(result); return result; } }
通过DubboService注解, 声明为RPC服务,version可以标识具体的版本号, 消费端需匹配保持一致。
- 工程配置
# 服务端口 server.port=18081 # 应用程序名称 spring.application.name=dubbo-spring-provider # Dubbo服务扫描路径 dubbo.scan.base-packages=com.oldlu # Dubbo 通讯协议 dubbo.protocol.name=dubbo # Dubbo服务提供的端口, 配置为-1,代表为随机端口 默认20880 dubbo.protocol.port=-1 ## Dubbo 注册器配置信息 dubbo.registry.address=zookeeper://127.0.0.1:2181 dubbo.registry.file = ${user.home}/dubbo-cache/${spring.application.name}/dubbo.cache dubbo.spring.provider.version = 1.0.0
- Spring Boot启动程序
@SpringBootApplication @ComponentScan(basePackages = {"com.itheima"}) public class DubboSpringProviderApplication { public static void main(String[] args) { SpringApplication.run(DubboSpringProviderApplication.class, args); } }
4. 消费端工程
- 工程结构
- POM依赖:
<dependencies> <!-- Dubbo Spring Boot Starter --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo-version}</version> </dependency> <!-- 公用RPC接口依赖 --> <dependency> <groupId>com.itheima</groupId> <artifactId>dubbo-spring-interface</artifactId> <version>${project.version}</version> </dependency> </dependencies>
- 消费端调用
@Controller @RequestMapping("/order") public class OrderController { private final Logger logger = LoggerFactory.getLogger(getClass()); /** * 订单服务接口 */ @DubboReference(version = "${dubbo.spring.provider.version}") private OrderService orderService; /** * 获取订单详情接口 * @param orderId * @return */ @RequestMapping("/getOrder") @ResponseBody public String getOrder(Long orderId) { String result = null; try { result = orderService.getOrder(orderId); }catch(Exception e) { logger.error(e.getMessage(), e); } return result; } }
- 工程配置
# 服务端口 server.port=18084 #服务名称 spring.application.name=dubbo-spring-consumer #服务版本号 dubbo.spring.provider.version = 1.0.0 #消费端注册器配置信息 dubbo.registry.address=zookeeper://127.0.0.1:2181 dubbo.registry.file = ${user.home}/dubbo-cache/${spring.application.name}/dubbo.cache
- Spring Boot启动程序
@SpringBootApplication @ComponentScan(basePackages = {"com.itheima"}) public class DubboSpringConsumerApplication { public static void main(String[] args) { SpringApplication.run(DubboSpringConsumerApplication.class, args); } }
5. 工程调用验证
- 启动ZK注册中心
- 启动服务端, 运行DubboSpringProviderApplication
- 启动消费端, 运行DubboSpringConsumerApplication
- 请求获取订单接口, 地址: http://127.0.0.1:18084/order/getOrder?orderId=1001
调用成功: