Ribbon簡介
1. 負載均衡框架,支持可插拔式的負載均衡規則
2. 支持多種協議,如HTTP、UDP等
3. 提供負載均衡客戶端
Ribbon子模塊
1. ribbon-core(ribbon的核心,主要包含負載均衡器、負載均衡接口、客戶端接口、內置負載均衡實現API)
2. ribbon-eureka(為eureka客戶端提供的客戶端實現類)
3. ribbon-httpclient(為負載均衡提供了REST客戶端)
負載均衡器組件
1. 一個負載均衡器,至少提供以下功能
1.1 要維護各個服務器的IP等信息
1.2 根據特定邏輯選取服務器
2. 為了實現基本的負載均衡功能,Ribbon的負載均衡器有三大子模塊
2.1 Rule
2.2 Ping
2.3 ServerList
至於負載均衡的機制和規則,筆者會在下一章的 “Ribbon負載均衡機制” 中進行講解,那么在這里就不多說了。
由於本次的教程是沒有與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":"請求成功"}