Router

本文讲解如何通过扩展 Router 实现自定义路由策略,可以根据业务场景的特点来实现特定的路由方式。

通过自定义路由,可以根据业务场景的特点来实现特定的路由方式。本示例 router 扩展实现源码请参见 dubbo-samples-extensibility。

开始之前

任务详情

对所有的请求都使用第一提供服务的Provider,如果该Provider下线,则从新选择一个新的Provider。

实现方式

在Consumer中自定义一个Router,在Router中将第一次调用的Provider保存下来,如果后续有请求调用且Provider列表中包含第一次调用时使用的Provider,则继续使用第一次调用时使用的Provider,否则重新选去一个Provider。

代码结构

1. src
2. |-main
3. |-java
4. |-org
5. |-apache
6. |-dubbo
7. |-samples
8. |-extensibility
9. |-router
10. |-consumer
11. |-router
12. |-StickFirstStateRouter.java (实现StateRouter接口)
13. |-StickFirstStateRouterFactory.java (实现StateRouterFactory接口)
14. |-resources
15. |-META-INF
16. |-application.properties (Dubbo Consumer配置文件)
17. |-dubbo
18. |-org.apache.dubbo.rpc.cluster.router.state.StateRouterFactory (纯文本文件)

代码详情

  • StickFirstStateRouter
1. package org.apache.dubbo.samples.extensibility.router.consumer.router;

3. import org.apache.dubbo.common.URL;
4. import org.apache.dubbo.common.config.configcenter.ConfigChangeType;
5. import org.apache.dubbo.common.config.configcenter.ConfigChangedEvent;
6. import org.apache.dubbo.common.config.configcenter.ConfigurationListener;
7. import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
8. import org.apache.dubbo.common.logger.LoggerFactory;
9. import org.apache.dubbo.common.utils.CollectionUtils;
10. import org.apache.dubbo.common.utils.Holder;
11. import org.apache.dubbo.rpc.Invocation;
12. import org.apache.dubbo.rpc.Invoker;
13. import org.apache.dubbo.rpc.RpcException;
14. import org.apache.dubbo.rpc.cluster.router.RouterSnapshotNode;
15. import org.apache.dubbo.rpc.cluster.router.state.AbstractStateRouter;
16. import org.apache.dubbo.rpc.cluster.router.state.BitList;

18. public class StickFirstStateRouter<T> extends AbstractStateRouter<T> implements ConfigurationListener {
19. public StickFirstStateRouter(URL url) {
20. super(url);
21. }

23. public static final String NAME = "STICK_FIRST_ROUTER";
24. private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(StickFirstStateRouter.class);
25. private volatile BitList<Invoker<T>> firstInvokers;

27. @Override
28. protected BitList<Invoker<T>> doRoute(BitList<Invoker<T>> invokers, URL url, Invocation invocation, boolean needToPrintMessage, Holder<RouterSnapshotNode<T>> routerSnapshotNodeHolder, Holder<String> messageHolder) throws RpcException {
29. if (CollectionUtils.isEmpty(invokers)) {
30. if (needToPrintMessage) {
31. messageHolder.set("Directly Return. Reason: Invokers from previous router is empty.");
32. }
33. return invokers;
34. }
35. BitList<Invoker<T>> copy = invokers.clone();
36. if (CollectionUtils.isEmpty(copy)) {
37. this.firstInvokers = new BitList<>(BitList.emptyList());
38. this.firstInvokers.add(copy.get(0));
39. } else {
40. this.firstInvokers = copy.and(invokers);
41. if(CollectionUtils.isEmpty(this.firstInvokers)){
42. this.firstInvokers.add(copy.get(0));
43. }
44. }
45. return this.firstInvokers;
46. }

48. @Override
49. public void process(ConfigChangedEvent event) {
50. if (logger.isDebugEnabled()) {
51. logger.debug("Notification of tag rule, change type is: " + event.getChangeType() + ", raw rule is:\n " +
52. event.getContent());
53. }
54. // Reset
55. if (event.getChangeType().equals(ConfigChangeType.DELETED)) {
56. this.firstInvokers = null;
57. }
58. }

60. @Override
61. public void stop() {
62. super.stop();
63. this.firstInvokers = null;
64. }
65. }
  • StickFirstStateRouterFactory
1. package org.apache.dubbo.samples.extensibility.router.consumer.router;

3. import org.apache.dubbo.common.URL;
4. import org.apache.dubbo.rpc.cluster.router.state.StateRouter;
5. import org.apache.dubbo.rpc.cluster.router.state.StateRouterFactory;

7. public class StickFirstStateRouterFactory implements StateRouterFactory {
8. @Override
9. public <T> StateRouter<T> getRouter(Class<T> interfaceClass, URL url) {
10. return new StickFirstStateRouter<>(url);
11. }
12. }

SPI配置

在resources/META-INF/dubbo/org.apache.dubbo.rpc.cluster.router.state.StateRouterFactory文件中添加如下配置:

1. stickfirst=org.apache.dubbo.samples.extensibility.router.consumer.router.StickFirstStateRouterFactory

配置文件

在resources/application.properties文件中添加如下配置:

1. # 配置自定义路由
2. dubbo.consumer.router=stickfirst

运行结果

以使用本地IDE的方式来运行任务,结果如下:

dubbo-samples-extensibility-router-output.png

最后修改 September 13, 2024: Refactor website structure (#2860) (1a4b998f54b)