zoukankan      html  css  js  c++  java
  • dubbo线程模型

    dubbo的provider有2种线程池: 

    • IO处理线程池。(直接通过netty等来配置)
    • 服务调用线程池。

      如果事件处理的逻辑能迅速完成,并且不会发起新的 IO 请求,比如只是在内存中记个标识,则直接在 IO 线程上处理更快,因为减少了线程池调度。

      但如果事件处理逻辑较慢,或者需要发起新的 IO 请求,比如需要查询数据库,则必须派发到线程池,否则 IO 线程阻塞,将导致不能接收其它请求。

      如果用 IO 线程处理事件,又在事件处理过程中发起新的 IO 请求,比如在连接事件中发起登录请求,会报“可能引发死锁”异常,但不会真死锁。

    因此,需要通过不同的派发策略和不同的线程池配置的组合来应对不同的场景:

    <dubbo:protocol name="dubbo" dispatcher="all" threadpool="fixed" threads="100" />
    • Dispatcher

    all 所有消息都派发到线程池,包括请求,响应,连接事件,断开事件,心跳等。
    direct 所有消息都不派发到线程池,全部在 IO 线程上直接执行。
    message 只有请求响应消息派发到线程池,其它连接断开事件,心跳等消息,直接在 IO 线程上执行。
    execution 只请求消息派发到线程池,不含响应,响应和其它连接断开事件,心跳等消息,直接在 IO 线程上执行。
    connection 在 IO 线程上,将连接断开事件放入队列,有序逐个执行,其它消息派发到线程池。

    • ThreadPool

    fixed 固定大小线程池,启动时建立线程,不关闭,一直持有。(缺省)
    cached 缓存线程池,空闲一分钟自动删除,需要时重建。
    limited 可伸缩线程池,但池中的线程数只会增长不会收缩。只增长不收缩的目的是为了避免收缩时突然来了大流量引起的性能问题。
    eager 优先创建Worker线程池。在任务数量大于corePoolSize但是小于maximumPoolSize时,优先创建Worker来处理任务。当任务数量大于maximumPoolSize时,将任务放入阻塞队列中。阻塞队列充满时抛出RejectedExecutionException。(相比于cached:cached在任务数量超过maximumPoolSize时直接抛出异常而不是将任务放入阻塞队列)

      上面是摘自官方的解释,下面进行研究:

    producer.xml

    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    
        <!-- 提供方应用信息,用于计算依赖关系 -->
        <dubbo:application name="hello-world-app">
            <!-- 关闭QOS:Quality of Service -->
            <dubbo:parameter key="qos.enable" value="false" />
            <dubbo:parameter key="qos.accept.foreign.ip" value="true" />
            <dubbo:parameter key="qos.port" value="2222" />
        </dubbo:application>
    
        <dubbo:registry address="zookeeper://127.0.0.1:2181"
            subscribe="false" dynamic="false" />
    
        <!-- 用dubbo协议在20880端口暴露服务 -->
        <dubbo:protocol name="dubbo" port="20880"/>
    
        <!-- 声明需要暴露的服务接口 -->
        <dubbo:service interface="dubbo.DubboService" ref="demoService"
            timeout="500000" />
    
        <!-- 和本地bean一样实现服务 -->
        <bean id="demoService" class="dubbo.DubboServiceImpl" />
    </beans>

    consumer.xml

    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
           http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    
        <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
        <dubbo:application name="consumer-of-helloworld-app">
            <!-- 关闭QOS:Quality of Service -->
            <dubbo:parameter key="qos.enable" value="false" />
        </dubbo:application>
    
        <!-- 使用multicast广播注册中心暴露发现服务地址 -->
        <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    
        <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
        <dubbo:reference id="dubboService" interface="dubbo.DubboService" />
    </beans>

    Consumer.java开启20个线程消费服务

    package dubbo;
    
    import java.util.concurrent.CountDownLatch;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Consumer {
    
        private static final Logger LOGGER = LoggerFactory.getLogger(Consumer.class);
    
        public static void main(String[] args) throws InterruptedException {
            final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                    new String[] { "consumer.xml" });
            context.start();
            for (int i = 0; i < 20; i++) {
                new Thread(new Runnable() {
    
                    @Override
                    public void run() {
                        DubboService service = (DubboService) context.getBean("dubboService");
                        LOGGER.info(service.sayHello(" china "));
                    }
                }).start();
            }
    
            CountDownLatch countDownLatch = new CountDownLatch(1);
            countDownLatch.await();
        }
    }

    多次访问之后,消费者线程数量会一直增加,并且消费完成之后线程并不会结束,并且每次访问结束都会增加20个线程。如下:

    (1)测试  direct  所有消息都不派发到线程池,全部在 IO 线程上直接执行。(多个线程同时请求是一种同步执行的方式,服务端始终只有一条线程处理请求),如下:

        <!-- 用dubbo协议在20880端口暴露服务 -->
        <dubbo:protocol name="dubbo" port="20880" dispatcher="direct"/>

    查看线程信息:

    (2)使用固定fixed线程池处理并且大小定为20

        <!-- 用dubbo协议在20880端口暴露服务 -->
        <dubbo:protocol name="dubbo" port="20880" dispatcher="all"
            threadpool="fixed" threads="20" />

    多次访问线程数量仍然定在最高的20,不会增加。(这种情况适用于最大并发量是20的请求)

    如果同时访问超过20个,会报下面错误:

    Exception in thread "Thread-0" com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method sayHello in the service dubbo.DubboService. Tried 3 times of the providers [192.168.0.232:20880] (1/13) from the registry 127.0.0.1:2181 on the consumer 192.168.0.232 using the dubbo version 2.6.6. Last error is: Failed to invoke remote method: sayHello, provider: dubbo://192.168.0.232:20880/dubbo.DubboService?anyhost=true&application=consumer-of-helloworld-app&bean.name=dubbo.DubboService&check=false&dubbo=2.0.2&dynamic=false&generic=false&interface=dubbo.DubboService&methods=sayHello&pid=5132&qos.enable=false&register.ip=192.168.0.232&remote.timestamp=1554373603443&side=consumer&timeout=500000&timestamp=1554374765210, cause: Server side(192.168.0.232,20880) threadpool is exhausted ,detail msg:Thread pool is EXHAUSTED! Thread Name: DubboServerHandler-192.168.0.232:20880, Pool Size: 20 (active: 20, core: 20, max: 20, largest: 20), Task: 67 (completed: 47), Executor status:(isShutdown:false, isTerminated:false, isTerminating:false), in dubbo://192.168.0.232:20880!
    at com.alibaba.dubbo.rpc.cluster.support.FailoverClusterInvoker.doInvoke(FailoverClusterInvoker.java:102)
    (3)limited  在同时访问超过线程数的时候多余请求被拒绝

      并且在生产者控制台打印如下信息

    18:54:22 [com.alibaba.dubbo.common.threadpool.support.AbortPolicyWithReport]-[WARN]  [DUBBO] Thread pool is EXHAUSTED! Thread Name: DubboServerHandler-192.168.0.232:20880, Pool Size: 20 (active: 20, core: 0, max: 20, largest: 20), Task: 21 (completed: 1), Executor status:(isShutdown:false, isTerminated:false, isTerminating:false), in dubbo://192.168.0.232:20880!, dubbo version: 2.6.6, current host: 192.168.0.232

      多余的客户端请求报错同上面一样,

    Server side(192.168.0.232,20880) threadpool is exhausted ,detail msg:Thread pool is EXHAUSTED! Thread Name: DubboServerHandler-192.168.0.232:20880, Pool Size: 20 (active: 20, core: 0, max: 20, largest: 20), Task: 21 (completed: 1), Executor status:(isShutdown:false, isTerminated:false, isTerminating:false), in dubbo://192.168.0.232:20880!

  • 相关阅读:
    [ jquery 选择器 :hidden ] 此方法选取匹配所有不可见元素,或者type为hidden的元素
    剑指 Offer 03. 数组中重复的数字 哈希
    LeetCode 1736. 替换隐藏数字得到的最晚时间 贪心
    Leetcode 1552. 两球之间的磁力 二分
    Leetcode 88. 合并两个有序数组 双指针
    LeetCode 1744. 你能在你最喜欢的那天吃到你最喜欢的糖果吗?
    LeetCode 1743. 相邻元素对还原数组 哈希
    LeetCode 1745. 回文串分割 IV dp
    剑指 Offer 47. 礼物的最大价值 dp
    剑指 Offer 33. 二叉搜索树的后序遍历序列 树的遍历
  • 原文地址:https://www.cnblogs.com/qlqwjy/p/10656585.html
Copyright © 2011-2022 走看看