1前沿
使用Feign调用接口分两层,ribbon的调用和hystrix的调用,所以ribbon的超时时间和Hystrix的超时时间的结合就是Feign的超时时间
1.1ribbon配置
ribbon:
OkToRetryOnAllOperations: false #对所有操作请求都进行重试,默认false
ReadTimeout: 3000 #负载均衡超时时间,默认值5000
ConnectTimeout: 2000 #ribbon请求连接的超时时间,默认值2000
MaxAutoRetries: 0 #对当前实例的重试次数,默认0
MaxAutoRetriesNextServer: 0 #对切换实例的重试次数,默认1
1.2 hystrix熔断配置
hystrix:
command:
default: #default全局有效,service id指定应用有效
execution:
timeout:
#是否开启超时熔断
enabled: true
isolation:
thread:
timeoutInMilliseconds: 4000 #断路器超时时间,默认1000ms
feign:
hystrix:
enabled: true
2测试各个配置的效果
开了一个Eureka服务中心
开了两个个服务eureka-client,端口分别为8762和8763,进行负载均衡
eureka-client的方法:
package com.example.cloud_client;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
/**
* @author 庄勇
* @date 2018/12/11 16:27
*/
@RestController
public class TestController {
@Value("${server.port}")
String port;
@Value("${spring.application.name}")
String serviceName;
@RequestMapping(value="/test", method = RequestMethod.GET)
public String index(@RequestParam("millis") int millis) throws InterruptedException {
System.out.println("8762---"+System.currentTimeMillis());
Thread.sleep(millis);
return "serviceName=" + serviceName + "-------port=" + port;
}
}
feignClient
eureka-feign调用client的方法,通过传参数millis来控制client线程休眠的时间
controller
package com.example.eurekafeignclient;
import com.example.eurekafeignclient.inter.FeignInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import