初始化

master
黄大举 2024-04-17 09:40:02 +08:00
commit 4c0155d1cc
25 changed files with 2472 additions and 0 deletions

46
.gitignore vendored 100644
View File

@ -0,0 +1,46 @@
######################################################################
# Build Tools
.gradle
/build/
!gradle/wrapper/gradle-wrapper.jar
target/
!.mvn/wrapper/maven-wrapper.jar
######################################################################
# IDE
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
### JRebel ###
rebel.xml
### NetBeans ###
nbproject/private/
build/*
nbbuild/
dist/
nbdist/
.nb-gradle/
######################################################################
# Others
*.log
*.xml.versionsBackup
*.swp
!*/build/*.java
!*/build/*.html
!*/build/*.xml

260
pom.xml 100644
View File

@ -0,0 +1,260 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.muyu</groupId>
<artifactId>LoadCenter</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
<spring-boot.version>2.7.18</spring-boot.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<!-- SpringBoot 依赖配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<packaging>jar</packaging>
<name>sample</name>
<description>Alibaba Cloud SDK Code Sample for Java
</description>
<url>https://github.com/aliyun/alibabacloud-code-sample</url>
<licenses>
<license>
<name>The Apache License, Version 2.0</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
</license>
</licenses>
<developers>
<developer>
<id>aliyundeveloper</id>
<name>Aliyun SDK</name>
<email>aliyunsdk@aliyun.com</email>
</developer>
</developers>
<distributionManagement>
<snapshotRepository>
<id>sonatype-nexus-snapshots</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>sonatype-nexus-staging</id>
<url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
<dependencies>
<!-- SpringBoot Web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- redis依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- lombok依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.47</version>
</dependency>
<!-- 阿里云openAPI创建实例依赖 开始 -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>ecs20140526</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>tea-openapi</artifactId>
<version>0.3.2</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>tea-util</artifactId>
<version>0.2.21</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>darabonba-number</artifactId>
<version>0.0.3</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>tea-console</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>darabonba-env</artifactId>
<version>0.1.1</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>tea</artifactId>
<version>1.1.14</version>
</dependency>
<!-- 阿里云openAPI创建实例依赖 结束 -->
<!-- 阿里云openAPI释放实例依赖 开始 -->
<!-- <dependency>-->
<!-- <groupId>com.aliyun</groupId>-->
<!-- <artifactId>tea-console</artifactId>-->
<!-- <version>0.0.1</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.aliyun</groupId>-->
<!-- <artifactId>darabonba-env</artifactId>-->
<!-- <version>0.1.1</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.aliyun</groupId>-->
<!-- <artifactId>tea-openapi</artifactId>-->
<!-- <version>0.0.11</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.aliyun</groupId>-->
<!-- <artifactId>ecs20140526</artifactId>-->
<!-- <version>1.0.1</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.aliyun</groupId>-->
<!-- <artifactId>tea-util</artifactId>-->
<!-- <version>0.2.10</version>-->
<!-- </dependency>-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>darabonba-string</artifactId>
<version>0.0.3</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.aliyun</groupId>-->
<!-- <artifactId>tea</artifactId>-->
<!-- <version>[1.0.3, 2.0.0)</version>-->
<!-- </dependency>-->
<!-- 阿里云openAPI释放实例依赖 结束 -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>16</source>
<target>16</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.3</version>
<extensions>true</extensions>
<configuration>
<serverId>sonatype-nexus-staging</serverId>
<nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<encoding>UTF-8</encoding>
<doclint>none</doclint>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.aliyun.sample.Sample1</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<finalName>${project.artifactId}</finalName>
</build>
</project>

View File

@ -0,0 +1,22 @@
package com.muyu.loadCenter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* @ProjectName: LoadCenter
* @PackageName: com.muyu.loadCenter
* @Description TODO
* @Author HuangDaJu
* @Date 2024/4/13 08:32
* @Version 1.0
*/
@EnableScheduling
@SpringBootApplication
public class LoadCenterApplication {
public static void main(String[] args) {
SpringApplication.run(LoadCenterApplication.class, args);
}
}

View File

@ -0,0 +1,54 @@
package com.muyu.loadCenter.aliyun.config;
import com.aliyun.ecs20140526.Client;
import com.aliyun.teaopenapi.models.Config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @ProjectName: LoadCenter
* @PackageName: com.muyu.loadCenter.aliyun.config
* @Description
* @Author HuangDaJu
* @Date 2024/4/16 14:48
* @Version 1.0
*/
@Data
@Configuration
@ConfigurationProperties(prefix = "config.ali")
public class AliConfig {
/**
* access-key-id
*/
private String accessKeyId;
/**
* access-key-secret
*/
private String accessKeySecret;
/**
* id
*/
private String regionId;
@Bean
public Client createEcsClient(AliConfig aliConfig) throws Exception {
Config config = new Config()
// 您的AccessKey ID
.setAccessKeyId(aliConfig.getAccessKeyId())
// 您的AccessKey Secret
.setAccessKeySecret(aliConfig.getAccessKeySecret())
// 您的可用区ID
.setRegionId(aliConfig.getRegionId());
return new Client(config);
}
}

View File

@ -0,0 +1,62 @@
package com.muyu.loadCenter.aliyun.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/**
*yml
*/
@Data
@Configuration
@ConfigurationProperties(prefix = "config.instance")
public class InstanceConfig {
/**
* Id
*/
private String regionId;
/**
* ID
*/
private String imageId;
/**
*
*/
private String instanceType;
/**
* id
*/
private String securityGroupId;
/**
*
*/
private String vSwitchId;
/**
* Mbit/s0~100 0
*/
private String internetMaxBandwidthOut;
/**
*
*/
private String size;
/**
*
*/
private String category;
/**
* ECS
*/
private String instanceChargeType;
/**
*
*/
private String internetChargeType;
}

View File

@ -0,0 +1,23 @@
package com.muyu.loadCenter.aliyun.config;//package com.muyu.business.domain.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @ProjectName: cloud-vehicles
* @PackageName: com.muyu.system.common.domain.config
* @Description TODO
* @Author XiaoFan
* @Date 2024/4/2 19:37
* @Version 1.0
*/
@Configuration
public class RestClientConfig {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}

View File

