springcloud之Feign的基本使用
一、使用Feign
Feign 提供宣告式REST呼叫。一開始我們還在懊惱,dubbo都已經提供了配置式的服務呼叫了,為什麼SpringCloud沒有相應的解決方案?有SpringCloud提供了Feign宣告式事務,使用Feign有個非常大的好處就是,只要你屬性SpringMVC的annotation配置,Feign就沒有什麼學習成本。
事不宜遲,我們首先將Feign整合到我們的微服務當中:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId></dependency>
之前的筆記中,使用者服務系統提供了二個介面,第一個介面是查詢使用者、第二個是新增使用者。以下我們就在商品服務中通過Feign整合兩個API
首先我們需要在配置類當中打上@EnableFeignClients
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class ProductsystemApplication {
然後我們建立一個UserFeignClient介面:
@FeignClient(name = "tony-mall-provider-user") public interface介面上面使用了@FeignClient annotation 標記目標的微服務,然後建立相關的方法宣告。UserFeignClient { @GetMapping("/user/getUserById/{id}") User getUserById(@PathVariable("id") int id); @PutMapping("/user/add") User addUser(@RequestParam("username") String username, @RequestParam("password")String password,@RequestParam("balance") long balance); }
或許你已經注意到,所有的操作跟我們SpringMVC中的annotation一模一樣。不過需要注意的是,方法的引數預設是使用@RequestBody進行交換的,所以如果我們希望使用普通的引數傳參,還需要打上SpringMVC的@RequestParam annotation。
之後在我們呼叫的service層或者是controller層 直接注入UserFeignClient。由於演示我就不劃分業務層和服務層了。
@RestController public class ProductController { @Autowired private ProductRepository productRepository;@Autowired private UserFeignClient userFeignClient; @GetMapping("/getProductByFeign/{productId}/userId/{userId}") public Map<String, Object> getProductByFeign(@PathVariable int productId, @PathVariable int userId) { Map<String, Object> map = new HashMap<>(); User user = this.userFeignClient.getUserById(userId); Product product = this.productRepository.getByProductId(productId); map.put("user", user); map.put("product", product); return map; } }事實上Feign提供了自己的annotation配置,但是如果習慣使用SpringMVC的你,一定會和筆者一樣選擇如上的配置。
二、Feign壓縮
feign.compression.request.mime-types=text/xml,application/xml,application/json feign.compression.request.min-request-size=2024mime-types 指定壓縮的型別,min-reqest-size指定壓縮的閾值。
三、Feign日誌
在配置類中配置Feign的日誌級別:
@Bean public Logger.Level feignLoggerLevel(){ return Logger.Level.FULL; }
使用application.properties指定某個介面的日誌級別:
logging.level.com.tony.mall.rest.UserFeignClient=DEBUG
輸出的Feign請求日誌如下:
2017-07-14 15:58:34.044 DEBUG 12184 --- [nio-8802-exec-4] com.tony.mall.rest.UserFeignClient : [UserFeignClient#getUserById] ---> GET http://tony-mall-provider-user/user/getUserById/1 HTTP/1.1
2017-07-14 15:58:34.047 DEBUG 12184 --- [nio-8802-exec-4] com.tony.mall.rest.UserFeignClient : [UserFeignClient#getUserById] ---> END HTTP (0-byte body)
2017-07-14 15:58:34.173 DEBUG 12184 --- [nio-8802-exec-4] com.tony.mall.rest.UserFeignClient : [UserFeignClient#getUserById] <--- HTTP/1.1 200 (125ms)
2017-07-14 15:58:34.174 DEBUG 12184 --- [nio-8802-exec-4] com.tony.mall.rest.UserFeignClient : [UserFeignClient#getUserById] content-type: application/json;charset=UTF-8
2017-07-14 15:58:34.174 DEBUG 12184 --- [nio-8802-exec-4] com.tony.mall.rest.UserFeignClient : [UserFeignClient#getUserById] date: Fri, 14 Jul 2017 07:58:34 GMT
2017-07-14 15:58:34.174 DEBUG 12184 --- [nio-8802-exec-4] com.tony.mall.rest.UserFeignClient : [UserFeignClient#getUserById] transfer-encoding: chunked
2017-07-14 15:58:34.174 DEBUG 12184 --- [nio-8802-exec-4] com.tony.mall.rest.UserFeignClient : [UserFeignClient#getUserById] x-application-context: tony-mall-provider-user:8803
2017-07-14 15:58:34.174 DEBUG 12184 --- [nio-8802-exec-4] com.tony.mall.rest.UserFeignClient : [UserFeignClient#getUserById]
2017-07-14 15:58:34.176 DEBUG 12184 --- [nio-8802-exec-4] com.tony.mall.rest.UserFeignClient : [UserFeignClient#getUserById] {"userId":1,"username":"TONY","userpwd":"TONYPWD","balance":1000000}
2017-07-14 15:58:34.176 DEBUG 12184 --- [nio-8802-exec-4] com.tony.mall.rest.UserFeignClient : [UserFeignClient#getUserById] <--- END HTTP (68-byte body)