在spring cloud的项目中,使用Fegin做多个服务之间的调用,是很常见的事情,但是调用失败后虽然能进入熔断器中,但是具体的失败原因,或者日志,如果能看到,那么对开发调试,将会有很大的帮助。
步骤
1、配置FeignConfig
@Configuration
public class FeignConfig {
/**
* 配置请求重试
*
*/
@Bean
public Retryer feignRetryer() {
return new Retryer.Default(200, SECONDS.toMillis(2), 10);
}
/**
* 设置请求超时时间
*默认
* public Options() {
* this(10 * 1000, 60 * 1000);
* }
*
*/
@Bean
Request.Options feignOptions() {
return new Request.Options(60 * 1000, 60 * 1000);
}
/**
* 打印请求日志
* @return
*/
@Bean
public feign.Logger.Level multipartLoggerLevel() {
return feign.Logger.Level.FULL;
}
}
2、配置 FeignClient
@FeignClient(value = "test-service", fallback = TestServiceHystrix.class, configuration = FeignConfig.class)
public interface TestServiceClient {
@PostMapping( value = "/upload",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
UpLoadResult upload(@RequestParam("upload") MultipartFile file);
}
3、请求接口,查看控制台日志
2017-10-09 19:31:01.438 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2]
c.x.test.client.testServiceClient :
[testServiceClient#upload] ---> POST http://test-service/upload?
upload=org.springframework.mock.web.MockMultipartFile%4021d2b412 HTTP/1.1
2017-10-09 19:31:01.438 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient :
[testServiceClient#upload] Accept: application/json;charset=UTF-8
2017-10-09 19:31:01.439 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient :
[testServiceClient#upload] Content-Type: multipart/form-data
2017-10-09 19:31:01.439 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient :
[testServiceClient#upload] ---> END HTTP (0-byte body)
[test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@45a92394: startup date [Mon Oct 09 19:31:01 CST 2017]; parent: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@64b3b1ce
2017-10-09 19:31:01.533 INFO
[test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2017-10-09 19:31:01.776 INFO
[test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.netflix.config.ChainedDynamicProperty : Flipping property: test-test-service.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2017-10-09 19:31:01.819 INFO [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.n.u.concurrent.ShutdownEnabledTimer : Shutdown hook installed for: NFLoadBalancer-PingTimer-test-test-service
2017-10-09 19:31:01.824 INFO
[test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.netflix.loadbalancer.BaseLoadBalancer : Client: test-test-service instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=test-test-service,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2017-10-09 19:31:01.830 INFO [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.n.l.DynamicServerListLoadBalancer : Using serverListUpdater PollingServerListUpdater
2017-10-09 19:31:01.851 INFO [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.netflix.config.ChainedDynamicProperty : Flipping property: test-test-service.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2017-10-09 19:31:01.852 INFO [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.n.l.DynamicServerListLoadBalancer :
DynamicServerListLoadBalancer for client test-test-service initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=test-test-service,current list of Servers=[10.10.0.176:8771],Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone; Instance count:1; Active connections count: 0; Circuit breaker tripped count: 0; Active connections per server: 0.0;]
},Server stats: [[Server:10.20.0.176:8700; Zone:defaultZone;
Total Requests:0; Successive connection failure:0; Total blackout seconds:0; Last connection made:Thu Jan 01 08:00:00 CST 1970; First connection made: Thu Jan 01 08:00:00 CST 1970; Active Connections:0; total failure count in last (1000) msecs:0; average resp time:0.0; 90 percentile resp time:0.0; 95 percentile resp time:0.0; min resp time:0.0; max resp time:0.0; stddev resp time:0.0]
]}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@15044fde
2017-10-09 19:31:02.047 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload] <--- HTTP/1.1 500 (607ms)
2017-10-09 19:31:02.047 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload] cache-control: no-cache, no-store, max-age=0, must-revalidate
2017-10-09 19:31:02.047 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload] connection: close
2017-10-09 19:31:02.047 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload] content-type: application/json;charset=UTF-8
2017-10-09 19:31:02.047 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload] date: Mon, 09 Oct 2017 11:31:07 GMT
2017-10-09 19:31:02.047 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload] expires: 0
2017-10-09 19:31:02.048 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload] pragma: no-cache
2017-10-09 19:31:02.048 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload] transfer-encoding: chunked
2017-10-09 19:31:02.048 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload] x-application-context: test-test-service:dev:8771
2017-10-09 19:31:02.048 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload] x-content-type-options: nosniff
2017-10-09 19:31:02.048 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload] x-frame-options: DENY
2017-10-09 19:31:02.048 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload] x-xss-protection: 1; mode=block
2017-10-09 19:31:02.048 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload]
2017-10-09 19:31:02.049 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload]
{
"code" : 500,
"error" : "Internal Server Error",
"data" : null
}
2017-10-09 19:31:02.049 DEBUG [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.test.client.testServiceClient : [testServiceClient#upload] <--- END HTTP (72-byte body)
2017-10-09 19:31:02.061 INFO [test-sys-test-service,e2818797113202e3,e2818797113202e3,true] 12972 --- [nio-8773-exec-2] c.x.t.c.hystrix.PlUserServiceHystrix : ---upload hystrix---
从我的日志中可以看到,这次请求失败了,并且能看到发出的请求url,方法类型,返回数据,负载均衡信息,请求的具体的集群ip等,这对我们调试有很大的帮助
关键设置
@Bean
public feign.Logger.Level multipartLoggerLevel() {
return feign.Logger.Level.FULL;
}
//-----------------------------------
configuration = FeignConfig.class
我的官网http://guan2ye.com
我的CSDN地址http://blog.csdn.net/chenjianandiyi
我的简书地址http://www.jianshu.com/u/9b5d1921ce34
我的githubhttps://github.com/javanan
我的码云地址https://gitee.com/jamen/
时间: 2024-10-09 13:58:04