SpringBoot 集成 Dubbo 完整案例

176 阅读2分钟

SpringBoot 集成 Dubbo 完整案例

本文将详细介绍SpringBoot项目如何集成Dubbo,并使用Nacos作为注册中心,实现服务提供者与消费者之间的RPC调用。

一、项目结构设计

推荐采用多模块项目结构,便于代码管理和复用:

dubbo-demo
├── dubbo-api          # 公共接口模块
├── dubbo-provider     # 服务提供者模块
└── dubbo-consumer     # 服务消费者模块

二、公共接口模块(api)

1. 创建接口定义

public interface DemoService {
    String sayHello(String name);
    User getUserById(Long id);
}

// 实体类
public class User implements Serializable {
    private Long id;
    private String name;
    private Integer age;
    // getter和setter方法
}

三、服务提供者模块(provider)

1. 添加依赖

<dependencies>
    <!-- 引入公共接口模块 -->
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>dubbo-api</artifactId>
        <version>${project.version}</version>
    </dependency>
    
    <!-- Spring Boot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <!-- Dubbo Spring Boot Starter -->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>3.1.3</version>
    </dependency>
    
    <!-- Dubbo Nacos Registry -->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-registry-nacos</artifactId>
        <version>3.1.3</version>
    </dependency>
</dependencies>

2. 实现服务接口

import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

@DubboService(version = "1.0.0")
@Component
public class DemoServiceImpl implements DemoService {
    
    @Override
    public String sayHello(String name) {
        return "Hello, " + name + "! Welcome to Dubbo world.";
    }
    
    @Override
    public User getUserById(Long id) {
        User user = new User();
        user.setId(id);
        user.setName("User " + id);
        user.setAge(20 + id.intValue() % 10);
        return user;
    }
}

3. 配置文件(application.yml)

server:
  port: 8081

spring:
  application:
    name: dubbo-provider

# Dubbo 配置
dubbo:
  application:
    name: dubbo-provider
    logger: slf4j
  protocol:
    name: dubbo
    port: 20880
  registry:
    address: nacos://localhost:8848
    register-mode: instance  # 启用应用级服务发现
  scan:
    base-packages: com.example.provider.service.impl

4. 启动类

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);
    }
}

四、服务消费者模块(consumer)

1. 添加依赖

<dependencies>
    <!-- 引入公共接口模块 -->
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>dubbo-api</artifactId>
        <version>${project.version}</version>
    </dependency>
    
    <!-- Spring Boot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <!-- Dubbo Spring Boot Starter -->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>3.1.3</version>
    </dependency>
    
    <!-- Dubbo Nacos Registry -->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-registry-nacos</artifactId>
        <version>3.1.3</version>
    </dependency>
</dependencies>

2. 调用远程服务

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {
    
    @DubboReference(version = "1.0.0", check = false)
    private DemoService demoService;
    
    @GetMapping("/hello/{name}")
    public String sayHello(@PathVariable String name) {
        return demoService.sayHello(name);
    }
    
    @GetMapping("/user/{id}")
    public User getUser(@PathVariable Long id) {
        return demoService.getUserById(id);
    }
}

3. 配置文件(application.yml)

server:
  port: 8082

spring:
  application:
    name: dubbo-consumer

# Dubbo 配置
dubbo:
  application:
    name: dubbo-consumer
    logger: slf4j
  registry:
    address: nacos://localhost:8848
    register-mode: instance  # 启用应用级服务发现

4. 启动类

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);
    }
}

五、Nacos注册中心配置

  1. 确保已安装并启动Nacos服务(默认端口8848)
  2. 访问Nacos控制台:http://localhost:8848/nacos
  3. 用户名密码默认都是:nacos/nacos

六、测试流程

  1. 启动Nacos服务
  2. 启动服务提供者(DubboProviderApplication)
  3. 启动服务消费者(DubboConsumerApplication)
  4. 在Nacos控制台验证服务是否成功注册
  5. 访问消费者接口进行测试:

七、进阶配置

1. 超时配置

# 在消费者端配置
dubbo:
  consumer:
    timeout: 3000  # 全局超时配置
  reference:
    com.example.api.DemoService:
      timeout: 5000  # 指定接口超时配置

2. 负载均衡配置

# 在消费者端配置
dubbo:
  consumer:
    loadbalance: random  # 随机、roundrobin、leastactive、consistenthash

3. 集群容错配置

# 在消费者端配置
dubbo:
  consumer:
    cluster: failover  # 失败自动切换、failfast、failsafe、failback、forking

八、常见问题排查

  1. 服务注册失败:检查Nacos地址配置是否正确,Nacos服务是否正常运行
  2. 服务调用超时:调整超时配置,检查网络连接
  3. 依赖版本冲突:确保Dubbo相关依赖版本一致
  4. @DubboService不生效:检查@EnableDubbo注解是否添加,包扫描路径是否正确

通过以上步骤,您可以成功实现SpringBoot与Dubbo的集成,并使用Nacos作为注册中心。这种架构模式可以有效提高系统的可扩展性和维护性,适合构建大型分布式系统。

九、代码链接

gitee.com/tree_boss/d…