LoadCenter/src/main/java/com/lyh/job/Timer.java

80 lines
2.1 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.lyh.job;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.lyh.common.aliyun.service.AliYunEcsService;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @ProjectName: LoadCenter
* @Author: LiuYunHu
* @CreateTime: 2024/4/13
* @Description:
*/
@Component
@Slf4j
public class Timer {
/*
* 阿里云api接口类
* */
@Autowired
private AliYunEcsService aliYunEcsService;
// @Scheduled(cron = "0/10 * * * * ?")
public void test() {
String ip = "47.102.123.209";
//请求路径
String URL = "http://" + ip + ":8080/public/cluster";
OkHttpClient client = new OkHttpClient();
Request req = new Request.Builder()
.url(URL)
.get()
.addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
.addHeader("Accesstoken", "")
.build();
try {
Response response = client.newCall(req).execute();
log.info(String.valueOf(response));
JSONArray jsonArray = JSONArray.parseArray(response.body().string());
JSONObject jsonObject = jsonArray.getJSONObject(0);
//获取mqttInfo对象的值
JSONObject mqttInfo = jsonObject.getJSONObject("mqttInfo");
//获取连接数
int connectSize = mqttInfo.getIntValue("connectSize");
log.info(ip + " 的fluxmq连接数为" + connectSize);
if (connectSize >= 80) {
//执行节点扩容
//返回实例的ID
String instanceId = aliYunEcsService.createAndRunInstance();
if (!instanceId.isEmpty()) {
log.info("扩容 成功!");
log.info("扩容的节点ip为" + instanceId);
}
}
} catch (Exception e) {
log.error(e.getMessage());
}
}
}