• 【Dubbo3高级特性】「框架与服务」服务端通过线程池隔离技术实现资源限制和资源隔离机制


    线程池隔离

    Dubbo3会提供一种新的线程池管理方式,用于隔离服务之间的线程池调用机制,主要用于服务提供者端进行实现服务资源隔离和容器隔离机制,最终的效果就是服务提供者内部的各个服务通过线程池隔离且互相独立,任何一个服务的线程池资源耗尽都不会影响其他正常服务,支持线程池可配置化,由用户手动指定。

    使用场景

    针对于指定服务会出现IO时间过长或者资源消耗时间过长的问题,因此可以实现独立处理功能服务而不会影响其他服务运行的执行,例如一些比较考虑客户体验度的场景服务以及比较注重响应时长的工作问题。

    使用方式(服务端)

    原始API配置方式
    采用Dubbo服务容器进行启动JVM容器
            DubboBootstrap providerBootstrap = DubboBootstrap.newInstance();
    
    • 1
    建立ApplicationConfig设置参数
    • ApplicationConfig新增String executor-management-mode参数,配置值为 default 和 isolation ,默认为 default。
      • executor-management-mode = default 使用原有以协议端口为粒度、服务间共享的线程池管理方式
      • executor-management-mode = isolation 使用新增的以服务三元组为粒度、服务间隔离的线程池管理方式
            // It takes effect only if [executor-management-mode=isolation] is configured
            ApplicationConfig applicationConfig = new ApplicationConfig("provider-app");
            applicationConfig.setExecutorManagementMode("isolation");
    
    • 1
    • 2
    • 3
    ServiceConfig配置选项
    • ServiceConfig新增Executor executor 参数,用以服务间隔离的线程池,可以由用户配置化、提供自己想要的线程池,若没有指定,则会根据协议配置(ProtocolConfig)信息构建默认的线程池用以服务隔离。ServiceConfig 新增 Executor executor 配置参数只有指定executor-management-mode = isolation 才生效。
            ServiceConfig serviceConfig1 = new ServiceConfig();
            serviceConfig1.setInterface(APIService.class);
            serviceConfig1.setRef(new DefaultAPIService());
            serviceConfig1.setVersion("1.0.0");
    
    • 1
    • 2
    • 3
    • 4
    ServiceConfig设置对应的线程池
            NamedThreadFactory threadFactory1 = new NamedThreadFactory("testService-executor");
            ExecutorService executor1 = Executors.newFixedThreadPool(10, threadFactory1);
            serviceConfig1.setExecutor(executor1);
    
    • 1
    • 2
    • 3
    启动整个Dubbo容器机制暴漏服务端接口
            providerBootstrap
            .application(applicationConfig)
            .registry(registryConfig)
            // export with tri and dubbo protocol
            .protocol(new ProtocolConfig("tri", 20001))
            .protocol(new ProtocolConfig("dubbo", 20002))
            .service(serviceConfig)
            providerBootstrap.start();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    XML配置方式
    配置应用服务Application层级配置
    • executor-management-mode = default 使用原有以协议端口为粒度、服务间共享的线程池管理方式
    • executor-management-mode = isolation 使用新增的以服务三元组为粒度、服务间隔离的线程池管理方式
      <dubbo:application name="application-provider" executor-management-mode="isolation">
      dubbo:application>
    
    
    • 1
    • 2
    • 3
    配置dubbo的三中心

    配置dubbo的三中心体系:注册中心、元数据中心、配置中心,同时都将zookeeper作为实例、此外还可以采用nacos的方式进行注册部署。

      <dubbo:config-center address="zookeeper://127.0.0.1:2181"/>
      <dubbo:metadata-report address="zookeeper://127.0.0.1:2181"/>
      <dubbo:registry id="registry1" address="zookeeper://127.0.0.1:2181?registry-type=service"/>
    
    • 1
    • 2
    • 3
    设置多协议-tri和dubbo
      <dubbo:protocol name="dubbo" port="-1"/>
      <dubbo:protocol name="tri" port="-1"/>
    
    • 1
    • 2
    设置对应的接口给dubbo协议和tri协议
      <bean id="apiService" class="org.apache.dubbo.config.spring.impl.ApiServiceImpl"/>
    
    • 1
    定义定制化线程池
      <bean id="executor-task-service"
            class="org.apache.dubbo.config.spring.isolation.spring.support.CustomerTaskExecutor"/>
    
    • 1
    • 2
    将接口服务绑定对应的定制化线程池
      <!-- this service use [executor="executor-task-service"] as isolated thread pool-->
      <dubbo:service executor="executor-task-service"
                     interface="org.apache.dubbo.config.spring.api.ApiService" version="1.0.0" group="Group1"
                     timeout="3000" ref="apiService" registry="registry1" protocol="dubbo,tri"/>
    
    • 1
    • 2
    • 3
    • 4
    JavaConfig的配置方式
    @Configuration
    @EnableDubbo(scanBasePackages = "org.apache.dubbo.config.spring.isolation.spring.annotation.provider")
    
    
    • 1
    • 2
    • 3
    注册Bean的RegistryConfig
     @Bean
        public RegistryConfig registryConfig() {
            RegistryConfig registryConfig = new RegistryConfig();
            registryConfig.setAddress("zookeeper://127.0.0.1:2181");
            return registryConfig;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    注册Bean的ApplicationConfig
        // NOTE: we need config executor-management-mode="isolation"
        @Bean
        public ApplicationConfig applicationConfig() {
            ApplicationConfig applicationConfig = new ApplicationConfig("provider-app");
            applicationConfig.setExecutorManagementMode("isolation");
            return applicationConfig;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    注册Bean的ProtocolConfig多协议
       // expose services with dubbo protocol
        @Bean
        public ProtocolConfig dubbo() {
            ProtocolConfig protocolConfig = new ProtocolConfig("dubbo");
            return protocolConfig;
        }
    
        // expose services with tri protocol
        @Bean
        public ProtocolConfig tri() {
            ProtocolConfig protocolConfig = new ProtocolConfig("tri");
            return protocolConfig;
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    定义定制化线程池
        // customized thread pool
        @Bean("executor-task-service")
        public Executor customerServiceExecutor() {
            return new CustomerServiceExecutor();
        }
    }
    // customized thread pool
    public class CustomerServiceExecutor extends ThreadPoolExecutor {
        public CustomerServiceExecutorextends () {
            super(10, 10, 60, TimeUnit.SECONDS, new LinkedBlockingDeque<>(),
                new NamedThreadFactory("CustomerServiceExecutor "));
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    开发实现类并且绑定线程池
    // "executor-task-service" is beanName
    @DubboService(executor = "executor-task-service", version = "1.0.0", group = "Group1")
    public class DefaultApiService implements ApiService {
      @Override
      public String sayName(String name) {
        return "server name";
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    通过yml文件的配置模型
    dubbo:
      application: 
        name: provider-app
        executor-management-mode: isolation
      registry:
         id: ${spring.application.name}_registry01
         address: zookeeper://${zookeeper.address:localhost}:2181?client=curator
      protocol:
        name: dubbo
        port: 28081
        dispatcher: all
        threadpool: fixed
       protocol:
        name: tri
        port: 28082
        dispatcher: all
        threadpool: fixed
      provider:
        retries: 0        # 服务提供者无需重试
        timeout: 6000     # 默认只有1s
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    其他的方式与JavaConfig模式保持一致即可。

  • 相关阅读:
    huggingface 使用入门笔记
    华为云数据库战略启示录
    AI技术:分享8个非常实用的AI绘画网站
    第三章、注册中心-Zookeeper(实例1)
    超级会员卡积分收银系统源码 会员卡+积分商城+收银功能+多门店系统 附带完整的搭建教程
    Ask Milvus Anything!聊聊被社区反复@的那些事儿ⅠⅠ
    统一异常的处理方法
    OpenCV4.60安装教程-OpenCV怎么下载安装?OpenCV怎么配置?
    can中继器 can隔离器 本安型CAN中继器在无轨胶轮车中的应用
    NumPy 通用函数(ufunc):高性能数组运算的利器
  • 原文地址:https://blog.csdn.net/l569590478/article/details/127892787