SpringCloud版本:Finchley.SR2
SpringBoot版本:2.0.3.RELEASE
源碼位址:
https://gitee.com/bingqilinpeishenme/Java-Tutorials
前言
寫部落格一個多月了,斷斷續續的更新,今天有小夥伴催更新了,很高興,說明我的分享是有意義的。
于是乎,更新來了,還順便給該系列教程改了個名兒《最适合入門的SpringCloud教程》
通過之前的幾篇文章,在代碼中會有三個項目,分别是兩個注冊中心和一個用戶端,如下圖:
今天将會在這個代碼的基礎上:
- 将 eureka-client-8803 作為服務提供者
- 通過IDEA将eureka-client-8803啟動在8803和8804端口上,模拟服務提供者叢集
- 再建立一個服務消費者eureka-consumer-8805
- 讓消費者通過服務調用和負載均衡調用服務提供者的服務。
Tips:需要了解過RestTemplate的使用
SpringBoot圖文教程17—上手就會 RestTemplate 使用指南「Get Post」「設定請求頭」服務提供者叢集運作,建立服務消費者
服務提供者寫Controller接口
在服務提供者eureka-client-8803中寫入一個TestController類
package com.lby.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author luxiaoyang
* @create 2020-04-02-20:13
*/
@RestController
public class TestController {
/**
* @Value("${server.port}") 讀取application配置檔案中的值
* 指派到成員變量上
*
* ${server.port} 參數為 application配置檔案中的key
*/
@Value("${server.port}")
private String port;
/**
* @RequestParam 擷取Request參數的 用于RestFul風格中
* @PathVariable 擷取路徑中的參數
*/
@GetMapping("showImgById")
public String showImgById(@RequestParam("id") Integer id){
return "查詢到id為:"+id+"的資訊,使用端口号為:"+port;
}
}
通過IDEA在8803和8804端口号啟動服務提供者「模拟叢集」
IDEA 中 預設項目啟動是單例的,即一個項目隻能夠在一個端口号啟動一次。但是在IDEA 實際上是支援多執行個體的,一個項目可以通過修改端口号啟動多次。
以eureka-client-8803為例
1.修改eureka-client-8803的IDEA啟動設定
IDEA的版本不同,還會出現如圖所示的配置
2.在 8803 端口号啟動項目
3.不要關閉 8803 這個服務,然後直接修改yml中的端口号為8804,再次通過啟動類啟動
通過以上步驟,就啟動了兩個服務提供者,用來模拟叢集,效果如下
建立服務消費者 eureka-consumer-8805
根據之前教程中的步驟,再建立一個用戶端eureka-consumer-8805作為消費者
pom配置
<dependencies>
<!-- Eureka 用戶端的依賴-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- web的依賴-->
<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>
application配置檔案
server:
port: 8805
#指定目前服務的名稱 會注冊到注冊中心
spring:
application:
name: eureka-consumer-8805
# 指定 服務注冊中心的位址
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8801/eureka,http://localhost:8800/eureka
啟動類
package com.lby;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author luxiaoyang
* @create 2020-04-02-20:43
*/
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaConsumer8805 {
public static void main(String[] args) {
SpringApplication.run(EurekaConsumer8805.class,args);
}
}
服務調用和負載均衡
Ribbon負載均衡
Ribbon是一個基于HTTP和TCP的用戶端負載均衡工具。
Ribbon是Netflix釋出的開源項目,主要功能是提供用戶端的負載均衡算法。
關于Ribbon的簡介,有一個名詞需要進行解釋,用戶端負載均衡?
負載均衡是一種非常常見的技術,例如:Nginx,F5。
對于Nginx來說,在Nginx中存儲一份服務端的清單,使用者的請求到達Nginx之後,Nginx會根據負載均衡政策從服務清單中選擇一台伺服器去處理用戶端的請求。
服務清單存儲在負載均衡服務中,這就是服務端負載均衡。
而用戶端負責均衡,例如Ribbon,本身是不存儲可用服務清單的,需要服務清單的時候通過服務節點找注冊中心擷取。
服務消費者 eureka-consumer-8805 中通過RestTemplate+Ribbon調用服務提供者
RestTemplate+Ribbon的配置
1.在服務消費者 eureka-consumer-8805中導入Ribbon的依賴
<!--ribbon-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
2.在啟動類中寫RestTemplate+Ribbon的配置
示範Ribbon負載均衡的效果
1.在消費者中建立接口 通過RestTemplate調用服務提供者
package com.lby.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @author luxiaoyang
* @create 2020-04-02-20:49
*/
@RestController
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
/**
* 調用服務提供者
*/
@RequestMapping("/consumer/showConsumer")
public String showConsumer(){
/**
* 通過Ribbon 發送服務調用 用的是RestTemplate
* RestTemplate 本身沒有負載均衡的能力
*
* 注意:RestTemplate請求位址中寫的不是 ip+端口号 而是被調用服務的服務名稱
*/
String object = restTemplate.getForObject("http://eureka-client-8803/showImgById?id=1", String.class);
return "查詢到服務提供者的資料,"+object;
}
}
注意:RestTemplate請求位址中寫的不是 ip+端口号 而是被調用服務的服務名稱
2.重新開機所有的服務,兩個服務提供者,一個服務消費者
3.通路服務消費者的接口
請求位址:
http://localhost:8805/consumer/showConsumer可以看到每次請求端口号不一樣
總結
以上就是RestTemplate+Ribbon的負載均衡的基本使用
- RestTemplate負責服務調用
- Ribbon實作負載均衡
恭喜你完成了本章的學習,為你鼓掌!如果本文對你有幫助,請幫忙點贊,評論,轉發,這對作者很重要,謝謝。
要掌握SpringCloud更多的用法,請持續關注本系列教程。