content: package com.alibabacloud.hipstershop.common;
  
  import java.util.List;
  import java.util.Random;
  
  import com.netflix.client.config.IClientConfig;
  import com.netflix.loadbalancer.AbstractLoadBalancerRule;
  import com.netflix.loadbalancer.ILoadBalancer;
  import com.netflix.loadbalancer.Server;
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  
  /**
   * @author wangtao 2020/7/1 22:53
   */
  public class MyRule extends AbstractLoadBalancerRule {
  
      private static final Logger LOGGER = LoggerFactory.getLogger(MyRule.class);
  
      Random rand;
  
      public MyRule() {
          rand = new Random();
      }
  
      /**
       * Randomly choose from all living servers
       */
      public Server choose(ILoadBalancer lb, Object key) {
          if (lb == null) {
              return null;
          }
          Server server = null;
  
          int count = 1;
  
          while (server == null) {
              if (Thread.interrupted()) {
                  return null;
              }
              List<Server> upList = lb.getReachableServers();
              LOGGER.info("第 " + count + " 次 upList: " + upList);
              List<Server> allList = lb.getAllServers();
              LOGGER.info("第 " + count + " 次 allList: " + allList);
              count++;
  
              int serverCount = allList.size();
              if (serverCount == 0) {
                  /*
                   * No servers. End regardless of pass, because subsequent passes
                   * only get more restrictive.
                   */
                  return null;
              }
  
              int index = rand.nextInt(serverCount);
              server = upList.get(index);
  
              if (server == null) {
                  /*
                   * The only time this should happen is if the server list were
                   * somehow trimmed. This is a transient condition. Retry after
                   * yielding.
                   */
                  Thread.yield();
                  continue;
              }
  
              if (server.isAlive()) {
                  return (server);
              }
  
              // Shouldn't actually happen.. but must be transient or a bug.
              server = null;
              Thread.yield();
          }
  
          return server;
  
      }
  
      @Override
      public Server choose(Object key) {
          return choose(getLoadBalancer(), key);
      }
  
      @Override
      public void initWithNiwsConfig(IClientConfig clientConfig) {
          // TODO Auto-generated method stub
  
      }
  
  }