1.自定义背景

dubbo2.7.5之后的版本,对线程池做了优化。具体实现在DefaultExecutorRepository类中。

  1. public class DefaultExecutorRepository implements ExecutorRepository {
  2. private static final Logger logger = LoggerFactory.getLogger(DefaultExecutorRepository.class);
  3. private int DEFAULT_SCHEDULER_SIZE = Runtime.getRuntime().availableProcessors();
  4. private final ExecutorService SHARED_EXECUTOR = Executors.newCachedThreadPool(new NamedThreadFactory("DubboSharedHandler", true));
  5. private Ring<ScheduledExecutorService> scheduledExecutors = new Ring<>();
  6. private ScheduledExecutorService serviceExporterExecutor;
  7. private ScheduledExecutorService reconnectScheduledExecutor;
  8. private ConcurrentMap<String, ConcurrentMap<Integer, ExecutorService>> data = new ConcurrentHashMap<>();
  9. public DefaultExecutorRepository() {
  10. // for (int i = 0; i < DEFAULT_SCHEDULER_SIZE; i++) {
  11. // ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("Dubbo-framework-scheduler"));
  12. // scheduledExecutors.addItem(scheduler);
  13. // }
  14. //
  15. // reconnectScheduledExecutor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("Dubbo-reconnect-scheduler"));
  16. serviceExporterExecutor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("Dubbo-exporter-scheduler"));
  17. }
  18. }

线程池维护在私有成员变量data中,但是整个DefaultExecutorRepository并没有对外暴露data。网上提供的方案,基本都是利用反射。

2.自定义XyDefaultExecutorRepository

代码与DefaultExecutorRepository相同,添加一个自定义的暴露data的方法。

  1. public ConcurrentMap<String, ConcurrentMap<Integer, ExecutorService>> getExecutors() {
  2. return data;
  3. }

�3.配置dubboSPI加载自定义ExecutorRepository

在resources资源目录下,添加META-INF/dubbo/目录。

将原来dubbo的扩展文件org.apache.dubbo.common.threadpool.manager.ExecutorRepository拷贝过来,设置自己的自定义ExecutorRepository。

  1. default=com.xy.rbac.api.config.XyDefaultExecutorRepository

实际包名用自己真是包名。