由於項目需要調用其他微服務的數據,首先想到的就是寫一個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, args); } }
3)Feign 客戶端接口

@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; } } }
到這里結束,如果遇到了新的問題歡迎一起探討,上述所有都是在使用過程中遇到的一些問題,僅做記錄,供君參考