springcloud线上发布超时方案之终极杀招:预热(测试用例)

springcloud线上发布超时系列文章:

springcloud线上发布超时之feign(ribbon饥饿加载)

springcloud线上发布超时之grpc

springcloud线上发布超时方案之终极杀招:预热(测试用例)

前言

经过上面两章的优化,超时报错有所减少,但是只是得到了缓解但是当流量切换时还是会有大量超时。

方案

这里又增加了一个启动后预热,即在程序启动后执行测试用例n次,让hystrix、web容器线程池等资源初始化。在测试用例执行完成之前,为了保证服务不对外提供服务,这里可以分两种。

延迟注册到注册中心

如果时使用注册中心来进行服务发现等,这里可以采用延迟注册来保证测试用例的成功执行, 如果时eureka为注册中心可以配置initial-instance-info-replication-interval-seconds参数,默认是40s,可以将其改为1分钟

如果是consul或者zk,可以修改响应的延时注册时间,或者修改服务端有效时间

kubernetes中增加服务ready延时时间

这里再deploy.yml中配置如下:

1spec: 2 replicas: 1 3 template: 4 spec: 5 containers: 6 - args: 7 livenessProbe: 8 failureThreshold: 5 9 httpGet: 10 path: /health 11 port: 8080 12 scheme: HTTP 13 initialDelaySeconds: 60 //延迟时间s

案例

这里测试用例不建议使用spring的 @PostConstruct注解,最好是等web容器启动就绪后再执行测试用例,这里给一个我的demo

1@Slf4j 2public class WebContextInit { 3 4 private RestTemplate restTemplate = new RestTemplate(); 5 private static WebContextInit webContextInit = new WebContextInit(); 6 private WebContextInit(){} 7 public static WebContextInit getInstance() { 8 return webContextInit; 9 } 10 11 public void init() { 12 if (isTestEnabled()) { 13 for(int i=0;i&lt;10;i++){ 14 Thread thread = new Thread(new Runnable() { 15 @Override 16 public void run() { 17 String url = "http://localhost:8080/srch-recommend-plt/re-sort"; 18 log.warn("WebContextInit.testResort start"); 19 String body = "xxxbody"; 20 HttpHeaders headers = new HttpHeaders(); 21 headers.add("Content-Type", "application/json"); 22 HttpEntity<string> entity = new HttpEntity&lt;&gt;(body, headers); 23 for (int i = 0; i &lt; appConfig.getTestCount(); i++) { 24 try { 25 restTemplate.postForObject(url, entity, String.class); 26 } catch (Exception e) { 27 log.error("WebContextInit.testDemo error" + e.getMessage()); 28 } 29 } 30 log.warn("WebContextInit.testDemo end"); 31 } 32 }); 33 thread.start(); 34 } 35 } 36 37 } 38 39 40

然后在启动类中初始化:

1public class DemoApplication { 2 3 public static void main(String[] args) { 4 SpringApplication.run(DemoApplication.class, args); 5 WebContextInit.getInstance().init(); 6 } 7 8}
</string>
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

springcloud线上发布超时之feign(ribbon饥饿加载)

springcloud线上发布超时系列文章:springcloud线上发布超时之feign(ribbon饥饿加载)(https://my.oschina.net/u/560547/blog/3275636)springcloud线上发布超时之grpc(https://my.oschina.net/u/560547/blog/3275637)

Java爬虫之JSoup使用教程

title:Java爬虫之JSoup使用教程date:201812248:00:000800update:201812248:00:000800author:mecover:https://imgblog.csdnimg.cn/20181224144920712(https://www.oschin