@ -0,0 +1,197 @@
package com.muyu.loadCenter.aliyun.service;
import com.aliyun.ecs20140526.Client;
import com.aliyun.ecs20140526.models.*;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.models.RuntimeOptions;
import com.muyu.loadCenter.aliyun.config.AliConfig;
import com.muyu.loadCenter.aliyun.utils.UserUtil;
import com.muyu.loadCenter.domain.EcsInstanceInfo;
import com.muyu.loadCenter.aliyun.config.InstanceConfig;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
/**
* @ProjectName: LoadCenter
* @PackageName: com.muyu.loadCenter.aliyun
* @Description OpenApi
* @Author HuangDaJu
* @Date 2024/4/16 14:57
* @Version 1.0
*/
@Log4j2
@Service
public class AliYunEcsService {
private final AliConfig aliConfig;
private final InstanceConfig instanceConfig;
private final Client client;
public AliYunEcsService(AliConfig aliConfig, InstanceConfig instanceConfig, Client client) {
this.aliConfig = aliConfig;
this.instanceConfig = instanceConfig;
this.client = client;
}
/**
* RunInstances ECS
*/
public String runInstances() throws Exception {
RunInstancesRequest request = new RunInstancesRequest()
.setRegionId(aliConfig.getRegionId())
.setImageId(instanceConfig.getImageId())
.setInstanceType(instanceConfig.getInstanceType())
.setSecurityGroupId(instanceConfig.getSecurityGroupId())
.setVSwitchId(instanceConfig.getVSwitchId())
.setInstanceName("gateway:node:HuangDaJu")
.setDescription(new Date().toLocaleString() + " 创建的实例节点")
.setInternetMaxBandwidthOut(Integer.valueOf(instanceConfig.getInternetMaxBandwidthOut()))
.setInternetChargeType(instanceConfig.getInternetChargeType())
.setInstanceChargeType(instanceConfig.getInstanceChargeType())
// 批量创建五台ECS实例如果不设置该参数默认创建一台ECS实例。
// amount = 5,
// 如果缺少库存可以接受的最低创建数量。
// minAmount = 2,
// 打开预检参数功能不会实际创建ECS实例只检查参数正确性、用户权限或者ECS库存等问题。
// 实际情况下设置了DryRun参数后Amount必须为1MinAmount必须为空您可以根据实际需求修改代码。
// .setDryRun(true)
.setDryRun(false)
.setSystemDisk(new RunInstancesRequest.RunInstancesRequestSystemDisk()
.setSize(instanceConfig.getSize())
.setCategory(instanceConfig.getCategory()));
String result = "";
try {
com.aliyun.teaconsole.Client.log("--------------------批量创建实例开始--------------------");
RunInstancesResponse responces = client.runInstances(request);
com.aliyun.teaconsole.Client.log("--------------------创建实例成功实例ID:" + com.aliyun.teautil.Common.toJSONString(responces.body.instanceIdSets.instanceIdSet) + "--------------------");
//返回实例ID
result = responces.body.instanceIdSets.instanceIdSet + "";//前后带 []
result = UserUtil.removeBrackets(result);//前后不带[]
} catch (TeaException error) {
com.aliyun.teaconsole.Client.log("--------------------创建实例失败:" + com.aliyun.teautil.Common.toJSONString(error.code) + "--------------------" + error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
com.aliyun.teaconsole.Client.log("--------------------创建实例失败:" + com.aliyun.teautil.Common.toJSONString(error.code) + "--------------------" + error.message);
}
return result;
}
/**
* ID
*/
public EcsInstanceInfo selectList(String instanceId) throws Exception {
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest()
.setRegionId(aliConfig.getRegionId())
.setInstanceName("*")
.setInstanceIds(com.aliyun.teautil.Common.toJSONString(com.aliyun.darabonbastring.Client.split(instanceId, ",", 50)))
.setPageSize(10);
//初始化返回值
List<DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance> instanceList = null;
try {
// 复制代码运行请自行打印 API 的返回值
DescribeInstancesResponse describeInstancesResponse = client.describeInstancesWithOptions(describeInstancesRequest, new RuntimeOptions());
DescribeInstancesResponseBody body = describeInstancesResponse.getBody();
DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstances instances = body.getInstances();
instanceList = instances.getInstance();
if (instanceList == null || instanceList.isEmpty()){
return new EcsInstanceInfo();
}
EcsInstanceInfo ecsInstanceInfo = new EcsInstanceInfo();
instanceList.forEach(item -> {
ecsInstanceInfo.setInstanceName(item.getInstanceName());
ecsInstanceInfo.setInstanceId(item.getInstanceId());
ecsInstanceInfo.setStatus(item.status);
ecsInstanceInfo.setPublicIpAddress(UserUtil.removeBrackets(item.getPublicIpAddress().getIpAddress().toString()));
ecsInstanceInfo.setRecyclable(item.getRecyclable());
ecsInstanceInfo.setPrivateIpAddress(item.getVpcAttributes().getPrivateIpAddress().ipAddress.toString());
});
return ecsInstanceInfo;
} catch (TeaException error) {
log.error("code:[{}], message: [{}],data: [{}]",error.getCode(),error.getMessage(),error.getData());
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
// 错误 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);
log.error("message: [{}]",_error.getMessage(),_error);
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
// 错误 message
System.out.println(error.getMessage());
// 诊断地址
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
return new EcsInstanceInfo();
}
/**
*
*/
public void releaseECS(String instanceId) throws Exception {
DeleteInstancesRequest deleteInstancesRequest = new DeleteInstancesRequest()
.setRegionId(aliConfig.getRegionId())
.setDryRun(false)
.setForce(true)
.setTerminateSubscription(false)
.setInstanceId(java.util.Arrays.asList( instanceId));
try {
// 复制代码运行请自行打印 API 的返回值
client.deleteInstancesWithOptions(deleteInstancesRequest, new RuntimeOptions());
} catch (TeaException error) {
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
// 错误 message
log.error("code:[{}], message: [{}],data: [{}]",error.getCode(),error.getMessage(),error.getData());
System.out.println(error);
// 诊断地址
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
// 错误 message
log.error("message: [{}]",_error.getMessage());
}
}
}

View File

@ -0,0 +1,53 @@
package com.muyu.loadCenter.aliyun.utils;
import com.muyu.loadCenter.aliyun.utils.uuid.IdUtils;
import com.muyu.loadCenter.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
/**
* @ProjectName: LoadCenter
* @Author: LiuYunHu
* @CreateTime: 2024/4/13
* @Description:
*/
@Component
public class UserUtil {
@Autowired
private RedisService redisService;
/*
* @Author: LiuYunHu
* @Date: 2024/4/13 9:25
* @Description: []
* @Param: [input]
* @Return: java.lang.String
**/
public static String removeBrackets(String input) {
// 使用正则表达式匹配并移除括号
String pattern = "\\[|\\]"; // 匹配 [ 或 ]
return input.replaceAll(pattern, "");
}
/*
* @Author: LiuYunHu
* @Date: 2024/4/13 11:42
* @Description: token
* @Param:
* @Return:
**/
public String getToken(String instanceIp) {
//生成令牌
String randomUUID = IdUtils.randomUUID();
//缓存两分钟
redisService.setCacheObject("lyhToken:" + instanceIp, randomUUID, 2L, TimeUnit.MINUTES);
//令牌返回
return randomUUID;
}
}

View File

@ -0,0 +1,44 @@
package com.muyu.loadCenter.aliyun.utils.uuid;
/**
* ID
*
* @author couplet
*/
public class IdUtils {
/**
* UUID
*
* @return UUID
*/
public static String randomUUID () {
return UUID.randomUUID().toString();
}
/**
* UUID线
*
* @return UUID线
*/
public static String simpleUUID () {
return UUID.randomUUID().toString(true);
}
/**
* UUID使ThreadLocalRandomUUID
*
* @return UUID
*/
public static String fastUUID () {
return UUID.fastUUID().toString();
}
/**
* UUID线使ThreadLocalRandomUUID
*
* @return UUID线
*/
public static String fastSimpleUUID () {
return UUID.fastUUID().toString(true);
}
}

View File

@ -0,0 +1,449 @@
package com.muyu.loadCenter.aliyun.utils.uuid;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
/**
* universally unique identifierUUID
*
* @author couplet
*/
public final class UUID implements java.io.Serializable, Comparable<UUID> {
private static final long serialVersionUID = -1185015143654744140L;
/**
* UUID64
*/
private final long mostSigBits;
/**
* UUID64
*/
private final long leastSigBits;
/**
*
*
* @param data
*/
private UUID (byte[] data) {
long msb = 0;
long lsb = 0;
assert data.length == 16 : "data must be 16 bytes in length";
for (int i = 0 ; i < 8 ; i++) {
msb = (msb << 8) | (data[i] & 0xff);
}
for (int i = 8 ; i < 16 ; i++) {
lsb = (lsb << 8) | (data[i] & 0xff);
}
this.mostSigBits = msb;
this.leastSigBits = lsb;
}
/**
* 使 UUID
*
* @param mostSigBits {@code UUID} 64
* @param leastSigBits {@code UUID} 64
*/
public UUID (long mostSigBits, long leastSigBits) {
this.mostSigBits = mostSigBits;
this.leastSigBits = leastSigBits;
}
/**
* 4UUID
*
* @return {@code UUID}
*/
public static UUID fastUUID () {
return randomUUID(false);
}
/**
* 4UUID 使 UUID
*
* @return {@code UUID}
*/
public static UUID randomUUID () {
return randomUUID(true);
}
/**
* 4UUID 使 UUID
*
* @param isSecure 使{@link SecureRandom}
*
* @return {@code UUID}
*/
public static UUID randomUUID (boolean isSecure) {
final Random ng = isSecure ? Holder.numberGenerator : getRandom();
byte[] randomBytes = new byte[16];
ng.nextBytes(randomBytes);
randomBytes[6] &= 0x0f; /* clear version */
randomBytes[6] |= 0x40; /* set to version 4 */
randomBytes[8] &= 0x3f; /* clear variant */
randomBytes[8] |= 0x80; /* set to IETF variant */
return new UUID(randomBytes);
}
/**
* 3UUID
*
* @param name UUID
*
* @return {@code UUID}
*/
public static UUID nameUUIDFromBytes (byte[] name) {
MessageDigest md;
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException nsae) {
throw new InternalError("MD5 not supported");
}
byte[] md5Bytes = md.digest(name);
md5Bytes[6] &= 0x0f; /* clear version */
md5Bytes[6] |= 0x30; /* set to version 3 */
md5Bytes[8] &= 0x3f; /* clear variant */
md5Bytes[8] |= 0x80; /* set to IETF variant */
return new UUID(md5Bytes);
}
/**
* {@link #toString()} {@code UUID}
*
* @param name {@code UUID}
*
* @return {@code UUID}
*
* @throws IllegalArgumentException name {@link #toString}
*/
public static UUID fromString (String name) {
String[] components = name.split("-");
if (components.length != 5) {
throw new IllegalArgumentException("Invalid UUID string: " + name);
}
for (int i = 0 ; i < 5 ; i++) {
components[i] = "0x" + components[i];
}
long mostSigBits = Long.decode(components[0]).longValue();
mostSigBits <<= 16;
mostSigBits |= Long.decode(components[1]).longValue();
mostSigBits <<= 16;
mostSigBits |= Long.decode(components[2]).longValue();
long leastSigBits = Long.decode(components[3]).longValue();
leastSigBits <<= 48;
leastSigBits |= Long.decode(components[4]).longValue();
return new UUID(mostSigBits, leastSigBits);
}
/**
* hex
*
* @param val
* @param digits
*
* @return
*/
private static String digits (long val, int digits) {
long hi = 1L << (digits * 4);
return Long.toHexString(hi | (val & (hi - 1))).substring(1);
}
/**
* {@link SecureRandom} (RNG)
*
* @return {@link SecureRandom}
*/
public static SecureRandom getSecureRandom () {
try {
return SecureRandom.getInstance("SHA1PRNG");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
/**
* <br>
* ThreadLocalRandomJDK 7线
*
* @return {@link ThreadLocalRandom}
*/
public static ThreadLocalRandom getRandom () {
return ThreadLocalRandom.current();
}
/**
* UUID 128 64
*
* @return UUID 128 64
*/
public long getLeastSignificantBits () {
return leastSigBits;
}
/**
* UUID 128 64
*
* @return UUID 128 64
*/
public long getMostSignificantBits () {
return mostSigBits;
}
/**
* {@code UUID} . {@code UUID}
* <p>
* :
* <ul>
* <li>1 UUID
* <li>2 DCE UUID
* <li>3 UUID
* <li>4 UUID
* </ul>
*
* @return {@code UUID}
*/
public int version () {
// Version is bits masked by 0x000000000000F000 in MS long
return (int) ((mostSigBits >> 12) & 0x0f);
}
/**
* {@code UUID} {@code UUID}
* <p>
*
* <ul>
* <li>0 NCS
* <li>2 <a href="http://www.ietf.org/rfc/rfc4122.txt">IETF&nbsp;RFC&nbsp;4122</a>(Leach-Salz),
* <li>6
* <li>7 使
* </ul>
*
* @return {@code UUID}
*/
public int variant () {
// This field is composed of a varying number of bits.
// 0 - - Reserved for NCS backward compatibility
// 1 0 - The IETF aka Leach-Salz variant (used by this class)
// 1 1 0 Reserved, Microsoft backward compatibility
// 1 1 1 Reserved for future definition.
return (int) ((leastSigBits >>> (64 - (leastSigBits >>> 62))) & (leastSigBits >> 63));
}
/**
* UUID
*
* <p>
* 60 {@code UUID} time_lowtime_mid time_hi <br>
* 100 UTC 1582 10 15
*
* <p>
* UUID version 1<br>
* {@code UUID} UUID UnsupportedOperationException
*
* @throws UnsupportedOperationException {@code UUID} version 1 UUID
*/
public long timestamp () throws UnsupportedOperationException {
checkTimeBase();
return (mostSigBits & 0x0FFFL) << 48//
| ((mostSigBits >> 16) & 0x0FFFFL) << 32//
| mostSigBits >>> 32;
}
/**
* UUID
*
* <p>
* 14 UUID clock_seq clock_seq UUID
* <p>
* {@code clockSequence} UUID version 1 UUID UUID
* UnsupportedOperationException
*
* @return {@code UUID}
*
* @throws UnsupportedOperationException UUID version 1
*/
public int clockSequence () throws UnsupportedOperationException {
checkTimeBase();
return (int) ((leastSigBits & 0x3FFF000000000000L) >>> 48);
}
/**
* UUID
*
* <p>
* 48 UUID node IEEE 802 UUID
* <p>
* UUID version 1<br>
* UUID UUID UnsupportedOperationException
*
* @return {@code UUID}
*
* @throws UnsupportedOperationException UUID version 1
*/
public long node () throws UnsupportedOperationException {
checkTimeBase();
return leastSigBits & 0x0000FFFFFFFFFFFFL;
}
/**
* {@code UUID}
*
* <p>
* UUID BNF
*
* <pre>
* {@code
* UUID = <time_low>-<time_mid>-<time_high_and_version>-<variant_and_sequence>-<node>
* time_low = 4*<hexOctet>
* time_mid = 2*<hexOctet>
* time_high_and_version = 2*<hexOctet>
* variant_and_sequence = 2*<hexOctet>
* node = 6*<hexOctet>
* hexOctet = <hexDigit><hexDigit>
* hexDigit = [0-9a-fA-F]
* }
* </pre>
*
* </blockquote>
*
* @return {@code UUID}
*
* @see #toString(boolean)
*/
@Override
public String toString () {
return toString(false);
}
/**
* {@code UUID}
*
* <p>
* UUID BNF
*
* <pre>
* {@code
* UUID = <time_low>-<time_mid>-<time_high_and_version>-<variant_and_sequence>-<node>
* time_low = 4*<hexOctet>
* time_mid = 2*<hexOctet>
* time_high_and_version = 2*<hexOctet>
* variant_and_sequence = 2*<hexOctet>
* node = 6*<hexOctet>
* hexOctet = <hexDigit><hexDigit>
* hexDigit = [0-9a-fA-F]
* }
* </pre>
*
* </blockquote>
*
* @param isSimple '-'UUID
*
* @return {@code UUID}
*/
public String toString (boolean isSimple) {
final StringBuilder builder = new StringBuilder(isSimple ? 32 : 36);
// time_low
builder.append(digits(mostSigBits >> 32, 8));
if (false == isSimple) {
builder.append('-');
}
// time_mid
builder.append(digits(mostSigBits >> 16, 4));
if (false == isSimple) {
builder.append('-');
}
// time_high_and_version
builder.append(digits(mostSigBits, 4));
if (false == isSimple) {
builder.append('-');
}
// variant_and_sequence
builder.append(digits(leastSigBits >> 48, 4));
if (false == isSimple) {
builder.append('-');
}
// node
builder.append(digits(leastSigBits, 12));
return builder.toString();
}
// Comparison Operations
/**
* UUID
*
* @return UUID
*/
@Override
public int hashCode () {
long hilo = mostSigBits ^ leastSigBits;
return ((int) (hilo >> 32)) ^ (int) hilo;
}
// -------------------------------------------------------------------------------------------------------------------
// Private method start
/**
*
* <p>
* {@code null} UUID UUID varriant {@code true}
*
* @param obj
*
* @return {@code true} {@code false}
*/
@Override
public boolean equals (Object obj) {
if ((null == obj) || (obj.getClass() != UUID.class)) {
return false;
}
UUID id = (UUID) obj;
return (mostSigBits == id.mostSigBits && leastSigBits == id.leastSigBits);
}
/**
* UUID UUID
*
* <p>
* UUID UUID UUID UUID UUID
*
* @param val UUID UUID
*
* @return UUID val -10 1
*/
@Override
public int compareTo (UUID val) {
// The ordering is intentionally set up so that the UUIDs
// can simply be numerically compared as two numbers
return (this.mostSigBits < val.mostSigBits ? -1 : //
(this.mostSigBits > val.mostSigBits ? 1 : //
(this.leastSigBits < val.leastSigBits ? -1 : //
(this.leastSigBits > val.leastSigBits ? 1 : //
0))));
}
/**
* time-basedUUID
*/
private void checkTimeBase () {
if (version() != 1) {
throw new UnsupportedOperationException("Not a time-based UUID");
}
}
/**
* SecureRandom
*/
private static class Holder {
static final SecureRandom numberGenerator = getSecureRandom();
}
}

View File

@ -0,0 +1,124 @@
package com.muyu.loadCenter.controller;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.muyu.loadCenter.aliyun.service.AliYunEcsService;
import com.muyu.loadCenter.domain.EcsInstanceInfo;
import com.muyu.loadCenter.domain.Result;
import lombok.extern.log4j.Log4j2;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.muyu.loadCenter.redis.service.RedisService;
import org.springframework.web.client.RestTemplate;
import java.util.Set;
/**
*
* @Author HuangDaJu
* @Date 2024/4/13 08:43
*/
@Component
@Log4j2
public class LoadCenterController {
// 用于临时统计连接数以判断是否需要扩容的变量
int aa=0;
// 临时变量,无特定用途(根据现有代码)
int bb=0;
@Autowired
StringRedisTemplate redisTemplate; // Redis字符串模板用于与Redis进行交互
@Autowired
private RedisService redisService; // Redis服务封装了与Redis操作相关的功能
@Autowired
private AliYunEcsService aliYunEcsService; // 阿里云ECS服务用于管理云服务器
@Autowired
private RestTemplate restTemplate; // 用于与其他服务进行HTTP交互的模板
/**
* 30
*
*
*/
@Scheduled(cron = "0/5 * * * * ?")
public void scheduleECS() throws Exception {
// 客户端初始化
OkHttpClient client = new OkHttpClient();
// 从Redis获取服务器IP集合
Set<String> ipCacheSet = redisService.getCacheZSet("ECS");
log.info("共有个"+ipCacheSet.size()+"服务器");
// 遍历每台服务器进行负载检查
for (String ip : ipCacheSet) {
// 构建请求URL和请求头
String URL = "http://"+ip+":8080/public/cluster";
Request request = new Request.Builder()
.url(URL)
.get()
.addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
.addHeader("Accesstoken", "")
.build();
try {
Response response = client.newCall(request).execute();
// 解析响应数据
JSONArray jsonArray = JSONArray.parseArray(response.body().string());
JSONObject jsonObject = jsonArray.getJSONObject(0);
JSONObject mqttInfo = jsonObject.getJSONObject("mqttInfo");
int connectSize = mqttInfo.getIntValue("connectSize");
log.info("服务器:"+ip+"-车辆连接数:"+connectSize);
// 更新Redis中服务器的连接数
redisService.setCacheZSet("ECS", ip, connectSize);
// 根据连接数判断是否需要进行扩容或缩容
if (connectSize >= 5) {
aa++;
// 当满足扩容条件时,记录日志并执行扩容操作
if (aa == ipCacheSet.size()) {
log.info("需要扩容");
// 调用阿里云ECS服务创建新实例
String instanceId = aliYunEcsService.runInstances();
log.info("扩容的节点ip为" + instanceId);
// 休眠5秒以确保新实例创建完成
Thread.sleep(5000);
// 获取新实例信息并将其持久化到本地数据库
EcsInstanceInfo ecsInstanceInfo = aliYunEcsService.selectList(instanceId);
String url = "http://127.0.0.1:9006/ecsInstance/add";
restTemplate.postForObject(url, ecsInstanceInfo, Result.class);
log.info("实例信息持久化本地");
// 将新实例的IP和ID存入Redis
redisService.setCacheZSet("ECS", ecsInstanceInfo.getPublicIpAddress(), 0);
log.info("实例id和公网ip存入redis");
aa = 0; // 重置计数器
}
} else if (connectSize <= 2) {
// 缩容逻辑:删除连接数过低的服务器实例
// String url = "http://127.0.0.1:9006/ecsInstance/select/" + ip;
// Result result = restTemplate.postForObject(url, null, Result.class);
// String instanceId = (String) result.getData();
// aliYunEcsService.releaseECS(instanceId); // 释放ECS实例
Long i = redisService.deleteCacheZset("ECS", ip);// 从Redis中删除该服务器的记录
log.info(i+"连接数小于2,服务器缩容:" + ip);
aa = 0; // 重置计数器
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

View File

@ -0,0 +1,165 @@
package com.muyu.loadCenter.controller;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.muyu.loadCenter.LoadCenterApplication;
import com.muyu.loadCenter.domain.Result;
import com.muyu.loadCenter.domain.WorkGatewayNode;
import com.muyu.loadCenter.redis.service.RedisService;
import lombok.extern.log4j.Log4j2;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;
@Log4j2
@RestController
@RequestMapping("carGoGoGo")
public class WorkGatewayNodeController {
@Autowired
private RedisService redisService;
@PostMapping("/workGatewayNode")
public Result getWorkGatewayNode() throws Exception {
// redisService.setCacheObject("cursor", 0);
ArrayList<WorkGatewayNode> nodeIdList = new ArrayList<>();
Map<Object, Double> map = redisService.getCacheZSetScore("ECS");
for (Map.Entry<Object, Double> entry : map.entrySet()) {
WorkGatewayNode workGatewayNode1 = new WorkGatewayNode();
log.info(entry.getKey().toString()+"--"+entry.getValue());
workGatewayNode1.setNodeId(entry.getKey().toString());
workGatewayNode1.setWeight(entry.getValue().intValue());
nodeIdList.add(workGatewayNode1);
}
log.info("----------------------------分割线----↓--------------------------");
System.out.println(nodeIdList);
log.info("----------------------------分割线----↑---------------------------");
// ArrayList<WorkGatewayNode> nodeIdList = carWorkGatewayNode();
List<String> loadNodeList = new ArrayList<>();
long count = nodeIdList.stream().mapToInt(WorkGatewayNode::getWeight).sum();
if (count < 100) {
List<WorkGatewayNode> list = nodeIdList.stream()
.sorted((o1, o2) -> o2.getWeight() - o1.getWeight())
.toList();
int countWeight = 0;
for (long i = count; i < 100; i++) {
WorkGatewayNode workGatewayNode = list.get(countWeight++ % list.size());
workGatewayNode.setWeight(workGatewayNode.getWeight() + 1);
}
}
while (nodeIdList.stream().anyMatch(node -> node.getWeight() > 0)) {
for (WorkGatewayNode workGatewayNode : nodeIdList) {
int weight = workGatewayNode.getWeight();
if (weight > 0) {
loadNodeList.add(workGatewayNode.getNodeId());
workGatewayNode.setWeight(weight - 1);
}
}
}
log.info("----------------------------分割线--------------------------");
// redisService.setCacheList("work:node:gateway", loadNodeList);
Map<Object, Integer> countMap = new HashMap<>();
// 统计每个对象出现的次数
for (Object obj : loadNodeList) {
countMap.put(obj, countMap.getOrDefault(obj, 0) + 1);
}
// 找出出现次数最少的对象
Object nodeId = null;
int minCount = Integer.MAX_VALUE;
for (Map.Entry<Object, Integer> entry : countMap.entrySet()) {
if (entry.getValue() < minCount) {
minCount = entry.getValue();
nodeId = entry.getKey();
}
}
// 如果有最小数量的对象,则返回最小数量的对象;否则随机选择一个对象
if (nodeId != null) {
return Result.success(nodeId);
} else {
Random random = new Random();
return Result.success(random.nextInt(loadNodeList.size()));
}
}
//这里是查询连接数的现在使用redis的zset方法获得就不需要下面这些了
// public ArrayList<WorkGatewayNode> carWorkGatewayNode() throws Exception {
//
// Set<String> ipCacheSet = redisService.getCacheZSet("ECS");
// ArrayList<WorkGatewayNode> nodeIdList = new ArrayList<>();
//
// OkHttpClient client = new OkHttpClient();
// for (String ip : ipCacheSet) {
// System.out.println("ip:"+ip);
// String URL = "http://"+ip+":8080/public/cluster";
// Request request = new Request.Builder()
// .url(URL)
// .get()
// .addHeader("User-Agent", "Apifox/1.0.0 (https://apifox.com)")
// .addHeader("Accesstoken", "")
// .build();
//
// try {
// Response response = client.newCall(request).execute();
// JSONArray jsonArray = JSONArray.parseArray(response.body().string());
// JSONObject jsonObject = jsonArray.getJSONObject(0);
// JSONObject mqttInfo = jsonObject.getJSONObject("mqttInfo");
// int connectSize = mqttInfo.getIntValue("connectSize");
// WorkGatewayNode workGatewayNode = new WorkGatewayNode();
// log.info("服务器:"+ip+"-车辆连接数:"+connectSize);
//
// workGatewayNode.setWeight(connectSize);
// workGatewayNode.setNodeId(ip);
//
// nodeIdList.add(workGatewayNode);
//
//
// } catch (Exception e) {
// e.printStackTrace();
// }
//
//
// }
// return nodeIdList;
//
// }
}

View File

@ -0,0 +1,55 @@
package com.muyu.loadCenter.domain;
import lombok.Data;
@Data
public class ApifoxModel {
/**
* ID
*/
private String clusterId;
/**
* CPU使
*/
private CPUInfo cpuInfo;
/**
*
*/
private FlowInfo flowInfo;
/**
* HTTP
*/
private String httpUrl;
/**
* JVM使
*/
private JVMInfo jvmInfo;
/**
* MQTT
*/
private MqttInfo mqttInfo;
/**
* MQTTS
*/
private String mqttsUrl;
/**
* MQTT
*/
private String mqttUrl;
/**
*
*/
private String nodeName;
/**
*
*/
private String startJvmTime;
/**
*
*/
private String version;
/**
* websocket
*/
private String websocketUrl;
}

View File

@ -0,0 +1,30 @@
package com.muyu.loadCenter.domain;
import lombok.Data;
/**
* CPU使
*/
@Data
public class CPUInfo {
/**
* CPU
*/
private long cpuNum;
/**
* 使
*/
private String cSys;
/**
*
*/
private String idle;
/**
* I/O
*/
private String iowait;
/**
* 使
*/
private String user;
}

View File

@ -0,0 +1,46 @@
package com.muyu.loadCenter.domain;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
*
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class EcsInstanceInfo {
/**
* ID
*/
private String instanceId;
/**
*
*/
private String instanceName;
/**
*
*/
private String status;
/**
* Ip
*/
private String publicIpAddress;
/**
* Ip
*/
private String privateIpAddress;
/**
*
*/
private String creationTime;
/**
*
*/
private boolean recyclable;
}

View File

@ -0,0 +1,28 @@
package com.muyu.loadCenter.domain;
import lombok.Data; /**
*
*/
@Data
public class FlowInfo {
/**
*
*/
private String lastReadThroughput;
/**
*
*/
private String lastWriteThroughput;
/**
*
*/
private String readBytesHistory;
/**
*
*/
private String realWriteBytes;
/**
*
*/
private String writeBytesHistory;
}

View File

@ -0,0 +1,58 @@
package com.muyu.loadCenter.domain;
import lombok.Data;
/**
* JVM使
*/
@Data
public class JVMInfo {
/**
* ()
*/
private String fileDescriptors;
/**
*
*/
private String heapCommit;
/**
*
*/
private String heapInit;
/**
*
*/
private String heapMax;
/**
* 使
*/
private String heapUsed;
/**
* JAVA
*/
private String jdkHome;
/**
* JDK
*/
private String jdkVersion;
/**
*
*/
private String noHeapCommit;
/**
*
*/
private String noHeapInit;
/**
*
*/
private String noHeapMax;
/**
* 使
*/
private String noHeapUsed;
/**
* 线
*/
private long threadCount;
}

View File

@ -0,0 +1,64 @@
package com.muyu.loadCenter.domain;// ApifoxModel.java
import com.muyu.loadCenter.domain.CPUInfo;
import com.muyu.loadCenter.domain.JVMInfo;
import lombok.Data;
// JVMInfo.java
// MqttInfo.java
/**
* MQTT
*/
@Data
public class MqttInfo {
/**
*
*/
private long closeEventSize;
/**
*
*/
private long connectEventSize;
/**
*
*/
private long connectSize;
/**
*
*/
private long disconnectEventSize;
/**
*
*/
private long publishEventSize;
/**
*
*/
private long publishRetryEventSize;
/**
*
*/
private long retainSize;
/**
*
*/
private long subscribeEventSize;
/**
*
*/
private long subscribeSize;
/**
*
*/
private long topicSize;
/**
*
*/
private long unSubscribeEventSize;
}

View File

@ -0,0 +1,117 @@
package com.muyu.loadCenter.domain;
import java.io.Serializable;
/**
*
*
* @author coderjacky
*/
public class Result<T> implements Serializable
{
private static final long serialVersionUID = 1L;
/** 成功 */
public static final int SUCCESS = 200;
/** 失败 */
public static final int FAIL = 500;
private int code;
private String msg;
private T data;
public static <T> Result<T> success()
{
return restResult(null, SUCCESS, null);
}
public static <T> Result<T> success(T data)
{
return restResult(data, SUCCESS, null);
}
public static <T> Result<T> success(T data, String msg)
{
return restResult(data, SUCCESS, msg);
}
public static <T> Result<T> error()
{
return restResult(null, FAIL, null);
}
public static <T> Result<T> error(String msg)
{
return restResult(null, FAIL, msg);
}
public static <T> Result<T> error(T data)
{
return restResult(data, FAIL, null);
}
public static <T> Result<T> error(T data, String msg)
{
return restResult(data, FAIL, msg);
}
public static <T> Result<T> error(int code, String msg)
{
return restResult(null, code, msg);
}
private static <T> Result<T> restResult(T data, int code, String msg)
{
Result<T> apiResult = new Result<>();
apiResult.setCode(code);
apiResult.setData(data);
apiResult.setMsg(msg);
return apiResult;
}
public int getCode()
{
return code;
}
public void setCode(int code)
{
this.code = code;
}
public String getMsg()
{
return msg;
}
public void setMsg(String msg)
{
this.msg = msg;
}
public T getData()
{
return data;
}
public void setData(T data)
{
this.data = data;
}
public static <T> Boolean isError(Result<T> ret)
{
return !isSuccess(ret);
}
public static <T> Boolean isSuccess(Result<T> ret)
{
return Result.SUCCESS == ret.getCode();
}
}

View File

@ -0,0 +1,23 @@
package com.muyu.loadCenter.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @ProjectName: LoadCenter
* @PackageName: com.muyu.loadCenter.domain
* @Description TODO
* @Author HuangDaJu
* @Date 2024/4/14 11:55
* @Version 1.0
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class WorkGatewayNode {
private String nodeId;
private int weight;
}

View File

@ -0,0 +1,56 @@
package com.muyu.loadCenter.redis.configure;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.filter.Filter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
/**
* Redis使FastJson
*
* @author muyu
*/
public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T>
{
public static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
public static final String[] JSON_WHITELIST_STR = { "org.springframework", "com.muyu" };
static final Filter AUTO_TYPE_FILTER = JSONReader.autoTypeFilter( JSON_WHITELIST_STR);
private Class<T> clazz;
public FastJson2JsonRedisSerializer(Class<T> clazz)
{
super();
this.clazz = clazz;
}
@Override
public byte[] serialize(T t) throws SerializationException
{
if (t == null)
{
return new byte[0];
}
return JSON.toJSONString(t, JSONWriter.Feature.WriteClassName).getBytes(DEFAULT_CHARSET);
}
@Override
public T deserialize(byte[] bytes) throws SerializationException
{
if (bytes == null || bytes.length <= 0)
{
return null;
}
String str = new String(bytes, DEFAULT_CHARSET);
return JSON.parseObject(str, clazz, AUTO_TYPE_FILTER);
}
}

View File

@ -0,0 +1,43 @@
package com.muyu.loadCenter.redis.configure;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* redis
*
* @author muyu
*/
@Configuration
@EnableCaching
@AutoConfigureBefore(RedisAutoConfiguration.class)
public class RedisConfig extends CachingConfigurerSupport
{
@Bean
@SuppressWarnings(value = { "unchecked", "rawtypes" })
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)
{
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);
// 使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(serializer);
// Hash的key也采用StringRedisSerializer的序列化方式
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(serializer);
template.afterPropertiesSet();
return template;
}
}

View File

@ -0,0 +1,362 @@
package com.muyu.loadCenter.redis.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.concurrent.TimeUnit;
/**
* spring redis
*
* @author muyu
**/
@SuppressWarnings(value = { "unchecked", "rawtypes" })
@Component
public class RedisService
{
@Autowired
public RedisTemplate redisTemplate;
/**
* IntegerString
*
* @param key
* @param value
*/
public <T> void setCacheObject(final String key, final T value)
{
redisTemplate.opsForValue().set(key, value);
}
/**
* IntegerString
*
* @param key
* @param value
* @param timeout
* @param timeUnit
*/
public <T> void setCacheObject(final String key, final T value, final Long timeout, final TimeUnit timeUnit)
{
redisTemplate.opsForValue().set(key, value, timeout, timeUnit);
}
/**
*
*
* @param key Redis
* @param timeout
* @return true=false=
*/
public boolean expire(final String key, final long timeout)
{
return expire(key, timeout, TimeUnit.SECONDS);
}
/**
*
*
* @param key Redis
* @param timeout
* @param unit
* @return true=false=
*/
public boolean expire(final String key, final long timeout, final TimeUnit unit)
{
return redisTemplate.expire(key, timeout, unit);
}
/**
*
*
* @param key Redis
* @return
*/
public long getExpire(final String key)
{
return redisTemplate.getExpire(key);
}
/**
* key
*
* @param key
* @return true false
*/
public Boolean hasKey(String key)
{
return redisTemplate.hasKey(key);
}
/**
*
*
* @param key
* @return
*/
public <T> T getCacheObject(final String key)
{
ValueOperations<String, T> operation = redisTemplate.opsForValue();
return operation.get(key);
}
/**
*
*
* @param key
*/
public boolean deleteObject(final String key)
{
return redisTemplate.delete(key);
}
/**
*
*
* @param collection
* @return
*/
public boolean deleteObject(final Collection collection)
{
return redisTemplate.delete(collection) > 0;
}
/**
* List
*
* @param key
* @param dataList List
* @return
*/
public <T> long setCacheList(final String key, final List<T> dataList)
{
Long count = redisTemplate.opsForList().rightPushAll(key, dataList);
return count == null ? 0 : count;
}
public Object leftPopAndRemove(String key) {
return redisTemplate.opsForList().leftPop(key);
}
public Long rightPush(String key, String value) {
return redisTemplate.opsForList().rightPush(key, value);
}
/**
* list
*
* @param key
* @return
*/
public <T> List<T> getCacheList(final String key) {
return redisTemplate.opsForList().range(key, 0, -1);
}
public <T> T getCacheListValue(final String key,long index) {
return (T) redisTemplate.opsForList().index(key,index);
}
/**
* Set
*
* @param key
* @param dataSet
* @return
*/
public <T> BoundSetOperations<String, T> setCacheSet(final String key, final Set<T> dataSet)
{
BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
Iterator<T> it = dataSet.iterator();
while (it.hasNext())
{
setOperation.add(it.next());
}
return setOperation;
}
/**
* zSet
*
* @param key
* @param setValue
* @param score
* @return
*/
public <T> BoundZSetOperations<String, T> setCacheZSet(final String key, final T setValue, double score) {
BoundZSetOperations boundZSetOperations = redisTemplate.boundZSetOps(key);
boundZSetOperations.add(setValue, score);
return boundZSetOperations;
}
public <T> Set<String> getCacheZSet(final String key) {
ZSetOperations<String, String> zSetOperations = redisTemplate.opsForZSet();
Set<String> ipCacheSet = zSetOperations.range(key, 0, -1);
return ipCacheSet;
}
public <T> Map<Object, Double> getCacheZSetScore(final String key) {
ZSetOperations<String, Object> zSetOperations = redisTemplate.opsForZSet();
// 构建一个 Map 用于存储成员和分数的对应关系
Map<Object, Double> memberScores = new HashMap<>();
zSetOperations.rangeWithScores(key, 0, -1).forEach(tuple -> {
memberScores.put(tuple.getValue(), tuple.getScore());
});
return memberScores;
}
/**
* Set
*
* @param key
* @param setValue
* @return
*/
public <T> BoundSetOperations<String, T> setCacheSet(final String key, final T setValue)
{
BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
setOperation.add(setValue);
return setOperation;
}
/**
* Set
*
* @param key
* @param setValue
* @return
*/
public <T> void deleteCacheSet(String key, T setValue) {
BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
setOperation.remove(setValue);
}
/**
* set
*
* @param key
* @return
*/
public <T> Set<T> getCacheSet(final String key)
{
return redisTemplate.opsForSet().members(key);
}
/**
* zset
*
* @param key
* @return
*/
public Long deleteCacheZset(final String key, String value){
return redisTemplate.opsForZSet().remove(key,value);
}
/**
* Map
*
* @param key
* @param dataMap
*/
public <T> void setCacheMap(final String key, final Map<String, T> dataMap)
{
if (dataMap != null) {
redisTemplate.opsForHash().putAll(key, dataMap);
}
}
/**
* Map
*
* @param key
* @return
*/
public <T> Map<String, T> getCacheMap(final String key)
{
return redisTemplate.opsForHash().entries(key);
}
/**
* Hash
*
* @param key Redis
* @param hKey Hash
* @param value
*/
public <T> void setCacheMapValue(final String key, final String hKey, final T value)
{
redisTemplate.opsForHash().put(key, hKey, value);
}
/**
* Hash
*
* @param key Redis
* @param hKey Hash
* @return Hash
*/
public <T> T getCacheMapValue(final String key, final String hKey)
{
HashOperations<String, String, T> opsForHash = redisTemplate.opsForHash();
return opsForHash.get(key, hKey);
}
/**
* Hash
*
* @param key Redis
* @param hKeys Hash
* @return Hash
*/
public <T> List<T> getMultiCacheMapValue(final String key, final Collection<Object> hKeys)
{
return redisTemplate.opsForHash().multiGet(key, hKeys);
}
/**
* Hash
*
* @param key Redis
* @param hKey Hash
* @return
*/
public boolean deleteCacheMapValue(final String key, final String hKey)
{
return redisTemplate.opsForHash().delete(key, hKey) > 0;
}
/**
*
*
* @param pattern
* @return
*/
public Collection<String> keys(final String pattern) {
return redisTemplate.keys(pattern);
}
/**
*
*
* @param key
* @param number
* @return
*/
public Long increment(final String key,Long number) {
return redisTemplate.opsForValue().increment(key,number);
}
public void deleteCacheObject(String cursor) {
redisTemplate.delete(cursor);
}
}

View File

@ -0,0 +1,30 @@
# Tomcat
server:
port: 9010
Spring:
redis:
# 公网地址
host: 121.89.212.0
# host: 10.10.26.238
# host: 127.0.0.1
port: 6379
config:
ali:
access-key-id: LTAI5tANGefs2gi8nsu4AoSZ
access-key-secret: Ut5RaJvvG7dP8hgK82qjdtvyUA6x8g
region-id: cn-zhangjiakou
instance:
imageId: m-8vb8z0ygyrzgqt54k3wi
instanceType: ecs.t6-c1m1.large
securityGroupId: sg-8vbfx0e48cekrpzgsa72
vSwitchId: vsw-8vbn6cq2uy0mmw69l6ryq
internetMaxBandwidthOut: 5
size: 20
category: cloud_efficiency
instanceChargeType: PostPaid
internetChargeType: PayByTraffic

View File

@ -0,0 +1,61 @@
import com.muyu.loadCenter.LoadCenterApplication;
import com.muyu.loadCenter.domain.WorkGatewayNode;
import com.muyu.loadCenter.redis.service.RedisService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest(classes = LoadCenterApplication.class)
public class Test1 {
@Autowired
private RedisService redisService;
@Test
public void load() {
// 初始化序列
redisService.increment("cursor", 0L);
List<WorkGatewayNode> nodeIdList = new ArrayList<>() {{
add(new WorkGatewayNode("node1:39.98.113.79", 20));
add(new WorkGatewayNode("node2:39.98.113.80", 1));
add(new WorkGatewayNode("node2:39.98.113.80", 2));
add(new WorkGatewayNode("node2:39.98.113.80", 43));
add(new WorkGatewayNode("node2:39.98.113.80", 92));
}};
long count = nodeIdList.stream().mapToInt(WorkGatewayNode::getWeight).sum();
if (count < 100) {
List<WorkGatewayNode> list = nodeIdList.stream()
.sorted((o1, o2) -> o2.getWeight() - o1.getWeight())
.toList();
int countWeight = 0;
for (long i = count; i < 100; i++) {
WorkGatewayNode workGatewayNode = list.get(countWeight++ % list.size());
workGatewayNode.setWeight(workGatewayNode.getWeight() + 1);
}
}
// List<String> loadNodeList = new ArrayList<>();
// while (nodeIdList.stream().anyMatch(node -> node.getWeight() > 0)) {
for (WorkGatewayNode workGatewayNode : nodeIdList) {
int weight = workGatewayNode.getWeight();
if (weight > 0) {
// loadNodeList.add(workGatewayNode.getNodeId());
workGatewayNode.setWeight(weight - 1);
System.out.println(workGatewayNode.getNodeId());
break; // 只输出一个 IP 地址
}
}
// }
nodeIdList.forEach(System.out::println);
// redisService.setCacheList("work:node:gateway", loadNodeList);
}
}