开启日志支持
@Bean public Logger.Level feignLoggerLevel() { return Logger.Level.FULL; }
源码:
package feign;... public enum Level { /** * No logging. */ NONE, /** * Log only the request method and URL and the response status code and execution time. */ BASIC, /** * Log the basic information along with request and response headers. */ HEADERS, /** * Log the headers, body, and metadata for both requests and responses. */ FULL }...
使用http client发起请求,替换原生的connection
加入dependency,自动注入使用httpclient
io.github.openfeign feign-httpclient 9.5.0 org.apache.httpcomponents httpclient 4.5.3
查看源码 spring-cloud-netflix-core-1.3.1.RELEASE.jar,可以明显看到如下代码,当存在ApacheHttpClient.class...
org.springframework.cloud.netflix.feign.ribbon;,,,public class FeignRibbonClientAutoConfiguration{ ... @Configuration @ConditionalOnClass(ApacheHttpClient.class) @ConditionalOnProperty(value = "feign.httpclient.enabled", matchIfMissing = true) protected static class HttpClientFeignLoadBalancedConfiguration { @Autowired(required = false) private HttpClient httpClient; @Bean @ConditionalOnMissingBean(Client.class) public Client feignClient(CachingSpringLoadBalancerFactory cachingFactory, SpringClientFactory clientFactory) { ApacheHttpClient delegate; if (this.httpClient != null) { delegate = new ApacheHttpClient(this.httpClient); } else { delegate = new ApacheHttpClient(); } return new LoadBalancerFeignClient(delegate, cachingFactory, clientFactory); } },,,
feign 执行逻辑
1 将FeignClient注解的interface 交由feign.ReflectiveFeign创建代理feign.ReflectiveFeign.FeignInvocationHandler.
2 method代理feign.InvocationHandlerFactory.MethodHandler.默认是SynchronousMethodHandler实现类,执行method时,调用executeAndDecode第一行,顺序调用拦截器.
... Object executeAndDecode(RequestTemplate template) throws Throwable { Request request = targetRequest(template);... Request targetRequest(RequestTemplate template) { for (RequestInterceptor interceptor : requestInterceptors) { interceptor.apply(template); } return target.apply(new RequestTemplate(template)); }...
3 然后交由 client执行http请求,client有不同实现,具体可看上面FeignRibbonClientAutoConfiguration
FeignClient注解
-
value,name: 二选一,都是一个东西.可以注入
-
serviceId: 过期了.
-
url: 不用多说,可以注入(注:一个cloud集群可以不用url,会自动根据name去找集群中服务发起请求)
-
configuration: 只对当前fegin生效的配置,而不是全局配置.(注:该类不要被扫描注入了)
-
fallback: 失败回调
-
path: 该path会作为method上path的前缀
其他参数略
例:
@Slf4jpublic class UserFeignConfiguration { @Value("${spring.application.name:}") private String applicationName; @Bean public RequestInterceptor userInterceptor() { log.info("--------定义RequestInterceptor"); RequestInterceptor requestInterceptor = new RequestInterceptor() { @Override public void apply(RequestTemplate template) { template.header("sysCode", applicationName); } }; return requestInterceptor; }}@FeignClient(value = "token", url = "${nirvana.user.url}/v2", configuration = UserFeignConfiguration.class,fallback = TokenRemoteService.TokenRemoteServiceFallback.class)public interface TokenRemoteService { @PostMapping(value = "/queryToken") ResultqueryToken(TokenBean tokenBean); @Component @Slf4j class TokenRemoteServiceFallback implements TokenRemoteService{ @Override public Result queryToken(TokenBean tokenBean) { log.info("TokenRemoteServiceFallback"); return null; } }}