文章目录
- [SpringBoot 快速开始 Dubbo RPC](#SpringBoot 快速开始 Dubbo RPC)
SpringBoot 快速开始 Dubbo RPC
这里是用 Nacos 作为服务的注册与发现中心
下载 Nacos
nacos 启动成功如图:
只要启动了就可以了。
项目启动
这里创建项目分三个模块,一个interface(接口定义)、一个provider(服务的提供者)、一个consumer(服务的消费者)
项目的创建
创建主项目
步骤如下:
主工程创建好效果:
接口定义服务的创建
Dubbo 服务提供者的创建
服务的消费者创建
过程和上面一样,换个名字而已
添加依赖
给 Provider、Consumer 添加依赖
注意其中 dubbo 的依赖就好了
Provider 的 pom.xml 内容如下
xml
<?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>DubboTeach</artifactId>
<groupId>com.xwhking</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>DubboProvider</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-boot.version>3.0.2</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>com.xwhking</groupId>
<artifactId>DubboInterface</artifactId>
<version>${parent.version}</version>
</dependency>
<!-- spring boot starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.3.0-beta.1</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-nacos-spring-boot-starter</artifactId>
<version>3.3.0-beta.1</version>
</dependency>
</dependencies>
</project>
Consumer 的 pom.xml 文件如下:
xml
<?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>
<artifactId>DubboTeach</artifactId>
<groupId>com.xwhking</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.xwhking</groupId>
<artifactId>DubboConsumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>3.0.2</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>com.xwhking</groupId>
<artifactId>DubboInterface</artifactId>
<version>${parent.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.3.0-beta.1</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-nacos-spring-boot-starter</artifactId>
<version>3.3.0-beta.1</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>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.xwhking.dubbo_consumer.DubboConsumerApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
开始写代码
定义接口
在 interface 项目中定义接口,然后再由 provider 进行实现,在依赖中我们引入了 interface 的包。
我们这里就简单定义一个 Hello 的接口.
代码如下:
java
package com.xwhking;
public interface HelloService {
String sayHello(String name);
}
在 Provider 中实现
接口在微服务中一般是不太会变了的,所以我们要在服务的提供者中去实现它。
我们就简单实现一下。
这里因为中途出现问题,我把名字进行了一个更换,其他没有影响
一定要注意这里进行使用 HelloService 前,对Interface 包进行maven 的install 操作
java
package com.xwhking.dubbo_provider;
import com.xwhking.dubbo_interface.HelloService;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "Hello " + name + "\n This is Dubbo";
}
}
在 Consumer 里面使用
通过注解 @DubboReference
得到服务
具体如下
java
package com.xwhking.dubbo_consumer;
import com.xwhking.dubbo_interface.HelloService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class TestController {
@DubboReference
private HelloService helloService;
@GetMapping("/hello")
public String hello(String name) {
String result = helloService.sayHello(name);
return result;
}
}
创建启动类
Consumer 启动类
java
package com.xwhking.dubbo_consumer;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}
Privider 启动类
java
package com.xwhking.dubbo_provider;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
注册中心配置
在 Provider 和 Consumer 的项目中的resource 目录下添加 application.yml 文件进行注册中心的配置
yaml
dubbo:
application:
name: DubboTeachProvider
protocol:
name: dubbo
port: -1
registry:
address: nacos://localhost:8848
yaml
server:
port: 8080
dubbo:
application:
name: DubboTeachConsumer
protocol:
name: dubbo
port: -1
registry:
address: nacos://localhost:8848
启动
首先要启动的是 Provider
然后再启动 Consumer
确认是否成功
在网站访问:localhost:8080/test/hello?name=xwhking
进行调试可以清楚的看到我们调用成功了
这就是一次简单的 Dubbo RPC 调用尝试
代码仓库,如果对你有帮助的话欢迎点个小小的 Star ✨
参考:
- https://dubbo-next.staged.apache.org/zh-cn/overview/mannual/java-sdk/quick-start/spring-boot/#1-启动注册中心
- https://dubbo-next.staged.apache.org/zh-cn/overview/mannual/java-sdk/reference-manual/registry/nacos/
- https://dubbo-next.staged.apache.org/zh-cn/overview/mannual/java-sdk/tasks/develop/springboot/
大家好,我是xwhking,一名技术爱好者,目前正在全力学习 Java,前端也会一点,如果你有任何疑问请你评论,或者可以加我QQ(2837468248)说明来意!希望能够与你共同进步