使用quickstart方式构建springboot项目,创建一个dubbo服务提供者,消费者,以及api模块:
父pom:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lchtest.springboot.dubbo</groupId>
<artifactId>springboot-dubbo-example</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
<wicket.version>1.3.2</wicket.version>
<jetty.version>6.1.4</jetty.version>
</properties>
<modules>
<module>springboot-dubbo-sample-api</module>
<module>springboot-dubbo-sample-provider</module>
<module>spring-boot-dubbo-sample-consumer</module>
</modules>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- springboot继承dubbo,只需要dubbo-spring-boot-starter和nacos-client -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<!-- 第三方的包,不是spirng的包有前缀,spirng的包是以spring开头 -->
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.2.1</version>
</dependency>
</dependencies>
</project>
API模块:
provider模块 添加
<?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">
<parent>
<groupId>com.lchtest.springboot.dubbo</groupId>
<artifactId>springboot-dubbo-example</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.lchtest.springboot.dubbo</groupId>
<artifactId>springboot-dubbo-sample-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-dubbo-sample-provider</name>
<description>Demo project for Spring Boot</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.lchtest.springboot.dubbo</groupId>
<artifactId>springboot-dubbo-sample-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
实现API模块的接口,使用@DubboService注解发布服务
配置文件如下:
# 应用名称
spring.application.name=springboot-dubbo-sample-provider
dubbo.protocol.name=dubbo
dubbo.scan.base-packages=com.lchtest.springboot.dubbo.springbootdubbosampleprovider.service
# -1 让系统生成端口号
dubbo.protocol.port=-1
# 注册中心地址,跟springcloud集成dubbo不同,这里是用dubbo的注册中心配置项
dubbo.registry.address=nacos://192.168.61.1:8848
服务消费者consumer模块,添加dubbo-spring-boot-starter, nacos-client,spring-boot-starter-web 的依赖,
<?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">
<parent>
<groupId>com.lchtest.springboot.dubbo</groupId>
<artifactId>springboot-dubbo-example</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.lchtest.springboot.dubbo</groupId>
<artifactId>spring-boot-dubbo-sample-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-dubbo-sample-consumer</name>
<dependencies>
<dependency>
<groupId>com.lchtest.springboot.dubbo</groupId>
<artifactId>springboot-dubbo-sample-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- springboot集成dubbo,只需要dubbo-spring-boot-starter和nacos-client -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<!-- 第三方的包,不是spirng的包有前缀,spring的包是以spring开头 -->
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
并调用provider模块发布的服务:
配置文件:
# 应用名称
spring.application.name=spring-boot-dubbo-sample-consumer
server.port=8888
# 注册中心地址,跟springcloud集成dubbo不同,这里是用dubbo的注册中心配置项
dubbo.registry.address=nacos://192.168.61.1:8848
然后启动nacos注册中心,provider模块和consumer模块,调用接口:
代码地址:https://download.youkuaiyun.com/download/weixin_41300437/87239652