fix 修改负载中心
parent
2a418ee35b
commit
25b1e1d843
|
@ -1,5 +1,6 @@
|
||||||
<component name="InspectionProjectProfileManager">
|
<component name="InspectionProjectProfileManager">
|
||||||
<profile version="1.0">
|
<profile version="1.0">
|
||||||
<option name="myName" value="Project Default" />
|
<option name="myName" value="Project Default" />
|
||||||
|
<inspection_tool class="AliAccessStaticViaInstance" enabled="true" level="WARNING" enabled_by_default="true" />
|
||||||
</profile>
|
</profile>
|
||||||
</component>
|
</component>
|
3144
logs/vehicle.log
3144
logs/vehicle.log
File diff suppressed because it is too large
Load Diff
Binary file not shown.
|
@ -10,7 +10,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
* @description 车辆模拟启动类
|
* @description 车辆模拟启动类
|
||||||
* @date 2023/11/9
|
* @date 2023/11/9
|
||||||
*/
|
*/
|
||||||
@SpringBootApplication
|
@SpringBootApplication(scanBasePackages = "com.muyu")
|
||||||
@MapperScan(value = "com.muyu.mapper")
|
@MapperScan(value = "com.muyu.mapper")
|
||||||
public class VehicleSimulationApplication {
|
public class VehicleSimulationApplication {
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,176 @@
|
||||||
|
package com.muyu.config;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONArray;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.aliyun.ecs20140526.models.DescribeInstancesResponse;
|
||||||
|
import com.aliyun.tea.TeaException;
|
||||||
|
import com.aliyun.tea.TeaModel;
|
||||||
|
import com.aliyun.teautil.Common;
|
||||||
|
import com.muyu.ApifoxModel;
|
||||||
|
import com.muyu.NodeInfo;
|
||||||
|
import com.muyu.Sample;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.ApplicationArguments;
|
||||||
|
import org.springframework.boot.ApplicationRunner;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.math.RoundingMode;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.DoubleStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 初始化权重值 InitializeWeightConfig
|
||||||
|
*
|
||||||
|
* @author Yangle
|
||||||
|
* Date 2024/5/28 9:32
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class InitializeWeightConfig implements ApplicationRunner {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RestTemplate restTemplate;
|
||||||
|
|
||||||
|
|
||||||
|
public static com.aliyun.ecs20140526.Client createClient() throws Exception {
|
||||||
|
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
||||||
|
// 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html。
|
||||||
|
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
||||||
|
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||||||
|
.setAccessKeyId("LTAI5tDneNu1AnsDijLV5Q4V")
|
||||||
|
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||||
|
.setAccessKeySecret("gUnaWRZocYftNfm5YAIadE72gJsapR");
|
||||||
|
// Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
||||||
|
config.endpoint = "ecs.cn-shanghai.aliyuncs.com";
|
||||||
|
return new com.aliyun.ecs20140526.Client(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String,Double> hashMap = new HashMap<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(ApplicationArguments args) throws Exception {
|
||||||
|
List<String> ips=new ArrayList<>();
|
||||||
|
|
||||||
|
com.aliyun.ecs20140526.Client client = Sample.createClient();
|
||||||
|
com.aliyun.ecs20140526.models.DescribeInstancesRequest describeInstancesRequest = new com.aliyun.ecs20140526.models.DescribeInstancesRequest()
|
||||||
|
.setRegionId("cn-shanghai")
|
||||||
|
.setStatus("Running");
|
||||||
|
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||||
|
try {
|
||||||
|
// 复制代码运行请自行打印 API 的返回值
|
||||||
|
DescribeInstancesResponse response = client.describeInstancesWithOptions(describeInstancesRequest, runtime);
|
||||||
|
String jsonString = Common.toJSONString(TeaModel.buildMap(response));
|
||||||
|
System.out.println("hhh" + jsonString);
|
||||||
|
JSONObject jsonObjec = JSONObject.parseObject(jsonString);
|
||||||
|
JSONObject body = jsonObjec.getJSONObject("body");
|
||||||
|
JSONObject instanceStatuses = body.getJSONObject("Instances");
|
||||||
|
JSONArray instanceStatus = instanceStatuses.getJSONArray("Instance");
|
||||||
|
|
||||||
|
for (int i = 0; i < instanceStatus.size(); i++) {
|
||||||
|
JSONObject instanceStatus1 = instanceStatus.getJSONObject(i);
|
||||||
|
JSONObject publicIpAddress = instanceStatus1.getJSONObject("PublicIpAddress");
|
||||||
|
JSONArray ipAddress = publicIpAddress.getJSONArray("IpAddress");
|
||||||
|
for (int i1 = 0; i1 < ipAddress.size(); i1++) {
|
||||||
|
ips.add(ipAddress.getString(i1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch(TeaException error){
|
||||||
|
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||||
|
// 错误 message
|
||||||
|
System.out.println(error.getMessage());
|
||||||
|
// 诊断地址
|
||||||
|
System.out.println(error.getData().get("Recommend"));
|
||||||
|
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||||
|
} catch(Exception _error){
|
||||||
|
TeaException error = new TeaException(_error.getMessage(), _error);
|
||||||
|
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||||
|
// 错误 message
|
||||||
|
System.out.println(error.getMessage());
|
||||||
|
// 诊断地址
|
||||||
|
System.out.println(error.getData().get("Recommend"));
|
||||||
|
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
List<String> strings = new ArrayList<>();
|
||||||
|
ApifoxModel apifoxModel = new ApifoxModel();
|
||||||
|
apifoxModel.setPassword("fluxmq");
|
||||||
|
apifoxModel.setUsername("fluxmq");
|
||||||
|
for (String s : ips) {
|
||||||
|
String token2 = restTemplate.postForObject("http://" + s + ":8080/public/login", apifoxModel, String.class);
|
||||||
|
System.out.println("ss"+token2);
|
||||||
|
JSONObject token= JSONObject.parseObject(token2);
|
||||||
|
String accessToken = token.getString("accessToken");
|
||||||
|
System.out.println("Access Token: " + accessToken);
|
||||||
|
strings.add(token2);
|
||||||
|
String restTemplateForObject = restTemplate.getForObject("http://" + s + ":8080/public/cluster?accesstoken=" + accessToken, String.class);
|
||||||
|
|
||||||
|
JSONArray jsonArray = JSONArray.parseArray(restTemplateForObject);
|
||||||
|
|
||||||
|
// 获取JSONArray中的第一个JSONObject(假设你只对第一个对象感兴趣)
|
||||||
|
JSONObject jsonObject = jsonArray.getJSONObject(0);
|
||||||
|
|
||||||
|
// 从JSONObject中获取cpuInfo对象
|
||||||
|
JSONObject cpuInfo = jsonObject.getJSONObject("cpuInfo");
|
||||||
|
|
||||||
|
// 从cpuInfo对象中获取cpuNum的值
|
||||||
|
int cpuNum = cpuInfo.getIntValue("cpuNum");
|
||||||
|
|
||||||
|
// 打印结果
|
||||||
|
System.out.println("CPU数量: " + cpuNum);
|
||||||
|
}
|
||||||
|
// hashMap.put(s, );
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void ff() {
|
||||||
|
|
||||||
|
// 将map转成list
|
||||||
|
List<NodeInfo> nodeInfoList = hashMap.entrySet()
|
||||||
|
.stream().map(NodeInfo::new)
|
||||||
|
.toList();
|
||||||
|
System.out.println(nodeInfoList);
|
||||||
|
//计算出权重的和
|
||||||
|
double sum = nodeInfoList.stream().flatMapToDouble(nodeInfo -> DoubleStream.of(nodeInfo.getWeight()))
|
||||||
|
.sum();
|
||||||
|
System.out.println(sum);
|
||||||
|
|
||||||
|
BigDecimal spec = new BigDecimal(100).divide(new BigDecimal(sum), 2, BigDecimal.ROUND_HALF_UP);
|
||||||
|
System.out.println("spec" + spec);
|
||||||
|
java.util.Map<String, Integer> collect = nodeInfoList.stream()
|
||||||
|
.collect(Collectors.toMap(NodeInfo::getId,
|
||||||
|
nodeInfo -> spec.multiply(BigDecimal.valueOf(nodeInfo.getWeight())).setScale(0, RoundingMode.DOWN).intValue(),
|
||||||
|
(o1, o2) -> o1));
|
||||||
|
System.out.println(collect);
|
||||||
|
int reduce = collect.values().stream().reduce(0, Integer::sum);
|
||||||
|
System.out.println(reduce);
|
||||||
|
int free = 100 - reduce;
|
||||||
|
nodeInfoList.stream()
|
||||||
|
.sorted(Comparator.comparingInt(o -> o.getWeight().intValue()))
|
||||||
|
.limit(free)
|
||||||
|
.forEach(nodeInfo -> {
|
||||||
|
collect.put(nodeInfo.getId(), collect.get(nodeInfo.getId()) + 1);
|
||||||
|
});
|
||||||
|
System.out.println(collect);
|
||||||
|
String[] nodeArray = new String[100];
|
||||||
|
int intArrCursor = -1;
|
||||||
|
while (!collect.isEmpty()) {
|
||||||
|
Iterator<Map.Entry<String, Integer>> specMapIterator = collect.entrySet().iterator();
|
||||||
|
while (specMapIterator.hasNext()) {
|
||||||
|
Map.Entry<String, Integer> entry = specMapIterator.next();
|
||||||
|
Integer value = entry.getValue();
|
||||||
|
if (value > 0) {
|
||||||
|
nodeArray[++intArrCursor] = entry.getKey();
|
||||||
|
entry.setValue(value - 1);
|
||||||
|
}
|
||||||
|
if (value == 0) {
|
||||||
|
specMapIterator.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println(collect);
|
||||||
|
System.out.println("lll" + Arrays.toString(nodeArray));
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,13 +9,15 @@ import com.aliyun.teautil.Common;
|
||||||
import com.muyu.ApifoxModel;
|
import com.muyu.ApifoxModel;
|
||||||
import com.muyu.NodeInfo;
|
import com.muyu.NodeInfo;
|
||||||
import com.muyu.Sample;
|
import com.muyu.Sample;
|
||||||
|
import com.muyu.config.RedisConfig;
|
||||||
import lombok.extern.log4j.Log4j2;
|
import lombok.extern.log4j.Log4j2;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.autoconfigure.cache.CacheProperties;
|
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
@ -48,36 +50,30 @@ public class FluxMqController {
|
||||||
}
|
}
|
||||||
return strings;
|
return strings;
|
||||||
}
|
}
|
||||||
// @Autowired
|
@Autowired
|
||||||
// private RedisTemplate<String,HashMap<String,Double>> redisTemplate;
|
private RedisTemplate<String,String> redisTemplate;
|
||||||
@GetMapping("/select")
|
@GetMapping("/select")
|
||||||
public List<Object> getToken(@RequestParam String accessToken) throws Exception {
|
public List<String> getToken(@RequestParam String accessToken) throws Exception {
|
||||||
List<String> posted = this.post();
|
List<String> posted = this.post();
|
||||||
System.out.println(posted);
|
System.out.println(posted);
|
||||||
List<Object> strings = new ArrayList<>();
|
List<String> strings = new ArrayList<>();
|
||||||
for (String s : posted) {
|
for (String s : posted) {
|
||||||
Object s1 = restTemplate.getForObject("http://" + s + ":8080/public/cluster?accesstoken="+accessToken,Object.class);
|
String s1 = restTemplate.getForObject("http://" + s + ":8080/public/cluster?accesstoken=" + accessToken, String.class);
|
||||||
System.out.println(s1);
|
System.out.println(s1);
|
||||||
strings.add(s1);
|
strings.add(s1);
|
||||||
// ff();
|
|
||||||
// JSONObject jsonObject = JSONObject.parseObject(s1.toString());
|
// JSONObject.parseObject()
|
||||||
// JSONObject cpuInfo = jsonObject.getJSONObject("cpuInfo");
|
// JSONObject cpuInfo = jsonObject.getJSONObject("cpuInfo");
|
||||||
// Double cpu = cpuInfo.getDouble("cpu");
|
// Double cpu = cpuInfo.getDouble("cpu");
|
||||||
// hashMap = new HashMap<>(){{
|
// hashMap.put(s,cpu);
|
||||||
// put(s,cpu );
|
|
||||||
// }};
|
|
||||||
// redisTemplate.opsForHash().putAll("cpu"+s,hashMap);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
// ff();
|
||||||
|
|
||||||
return strings;
|
return strings;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Map<String,Double> hashMap = new HashMap<>();
|
public static Map<String,Double> hashMap = new HashMap<>();
|
||||||
|
|
||||||
public static void ff(){
|
public static void ff(){
|
||||||
|
|
||||||
// 将map转成list
|
// 将map转成list
|
||||||
List<NodeInfo> nodeInfoList = hashMap.entrySet()
|
List<NodeInfo> nodeInfoList = hashMap.entrySet()
|
||||||
.stream().map(NodeInfo::new)
|
.stream().map(NodeInfo::new)
|
||||||
|
@ -128,18 +124,6 @@ public static void ff(){
|
||||||
|
|
||||||
public static final String postIp = null;
|
public static final String postIp = null;
|
||||||
|
|
||||||
public static com.aliyun.ecs20140526.Client createClient() throws Exception {
|
|
||||||
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
|
||||||
// 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html。
|
|
||||||
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
|
||||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
|
||||||
.setAccessKeyId("LTAI5tDneNu1AnsDijLV5Q4V")
|
|
||||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
|
||||||
.setAccessKeySecret("gUnaWRZocYftNfm5YAIadE72gJsapR");
|
|
||||||
// Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
|
||||||
config.endpoint = "ecs.cn-shanghai.aliyuncs.com";
|
|
||||||
return new com.aliyun.ecs20140526.Client(config);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static List<String> post() throws Exception {
|
public static List<String> post() throws Exception {
|
||||||
List<String> ips=new ArrayList<>();
|
List<String> ips=new ArrayList<>();
|
||||||
|
|
|
@ -24,6 +24,7 @@ import lombok.extern.log4j.Log4j2;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -92,6 +93,8 @@ public class VehicleInstanceServiceImpl implements VehicleInstanceService {
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RestTemplate restTemplate;
|
||||||
/**
|
/**
|
||||||
* 车辆客户端初始化
|
* 车辆客户端初始化
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in New Issue