springboot-配置心跳

@Slf4j
@EnableScheduling
@Component
public class FyServeHeartConfig {

    @Autowired
    private RedisUtil redisUtil;

    @Scheduled(fixedRate = 5000)
    public void myTask() {
        log.debug("开始心跳");
        int expireCount = this.redisUtil.hasKey(FyConstant.FY_SERVE_EXPIRE) ? this.redisUtil.get(FyConstant.FY_SERVE_EXPIRE) : 0;
        HttpResponse httpResponse = new HttpGetUtil(BaseApi.BASE_PAY_URL.getBaseUrl() + "/heartbeat").buildGet().execute();
        if (httpResponse.getStatusLine().getStatusCode() != 200) {
            log.debug("心跳失败");
            this.redisUtil.set(FyConstant.FY_SERVE_EXPIRE, expireCount + 1);
        }

        if (httpResponse.getStatusLine().getStatusCode() != 200 && expireCount > 5) {
            log.debug("次数超过5次,切换地址");
            this.redisUtil.set(FyConstant.FY_SERVE_URL, BaseApi.BASE_PAY_URL_2.getBaseUrl());
        }

        if (httpResponse.getStatusLine().getStatusCode() == 200) {
            log.debug("心跳成功");
            //成功就删除缓存
            if (this.redisUtil.hasKey(FyConstant.FY_SERVE_EXPIRE)) {
                this.redisUtil.delete(FyConstant.FY_SERVE_EXPIRE);
            }

            //切换回主地址
            String payUrl = this.redisUtil.get(FyConstant.FY_SERVE_URL);
            if (payUrl.equals(BaseApi.BASE_PAY_URL_2.getBaseUrl())) {
                this.redisUtil.set(FyConstant.FY_SERVE_URL, BaseApi.BASE_PAY_URL.getBaseUrl());
            }

        }

    }

}

 

posted @ 2023-10-10 10:00  fnasklf  阅读(236)  评论(0编辑  收藏  举报