一、覆写fegin的默认配置
1、新增配置类FeignConfiguration.java
package com.config; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import feign.Contract;
import feign.Logger;
@Configuration
public class FeignConfiguration { @Bean
public Contract feignContract() {
//这里可以配置默认配置
return new feign.Contract.Default();
} @Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
需要之一的是此配置文件不能再spring cloud扫描包的路径下,否则会有问题出现
2、定义一个FeignClient2.java
package com.pupeiyuan.feignClient; import java.util.List; import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import com.config.FeignConfiguration;
import com.pupeiyuan.bean.NhReportStatusHistory; import feign.Param;
import feign.RequestLine; @FeignClient(name = "MULTIPLE",configuration = FeignConfiguration.class)
public interface FeignClient2 {
@RequestLine("GET /getDate/{id}")
public List<NhReportStatusHistory> dataList(@Param("id") Long id);
}
3、controller中调用
FeignClientController.java
package com.pupeiyuan.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.pupeiyuan.bean.NhReportStatusHistory;
import com.pupeiyuan.feignClient.FeignClient2;
import com.pupeiyuan.feignClient.UserFeignClient; @RestController
public class FeignClientController { @Autowired
private FeignClient2 feignClient2; @GetMapping("/movie2/{id}")
public List<NhReportStatusHistory> list2(@PathVariable Long id) {
return this.feignClient2.dataList(id);
}
}
实现的效果和上一篇文章是一样的
二、feign client的日志打印
默认情况下feign是没有日志打印出来的,需要增加相关配置:
1、创建Feign的配置文件,并在其中设置日志等级
/**
* Feign 客户端配置
*
* @author xushiling
* @date 2018/8/13
*/
@Configuration
public class FeignConfiguration {
@Bean
Logger.Level feignLoggerLevel() {
//这里记录所有,根据实际情况选择合适的日志level
return Logger.Level.FULL;
}
}
这里的level级别控制如下
NONE, No logging (DEFAULT).
BASIC, Log only the request method and URL and the response status code and execution time.
HEADERS, Log the basic information along with request and response headers.
FULL, Log the headers, body, and metadata for both requests and responses.
NONE, 不记录 (DEFAULT).
BASIC, 仅记录请求方式和URL及响应的状态代码与执行时间.
HEADERS, 日志的基本信息与请求及响应的头.
FULL, 记录请求与响应的头和正文及元数据.
2、在客户端接口指定此配置
package com.pupeiyuan.feignClient; import java.util.List; import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import com.config.FeignConfiguration;
import com.pupeiyuan.bean.NhReportStatusHistory; import feign.Param;
import feign.RequestLine; @FeignClient(name = "MULTIPLE",configuration = FeignConfiguration.class)
public interface FeignClient2 {
@RequestLine("GET /getDate/{id}")
public List<NhReportStatusHistory> dataList(@Param("id") Long id);
}
3、配置文件开启日志记录
application.properties设置:logging.level.com.haoait.client.UserServiceClient:debug
如果是yml配置文件则做如下配置:
logging:
level:
com.haoait.client.UserServiceClient:debug
日志输出如下: