标签:body port struct 服务发现 fail 配置 i++ 建议 end
经过上面两章的优化,超时报错有所减少,但是只是得到了缓解但是当流量切换时还是会有大量超时。
这里又增加了一个启动后预热,即在程序启动后执行测试用例n次,让hystrix、web容器线程池等资源初始化。在测试用例执行完成之前,为了保证服务不对外提供服务,这里可以分两种。
如果时使用注册中心来进行服务发现等,这里可以采用延迟注册来保证测试用例的成功执行, 如果时eureka为注册中心可以配置initial-instance-info-replication-interval-seconds参数,默认是40s,可以将其改为1分钟
如果是consul或者zk,可以修改响应的延时注册时间,或者修改服务端有效时间
这里再deploy.yml中配置如下:
spec:
  replicas: 1
  template:
    spec:
      containers:
      - args:
        livenessProbe:
          failureThreshold: 5
          httpGet:
            path: /health
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 60  //延迟时间s
这里测试用例不建议使用spring的 @PostConstruct注解,最好是等web容器启动就绪后再执行测试用例,这里给一个我的demo
@Slf4j
public class WebContextInit {
    private RestTemplate restTemplate = new RestTemplate();
    private static WebContextInit webContextInit = new WebContextInit();
    private WebContextInit(){}
    public static WebContextInit getInstance() {
        return webContextInit;
    }
    public void init() {
        if (isTestEnabled()) {
            for(int i=0;i<10;i++){
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    String url = "http://localhost:8080/srch-recommend-plt/re-sort";
                    log.warn("WebContextInit.testResort start");
                    String body = "xxxbody";
                    HttpHeaders headers = new HttpHeaders();
                    headers.add("Content-Type", "application/json");
                    HttpEntity<string> entity = new HttpEntity<>(body, headers);
                    for (int i = 0; i < appConfig.getTestCount(); i++) {
                        try {
                            restTemplate.postForObject(url, entity, String.class);
                        } catch (Exception e) {
                            log.error("WebContextInit.testDemo error" + e.getMessage());
                        }
                    }
                    log.warn("WebContextInit.testDemo  end");
                }
            });
            thread.start();
        }
        }
    }
然后在启动类中初始化:
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
        WebContextInit.getInstance().init();
    }
}
</string>
springcloud线上发布超时方案之终极杀招:预热(测试用例)
标签:body port struct 服务发现 fail 配置 i++ 建议 end
原文地址:https://www.cnblogs.com/vwvwvwgwgvervae/p/12815711.html