案例介绍
- 服务一:公共接口服务
- 服务二:档案服务 【远程接口提供者】
- 服务三:账户服务 【远程接口调用者】
档案接口和账户接口都依赖公共接口服务,公共接口服务中抽取需要rpc的接口
项目源码
接口服务
java
package com.dubbo.archive.api;
/**
* @Description 档案开放接口
* @Author lihw
* @Date 2025/1/4 上午9:58
*/
public interface ArchiveFacadeService {
String getArchiveInfo(String name);
}
档案服务 [rpc提供者]
- 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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.dubbo.archive</groupId>
<artifactId>dubbo_archive</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>archive-service</artifactId>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.dubbo.archive</groupId>
<artifactId>archive-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.2.12</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-nacos-spring-boot-starter</artifactId>
<version>3.2.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- application.yml
yml
dubbo:
registry:
address: nacos://${nacos.address:10.242.73.170}:8848
# This will enable application-level service discovery only (the recommended service discovery method for Dubbo3).
# For users upgrading from Dubbo2.x, please set the value to 'all' for smooth migration.
register-mode: instance
parameters:
prefer-ip: true # 优先使用 IP 进行服务注册
namespace: 17cbb377-52c8-415f-8135-b06e7676d906
group: test
protocol:
name: dubbo
port: 50051 # 客户端端口
host: 10.242.70.230 # 客户端ip
application:
qos-enable: true
name: QuickStartApplication
qos-port: 22222
qos-accept-foreign-ip: false
logger: slf4j
- ArchiveFacadeServiceImpl.java 【接口实现】
java
package com.dubbo.archive.service.facade;
import com.dubbo.archive.api.ArchiveFacadeService;
import org.apache.dubbo.config.annotation.DubboService;
/**
* @Description
* @Author lihw
* @Date 2025/1/4 上午9:59
*/
@DubboService(version = "1.0.1")
public class ArchiveFacadeServiceImpl implements ArchiveFacadeService {
@Override
public String getArchiveInfo(String name) {
return "hello" + name + ",dubbo success.";
}
}
注意: @DubboService(version = "1.0.1")
提供者和调用者的版本号要保持一致
- 启动类
java
package com.dubbo.archive.service;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Description
* @Author lihw
* @Date 2025/1/4 上午9:52
*/
@SpringBootApplication
@EnableDubbo
public class ArchiveServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ArchiveServiceApplication.class, args);
}
}
注意:
启动类加上: @EnableDubbo 开启dubbo
档案服务 【rpc调用者】
- pom
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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.dubbo.account</groupId>
<artifactId>dubbo-account</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-account</name>
<description>dubbo-account</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.dubbo.archive</groupId>
<artifactId>archive-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.2.12</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-nacos-spring-boot-starter</artifactId>
<version>3.2.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- application.yml
yml
dubbo:
registry:
address: nacos://${nacos.address:10.242.73.170}:8848
# This will enable application-level service discovery only (the recommended service discovery method for Dubbo3).
# For users upgrading from Dubbo2.x, please set the value to 'all' for smooth migration.
register-mode: instance
parameters:
prefer-ip: true # 优先使用 IP 进行服务注册
namespace: 17cbb377-52c8-415f-8135-b06e7676d906
group: test
protocol:
name: dubbo
port: 50055 # 客户端端口
host: 10.242.70.230 # 客户端ip
application:
qos-enable: true
name: QuickStartApplicationClient
qos-port: 22223
qos-accept-foreign-ip: false
logger: slf4j
server:
port: 8082
- TestController.java 【测试接口】
java
package com.dubbo.account.controller;
import com.dubbo.archive.api.ArchiveFacadeService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description
* @Author lihw
* @Date 2025/1/4 上午11:12
*/
@RestController
@RequestMapping("/account/test")
public class TestController {
@DubboReference(version = "1.0.1")
private ArchiveFacadeService archiveFacadeService;
@RequestMapping("/test")
public String test() {
return archiveFacadeService.getArchiveInfo("lhw");
}
}
注意:
@DubboReference(version = "1.0.1") 注入时如果使用版本号,要与提供者保持一致
@DubboReference准入的 bean 没有办法被 mock 的问题解决方案:
新建一个类,将所有的Dubbo接口全部注入,然后在其他地方使用时,直接@Autowired
使用即可
java
@Configuration
public class BusinessDubboConfiguration {
@DubboReference(version = "1.0.0")
private CollectionFacadeService collectionFacadeService;
@Bean
@ConditionalOnMissingBean(name = "collectionFacadeService")
public CollectionFacadeService collectionFacadeService() {
return collectionFacadeService;
}
}