springboot整合dubbo和nacos进行远程rpc调用案例

案例介绍

  • 服务一:公共接口服务
  • 服务二:档案服务 【远程接口提供者】
  • 服务三:账户服务 【远程接口调用者】

档案接口和账户接口都依赖公共接口服务,公共接口服务中抽取需要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;
    }
}
相关推荐
悟空码字3 小时前
Spring Boot 整合 MongoDB 最佳实践:CRUD、分页、事务、索引全覆盖
java·spring boot·后端
皮皮林5512 天前
拒绝写重复代码,试试这套开源的 SpringBoot 组件,效率翻倍~
java·spring boot
用户908324602734 天前
Spring AI 1.1.2 + Neo4j:用知识图谱增强 RAG 检索(上篇:图谱构建)
java·spring boot
用户8307196840825 天前
Spring Boot 集成 RabbitMQ :8 个最佳实践,杜绝消息丢失与队列阻塞
spring boot·后端·rabbitmq
Java水解5 天前
Spring Boot 视图层与模板引擎
spring boot·后端
clamlss5 天前
💥 踩坑实录:Dubbo 为什么把我的自定义异常“吃”了?
dubbo
Java水解5 天前
一文搞懂 Spring Boot 默认数据库连接池 HikariCP
spring boot·后端
洋洋技术笔记5 天前
Spring Boot Web MVC配置详解
spring boot·后端
初次攀爬者6 天前
Kafka 基础介绍
spring boot·kafka·消息队列
用户8307196840826 天前
spring ai alibaba + nacos +mcp 实现mcp服务负载均衡调用实战
spring boot·spring·mcp