- springboot 整合 dubbo 的简单实现
- dubbo 简介
- springboot 整合 dubbo
- common 模块
- provider 模块
- consumer 模块
- dubbo-admin
- 验证
springboot 整合 dubbo 的简单实现 dubbo 简介
Apache Dubbo 是一个高性能、基于 Java 的开源 RPC 框架。具备如下特征:
- 基于透明接口的 RPC
- 智能负载均衡
- 自动服务注册和发现
- 高扩展性
- 运行时流量路由
- 可视化服务治理
如图所示,dubbo 架构由以下角色构成:
- Provider 服务提供者: 暴露服务的服务提供方。
- Consumer 服务消费者: 调用远程服务的服务消费方
- Registry 注册中心: 服务注册与发现的注册中心
- Monitor 监控中心: 统计服务的调用次调和调用时间的监控中心
common 模块结构
pom 文件
4.0.0 org.springframework.boot spring-boot-starter-parent 2.7.1 com.dubbo common 0.0.1-SNAPSHOT common Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
ProviderService 接口类
package com.dubbo.common.service;
public interface ProviderService {
String sendMessage(String message);
}
provider 模块
provider 模块结构
pom 文件
4.0.0 org.springframework.boot spring-boot-starter-parent 2.7.1 com.dubbo provider 0.0.1-SNAPSHOT provider Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web org.apache.dubbo dubbo-spring-boot-starter 3.0.9 org.apache.dubbo dubbo-dependencies-zookeeper 3.0.9 pom org.apache.zookeeper zookeeper 3.8.0 com.dubbo common 0.0.1-SNAPSHOT org.projectlombok lombok 1.18.24 provided org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
application.yml 文件
server:
port: 8081
dubbo:
application:
name: dubbo-provider
registry:
address: zookeeper://127.0.0.1:2181
#扫描service
scan:
base-packages: com.dubbo.provider.service.impl
# 超时调用:dubbo 调用默认 1 秒钟超时。
# 建议由服务提供方设置超时,因为一个方法需要执行多长时间,服务提供方更清楚。
provider:
timeout: 3000
protocol:
name: dubbo
port: 20881
ProviderServiceImpl 实现类
package com.dubbo.provider.service.impl;
import com.dubbo.common.service.ProviderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService
@Slf4j
public class ProviderServiceImpl implements ProviderService {
@Override
public String sendMessage(String message) {
log.info("发送消息:" + message);
return "发送消息:" + message;
}
}
启动类
package com.dubbo.provider;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
consumer 模块
consumer 模块结构
pom 文件
4.0.0 org.springframework.boot spring-boot-starter-parent 2.7.1 com.dubbo provider 0.0.1-SNAPSHOT consumer Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-web org.apache.dubbo dubbo-spring-boot-starter 3.0.9 org.apache.dubbo dubbo-dependencies-zookeeper 3.0.9 pom org.apache.zookeeper zookeeper 3.8.0 com.dubbo common 0.0.1-SNAPSHOT org.projectlombok lombok 1.18.24 provided org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
application.yml 文件
server:
port: 8082
dubbo:
application:
name: dubbo-consumer
registry:
address: zookeeper://127.0.0.1:2181
protocol:
name: dubbo
port: 20881
controller 类
package com.dubbo.consumer.controller;
import com.dubbo.common.service.ProviderService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConsumerController {
@DubboReference
private ProviderService providerService;
@GetMapping("/receive")
public Object receive() {
return providerService.sendMessage("调用provider");
}
}
启动类
package com.dubbo.consumer;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
dubbo-admin
dubbo-admin 的安装请参考 http://t.csdn.cn/CqKPl
启动 provider 项目,可以看到 ProviderService 注册成功,如下图所示:
验证启动 consumer 项目,通过 postman 调用 http://localhost:8082/receive 接口,调用结果如下图所示:



