超时调优图解
![f19fec64b44b9c39f2a8bd9770128238.png](https://i-blog.csdnimg.cn/blog_migrate/37281ea4bf2e1162b98c922314a243b7.jpeg)
异常来源:在被调用product-provider服务中的ProductServiceImpl类,人为在findAll方法中增加线程睡眠2秒
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
当出现访问页面出现 Hystrix Read time out 如下:
![9a798711347da5ceebe8a994b6a71a57.png](https://i-blog.csdnimg.cn/blog_migrate/9f85ac0f617bbe075ee167f276e96c34.jpeg)
解决方法:
网关项目服务:
(1)版本:
SpringBoot版本:<version>2.1.11.RELEASE</version>
SpringCloud版本:<spring-cloud.version>Greenwich.SR4</spring-cloud.version>
(2)pom文件坐标:
(spring-boot-web、eureka-client、zuul)
(3)启动类:
@SpringBootApplication
@EnableZuulProxy
(4)全局配置文件:
spring.application.name=zuul-gateway-timeout
server.port=9020
#设置服务注册中心地址,指向另一个注册中心
eureka.client.service-url.defaultZone=http://admin:123456@eureka1:8761/eureka/,http://admin:123456@eureka2:8761/eureka/
#第一层 hystrix 超时时间设置
#默认情况下是线程池隔离,超时时间 1000ms
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=8000
#第二层 ribbon 超时时间设置:设置比第一层小
# 请求连接的超时时间: 默认 5s
ribbon.ConnectTimeout=5000
# 请求处理的超时时间: 默认 5s
ribbon.ReadTimeout=5000
(5)再次启动网关服务,最终调用product-provider服务数据正常显示。
但是,浏览器反应时长大概为2秒,原因是product-provider,加上2秒睡眠时长。
![2073e02c4defe3e9b9895be3c4d7de89.png](https://i-blog.csdnimg.cn/blog_migrate/0070db094735f2195dbdc94f855935d3.jpeg)