11.SpringCloud常见问题总结
11.1:Eureka 常见问题
11.1.1:Eureka注册服务慢
#心跳检测间隔
解决方案:eureka.instance.leaseRenewalIntervalInSec-onds=int(默认是30,可以设置较短时间提高注册效率,生产环境建议默认)
11.1.2:已停止的微服务节点注销慢或不注销
Server端:
#关闭自我保护,当服务关闭的时候会立刻注销
eureka.server.enable-self-preservation=false
#清理间隔(单位是ms毫秒,默认是60*1000)清理无效节点
eureka.server.eviction-interval-timer-in-ms=60*1000
Client端:
#开启健康检查,并按需配置续约更新时间和到期时间
Eureka.client.healthcheck.enabled=true (设置为true 开启健康检查)
#续约更新时间间隔
eureka.instance.lease-renewal-interval-in-seconds=30 (默认30秒)
#续约到期时间
eureka.instance.lease-expiration-duration-in-seconds=90(默认90秒)
11.1.3:如何自定义微服务的Instance ID
eureka.instance.instance-id=appname (一般采用ip:port)
11.1.4:Eureka 的 UNKNOEN 问题总结与解决
一般有两种情况:1 应用名称UNKNOEN 。2 应用状态UNKNOEN
11.1.4.1:spring.application.name 没有配置
或者 spring.application.name 与 eureka.instance.appname 都没有配置
11.1.4.2 微服务只会请求状态会UP的服务端。该问题一般由健康检查导致。Eureka.client.healthcheck.enabled=true 必须配置在application.yml 不能配置在Bootstrup.yml
11.2整合hystrix后首次请求失败
在某些场景下,整合了ZUUL 或 Feign 等组件后,会出现首次调用失败的问题。
11.2.1 原因分析
Hystrix 默认超时时间是 1s ,spring 懒加载问题 首次请求往往会超过1s
11.2.2 解决方案
*方法一:为Ribbon 配置饿加载
ribbon.eager-load.enabled=true,ribbon.eager-load.clients=client1,client2
为Zuul配置饿加载
zuul.ribbon.eager-load.enabled=true
*方法二:延长Hystrix超时时间
Hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000 (改配置设置断路器超时时间为5S)
*方法三:禁用Hystrix超时
Hystrix.command.default.execution.timeout.enabled=false
*方法四:禁用feing Hystrix超时
feign.hystrix.enabled=false (全局禁用,一般不建议使用)
11.3 Tubine 聚合的数据不完整(ZIPKIN也可能出现这种情况)
原因:当Tubine 聚合的多个微服务在同一台主机上会出现的问题
解决方案:
方法一:为每个微服务设置不同的HostName 并将preferIpAddress设置为false 或者不设置 eureka.instance.hostname=ribbon
方法二:设置turbine.combine-host-port=true
方法三:升级SpringCloud 到Camden 或者更高版本
11.4 SpringCloud 各组件超时
11.4.1 RestTemplate 配置超时时间
@LoadBlance
@Bean
public RestTemplate setRestTemplate () {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();//60s
requestFactory.setConnectTimeout(60*1000);
requestFactory.setReadTimeout(60*1000);
return new RestTemplate(requestFactory);
}
11.4.2 Ribbon 的超时
全局设置:
ribbon.readTimeout=1000 请求超时
ribbon.connectTimeout=1000 连接超时
局部设置:
Service-id.ribbon.readTimeout=1000
Service-id.ribbon.connectTimeout=1000
11.4.3 feign超时
从SpringCloud Edgware 开始可以使用属性配置超时(单位ms毫秒)
Feign.client.config.feignName.connectTimeout=1000
Feign.client.config.feignName.readTimeout=1000
如果需要全局配置 则把feignName 改成 default
11.4.4 hystrix超时
#默认情况下开启超时事件
Hystrix.command.<HystrixCommandKey>.execution.timeout.enabled=true
#<HystrixCommandKey> 改为default 为全局配置
Hystrix.command.<HystrixCommandKey>.execution.isolation.thread.timeOutInMilliseconds=1000
11.4.5 Zuul 超时
使用Ribbon hystrix 时候,见上面Ribbon hystrix 的配置;(转发路径使用service-id)
未使用 Ribbon hystrix 使用 (转发路径使用http://ip:port/)
zuul.host.socket-timeout-millis=1000
connect-timeout-millis=1000
11.5 SpringCloud各组件重试(in SringCloud Edgware easy config)
POM依赖:<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
具体配置自行百度
11.6 SpringCloud 各组件调优 (不涉及JVM参数)
11.6.1 Tomcat 调优参数
Server:
Tomcat:
Max-connections:<最大连接数>
Max-threads:<最大线程数>
Min-spare-threads:<最小空闲线程数>
Accept-count:<当tomcat启动的线程达到最大值时,接收排队的请求个数>
11.6.2 hystrix 参数
Hystrix.threadpool.<hystrixCommandKey>.coreSize=10
Hystrix.threadpool.<hystrixCommandKey>.maximumSize=10
Hystrix.threadpool.<hystrixCommandKey>.maxQueueSize=-1 (当值为-1的时候 使用的队列是 同步队列 set 进一个成员后 必须被get 走 才能继续set ,否则使用的是LinkedBlockQueue)
注意:若将hystrixCommandKey 改成default则是全局配置
若隔离策略为信号量 SEMAPHORE
Hystrix.command.default.execution.isolation.smephore.maxCurrentRequests:10 默认值表示信号量为10个(同时只能有最多10个人访问)
11.6.3 feign 参数
Feign默认的是使用JDK原生的URLConnecton 发送HTTP请求,没有连接池。
在实际项目中我们希望使用连接池来管理HTTP连接。要想要使用连接池,需要使用ApacheHttpClient 或者 okHttp
Feign:
httpClient(或okhttp):
Enabled:true
Max-connections: 200 默认值
Max-connections-per-route: 50 默认值
11.6.4 Zuul参数
Zuul 的Hystrix 默认隔离策略是 semaphore
设置默认最大信号量
Zuul:
Semaphore:
Max-semaphore: 100 默认值
设置指定服务的最大信号量
Zuul:
Eureka:
<commandKey>:
Max-semaphore: 100 默认值
当隔离策略为Thread时(zuul.ribbon-isolation-strategy=thread),首先,在Edgware版本 及更高版本中,Zuul支持微服务设置独立的Hystrix 线程池,除此之外 还可以使用如下配置进行调优:
zuul:
Routes:
User-route:
Path: /user/**
可使用一下参数进行并发配置
Zuul:
Host:
Max-total-connection: 200 默认值
Nax-per-route-connections: 200默认值
关闭过滤器:
如果你的服务中不包含Form表单请求,完全可以关闭 FormBodyWrapperFIlter过滤器 ,从而获得更好的性能表现.