Dubbo 动态调用 group

本文介绍了如何在Dubbo中实现动态调用服务,根据请求参数选择不同的服务Group。首先展示了创建生产者和消费者的静态调用过程,然后详细解释了动态调用的实现,包括设置ApplicationConfig、RegistryConfig和ReferenceConfig,并提供了测试用例。动态调用时,通过URL参数`group`来决定调用`live`或`test`组的服务。测试结果显示动态调用功能正常工作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Dubbo 动态调用 group

一:搭建Dubbo group(test-live) 案例:

  1. 生产方:公共接口+live实现+test实现

2.生产方:Spring注入+Dubbo服务注入

3.消费方:配置与生产方对应的组信息

4.消费方:根据已有的 group调用 对应的服务实现

5.测试:启动zk,并从页面发起请求

http://localhost:8882/controller/testService.action

http://localhost:8882/controller/liveService.action

二:以上案例是按照 注解的方式静态调用,关于动态调用(此处是根据 请求参数来确定到底调用哪个Group):

调用 group:live

http://localhost:8882/controller/dynamicCall.actiongroup=live

调用 group:test

http://localhost:8882/controller/dynamicCall.actiongroup=test

之前是通过如下方式来拿到远端服务的:

public MyDubboGroupService getInvokeService(String group) {
ApplicationConfig application = new ApplicationConfig();
application.setName(“students-consumer”);

RegistryConfig registry = new RegistryConfig();
registry.setAddress(“127.0.0.1:2181”);
registry.setProtocol(“zookeeper”);

ReferenceConfig referenceConfig = new ReferenceConfig<>();
referenceConfig.setApplication(application);
referenceConfig.setRegistry(registry);
referenceConfig.setGroup(group);
referenceConfig.setInterface(MyDubboGroupService.class);
return referenceConfig.get();
}

// 远程服务调用重试次数,不包括第一次调用,不需要重试请设为0
//referenceConfig.setRetries(0);

// 负载均衡策略,可选值:random,roundrobin,leastactive,分别表示:随机,轮询,最少活跃调用
//referenceConfig.setLoadbalance(“leastactive”);

random,roundrobin,leastactive的策略实现

测试结果:

如对你有帮助,请留言/点赞/转发/收藏

### 如何在 Spring Boot 中实现跨系统的 Dubbo 接口调用 #### 创建服务提供者模块 为了使两个独立的服务能够通过 Dubbo 进行交互,首先需要定义一个公共 API 模块来声明共享接口。该模块不依赖于任何特定的技术栈,仅包含业务逻辑相关的接口定义。 ```java // Common-API Module (common-api) public interface HelloService { String sayHello(String name); } ``` 接着,在服务端即服务提供者的项目里引入上述共同使用的 API 并实现具体的业务功能: ```xml <!-- Service Provider POM Dependencies --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <!-- Include the common api module here --> <groupId>com.example</groupId> <artifactId>common-api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> ``` 编写 `HelloServiceImpl` 类作为具体实现并注册到容器中以便被远程访问[^1]。 ```java @Service(version = "1.0.0") // Register as a service provider with version control. @Activate(group = {"provider"}) public class HelloServiceImpl implements HelloService { @Override public String sayHello(String name) { return "Hello, " + name; } } ``` 确保应用程序入口处已启用 Dubbo 支持并通过适当的方式暴露此服务给其他系统消费[^2]。 ```java @SpringBootApplication @EnableDubbo(scanBasePackages = "com.example.provider.service") public class ServiceProviderApplication { public static void main(String[] args) { SpringApplication.run(ServiceProviderApplication.class, args); } /** * Expose HTTP Endpoint for monitoring and governance purposes. */ @Bean public ActuatorAutoConfiguration actuatorConfig() { return new ActuatorAutoConfiguration(); } } ``` #### 构建服务消费者模块 对于客户端而言,则需同样加入相同的公共 API 和必要的 Dubbo 组件支持,从而允许其发现远端提供的服务实例并向之发起请求。 ```xml <!-- Consumer Project's pom.xml --> <dependencies> ... <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>${dubbo.version}</version> </dependency> <dependency> <groupId>com.example</groupId> <artifactId>common-api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> ``` 随后可以在任意位置注入所需的服务接口对象,并像本地方法一样调用来完成一次完整的 RPC 请求过程[^3]。 ```java @RestController @RequestMapping("/consumer") public class ConsumerController { private final Logger logger = LoggerFactory.getLogger(getClass()); @Reference(version = "1.0.0", check = false) private HelloService helloService; @GetMapping("/{name}") public ResponseEntity<String> consume(@PathVariable String name){ try{ String result = this.helloService.sayHello(name); return ResponseEntity.ok(result); } catch(Throwable t){ logger.error("Failed to invoke remote service.",t); throw t; } } } ``` 最后一步就是运行这两个相互隔离的应用程序,验证它们之间能否成功建立连接以及正确传递消息数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值