對於任何一個高可用高負載的系統來講,負載均衡是一個必不可少的名稱。在大型分佈式計算體系中,某個服務在單例的狀況下,很難應對各類突發狀況。所以,負載均衡是爲了讓系統在性能出現瓶頸或者其中一些出現狀態下能夠進行分發業務量的解決方案。在SpringCloud 體系當中,加入了Netflix公司的不少優秀產品,其中一個就是針對於服務端進行負載均衡的Ribbon。html
【微服務】輕鬆搞定SpringCloud微服務目錄
本系列爲連載文章,閱讀本文以前強烈建議您先閱讀前面幾篇。java
負載均衡:英文名稱爲Load Balance, 創建在現有網絡結構之上,它提供了一種廉價有效透明的方法擴展網絡設備和服務器的帶寬、增長吞吐量、增強網絡數據處理能力、提升網絡的靈活性和可用性。其意思就是分攤到多個操做單元上進行執行,例如Web服務器、FTP服務器、企業關鍵應用服務器和其它關鍵任務服務器等,從而共同完成工做任務。
負載均衡帶來的好處很明顯:git
Ribbon是Netflix開源的一款用於客戶端軟負載均衡的工具軟件。Spring Cloud對Ribbon進行了一些封裝以更好的使用Spring Boot的自動化配置理念。github
Spring Cloud Ribbon是基於Netflix Ribbon實現的一套客戶端負載均衡的工具。它是一個基於HTTP和TCP的客戶端負載均衡器。它能夠經過在客戶端中配置ribbonServerList來設置服務端列表去輪詢訪問以達到均衡負載的做用。web
起飛以前,先說明一下,本項目前幾篇文章中已經構建了相關子項目包括:註冊中心、配置中心。本文中繼續可使用。spring
須要建立兩個如出一轍的服務器,讓客戶端按照不一樣的機制進行分發,達到負載均衡的效果。咱們約定兩個子項目名稱:
cloud-hyh-service-1 端口號:8071
cloud-hyh-service-2 端口號:8072
對於服務名稱設置同樣:cloud-service ,其餘業務都同樣,能夠複製。【端口號不同】api
<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服務器
/** * @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); } }
/** * @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文件中加入如下依賴:app
<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: 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容器中。
/** * @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