负载均衡框架 ribbon 二
Ribbon 负载均衡机制
官方文档地址:https://github.com/Netflix/ribbon/wiki/Working-with-load-balancers
1. Ribbon 内置的负载均衡规则
① RoundRobinRule
此规则是默认规则,通过轮询服务器列表的方式选择服务器
② AvailabilityFilteringRule
此规则会跳过短路的服务器和有高并发连接数的服务器
默认情况下RestClient最近3次无法与服务建立连接,会被置为短路状态。可以通过配置修改次数,具体可通过上述连接查看文档
③ WeightedResponseTimeRule
每个服务器根据其平均响应时间给予权重。响应时间越长,权重就越小。此规则随机选择服务器,其中可能性有服务器的权重决定
④ ZoneAvoidanceRule
以可用的服务器为基础,对服务器进行分类选择
④ BestAvailiableRuel
略短路的机器,并选择并发数较低的服务器
⑤ RandomRule
随机选择一个可用的服务器
⑥ RetryRule
重试机制的选择逻辑
2.自定义规则
(1)默认轮询规则
import com.netflix.loadbalancer.BaseLoadBalancer; import com.netflix.loadbalancer.ILoadBalancer; import com.netflix.loadbalancer.Server; import java.util.ArrayList; import java.util.List; public class LoadBalancerTest { public static void main(String[] args) { ILoadBalancer loadBalancer = new BaseLoadBalancer(); List<Server> servers = new ArrayList<>(); servers.add(new Server("localhost", 8080)); servers.add(new Server("localhost", 8081)); loadBalancer.addServers(servers); for (int i = 0; i < 10; i++) { Server server = loadBalancer.chooseServer(null); System.out.println(server); } } }
(2)自定义规则
import com.netflix.loadbalancer.ILoadBalancer; import com.netflix.loadbalancer.IRule; import com.netflix.loadbalancer.Server; import java.util.List; import java.util.Random; public class MyRule implements IRule { private ILoadBalancer iLoadBalancer; @Override public Server choose(Object o) { List<Server> servers = iLoadBalancer.getAllServers(); Random random = new Random(); int num = random.nextInt(10); if (num > 7) { return getServerByPort(servers, 8081); } return getServerByPort(servers, 8080); } @Override public void setLoadBalancer(ILoadBalancer iLoadBalancer) { this.iLoadBalancer = iLoadBalancer; } @Override public ILoadBalancer getLoadBalancer() { return this.iLoadBalancer; } private Server getServerByPort(List<Server> servers, int port) { for (Server server : servers) { if (server.getPort() == port) { return server; } } return null; } }
import com.idelan.test.rules.MyRule; import com.netflix.client.ClientFactory; import com.netflix.client.http.HttpRequest; import com.netflix.client.http.HttpResponse; import com.netflix.config.ConfigurationManager; import com.netflix.loadbalancer.BaseLoadBalancer; import com.netflix.loadbalancer.Server; import com.netflix.niws.client.http.RestClient; import java.net.URI; import java.util.ArrayList; import java.util.List; public class TestMyRule { public static void main(String[] args) throws Exception{ //customRules(); testRibbon(); } //自定义负载均衡规则 public static void customRules() { BaseLoadBalancer loadBalancer = new BaseLoadBalancer(); MyRule rule = new MyRule(); rule.setLoadBalancer(loadBalancer); loadBalancer.setRule(rule); List<Server> servers = new ArrayList<>(); servers.add(new Server("localhost", 8080)); servers.add(new Server("localhost", 8081)); loadBalancer.addServers(servers); for (int i = 0; i < 10; i++) { Server server = loadBalancer.chooseServer(null); System.out.println(server); } } //通过自定义负载均衡规则访问服务端接口 public static void testRibbon() throws Exception{ //通过properties配置文件导入配置信息 //ConfigurationManager.loadPropertiesFromResources("my-ribbon.properties"); //通过java代码导入配置信息 ConfigurationManager.getConfigInstance().setProperty( "my-client.ribbon.listOfServers", "localhost:8080, localhost:8081"); ConfigurationManager.getConfigInstance().setProperty("my-client.ribbon.NFLoadBalancerRuleClassName", MyRule.class.getName()); RestClient client = (RestClient) ClientFactory.getNamedClient("my-client"); HttpRequest request = HttpRequest.newBuilder().uri(new URI("/person")).build(); // 3 for (int i = 0; i < 10; i++) { HttpResponse response = client.executeWithLoadBalancer(request); // 4 System.out.println("返回结果为 Status code for " + response.getRequestedURI() + " :" + response.getStatus()+"内容:"+response.getEntity(String.class)); } } }
说明:此处需要导入的jar包和上一篇博客(负载均衡框架 ribbon 一)中需要导入的jar包一样