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;
    }
}
相关推荐
莫问alicia6 分钟前
苍穹外卖 项目记录 day03
java·开发语言·spring boot·maven
m0_7482544724 分钟前
将 vue3 项目打包后部署在 springboot 项目运行
java·spring boot·后端
qq_441996052 小时前
手写RPC笔记
网络·网络协议·rpc
m0_548503032 小时前
打包部署若依(RuoYi)SpringBoot后端和Vue前端图文教程
前端·vue.js·spring boot
ITzhongzi3 小时前
springboot接入sentinel案例
spring boot·后端·sentinel
@Java小牛马4 小时前
分布式系统中的CAP理论(也称为 Brewer‘s 定理)
spring boot·redis·spring·spring cloud·分布式系统
Q_19284999064 小时前
基于Spring Boot的前后端分离的外卖点餐系统
java·spring boot·后端
毕业设计-015 小时前
0042.大学校园生活信息平台+论文
java·spring boot·后端·毕业设计·源代码管理
心之语歌11 小时前
Spring boot 项目 Spring 注入 代理 并支持 代理对象使用 @Autowired 去调用其他服务
spring boot·后端·spring
孤蓬&听雨11 小时前
Java SpringBoot使用Apache POI导入导出Excel文件
java·spring boot·apache·excel导出·excel导入