Ribbon负载均衡(客户端)
Posted wlwl
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Ribbon负载均衡(客户端)相关的知识,希望对你有一定的参考价值。
Ribbon是客户端的负载均衡机制,它有几种负载均衡机制。默认是轮询,我们也可以自定义规则。通过合理的分配网络请求来减小服务器的压力。
案例编写:
一:服务端搭建
目录结构:
1.1 导包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.springCloud</groupId> <artifactId>Ribbon_server</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.5.7.RELEASE</version> </dependency> </dependencies> </project>
1.2 建个实体类
public class Person { private Integer id; private String name; //用来记录请求路径 private String message; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
1.3编写controller层
@RestController public class MyController { @RequestMapping(value = "/person", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public Person getPerson(HttpServletRequest request) { Person p = new Person(); p.setId(1); p.setName("angus"); p.setMessage(request.getRequestURL().toString()); return p; } }
1.4编写启动类
import java.util.Scanner; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; @SpringBootApplication public class ServiceApp { public static void main(String[] args) { //1. 启动main方法后,在控制台输入端口号. Scanner scan = new Scanner(System.in); String port = scan.nextLine(); //以输入的端口号启动 new SpringApplicationBuilder(ServiceApp.class) .properties("server.port="+port).run(args); //2. 启动2次,分别以8080, 8081启动,浏览器访问测试一下。 } }
测试: 在控制台分别以8080 、 8081端口启动后,用浏览器访问目标接口。
二:客户端搭建
目录结构:
2.1: 导入ribbon核心包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.springCloud</groupId> <artifactId>Ribbon_client</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.netflix.ribbon</groupId> <artifactId>ribbon-core</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>com.netflix.ribbon</groupId> <artifactId>ribbon-httpclient</artifactId> <version>2.2.2</version> </dependency> </dependencies> </project>
2.2编写一个方法,用ribbon请求10次,看看是哪台服务器在为我们提供服务。
public class Ribbon { public static void main(String[] args) throws Exception { //根据ribbon-httpclient发送请求 ConfigurationManager.getConfigInstance().setProperty("my-client.ribbon.listOfServers", "localhost:8080,localhost:8081"); RestClient client = (RestClient) ClientFactory.getNamedClient("my-client"); HttpRequest request = HttpRequest.newBuilder().uri("/person").build(); //请求10次,看看每次的代理对象是谁 for(int i = 0; i < 10; i++) { HttpResponse response = client.executeWithLoadBalancer(request); String json = response.getEntity(String.class); System.out.println(json); } } }
=========================================
【控制台输出】
{"id":1,"name":"angus","message":"http://localhost:8081/person"}
{"id":1,"name":"angus","message":"http://localhost:8080/person"}
{"id":1,"name":"angus","message":"http://localhost:8081/person"}
{"id":1,"name":"angus","message":"http://localhost:8080/person"}
{"id":1,"name":"angus","message":"http://localhost:8081/person"}
{"id":1,"name":"angus","message":"http://localhost:8080/person"}
{"id":1,"name":"angus","message":"http://localhost:8081/person"}
{"id":1,"name":"angus","message":"http://localhost:8080/person"}
{"id":1,"name":"angus","message":"http://localhost:8081/person"}
{"id":1,"name":"angus","message":"http://localhost:8080/person"}
以上是关于Ribbon负载均衡(客户端)的主要内容,如果未能解决你的问题,请参考以下文章
Springcloud + nacos + gateway 负载均衡(ribbon)
SpringCloud -- Ribbon负载均衡(负载均衡流程负载均衡原理(探究原码)负载均衡策略懒加载(饥饿加载))