1. Ribbon
負載均衡框架,支持可插拔式的負載均衡規則
支持多種協議,如HTTP、UDP等
提供負載均衡客戶端
1.1 負載均衡器組件
一個負載均衡器,至少提供以下功能:
- 要維護各個服務器的IP等資訊
- 根據特定邏輯選取服務器
為了實作基本的負載均衡功能,Ribbon的負載均衡器有三大子模塊:
- Rule
- Ping
- ServerList

1.2 實作程式
創建專案:ribbon-service
pom.xml:
<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.crazyit.cloud</groupId>
<artifactId>ribbon-service</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>
Person:
package org.crazyit.cloud; 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; } }
MyController:
package org.crazyit.cloud; import javax.servlet.http.HttpServletRequest; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @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; } }
ServiceApp:
package org.crazyit.cloud; 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) throws Exception { Scanner scan = new Scanner(System.in); String port = scan.nextLine(); new SpringApplicationBuilder(ServiceApp.class).properties( "server.port=" + port).run(args); } }
啟動兩個服務:控制臺輸入8080和8081:

然后創建一個客戶端ribbon-client專案:
pom:
<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.crazyit.cloud</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>
TestRibbon:
package org.crazyit.cloud; 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.niws.client.http.RestClient; public class TestRibbon { public static void main(String[] args) throws Exception { 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(); for(int i = 0; i < 10; i++) { HttpResponse response = client.executeWithLoadBalancer(request); String json = response.getEntity(String.class); System.out.println(json); } } }
然后運行:

實作了負載均衡了,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/196873.html
標籤:Java
