IRule
public class MyRule implements IRule { private ILoadBalancer lb; public Server choose(Object key) { System.out.println("这是自定义的规则类"); Random r = new Random(); int randomNum = r.nextInt(10); List<Server> servers = lb.getAllServers(); if(randomNum > 7) { Server s = getServerByPort(servers, 8081); return s; } return getServerByPort(servers, 8080); } private Server getServerByPort(List<Server> servers, int port) { for(Server s : servers) { if(s.getPort() == port) { return s; } } return null; } public void setLoadBalancer(ILoadBalancer lb) { this.lb = lb; } public ILoadBalancer getLoadBalancer() { return lb; } }
配置
server: port: 9000 spring: application: name: spring-lb-invoker
#服务实例name spring-lb-provider: ribbon: NFLoadBalancerRuleClassName: org.xx.cloud.MyRule eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/