1. 负载均衡框架,支持可插拔式的负载均衡规则java
2. 支持多种协议,如HTTP、UDP等web
3. 提供负载均衡客户端spring
Ribbon子模块json
1. ribbon-core(ribbon的核心,主要包含负载均衡器、负载均衡接口、客户端接口、内置负载均衡实现API)服务器
2. ribbon-eureka(为eureka客户端提供的客户端实现类)app
3. ribbon-httpclient(为负载均衡提供了REST客户端)负载均衡
负载均衡器组件框架
1. 一个负载均衡器,至少提供如下功能spring-boot
1.1 要维护各个服务器的IP等信息测试
1.2 根据特定逻辑选取服务器
2. 为了实现基本的负载均衡功能,Ribbon的负载均衡器有三大子模块
2.1 Rule
2.2 Ping
2.3 ServerList
因为本次的教程是没有与SpringCloud整合的,是用来单独使用的,下面就教你们怎么搭建Ribbon程序 并 调用服务。
1:建立Ribbon服务器(一个单纯的SpringBoot程序)
pom.xml
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.5.7.RELEASE</version> </dependency> </dependencies>
为了方便Ribbon客户端测试,在这里建一个实体类:Person.java
public class Person { private String url;// 处理请求的服务器url private String message;// 提示信息 public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
PersonController.java
@RestController public class PersonController { @RequestMapping(value="/getPerson", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE) public Person getPerson(HttpServletRequest request){ Person p = new Person(); p.setMessage("请求成功"); p.setUrl(request.getRequestURL().toString()); return p; } }
启动类:Application.java(由于要测试负载均衡,全部这里须要启动多个服务,如下配置以手动输入端口号方式启动)
@SpringBootApplication public class Application { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String port = scan.nextLine(); new SpringApplicationBuilder(Application.class).properties("server.port="+port).run(args); } }
本次启动以端口:8080、8081分别启动,稍后咱们配置完客户端 统一测试(配置后,将服务启动)
2:建立Ribbon客户端
pom.xml 中只须要引入核心及客户端的依赖便可
<dependency> <groupId>com.netflix.ribbon</groupId> <artifactId>ribbon-core</artifactId> <version>2.2.5</version> </dependency> <dependency> <groupId>com.netflix.ribbon</groupId> <artifactId>ribbon-httpclient</artifactId> <version>2.2.5</version> </dependency>
编写main方法测试,调用服务(服务列表也能够直接在配置文件中配置,本次用setProperty进行配置)其中的my-client,这个名称能够任意起,由于这个名称是用来命名配置建立客户端的。
public static void main(String[] args) { try { // 写入服务列表 ConfigurationManager.getConfigInstance().setProperty("my-client.ribbon.listOfServers", "localhost:8080,localhost:8081"); // 输出服务列表 System.out.println("服务列表:" + ConfigurationManager.getConfigInstance().getProperty("my-client.ribbon.listOfServers")); // 获取客户端(若是获取不到,可经过getNamedClient方法自动建立) RestClient client = (RestClient) ClientFactory.getNamedClient("my-client"); // 建立request对象 HttpRequest request = HttpRequest.newBuilder().uri(new URI("/getPerson")).build();// 写入将要访问的接口 // 屡次访问测试 for (int i = 0; i < 10; i++) { // 建立response对象 HttpResponse response = client.executeWithLoadBalancer(request); // 接收请求结果 String json = response.getEntity(String.class); // 打印结果 System.out.println(json); } } catch (Exception e) { e.printStackTrace(); } }
以上就是Ribbon单独使用的所有过程,下面你们看一下Ribbon负载均衡默认的轮询规则
服务列表:[localhost:8080, localhost:8081] {"url":"http://localhost:8081/getPerson","message":"请求成功"} {"url":"http://localhost:8080/getPerson","message":"请求成功"} {"url":"http://localhost:8081/getPerson","message":"请求成功"} {"url":"http://localhost:8080/getPerson","message":"请求成功"} {"url":"http://localhost:8081/getPerson","message":"请求成功"} {"url":"http://localhost:8080/getPerson","message":"请求成功"} {"url":"http://localhost:8081/getPerson","message":"请求成功"} {"url":"http://localhost:8080/getPerson","message":"请求成功"} {"url":"http://localhost:8081/getPerson","message":"请求成功"} {"url":"http://localhost:8080/getPerson","message":"请求成功"}