1.自定义背景
dubbo2.7.5之后的版本,对线程池做了优化。具体实现在DefaultExecutorRepository类中。
public class DefaultExecutorRepository implements ExecutorRepository {
private static final Logger logger = LoggerFactory.getLogger(DefaultExecutorRepository.class);
private int DEFAULT_SCHEDULER_SIZE = Runtime.getRuntime().availableProcessors();
private final ExecutorService SHARED_EXECUTOR = Executors.newCachedThreadPool(new NamedThreadFactory("DubboSharedHandler", true));
private Ring<ScheduledExecutorService> scheduledExecutors = new Ring<>();
private ScheduledExecutorService serviceExporterExecutor;
private ScheduledExecutorService reconnectScheduledExecutor;
private ConcurrentMap<String, ConcurrentMap<Integer, ExecutorService>> data = new ConcurrentHashMap<>();
public DefaultExecutorRepository() {
// for (int i = 0; i < DEFAULT_SCHEDULER_SIZE; i++) {
// ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("Dubbo-framework-scheduler"));
// scheduledExecutors.addItem(scheduler);
// }
//
// reconnectScheduledExecutor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("Dubbo-reconnect-scheduler"));
serviceExporterExecutor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("Dubbo-exporter-scheduler"));
}
}
线程池维护在私有成员变量data中,但是整个DefaultExecutorRepository并没有对外暴露data。网上提供的方案,基本都是利用反射。
2.自定义XyDefaultExecutorRepository
代码与DefaultExecutorRepository相同,添加一个自定义的暴露data的方法。
public ConcurrentMap<String, ConcurrentMap<Integer, ExecutorService>> getExecutors() {
return data;
}
�3.配置dubboSPI加载自定义ExecutorRepository
在resources资源目录下,添加META-INF/dubbo/目录。
将原来dubbo的扩展文件org.apache.dubbo.common.threadpool.manager.ExecutorRepository拷贝过来,设置自己的自定义ExecutorRepository。
default=com.xy.rbac.api.config.XyDefaultExecutorRepository
实际包名用自己真是包名。