Spring Boot 集成 Dubbo 实现服务调用
服务提供者配置
添加核心依赖
在 Maven 项目的 pom.xml 中引入以下依赖以支持 Dubbo 和 ZooKeeper 注册中心:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.5.2</version>
</dependency>
<!-- Dubbo 核心启动器 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<!-- ZooKeeper 客户端支持 -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- Curator 框架用于连接 ZooKeeper -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.12.0</version>
</dependency>
<!-- ZooKeeper 服务依赖,排除日志冲突包 -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
配置文件设置
在 application.yml 中指定应用信息、注册中心地址以及 Dubbo 扫描路径:
server:
port: 8002
dubbo:
application:
name: service-provider
registry:
address: zookeeper://192.168.31.101:2181
scan:
base-packages: com.example.service.impl
启动类定义
创建 Spring Boot 启动类:
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
服务接口与实现
定义远程调用接口:
package com.example.service;
public interface GreetingService {
String greet(String userName);
}
使用 Dubbo 的 @Service 注解暴露服务:
package com.example.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.example.service.GreetingService;
import org.springframework.stereotype.Component;
@Component
@Service(version = "1.0.0")
public class GreetingServiceImpl implements GreetingService {
@Override
public String greet(String userName) {
return "Hello, " + userName;
}
}
注意:此处的 @Service 来自 com.alibaba.dubbo.config.annotation,用于将服务注册到 Dubbo 框架中。
服务消费者配置
依赖管理
消费者的依赖与提供者一致,确保能够解析相同的接口类型。
应用配置
server:
port: 8001
dubbo:
application:
name: service-consumer
registry:
address: zookeeper://192.168.31.101:2181
启动类
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
共享接口定义
消费者需拥有与提供者完全一致的接口定义(包括包名):
package com.example.service;
public interface GreetingService {
String greet(String userName);
}
控制器调用远程服务
通过 @Reference 注入远程服务实例:
package com.example.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.example.service.GreetingService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
@Reference(version = "1.0.0")
private GreetingService greetingService;
@GetMapping("/greet")
public String invokeGreeting(@RequestParam String name) {
return greetingService.greet(name);
}
}
验证集成结果
启动提供者和消费者后,可通过 ZooKeeper 客户端检查节点是否存在,确认服务是否成功注册与发现。访问消费者接口:
http://localhost:8001/greet?name=zhangsan
若返回 Hello, zhangsan,表明 Dubbo 在 Spring Boot 环境下已正确集成并完成远程调用。