最近刚入坑微服务,总是会碰到很多坑,一个坑一个脚印,默默记下。
Problem 1:
其中一个微服务模块,启动,本身没有问题,postman测试接口也没有问题。同时在网关中配置了相关转发,例如:
#serviceId 就是微服务的名字, 网关接收到 /ctrl/**的请求,就会转发到这个微服务
zuul:
prefix: /zuulName
routes:
signal:
path: /ctrl/**
serviceId: eureka-client-t1
strip-prefix: false
gis:
path: /gis/**
serviceId: eureka-client-t2
strip-prefix: false
但是通过网关访问就会出现问题,通过API网关路由来访问微服务,zuul默认路由规则 :http://zuul的Host地址:zuul端口/要调用的服务名/服务方法地址,报错:
com.netflix.zuul.exception.ZuulException: Forwarding error......
Caused by: com.netflix.client.ClientException: null......
Caused by: java.lang.RuntimeException: java.net.SocketTimeoutException: Read timed out
是因为接口调用的时间过长,超过了等待时长,于是配置一下时长,在网关模块中application.yml配置
hystrix:
command:
default:
execution:
isolation:
thread:
timeout-in-milliseconds: 60000
以及
zuul:
host:
connect-timeout-millis: 70000 #http连接超时要比hystrix大
socket-timeout-millis: 60000
还有
ribbon:
ReadTimeout: 50000
ConnectTimeout: 50000
进行这样的配置之后,可以通过API网关路由来访问服务了,postman接口测试正常。
Problem 2:
微服务之间通讯的时候,由于配置了熔断器,发现A服务中每次调用B的时候,都会进入fallback,由此判断调用过程出现了问题。
其实还是上面说到的时间问题,我将fallback去掉之后,在controller 中try...catch捕获到了错误,定位错误:spring cloud java.util.concurrent.TimeoutException
【此处记录下,不去掉fallback也能捕捉错误,在client中try...catch就可以】
首先我尝试了在A服务的application.yml中设置了熔断器的检测时间:(熔断器检测时间(默认1秒))
hystrix:
command:
default:
execution:
isolation:
thread:
timeout-in-milliseconds: 60000
但是并没有效果, 后来就关闭熔断器超时检测时间功能,也就是不超时
ribbon:
ReadTimeout: 50000
ConnectTimeout: 50000
hystrix:
command:
default:
execution:
timeout:
enabled: false
OK,到此问题都解决了,微服务自身运行正常,API网官访问也正常,微服务间通讯也正常
网友评论