SpringCloud 之 Fegin —— 傳送GET、POST請求以及檔案上傳
深信自己通過學習理解寫出來的才是自己的
----------------------------------------------------------------------------------------------------------------------------
由於專案需要呼叫其他微服務的資料,首先想到的就是寫一個http網路請求的工具類,但是想到在之前看springCloud的時候裡面有這個Fegin可以實現,就順便實踐一下,雖然過程有點坎坷,好在都順利解決了,在實踐的過程中主要遇見了以下幾個問題
1) 不同請求方式傳參方式不同
2) 同一請求方式請求頭資訊不同
3) 傳送請求時候的編碼器不同
4) 檔案上傳
(一) Fegin使用
1) 新增依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency>
2)在啟動類上加上註解
@EnableEurekaClient @EnableHystrixDashboard @EnableFeignClients //這個就是使用Feign需要新增的註解 @SpringBootApplication public class VideoProxyServiceApplication { public static void main(String[] args) { SpringApplication.run(VideoProxyServiceApplication.class點選檢視程式碼 3)Feign 客戶端介面, args); } }
@Component @FeignClient(name = "stream-service",url = "${stream_service}") //name指定FeignClient的名稱,url一般用於除錯,可以手動指定@FeignClient呼叫的地址 public interface StreamServiceClient { //GET請求 @RequestMapping(value = "/task/findById",method = RequestMethod.GET) String findById(@RequestParam(value = "id") String id);點選檢視程式碼
4)在Controller層裡呼叫
@RestController @RequestMapping(value = "stream") public class StreamServiceController { @Autowired private StreamServiceClient streamServiceClient; @RequestMapping(value = "/findById",method = RequestMethod.GET) public ResponseResult findById(String id) { String s = streamServiceClient.findById(id); return responseResult(s, "jsonObject"); //ResponseResult是封裝的一個返回物件,而responseResult是寫的一個處理結果的公共方法,這裡就不展示了 } 點選檢視程式碼點選檢視程式碼
到這裡整個Feign的使用基本上就結束了,但是如果你認為這樣你就可以順利的使用Feign,那麼恭喜你,你將會很鬧心,因為在呼叫別人的服務的時候你不確定人家到底是需要怎麼取請求,如果是你寫介面只要你自己測試通了那就萬事大吉,可是現在是別人寫的介面讓你調,那麼你就需要考慮很多問題了,至少在我實踐中遇到的有這幾種,請求頭需要設定、請求的時候請求引數在路徑上傳參該怎麼傳等等一系列問題
劃重點,我主要就是講的運用,也就是在實際使用過程中對於不同的請求,我們應該怎麼做
(二) GET請求
對於GET請求應該算是最簡單的了,在這裡我分兩種來說,一種引數就在請求頭上,還有一種是引數在路徑中的
1) 對於引數在請求頭中的請求
@RequestMapping(value = "/task/findById",method = RequestMethod.GET) String findById(@RequestParam(value = "id") String id);
在@RequestMapping註解中value值是介面,method規定請求方式,在傳參的時候注意一定要加上@RequestParam,值是請求的引數名
2)請求引數在路徑中的請求
@RequestMapping(value = "/shrekapi/job/{id}",method = RequestMethod.GET) String deletejob(@PathVariable("id") String id);這裡注意我們的註解是@PathVariable加上引數名
(三)POST請求
POST請求,請求的時候我遇到了三種情況,
1、請求引數在請求體中(這種方式其實是最方便的)
// @RequestMapping(value = /addLable",method = RequestMethod.POST) String addLable(@RequestBody PointMsg lableName);//PointMsg是實體類
請求的時候直接在引數前加上@RequestBody,定義方法為POST
2、請求引數在請求頭中
// @RequestMapping(value = "/updateStatusByCameraId",method = RequestMethod.POST) String updateStatusByCameraId(@RequestParam("camera") String camera);
這種請求的產生應該是在寫介面的時候引數前沒有加註解造成的,其實這種方式跟GET請求是一模一樣的
3、請求頭對映條件不同
//刪除標籤 @RequestMapping(value = "/deleteLable",method = RequestMethod.POST,headers = {"content-type=application/x-www-form-urlencoded"}) String deleteLable(@RequestParam("id") String id);
對於需要更改請求頭對映的直接使用headers,定義不同的對映
(四)檔案上傳、自定義編碼器
由於檔案上傳的時候我們我們傳引數的時候其實傳的是檔案,這個時候我們預設的編碼器是不支援這種的,需要我們自定義編碼器並應到我們的client
注:在網上很多提到了自定義編碼器並使用@Configuration使其生效,最好不要這樣,一旦使用了這個註解那就是全域性都使用這個編碼器了,那麼你的其他請求就會出現問題,報編碼器異常
我們的寫的時候可以直接在客戶端介面上指定使用哪個編碼器,並且只在這個客戶端介面生效,還有注意一點的就是,@FeignClient裡面的name屬性不可以和其他客戶端介面重複,重複的話等於是同一個客戶端介面還是會使用指定的編碼器
@Component @FeignClient(name = "stream-service-File",url = "${stream_service}",configuration = FileUploadServiceClient.FeignMultipartSupportConfig.class) public interface FileUploadServiceClient { //檔案上傳 @RequestMapping(value = "/importFile",method = RequestMethod.POST,produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) String upload(@RequestBody MultipartFile file); @RequestMapping(value = "/downloadExcel",method = RequestMethod.GET,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE) Response downloadFile(); //自定義檔案上傳編碼器 class FeignMultipartSupportConfig { @Bean public Encoder multipartFormEncoder() { return new SpringFormEncoder(); } @Bean public feign.Logger.Level multipartLoggerLevel() { return feign.Logger.Level.FULL; } } }
到這裡結束,如果遇到了新的問題歡迎一起探討,上述所有都是在使用過程中遇到的一些問題,僅做記錄,供君參考