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;
    }
}
相关推荐
战族狼魂1 小时前
CSGO 皮肤交易平台后端 (Spring Boot) 代码结构与示例
java·spring boot·后端
用键盘当武器的秋刀鱼5 小时前
springBoot统一响应类型3.5.1版本
java·spring boot·后端
小李同学_LHY5 小时前
三.微服务架构中的精妙设计:服务注册/服务发现-Eureka
java·spring boot·spring·springcloud
爱喝醋的雷达7 小时前
Spring SpringBoot 细节总结
java·spring boot·spring
嘵奇9 小时前
深入解析 Spring Boot 测试核心注解
java·spring boot·后端
技术liul10 小时前
解决Spring Boot Configuration Annotation Processor not configured
java·spring boot·后端
腥臭腐朽的日子熠熠生辉13 小时前
解决maven失效问题(现象:maven中只有jdk的工具包,没有springboot的包)
java·spring boot·maven
绝顶少年15 小时前
Spring Boot 注解:深度解析与应用场景
java·spring boot·后端
西木风落15 小时前
springboot整合Thymeleaf web开发出现Whitelabel Error Page
spring boot·thymeleaf error·whitelabelerror
Zfox_16 小时前
【C++项目】从零实现RPC框架「四」:业务层实现与项目使用
linux·开发语言·c++·rpc·项目