springboot2.2.2集成dubbo的实现方法
程序员文章站
2022-07-02 09:26:25
最近在学习dubbo,想着作一些笔记,从来没有在csdn上面写过博客,今天献出第一次,哈哈,直接上代码
一、创建父工程
最近在学习dubbo,想着作一些笔记,从来没有在csdn上面写过博客,今天献出第一次,哈哈,直接上代码
一、创建父工程
<?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> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.2.2.release</version> <relativepath/> </parent> <groupid>com.dubbo</groupid> <artifactid>demo01</artifactid> <version>1.0.0</version> <packaging>pom</packaging> <description>spring boot2.x 整合 dubbo</description> <modules> <module>api</module> <module>provider</module> <module>consumer</module> </modules> <!--统一管理依赖版本--> <properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding>utf-8</project.reporting.outputencoding> <java.version>1.8</java.version> <dubbo.version>2.7.5</dubbo.version> <curator.version>4.2.0</curator.version> <!-- 连接zookeeper的依赖,我的zookeeper版本是3.4.14,感觉这个jar版本和zookeeper是大概保持一致的,但是引入3.4.14会报错,我试了下,从3.4.13开始就不行了 --> <zookeeper.version>3.4.12</zookeeper.version> </properties> <!--依赖定义--> <!--dependencymanagement 定义依赖版本号。子工程直接加依赖即可,不需要再次加版本号,便于统一维护版本号--> <dependencymanagement> <dependencies> <dependency> <groupid>org.apache.dubbo</groupid> <artifactid>dubbo-spring-boot-starter</artifactid> <version>${dubbo.version}</version> </dependency> <!-- zookeeper的api管理依赖 --> <dependency> <groupid>org.apache.curator</groupid> <artifactid>curator-recipes</artifactid> <version>${curator.version}</version> </dependency> <!-- zookeeper依赖 --> <dependency> <groupid>org.apache.zookeeper</groupid> <artifactid>zookeeper</artifactid> <version>${zookeeper.version}</version> </dependency> </dependencies> </dependencymanagement> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-devtools</artifactid> <scope>runtime</scope> <optional>true</optional> </dependency> <!-- 使用该依赖,idea需要安装插件,没有用过的自行百度一下吧 --> <dependency> <groupid>org.projectlombok</groupid> <artifactid>lombok</artifactid> <optional>true</optional> </dependency> </dependencies> </project>
二、创建提供者与消费者共用的api
该模块没有什么好说的,提供者和消费者都需要使用的接口api,提供者和消费者都需要引入该模块
<?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>demo01</artifactid> <groupid>com.dubbo</groupid> <version>1.0.0</version> </parent> <modelversion>4.0.0</modelversion> <artifactid>api</artifactid> </project>
// 注解都是lombok的,真的很方便 @data @builder @noargsconstructor @allargsconstructor(access = accesslevel.private) public class user implements serializable { private integer id; private string name; private integer age; }
public interface userservice { user getuserbyid(integer id); }
三、创建提供者
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <parent> <groupid>com.dubbo</groupid> <version>1.0.0</version> <artifactid>demo01</artifactid> </parent> <groupid>com.dubbo</groupid> <artifactid>provider</artifactid> <version>0.0.1-snapshot</version> <name>provider</name> <description>demo project for spring boot</description> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.apache.dubbo</groupid> <artifactid>dubbo-spring-boot-starter</artifactid> </dependency> <dependency> <groupid>org.apache.curator</groupid> <artifactid>curator-recipes</artifactid> </dependency> <dependency> <groupid>org.apache.zookeeper</groupid> <artifactid>zookeeper</artifactid> </dependency> <!-- 导入公共接口依赖 --> <dependency> <groupid>com.dubbo</groupid> <artifactid>api</artifactid> <version>1.0.0</version> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> <exclusions> <exclusion> <groupid>org.junit.vintage</groupid> <artifactid>junit-vintage-engine</artifactid> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project>
dubbo: application: # 应用名称 name: user-provider protocol: # 协议名称 name: dubbo # 协议端口 port: 20880 registry: # 注册中心地址 address: zookeeper://192.168.104.231:2181
@springbootapplication // 提供服务的应用必须配置此项 @dubbocomponentscan("com.dubbo.provider.service") public class providerapplication { public static void main(string[] args) { springapplication.run(providerapplication.class, args); } }
@component // 该service是org.apache.dubbo.config.annotation.service @service public class userserviceimpl implements userservice { @override public user getuserbyid(integer id) { user user = user.builder() .id(id) .name("张三") .age(20 + id) .build(); return user; } }
四、创建消费者
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <parent> <groupid>com.dubbo</groupid> <version>1.0.0</version> <artifactid>demo01</artifactid> </parent> <groupid>com.dubbo</groupid> <artifactid>consumer</artifactid> <version>0.0.1-snapshot</version> <name>consumer</name> <description>demo project for spring boot</description> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.apache.dubbo</groupid> <artifactid>dubbo-spring-boot-starter</artifactid> </dependency> <dependency> <groupid>org.apache.curator</groupid> <artifactid>curator-recipes</artifactid> </dependency> <dependency> <groupid>org.apache.zookeeper</groupid> <artifactid>zookeeper</artifactid> </dependency> <!-- 导入公共接口依赖 --> <dependency> <groupid>com.dubbo</groupid> <artifactid>api</artifactid> <version>1.0.0</version> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> <exclusions> <exclusion> <groupid>org.junit.vintage</groupid> <artifactid>junit-vintage-engine</artifactid> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project>
# 端口 server: port: 8081 dubbo: application: name: user-consumer protocol: name: dubbo port: 20880 registry: address: zookeeper://192.168.104.231:2181
@springbootapplication public class consumerapplication { public static void main(string[] args) { springapplication.run(consumerapplication.class, args); } }
@restcontroller @requestmapping("/user") public class consumercontroller { @reference private userservice userservice; @getmapping("/{id}") public user getuserbyid(@pathvariable int id) { return userservice.getuserbyid(id); } }
五、启动并访问
先启动provider,启动结果如下
启动consumer,启动结果如下
浏览器访问:http://localhost:8081/user/4
好了,到此就完成了最基本的springboot与dubbo的整合,更多的dubbo的api请查阅 dubbo官方文档
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。