springboot2.0整合dubbo的示例代码

时间:2021-05-19

写在前面:

使用springboot作为web框架,方便开发许多,做分布式开发,dubbo又不可少,那么怎么整合在一起呢,

跟我学一遍,至少会用

注意,springboot2.0和springboot1.x与dubbo整合不一样,

1.环境

1.新建一个空的maven项目,作为父工程,新建moudle,,service(接口层,及实现层,没有具体分,),web(web层,springboot项目)

项目结构如下


父pom如下

<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencyManagement> <dependencies> <dependency> <!-- Import dependency management from Spring Boot --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.0.3.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--如果要把springboot工程打包成war执行,需要该jar--> <!--<dependency>--> <!--<groupId>org.springframework.boot</groupId>--> <!--<artifactId>spring-boot-legacy</artifactId>--> <!--<version>1.0.2.RELEASE</version>--> <!--</dependency>--> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> <!--引入zookeeper的客户端工具--> <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient --> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency> </dependencies> </dependencyManagement>

web层pom

<dependencies> <dependency> <groupId>com.itzmn</groupId> <artifactId>dubbo-service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> </dependency> <!--引入zookeeper的客户端工具--> <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient --> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>

service层

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> </dependency> <!--引入zookeeper的客户端工具--> <!-- https://mvnrepository.com/artifact/com.github.sgroschupf/zkclient --> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> </dependency> </dependencies>

2.接口设计


在service模块新建接口,


接口实现类的注解,service一定是dubbo的注解

3.配置文件

# Spring boot applicationspring.application.name = /springboot-dubboserver.port = 9099management.port = 9091# Service versiondemo.service.version = 1.0.0# Base packages to scan Dubbo Components (e.g @Service , @Reference)dubbo.scan.basePackages = com.itzmn.dubbo.service.impl# Dubbo Config properties## ApplicationConfig Beandubbo.application.id = springboot-dubbodubbo.application.name = springboot-dubbo## ProtocolConfig Beandubbo.protocol.id = dubbodubbo.protocol.name = dubbodubbo.protocol.port = 20880## RegistryConfig Beandubbo.registry.id = my-registry1dubbo.registry.address = zookeeper://47.106.64.158:2181

在web层的配置文件中,配置,即可,前提,要先安装zookeeper,才能进行服务的注册,然后启动即可

4.消费者

配置

# Spring boot applicationspring.application.name = dubbo-consumer-demoserver.port = 8080management.port = 8081# Service Versiondemo.service.version = 1.0.0# Dubbo Config properties## ApplicationConfig Beandubbo.application.id = dubbo-consumer-demodubbo.application.name = dubbo-consumer-demo## ProtocolConfig Beandubbo.protocol.id = dubbodubbo.protocol.name = dubbodubbo.protocol.port = 12345

只需将服务提供者的接口jar包引入,然后注入服务即可

注意,springboot2.0和springboot1.x与dubbo整合不一样,

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章