SpringBoot与Dubbo整合-项目搭建
Posted Luke-lu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot与Dubbo整合-项目搭建相关的知识,希望对你有一定的参考价值。
本章节建立生产者和消费者来演示dubbo的demo
生产者:springboot-dubbo-provider 和 消费者:springboot-dubbo-consumer 工程配置详解
Apache官方Demo:https://github.com/alibaba/dubbo-spring-boot-starter
阿里巴巴官方Demo:https://github.com/apache/incubator-dubbo-spring-boot-project
GitHub 上代码: https://github.com/JeffLi1993/springboot-learning-example。
springboot-dubbo-provider:
1.pom.xml导入依赖 :
pom.xml 中依赖了 spring-boot-starter-dubbo 工程,该项目地址是 https://github.com/teaey/spring-boot-starter-dubbo。
<?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>springboot</groupId> <artifactId>springboot-dubbo-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-dubbo 服务端:: 整合 Dubbo/ZooKeeper 详解 SOA 案例</name> <!-- Spring Boot 启动父依赖 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <properties> <dubbo-spring-boot>1.0.0</dubbo-spring-boot> </properties> <dependencies> <!-- Spring Boot Dubbo 依赖 --> <dependency> <groupId>io.dubbo.springboot</groupId> <artifactId>spring-boot-starter-dubbo</artifactId> <version>${dubbo-spring-boot}</version> </dependency> <!-- Spring Boot Web 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot Test 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> </project>
2.application.properties 配置
server.port=8080 ## Dubbo 配置 spring.dubbo.application.name=provider spring.dubbo.registry.address=zookeeper://127.0.0.1:2181 spring.dubbo.protocol.name=dubbo spring.dubbo.protocol.port=20880 spring.dubbo.scan=org.spring.springboot.dubbo
// 注册为 Dubbo 服务 @Service(version = "1.0.0") public class CityDubboServiceImpl implements CityDubboService { public City findCityByName(String cityName) { return new City(1L,2L,"温岭","是我的故乡"); } }
springboot-dubbo-consumer:
1.application.properties 配置
## 避免和 server 工程端口冲突 server.port=8081 ## Dubbo 服务消费者配置 spring.dubbo.application.name=consumer spring.dubbo.registry.address=zookeeper://127.0.0.1:2181 spring.dubbo.scan=org.spring.springboot.dubbo
2.CityDubboConsumerService.java 城市 Dubbo 服务消费者
@Component public class CityDubboConsumerService { @Reference(version = "1.0.0") CityDubboService cityDubboService; public void printCity() { String cityName="温岭"; City city = cityDubboService.findCityByName(cityName); System.out.println(city.toString()); } }
这里将 CityDubboConsumerService 注入 Spring 容器,是为了更方便的获取该 Bean,然后验证这个 Dubbo 调用是否成功。
3.ClientApplication.java 客户端启动类
@SpringBootApplication public class ClientApplication { public static void main(String[] args) { // 程序启动入口 // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件 ConfigurableApplicationContext run = SpringApplication.run(ClientApplication.class, args); CityDubboConsumerService cityService = run.getBean(CityDubboConsumerService.class); cityService.printCity(); } }
解释下这段逻辑,就是启动后从 Bean 容器中获取城市 Dubbo 服务消费者 Bean。然后调用该 Bean 方法去验证 Dubbo 调用是否成功。
先启动生产者provider,然后启动消费者consumer,控制台打印数据,则调用成功!
本文参考来自:https://www.cnblogs.com/Alandre/p/6490142.html
以上是关于SpringBoot与Dubbo整合-项目搭建的主要内容,如果未能解决你的问题,请参考以下文章
Spring Boot 整合 Dubbo和Zookeeper
dubbo入门学习-----dubbo整合springboot