[apache/dubbo]如何写DEMO能看到LeastActive运行的效果

2024-07-02 102 views
7

如何写DEMO能看到LeastActive运行的效果

回答

8

我的提供者代码:

package com.ghy.www.my.loadbalance.provider.service;

import com.ghy.www.api.IService3;
import org.springframework.beans.factory.annotation.Value;

public class HelloService3 implements IService3 {
    @Value("${server.port}")
    private int portValue;

    @Override
    public String getHello(String username) {
        System.out.println("HelloService3 portValue=" + portValue + " username=" + username);
        if (portValue == 8085) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (portValue == 8086) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (portValue == 8087) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return "hello3 " + username + " port=" + portValue;
    }
}

消费者引用代码:

    @DubboReference(loadbalance = LoadbalanceRules.LEAST_ACTIVE, timeout = 6000000, filter = "activelimit")
    private IService3 service3;

消费者调用代码:

private int test3_provider1_runtime = 0;
    private int test3_provider2_runtime = 0;
    private int test3_provider3_runtime = 0;

    class MyThread1 extends Thread {
        private int i;
        private CountDownLatch latch;

        public MyThread1(int i, CountDownLatch latch) {
            this.i = i;
            this.latch = latch;
        }

        @Override
        public void run() {
            String helloString = service3.getHello("中国人" + (i + 1));
            System.out.println("public String test3() " + helloString);
            int runPort = Integer.parseInt(helloString.substring(helloString.length() - 4, helloString.length()));
            System.out.println(runPort + " " + (i + 1) + "次运行");
            switch (runPort) {
                case 8085:
                    test3_provider1_runtime++;
                    break;
                case 8086:
                    test3_provider2_runtime++;
                    break;
                case 8087:
                    test3_provider3_runtime++;
                    break;
            }
            latch.countDown();
        }
    }

    @RequestMapping("Test3")
    public void test3(HttpServletRequest request, HttpServletResponse response) {
        CountDownLatch latch = new CountDownLatch(10000);
        for (int i = 0; i < 10000; i++) {
            MyThread1 t = new MyThread1(i, latch);
            t.start();
        }
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("test3_provider1_runtime=" + test3_provider1_runtime);
        System.out.println("test3_provider2_runtime=" + test3_provider2_runtime);
        System.out.println("test3_provider3_runtime=" + test3_provider3_runtime);
        test3_provider1_runtime=0;
        test3_provider2_runtime=0;
        test3_provider3_runtime=0;
    }

得出结果:

test3_provider1_runtime=2852
test3_provider2_runtime=3541
test3_provider3_runtime=3606

我不知道这个结果是不是LeastActive运行的效果。感谢

我总感觉这个结果不对劲,8085端口运行的最慢,为什么还有2852次运行呢?好像不太符合常理

3

当我加大提供者的sleep时间时: package com.ghy.www.my.loadbalance.provider.service;

import com.ghy.www.api.IService3; import org.springframework.beans.factory.annotation.Value;

public class HelloService3 implements IService3 { @Value("${server.port}") private int portValue;

@Override
public String getHello(String username) {
    System.out.println("HelloService3 portValue=" + portValue + " username=" + username);
    if (portValue == 8085) {
        try {
            Thread.sleep(800);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    if (portValue == 8086) {
        try {
            Thread.sleep(700);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    if (portValue == 8087) {
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    return "hello3 " + username + " port=" + portValue;
}

}

执行消费者后,提供者出现警告: Thread pool is EXHAUSTED! Thread Name: DubboServerHandler-192.168.30.188:20882, Pool Size: 200 (active: 200, core: 200, max: 200, largest: 200), Task: 991 (completed: 791), Executor status:(isShutdown:false, isTerminated:false, isTerminating:false), in dubbo://192.168.30.188:20882!, dubbo version: 3.0.3-SNAPSHOT, current host: 192.168.191.1

这个实验不管我如何测,都测不出理想中的效果(由于8085最慢,所以请求到8085的请求是“超级”少的)。

4

Any load balancer that uses the active parameter needs to be configured in consumer.

@DubboReference(loadbalance = LoadbalanceRules.LEAST_ACTIVE, timeout = 6000000, filter = "activelimit", active=100)
private IService3 service3;
9

@24kpure 大佬,我这么配置会报找不到是怎么回事啊? image

2
@DubboReference(loadbalance = LoadbalanceRules.LEAST_ACTIVE, timeout = 6000000, active=100)