You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2021/09/16 14:07:37 UTC

[GitHub] [dubbo] startjava edited a comment on issue #8826: 如何写DEMO能看到LeastActive运行的效果

startjava edited a comment on issue #8826:
URL: https://github.com/apache/dubbo/issues/8826#issuecomment-920934465


   我的提供者代码:
   ```
   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次运行呢?好像不太符合常理
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org