對於任何一個高可用高負載的系統來說,負載均衡是一個必不可少的名稱。在大型分布式計算體系中,某個服務在單例的情況下,很難應對各種突發情況。因此,負載均衡是為了讓系統在性能出現瓶頸或者其中一些出現狀態下可以進行分發業務量的解決方案。在SpringCloud 體系當中,加入了Netflix公司的很多優秀產品,其中一個就是針對於服務端進行負載均衡的Ribbon。
本系列博文目錄
【微服務】輕松搞定SpringCloud微服務目錄
本系列為連載文章,閱讀本文之前強烈建議您先閱讀前面幾篇。
相關簡介
負載均衡簡介
負載均衡:英文名稱為Load Balance, 建立在現有網絡結構之上,它提供了一種廉價有效透明的方法擴展網絡設備和服務器的帶寬、增加吞吐量、加強網絡數據處理能力、提高網絡的靈活性和可用性。其意思就是分攤到多個操作單元上進行執行,例如Web服務器、FTP服務器、企業關鍵應用服務器和其它關鍵任務服務器等,從而共同完成工作任務。
負載均衡帶來的好處很明顯:
Ribbon簡介
Ribbon是Netflix開源的一款用於客戶端軟負載均衡的工具軟件。Spring Cloud對Ribbon進行了一些封裝以更好的使用Spring Boot的自動化配置理念。
Spring Cloud Ribbon 簡介
Spring Cloud Ribbon是基於Netflix Ribbon實現的一套客戶端負載均衡的工具。它是一個基於HTTP和TCP的客戶端負載均衡器。它可以通過在客戶端中配置ribbonServerList來設置服務端列表去輪詢訪問以達到均衡負載的作用。
開始起飛
起飛之前,先說明一下,本項目前幾篇文章中已經構建了相關子項目包括:注冊中心、配置中心。本文中繼續可以使用。
創建兩個服務器
需要創建兩個一模一樣的服務器,讓客戶端按照不同的機制進行分發,達到負載均衡的效果。我們約定兩個子項目名稱:
cloud-hyh-service-1 端口號:8071
cloud-hyh-service-2 端口號:8072
對於服務名稱設置一樣:cloud-service ,其他業務都一樣,可以復制。【端口號不一樣】
pom.xml文件配置
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
服務器一參數配置
#服務注冊中心配置
eureka:
client:
service-url:
defaultZone: http://localhost:8081/eureka/
instance:
appname: cloud-service
lease-renewal-interval-in-seconds: 1
server:
port: 8071
spring:
application:
name: cloud-service
服務器二參數配置
#服務注冊中心配置
eureka:
client:
service-url:
defaultZone: http://localhost:8081/eureka/
instance:
appname: cloud-service
server:
port: 8072
spring:
application:
name: cloud-service
說明:與配置一其實基本一樣,只不過將端口號配置成 8072
服務器入口配置Application.yml
/**
* @Description :
* @Author hanyahong
* @Date 2017/12/7- 17:35
*/
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceTwoApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceTwoApplication.class, args);
}
}
新建測試API類
/**
* @Description :測試RibbonTest API
* @Author hanyahong
* @Date 2017/12/7- 17:40
*/
@RestController
@RequestMapping(value = "/ribbon")
public class RibbonTestApi {
/**
* 獲取博客名稱API
*
* @return 相關信息
*/
@RequestMapping(value = "name", method = RequestMethod.GET)
public String getMyBlogNameApi() {
return "千萬之路剛開始-www.hanyahong.com-beijing"+"該服務器端口號:8071";
}
}
備注:兩台服務器,除了返回的服務器端口號 8071 8072不同之外,其他都相同,就是為了看到效果。
創建測試客戶端
創建一個子項目,cloud-hyh-ribbon-client ,主要用來測試ribbon客戶端負載。
pom文件配置
在pom文件中加入以下依賴:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
配置文件application配置
eureka:
client:
service-url:
defaultZone: http://localhost:8081/eureka/
instance:
appname: ribbon-client
server:
port: 8092
spring:
application:
name: ribbon-client
配置子項目啟動類
/**
* @Description :啟動類,示范負載均衡服務器
* @Author hanyahong
* @Date 2017/12/7- 17:00
*/
@SpringBootApplication
@EnableDiscoveryClient
public class RibbonServiceApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonServiceApplication.class, args);
}
/**
* Spring提供的用於訪問Rest服務的客戶端
* @return
*/
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
說明:
RestTemplate是Spring提供的用於訪問Rest服務的客戶端。RestTemplate提供了多種便捷訪問遠程Http服務的方法,能夠大大提高客戶端的編寫效率。調用RestTemplate的默認構造函數,RestTemplate對象在底層通過使用java.net包下的實現創建HTTP 請求,可以通過使用ClientHttpRequestFactory指定不同的HTTP請求方式。
ClientHttpRequestFactory接口主要提供了兩種實現方式,一種是SimpleClientHttpRequestFactory,使用J2SE提供的方式(既java.net包提供的方式)創建底層的Http請求連接,還有一種方式是使用HttpComponentsClientHttpRequestFactory方式,底層使用HttpClient訪問遠程的Http服務,使用HttpClient可以配置連接池和證書等信息。
@LoadBalanced 注解加在RestTemplate上面,這個注解會自動構造LoadBalancerClient接口的實現類並注冊到Spring容器中。
創建接口API
/**
* @Description : 測試客戶端負載均衡的接口API
* @Author hanyahong
* @Date 2017/12/7- 18:01
*/
@RestController
@RequestMapping(value = "/test")
public class TestRibbonApi {
/**
* 注入RestTemplate
*/
@Autowired
RestTemplate restTemplate;
@RequestMapping(value = "/blog/name" ,method = RequestMethod.GET)
public String testGetNameOfBlog(){
String url="http://CLOUD-SERVICE/ribbon/name";
return restTemplate.getForObject(url,String.class);
}
}
注意:這個代碼中 url 設置的是 上面提到的服務器的服務名。
啟動項目群進行測試
經過全面的配置,服務器全面配置完畢,包括一個注冊中心、一個配置中心、兩個相同配置的服務器、一台測試客戶端負載均衡的測試服務器。
啟動成功以后會在注冊中心看到。

通過訪問客戶端地址:http://localhost:8092/test/name 就可以訪問。效果如下:

刷新一次:

至此所有配置成功。測試結果也成功。
本文源碼
Github源碼:https://github.com/hanyahong/spring-cloud-microservice
