OpenFeign提供了日志打印功能,我們可以通過配置來調整日恙級別,從而了解Feign 中 Http請求的細節。
說白了就是對Feign接口的調用情況進行監控和輸出
日志級別
- NONE:默認的,不顯示任何日志;
- BASIC:僅記錄請求方法、URL、響應狀態碼及執行時間;
- HEADERS:除了BASIC中定義的信息之外,還有請求和響應的頭信息;
- FULL:除了HEADERS中定義的信息之外,還有請求和響應的正文及元數據。
package com.wsm.product.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/product") public class ProductController { @Value("${server.port}") String port; @RequestMapping("/{id}") public String get(@PathVariable("id") Integer id){ System.out.println("查詢商品"); return "查詢商品"+id+":"+port; } }
package com.wsm.order.feign; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @FeignClient(name = "product-service",path="/product") public interface ProductFeignService { @RequestMapping("/{id}") public String get(@PathVariable("id") Integer id); }
package com.wsm.order.config; import feign.Logger; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 全局配置: 當使用@Configuration會將配置作用所有的報務提供方 * 局部配置: 如果只針對個別服務進行配置,就不要加@Configuration * */ @Configuration public class FeignConfig { @Bean public Logger.Level feignLoggerLevel(){ return Logger.Level.FULL; } }
package com.wsm.order.controller; import com.wsm.order.feign.ProductFeignService; import com.wsm.order.feign.StockFeignService; 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; import javax.swing.*; @RestController @RequestMapping("/order") public class OrderController { // @Autowired // RestTemplate restTemplate; @Autowired StockFeignService stockFeignService; @Autowired ProductFeignService productFeignService; @RequestMapping("/add") public String add(){ System.out.println("aaaaaaaaaaaaa"); // String msg = restTemplate.getForObject("http://localhost:8011/stock/reduct", String.class); // String msg = restTemplate.getForObject("http://stock-service/stock/reduct", String.class); String stock_msg = stockFeignService.reduct(); String product_msg = productFeignService.get(1); return "hello feign "+ stock_msg +" "+product_msg; } }
server: port: 8040 #應用名稱 (nacos 會將該名稱當作服務名稱) spring: application: name: order-openfeign-service cloud: nacos: # server-addr: 127.0.0.1:8848 server-addr: 192.168.133.128:8847 #集群 nginx 負載均衡訪問 nacos discovery: username: nacos password: nacos namespace: public #springboot 默認的日志級別是info,feign的debug日志級別就不會輸出 logging: level: com.wsm.order.feign: debug



package com.wsm.order.config; import feign.Logger; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * 全局配置: 當使用@Configuration會將配置作用所有的報務提供方 * 局部配置: 如果只針對個別服務進行配置,就不要加@Configuration * */ //@Configuration public class FeignConfig { @Bean public Logger.Level feignLoggerLevel(){ return Logger.Level.FULL; } }
package com.wsm.order.feign; import com.wsm.order.config.FeignConfig; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; /** * 添加feign接口的方法 * name 指定調用rest接口所對應的服務名 * path 指定調用rest接口所在的StockController指定的@RequestMapping */ @FeignClient(name = "stock-service",path="/stock",configuration = FeignConfig.class) public interface StockFeignService { //聲明需要調用的rest接口對應的方法 @RequestMapping("/reduct")//與 StockController 中的reduct()方法的@RequestMapping一致 public String reduct(); //與 StockController 中的reduct()方法對應 }
server: port: 8040 #應用名稱 (nacos 會將該名稱當作服務名稱) spring: application: name: order-openfeign-service cloud: nacos: # server-addr: 127.0.0.1:8848 server-addr: 192.168.133.128:8847 #集群 nginx 負載均衡訪問 nacos discovery: username: nacos password: nacos namespace: public #springboot 默認的日志級別是info,feign的debug日志級別就不會輸出 logging: level: # com.wsm.order.feign: debug com.wsm.order.feign.StockFeignService: debug



局部配置有兩種方式,一是上面的配置類,二是通過配置文件
server: port: 8040 #應用名稱 (nacos 會將該名稱當作服務名稱) spring: application: name: order-openfeign-service cloud: nacos: # server-addr: 127.0.0.1:8848 server-addr: 192.168.133.128:8847 #集群 nginx 負載均衡訪問 nacos discovery: username: nacos password: nacos namespace: public #springboot 默認的日志級別是info,feign的debug日志級別就不會輸出 logging: level: # com.wsm.order.feign: debug com.wsm.order.feign.StockFeignService: debug # Feign 日志局部配置 feign: client: config: product-service: loggerLevel: BASIC


