1. 回顧java
上文講解了手動建立Feign,比默認的使用更加靈活。web
本文將講解Feign對繼承、壓縮的支持以及日誌和多參數請求的構造等。spring
2. Feign對繼承的支持json
Feign支持繼承。使用繼承,可將一些公共操做分組到一些父接口中,從而簡化Feign的開發。服務器
儘管Feign的繼承可幫助咱們進一步簡化開發,可是Spring Cloud指出——一般狀況下,架構
不建議服務器端和客戶端之間共享接口,由於這種方式會形成服務器端和客戶端代碼的緊耦合。app
而且,Feign自己並不使用Spring MVC的工做機制(方法參數映射不被繼承)。ide
3. Feign對壓縮的支持微服務
在一些場景下,可能須要對請求或響應進行壓縮,此時可以使用啓用Feign的壓縮功能。post
其中,feign.compression.request.mime-types 用於支持的媒體類型列表,默認是 text/xml,application/xml,application/json
feign.compression.request.min-request-size用於設置請求的最小閾值,默認是2048
4. Feign的日誌
不少場景下,須要瞭解Feign處理請求的具體細節。
Feign對日誌的處理很是靈活,可爲每一個Feign客戶端指定日誌記錄策略,每一個Feign客戶端都會建立一個logger。
默認狀況下,logger的名稱是Feign接口的完整類名。須要注意的是,Feign的日誌打印只會對DEBUG級別做出響應。
咱們可爲每一個Feign客戶端配置各自的Logger.Level對象,告訴Feign記錄那些日誌。Logger.Level的值有如下選擇。
> 複製項目 microservice-consumer-movie-feign,將 ArtifactId 修改成 microservice-consumer-movie-feign-logging
> 建立Feign配置類
package com.itmuch.cloud.microserviceconsumermoviefeignlogging.config; import feign.Logger; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class FeignLogConfiguration { @Bean Logger.Level feignLoggerLevel() { return Logger.Level.FULL; } }
> 修改Feign的接口,指定其配置類
package com.itmuch.cloud.microserviceconsumermoviefeignlogging.feign; import com.itmuch.cloud.microserviceconsumermoviefeignlogging.config.FeignLogConfiguration; import com.itmuch.cloud.microserviceconsumermoviefeignlogging.pojo.User; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @FeignClient(name = "microservice-provider-user", configuration = FeignLogConfiguration.class) public interface UserFeignClient { @GetMapping(value = "/{id}") User findById(@PathVariable("id") Long id); }
> 在 application.yml 中添加如下內容,指定Feign接口的日誌級別爲DEBUG(由於Feign的Logger.Level只對DEBUG級別做出響應)
logging: level: com.itmuch.cloud.microserviceconsumermoviefeignlogging.feign.UserFeignClient: DEBUG # 將Feign接口的日誌級別設置爲DEBUG,由於Feign的Logger.Level只對DEBUG做出響應
> 啓動 microservice-discovery-eureka
> 啓動 microservice-provider-user
> 啓動 microservice-consumer-movie-feign-logging
> 訪問 http://localhost:8010/user/1,可在電影微服務的控制檯看見以下內容
5. 使用Feign構造多參數請求
5.1 GET請求多參數的URL
http://localhost:8001/get?id=1&username=張三
> 最直觀的方法,URL有幾個參數,Feign接口就有幾個參數
package com.itmuch.cloud.microserviceconsumermoviefeignlogging.feign; import com.itmuch.cloud.microserviceconsumermoviefeignlogging.config.FeignLogConfiguration; import com.itmuch.cloud.microserviceconsumermoviefeignlogging.pojo.User; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestParam; @FeignClient(name = "microservice-provider-user", configuration = FeignLogConfiguration.class) public interface UserFeignClient { @GetMapping(value = "/get") User findUserByCondi(@RequestParam("id") Long id, @RequestParam("username") String username); }
> 使用 Map 構建。當目標URL參數很是多時,使用Map構建可簡化Feign接口的編寫
package com.itmuch.cloud.microserviceconsumermoviefeignlogging.feign; import com.itmuch.cloud.microserviceconsumermoviefeignlogging.config.FeignLogConfiguration; import com.itmuch.cloud.microserviceconsumermoviefeignlogging.pojo.User; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestParam; import java.util.Map; @FeignClient(name = "microservice-provider-user", configuration = FeignLogConfiguration.class) public interface UserFeignClient { @GetMapping(value = "/get") User findUserByCondi(@RequestParam Map<String, Object> map); }
5.2 POST請求包含多個參數
package com.itmuch.cloud.microserviceconsumermoviefeignlogging.feign; import com.itmuch.cloud.microserviceconsumermoviefeignlogging.config.FeignLogConfiguration; import com.itmuch.cloud.microserviceconsumermoviefeignlogging.pojo.User; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.*; @FeignClient(name = "microservice-provider-user", configuration = FeignLogConfiguration.class) public interface UserFeignClient { @PostMapping(value = "/post") User findUserByCondi(@RequestBody User user); }
6. 總結
這幾章講解了Feign的相關知識。
下文將講解使用Hystrix實現微服務的容錯處理。敬請期待~~~
7. 參考
周立 --- 《Spring Cloud與Docker微服務架構與實戰》