Springcloud的核心組件之Ribbon
上篇文章詳細介紹了springcloud的注冊中心Eureka,那么這篇文章則會介紹springcloud的另外一個組件Spring Cloud Ribbon以及我們較為熟知同樣起到了負載均衡作用的Nginx,二者又有怎樣的區別呢?
Spring Cloud Ribbon是一個基于HTTP和TCP的客戶端負載均衡工具,它基于Netflix Ribbon實作,通過Spring Cloud的封裝,可以讓我們輕松地將面向服務的REST模版請求自動轉換成客戶端負載均衡的服務呼叫,
Spring Cloud Ribbon雖只是一個工具類框架,它在服務體系中起著重要作用,它不像服務注冊中心、配置中心、API網關那樣需要獨立部署,但是它幾乎存在于每一個Spring Cloud構建的微服務基礎設施中,
如何集成Ribbon?
① 首先引入Ribbon依賴
其實這里不參考ribbon依賴也是可以的,因為在spring-cloud-starter-netflix-eureka-client依賴中已經包含了Ribbon Starter,
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
② 在啟動類中添加@EnableDiscoveryClient注解
@EnableDiscoveryClient
@EnableEurekaClient
@SpringBootApplication
public class SpringcloudClientApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudClientApplication.class, args);
}
}
③ RestTemplate的使用
提到Ribbon就必須要介紹一下RestTemplate!RestTemplate是Spring提供的一個訪問Http服務的客戶端類! 傳統情況下訪問restful服務,一般使用Apache的HttpClient,不過這種方式使用太過繁瑣,spring提供了一種簡單便捷的模板類進行操作,也就是RestTemplate類,
如下所示:
/**
* LoadBalanced:使RestTemplate以負載均衡的方式呼叫服務
* @return
*/
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
④驗證Ribbon的負載均衡
代碼案例:
思路:1.啟用注冊中心,2.創建兩個服務提供者,3.創建服務消費者,以restTemplate的方式呼叫服務提供者,查看ribbon組件是否以負載均衡的方式呼叫服務提供者,
1 :啟動注冊中心,可以參考之前的文章

2:創建兩個服務者,并啟動
#服務埠 配置如下
server.port=8066
#服務名稱
spring.application.name=provider-client
#服務地址
eureka.instance.hostname=localhost
#取消檢索服務
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=true
#注冊中心路徑,表示向這個注冊中心注冊服務,如果向注冊中心注冊多個服務,用“,”進行分隔
eureka.client.serviceUrl.defaultZone=http://localhost:8011/eureka
3:創建控制器,提供服務,回傳埠資訊
@RestController
public class RibbonController {
@GetMapping("/testRibbon")
public String hello(String name , HttpServletRequest request){
//回傳埠資訊
return " 此時被呼叫的服務埠為: " + request.getServerPort() + " , hello " + name;
}
}
4:第二個服務提供者和上面類似只是組態檔的埠不一致
#服務埠
server.port=8077
#服務名稱
spring.application.name=provider-client
#服務地址
eureka.instance.hostname=localhost
#取消檢索服務
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=true
#注冊中心路徑,表示向這個注冊中心注冊服務,如果向注冊中心注冊多個服務,用“,”進行分隔
eureka.client.serviceUrl.defaultZone=http://localhost:8011/eureka
5:啟動消費者,以RestTemplate方式呼叫
- 組態檔如下:
#服務方埠
server.port=8091
#服務名稱
spring.application.name=consumer-client-01
#服務地址
eureka.instance.hostname=localhost
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=consumer-client-01
#取消檢索服務
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=true
#注冊中心路徑,表示向這個注冊中心注冊服務,如果向注冊中心注冊多個服務,用“,”進行分隔
eureka.client.serviceUrl.defaultZone=http://localhost:8011/eureka
- 使用RestTemplate類訪問restful服務
@Configuration
public class RestTemplateTest {
/**
* 添加LoadBalanced,使RestTemplate以負載均衡的方式呼叫服務
* @Configuration:類上面務往添加@Configuration注解
* @return
*/
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
- 創建控制器,呼叫服務提供者
@RestController
public class ConsumerController {
@Autowired
private RestTemplate restTemplate; //注入RestTemplate
@GetMapping("/sayHello")
public String sayHello(String name){
String url = "http://provider-client/test?name=" + name;
String result = restTemplate.getForObject(url , String.class);
return result;
}
}
6:啟動消費者服務,并查看注冊中心注冊情況

7:訪問服務消費者,驗證服務呼叫是否以負載均衡方式
第一次訪問埠為8066

第二次訪問埠則為8077

發現兩個服務提供者服務分別被呼叫了2次,說明Ribbon默認的負載均衡策略是輪詢的方式,小伙伴們也可以多呼叫幾次驗證一下
Ribbon和Nginx的區別
Ribbon和Nginx都是起到負載均衡的作用,但是二者又有怎么樣的區別呢??
Ribbon 是Netflix公司的一個處理負載均衡開源的專案,它是運行在服務端的負載均衡器,如下圖,

如上圖所示很清晰的描述了其作業原理就是Consumer端獲取到了所有的服務串列之后,在其內部使用負載均衡演算法,使用輪詢的方式對多個服務進行呼叫,

相比Nginx我們可以看到的是Nginx是接收了所有的請求進行之后進行的負載均衡處理,
簡單的總結來說:
Nginx: 服務器端負載均衡,ribbon在服務節點掛掉后,可以自動去除節點,
Ribbon:客戶端負載均衡,nginx剔除不可用節點相對復雜,
歡迎關注公眾號!
公眾號回復:入群,掃碼加入我們交流群!

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/276615.html
標籤:java
