第一次提交
commit
396742ce64
|
@ -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
|
|
@ -0,0 +1,221 @@
|
|||
<?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.lyh</groupId>
|
||||
<artifactId>LoadCenter</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<description>车辆网关负载中心</description>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<spring-boot.version>2.7.13</spring-boot.version>
|
||||
</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>
|
||||
|
||||
<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>
|
||||
<!-- 阿里云openAPI创建实例插件 开始 -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.6.1</version>
|
||||
<configuration>
|
||||
<source>17</source>
|
||||
<target>17</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.Sample</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>make-assembly</id>
|
||||
<phase>package</phase>
|
||||
<!-- <goals>-->
|
||||
<!-- <goal>single</goal>-->
|
||||
<!-- </goals>-->
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<!-- 阿里云openAPI创建实例插件 结束 -->
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,22 @@
|
|||
package com.lyh;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
/**
|
||||
* @ProjectName: Default (Template) Project
|
||||
* @Author: LiuYunHu
|
||||
* @CreateTime: 2024/4/1
|
||||
* @Description: 负载中心模块启动类
|
||||
*/
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableScheduling
|
||||
//@EnableFeignClients
|
||||
public class LoadCenterApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(LoadCenterApplication.class, args);
|
||||
System.out.println("负载中心模块启动成功");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.lyh.common.config;
|
||||
|
||||
import com.aliyun.teaopenapi.models.Config;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @ProjectName: LoadCenter
|
||||
* @Author: LiuYunHu
|
||||
* @CreateTime: 2024/4/13
|
||||
* @Description: 阿里云客户端配置类
|
||||
*/
|
||||
|
||||
@Component
|
||||
public class ClientService {
|
||||
|
||||
/*
|
||||
* @Author: LiuYunHu
|
||||
* @Date: 2024/4/13 9:52
|
||||
* @Description: 初始化公共请求参数
|
||||
* @Param:
|
||||
* @Return:
|
||||
**/
|
||||
|
||||
public static com.aliyun.ecs20140526.Client createEcsClient(String regionId) throws Exception {
|
||||
Config config = new Config()
|
||||
// 您的AccessKey ID
|
||||
.setAccessKeyId("LTAI5tEBb3cjLf8nRBJZmfD9")
|
||||
// 您的AccessKey Secret
|
||||
.setAccessKeySecret("Emzjl8SlqwlHP7xeFOrhwtXiJNfbkF")
|
||||
// 您的可用区ID
|
||||
.setRegionId(regionId)
|
||||
// 设置连接超时为5000毫秒
|
||||
.setConnectTimeout(5000)
|
||||
// 设置读超时为5000毫秒
|
||||
.setReadTimeout(5000);
|
||||
return new com.aliyun.ecs20140526.Client(config);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.lyh.common.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @ProjectName: LoadCenter
|
||||
* @Author: LiuYunHu
|
||||
* @CreateTime: 2024/4/15
|
||||
* @Description: IP和对应IP的负载量
|
||||
*/
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class IpAndLoadCount {
|
||||
private String ip;
|
||||
private Integer loadCount;
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.lyh.common.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @ProjectName: LoadCenter
|
||||
* @Author: LiuYunHu
|
||||
* @CreateTime: 2024/4/15
|
||||
* @Description: //节点IP和对应的权重
|
||||
*/
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class IpAndWeight {
|
||||
private String nodeIp;
|
||||
private Integer weight;
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
package com.lyh.common.domain;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @ProjectName: LoadCenter
|
||||
* @Author: LiuYunHu
|
||||
* @CreateTime: 2024/4/13
|
||||
* @Description: mqtt服务器信息
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
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;
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
package com.lyh.common.domain.resp;
|
||||
|
||||
/**
|
||||
* 返回状态码
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public class HttpStatus {
|
||||
/**
|
||||
* 操作成功
|
||||
*/
|
||||
public static final int SUCCESS = 200;
|
||||
|
||||
/**
|
||||
* 对象创建成功
|
||||
*/
|
||||
public static final int CREATED = 201;
|
||||
|
||||
/**
|
||||
* 请求已经被接受
|
||||
*/
|
||||
public static final int ACCEPTED = 202;
|
||||
|
||||
/**
|
||||
* 操作已经执行成功,但是没有返回数据
|
||||
*/
|
||||
public static final int NO_CONTENT = 204;
|
||||
|
||||
/**
|
||||
* 资源已被移除
|
||||
*/
|
||||
public static final int MOVED_PERM = 301;
|
||||
|
||||
/**
|
||||
* 重定向
|
||||
*/
|
||||
public static final int SEE_OTHER = 303;
|
||||
|
||||
/**
|
||||
* 资源没有被修改
|
||||
*/
|
||||
public static final int NOT_MODIFIED = 304;
|
||||
|
||||
/**
|
||||
* 参数列表错误(缺少,格式不匹配)
|
||||
*/
|
||||
public static final int BAD_REQUEST = 400;
|
||||
|
||||
/**
|
||||
* 未授权
|
||||
*/
|
||||
public static final int UNAUTHORIZED = 401;
|
||||
|
||||
/**
|
||||
* 访问受限,授权过期
|
||||
*/
|
||||
public static final int FORBIDDEN = 403;
|
||||
|
||||
/**
|
||||
* 资源,服务未找到
|
||||
*/
|
||||
public static final int NOT_FOUND = 404;
|
||||
|
||||
/**
|
||||
* 不允许的http方法
|
||||
*/
|
||||
public static final int BAD_METHOD = 405;
|
||||
|
||||
/**
|
||||
* 资源冲突,或者资源被锁
|
||||
*/
|
||||
public static final int CONFLICT = 409;
|
||||
|
||||
/**
|
||||
* 不支持的数据,媒体类型
|
||||
*/
|
||||
public static final int UNSUPPORTED_TYPE = 415;
|
||||
|
||||
/**
|
||||
* 系统内部错误
|
||||
*/
|
||||
public static final int ERROR = 500;
|
||||
|
||||
/**
|
||||
* 接口未实现
|
||||
*/
|
||||
public static final int NOT_IMPLEMENTED = 501;
|
||||
|
||||
/**
|
||||
* 系统警告消息
|
||||
*/
|
||||
public static final int WARN = 601;
|
||||
}
|
|
@ -0,0 +1,101 @@
|
|||
package com.lyh.common.domain.resp;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 响应信息主体
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
@Data
|
||||
public class Result<T> implements Serializable {
|
||||
/**
|
||||
* 成功
|
||||
*/
|
||||
public static final int SUCCESS = HttpStatus.SUCCESS;
|
||||
/**
|
||||
* 失败
|
||||
*/
|
||||
public static final int FAIL = HttpStatus.ERROR;
|
||||
private static final long serialVersionUID = 1L;
|
||||
/**
|
||||
* 系统警告消息
|
||||
*/
|
||||
private static final int WARN = HttpStatus.WARN;
|
||||
|
||||
private int code;
|
||||
|
||||
private String msg;
|
||||
|
||||
private T data;
|
||||
|
||||
public static <T> Result<T> success () {
|
||||
return restResult(null, SUCCESS, "操作成功");
|
||||
}
|
||||
|
||||
public static <T> Result<T> success (T data) {
|
||||
return restResult(data, SUCCESS, "操作成功");
|
||||
}
|
||||
|
||||
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, "操作失败");
|
||||
}
|
||||
|
||||
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, "操作失败");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public static <T> Result<T> warn () {
|
||||
return restResult(null, WARN, "操作失败");
|
||||
}
|
||||
|
||||
public static <T> Result<T> warn (String msg) {
|
||||
return restResult(null, WARN, msg);
|
||||
}
|
||||
|
||||
public static <T> Result<T> warn (T data) {
|
||||
return restResult(data, WARN, "操作失败");
|
||||
}
|
||||
|
||||
public static <T> Result<T> warn (T data, String msg) {
|
||||
return restResult(data, WARN, msg);
|
||||
}
|
||||
|
||||
public static <T> Result<T> warn (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 static <T> Boolean isError (Result<T> ret) {
|
||||
return !isSuccess(ret);
|
||||
}
|
||||
|
||||
public static <T> Boolean isSuccess (Result<T> ret) {
|
||||
return Result.SUCCESS == ret.getCode();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package com.lyh.common.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;
|
||||
|
||||
/**
|
||||
* Redis使用FastJson序列化
|
||||
*
|
||||
* @author couplet
|
||||
*/
|
||||
public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T> {
|
||||
public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
|
||||
|
||||
/**
|
||||
* 自动识别json对象白名单配置(仅允许解析的包名,范围越小越安全)
|
||||
*/
|
||||
public static final String[] JSON_WHITELIST_STR = {"org.springframework", "com.couplet"};
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.lyh.common.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 couplet
|
||||
*/
|
||||
@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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.lyh.common.redis.configure;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
|
||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||
|
||||
/**
|
||||
* @Author: LiJiaYao
|
||||
* @Date: 2024/4/4
|
||||
* @Description: redis监听配置
|
||||
*/
|
||||
@Configuration
|
||||
public class RedisListenerConfig {
|
||||
|
||||
@Bean
|
||||
RedisMessageListenerContainer listenerContainer(RedisConnectionFactory redisConnectionFactory) {
|
||||
|
||||
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
||||
container.setConnectionFactory(redisConnectionFactory);
|
||||
return container;
|
||||
}
|
||||
|
||||
@Bean
|
||||
KeyExpirationEventMessageListener redisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
|
||||
return new KeyExpirationEventMessageListener(listenerContainer);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,327 @@
|
|||
package com.lyh.common.redis.service;
|
||||
|
||||
import org.springframework.data.redis.core.BoundSetOperations;
|
||||
import org.springframework.data.redis.core.HashOperations;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.ValueOperations;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* spring redis 工具类
|
||||
*
|
||||
* @author couplet
|
||||
**/
|
||||
@SuppressWarnings(value = {"unchecked", "rawtypes"})
|
||||
@Component
|
||||
public class RedisService {
|
||||
@Resource
|
||||
public RedisTemplate redisTemplate;
|
||||
|
||||
|
||||
// ... 其他已有方法 ...
|
||||
|
||||
/**
|
||||
* 向指定集合中添加值,如果值不存在则添加并返回true,否则返回false。
|
||||
*
|
||||
* @param setKey 集合键名
|
||||
* @param value 要添加的值
|
||||
* @return true表示值已成功添加(之前不存在),false表示值已存在
|
||||
*/
|
||||
public boolean addToSetIfNotExists(String setKey, String value) {
|
||||
return redisTemplate.opsForSet().add(setKey, value) == 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 缓存基本的对象,Integer、String、实体类等
|
||||
*
|
||||
* @param key 缓存的键值
|
||||
* @param value 缓存的值
|
||||
*/
|
||||
public <T> void setCacheObject(final String key, final T value) {
|
||||
redisTemplate.opsForValue().set(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存基本的对象,Integer、String、实体类等
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得缓存的list对象
|
||||
*
|
||||
* @param key 缓存的键值
|
||||
* @return 缓存键值对应的数据
|
||||
*/
|
||||
public <T> List<T> getCacheList(final String key) {
|
||||
return redisTemplate.opsForList().range(key, 0, -1);
|
||||
}
|
||||
|
||||
public <T> T getCacheList(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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存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;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 缓存Zset
|
||||
*
|
||||
* @param key 缓存键值
|
||||
* @param setValue 缓存的数据
|
||||
* @return 缓存数据的对象
|
||||
**/
|
||||
public <T> void setCacheZset(final String key, final T setValue) {
|
||||
HashSet<String> set = new HashSet<>();
|
||||
set.add((String) setValue);
|
||||
Long add = redisTemplate.opsForZSet().add(key, set);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存Set
|
||||
*
|
||||
* @param key 缓存键值
|
||||
* @param setValue 缓存的数据
|
||||
* @return 缓存数据的对象
|
||||
*/
|
||||
public <T> void deleteSet(String key, String setValue) {
|
||||
|
||||
BoundSetOperations setOperations = redisTemplate.boundSetOps(key);
|
||||
setOperations.remove(setValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得缓存的set
|
||||
*
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public <T> Set<T> getCacheSet(final String key) {
|
||||
return redisTemplate.opsForSet().members(key);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 缓存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);
|
||||
}
|
||||
|
||||
/*
|
||||
* @Author: LiuYunHu
|
||||
* @Date: 2024/4/15 10:12
|
||||
* @Description: 减少序列值
|
||||
* @Param: [key, number]
|
||||
* @Return: java.lang.Long
|
||||
**/
|
||||
public Long decrement(final String key, Long number) {
|
||||
return redisTemplate.opsForValue().decrement(key, number);
|
||||
}
|
||||
|
||||
/*
|
||||
* @Author: LiuYunHu
|
||||
* @Date: 2024/4/15 10:14
|
||||
* @Description: 增加序列值
|
||||
* @Param: [key, number]
|
||||
* @Return: java.lang.Long
|
||||
**/
|
||||
public Long increment(final String key, Long number) {
|
||||
return redisTemplate.opsForValue().increment(key, number);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package com.lyh.common.utils;
|
||||
|
||||
import com.lyh.common.redis.service.RedisService;
|
||||
import com.lyh.common.utils.uuid.IdUtils;
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
package com.lyh.common.utils.mqtt;
|
||||
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @ProjectName: LoadCenter
|
||||
* @Author: LiuYunHu
|
||||
* @CreateTime: 2024/4/15
|
||||
* @Description: mqtt的工具类
|
||||
*/
|
||||
|
||||
@Component
|
||||
@Log4j2
|
||||
public class MqttUtil {
|
||||
|
||||
/*
|
||||
* @Author: LiuYunHu
|
||||
* @Date: 2024/4/15 17:26
|
||||
* @Description: 通过IP获取指定IP的fluxmq的负载量
|
||||
* @Param: [IP]
|
||||
* @Return: int
|
||||
**/
|
||||
public int getFetchLoad(String ip) {
|
||||
int result = 0;
|
||||
|
||||
|
||||
//请求路径
|
||||
String URL = "http://" + ip + ":8080/public/cluster";
|
||||
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
|
||||
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);
|
||||
//获取mqttInfo对象的值
|
||||
JSONObject mqttInfo = jsonObject.getJSONObject("mqttInfo");
|
||||
//获取连接数
|
||||
int connectSize = mqttInfo.getIntValue("connectSize");
|
||||
|
||||
log.info(ip + " 的fluxmq连接数为:" + connectSize);
|
||||
|
||||
result = connectSize;
|
||||
} catch (IOException e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.lyh.common.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,使用性能更好的ThreadLocalRandom生成UUID
|
||||
*
|
||||
* @return 随机UUID
|
||||
*/
|
||||
public static String fastUUID() {
|
||||
return UUID.fastUUID().toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 简化的UUID,去掉了横线,使用性能更好的ThreadLocalRandom生成UUID
|
||||
*
|
||||
* @return 简化的UUID,去掉了横线
|
||||
*/
|
||||
public static String fastSimpleUUID() {
|
||||
return UUID.fastUUID().toString(true);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,438 @@
|
|||
package com.lyh.common.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 identifier)(UUID)实现
|
||||
*
|
||||
* @author couplet
|
||||
*/
|
||||
public final class UUID implements java.io.Serializable, Comparable<UUID> {
|
||||
private static final long serialVersionUID = -1185015143654744140L;
|
||||
/**
|
||||
* 此UUID的最高64有效位
|
||||
*/
|
||||
private final long mostSigBits;
|
||||
/**
|
||||
* 此UUID的最低64有效位
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取类型 4(伪随机生成的)UUID 的静态工厂。
|
||||
*
|
||||
* @return 随机生成的 {@code UUID}
|
||||
*/
|
||||
public static UUID fastUUID() {
|
||||
return randomUUID(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取类型 4(伪随机生成的)UUID 的静态工厂。 使用加密的强伪随机数生成器生成该 UUID。
|
||||
*
|
||||
* @return 随机生成的 {@code UUID}
|
||||
*/
|
||||
public static UUID randomUUID() {
|
||||
return randomUUID(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取类型 4(伪随机生成的)UUID 的静态工厂。 使用加密的强伪随机数生成器生成该 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据指定的字节数组获取类型 3(基于名称的)UUID 的静态工厂。
|
||||
*
|
||||
* @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>
|
||||
* ThreadLocalRandom是JDK 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 RFC 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_low、time_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 时,分别返回 -1、0 或 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-based版本UUID
|
||||
*/
|
||||
private void checkTimeBase() {
|
||||
if (version() != 1) {
|
||||
throw new UnsupportedOperationException("Not a time-based UUID");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* SecureRandom 的单例
|
||||
*/
|
||||
private static class Holder {
|
||||
static final SecureRandom numberGenerator = getSecureRandom();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.lyh.controller;
|
||||
|
||||
import com.lyh.common.domain.resp.Result;
|
||||
import com.lyh.service.LoadCenterService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @ProjectName: LoadCenter
|
||||
* @Author: LiuYunHu
|
||||
* @CreateTime: 2024/4/15
|
||||
* @Description: 负载中心控制层
|
||||
*/
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/loadCenter")
|
||||
@Slf4j
|
||||
public class LoadCenterController {
|
||||
@Autowired
|
||||
private LoadCenterService loadCenterService;
|
||||
|
||||
|
||||
/*
|
||||
* @Author: LiuYunHu
|
||||
* @Date: 2024/4/15 14:57
|
||||
* @Description: 获取指定的服务器IP
|
||||
* @Param: []
|
||||
* @Return: com.lyh.common.domain.resp.Result<java.lang.String>
|
||||
**/
|
||||
@PostMapping("/getAssignedServer")
|
||||
public Result<String> getAssignedServer() {
|
||||
return loadCenterService.getAssignedServer();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
package com.lyh.job;
|
||||
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.lyh.openAPI.WorkCenter.CreateAndRunInstance;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @ProjectName: LoadCenter
|
||||
* @Author: LiuYunHu
|
||||
* @CreateTime: 2024/4/13
|
||||
* @Description:
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class Timer {
|
||||
// @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 = CreateAndRunInstance.startCreate();
|
||||
|
||||
if (!instanceId.isEmpty()) {
|
||||
log.info("扩容 成功!");
|
||||
log.info("扩容的节点ip为:" + instanceId);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,105 @@
|
|||
// This file is auto-generated, don't edit it. Thanks.
|
||||
package com.lyh.openAPI.WorkCenter;
|
||||
|
||||
import com.aliyun.ecs20140526.models.RunInstancesRequest;
|
||||
import com.aliyun.ecs20140526.models.RunInstancesResponse;
|
||||
import com.aliyun.tea.TeaException;
|
||||
import com.lyh.common.config.ClientService;
|
||||
import com.lyh.common.utils.UserUtil;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/*
|
||||
* 根据镜像创建并运行实例
|
||||
* */
|
||||
@Component
|
||||
public class CreateAndRunInstance {
|
||||
|
||||
|
||||
/*
|
||||
* @Author: LiuYunHu
|
||||
* @Date: 2024/4/13 9:28
|
||||
* @Description: 开始创建实例
|
||||
* @Param: []
|
||||
* @Return: java.lang.String
|
||||
**/
|
||||
public static String startCreate() throws Exception {
|
||||
// 地域Id
|
||||
String regionId = "cn-shanghai";
|
||||
// 镜像 ID,启动实例时选择的镜像资源。
|
||||
String imageId = "m-uf63t8i01vefni8piwen";
|
||||
// 实例规格
|
||||
String instanceType = "ecs.e-c1m1.large";
|
||||
// 新创建实例所属于的安全组 ID。
|
||||
String securityGroupId = "sg-uf6fvbhimn7xa54i36wc";
|
||||
// 虚拟交换机 ID。
|
||||
String vSwitchId = "vsw-uf62esxo4f5dfs43aqfu6";
|
||||
// 公网出带宽最大值,单位为 Mbit/s。取值范围:0~100。 默认值:0。
|
||||
Integer internetMaxBandwidthOut = com.aliyun.darabonbanumber.Client.parseInt("5");
|
||||
// 网络计费类型。取值范围:
|
||||
// PayByBandwidth: 按固定带宽计费。
|
||||
// PayByTraffic: 按使用流量计费。
|
||||
// 默认值:PayByTraffic。
|
||||
String internetChargeType = "PayByTraffic";
|
||||
// 系统盘大小
|
||||
String size = "20";
|
||||
// 系统盘的云盘种类
|
||||
String category = "cloud_essd_entry";
|
||||
// ECS实例的计费方式
|
||||
// PrePaid:包年包月
|
||||
// PostPaid:按量付费
|
||||
String instanceChargeType = "PostPaid";
|
||||
|
||||
|
||||
com.aliyun.ecs20140526.Client client = ClientService.createEcsClient(regionId);
|
||||
// 批量创建实例
|
||||
return CreateAndRunInstance.RunInstances(client, regionId, imageId, instanceType, securityGroupId, vSwitchId, internetMaxBandwidthOut, internetChargeType, size, category, instanceChargeType);
|
||||
}
|
||||
|
||||
/**
|
||||
* RunInstances 通过备选实例规格创建ECS实例最佳实践
|
||||
* 该场景中,在调用RunInstances创建ECS实例时判断是否发生库存不足等错误,如果发生错误,将调用DescribeRecommendInstanceType查询备选实例,然后通过备选实例规格重新创建ECS实例。
|
||||
*/
|
||||
public static String RunInstances(com.aliyun.ecs20140526.Client client, String regionId, String imageId, String instanceType, String securityGroupId, String vSwitchId, Integer internetMaxBandwidthOut, String internetChargeType, String size, String category, String instanceChargeType) throws Exception {
|
||||
RunInstancesRequest request1 = new RunInstancesRequest()
|
||||
.setRegionId(regionId)
|
||||
.setImageId(imageId)
|
||||
.setInstanceType(instanceType)
|
||||
.setSecurityGroupId(securityGroupId)
|
||||
.setVSwitchId(vSwitchId)
|
||||
.setInstanceName("自动创建的实例节点")
|
||||
.setDescription(new Date().toLocaleString() + " 创建的实例节点")
|
||||
.setInternetMaxBandwidthOut(internetMaxBandwidthOut)
|
||||
.setInternetChargeType(internetChargeType)
|
||||
.setInstanceChargeType(instanceChargeType)
|
||||
// 批量创建五台ECS实例,如果不设置该参数,默认创建一台ECS实例。
|
||||
// amount = 5,
|
||||
// 如果缺少库存可以接受的最低创建数量。
|
||||
// minAmount = 2,
|
||||
// 打开预检参数功能,不会实际创建ECS实例,只检查参数正确性、用户权限或者ECS库存等问题。
|
||||
// 实际情况下,设置了DryRun参数后,Amount必须为1,MinAmount必须为空,您可以根据实际需求修改代码。
|
||||
// .setDryRun(true)
|
||||
.setDryRun(false)
|
||||
.setSystemDisk(new RunInstancesRequest.RunInstancesRequestSystemDisk()
|
||||
.setSize(size)
|
||||
.setCategory(category));
|
||||
|
||||
|
||||
String result = "";
|
||||
try {
|
||||
com.aliyun.teaconsole.Client.log("--------------------批量创建实例开始--------------------");
|
||||
RunInstancesResponse responces = client.runInstances(request1);
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package com.lyh.openAPI.WorkCenter;
|
||||
|
||||
import com.aliyun.ecs20140526.Client;
|
||||
import com.aliyun.ecs20140526.models.DescribeInstancesRequest;
|
||||
import com.aliyun.ecs20140526.models.DescribeInstancesResponse;
|
||||
import com.aliyun.ecs20140526.models.DescribeInstancesResponseBody;
|
||||
import com.lyh.common.config.ClientService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @ProjectName: LoadCenter
|
||||
* @Author: LiuYunHu
|
||||
* @CreateTime: 2024/4/15
|
||||
* @Description: 获取指定区域的实例ID列表
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class GetInstanceIDList {
|
||||
public static List<String> getIDList() throws Exception {
|
||||
// // 1. 初始化配置
|
||||
// Config config = new Config();
|
||||
// // 您的AccessKey ID
|
||||
// config.accessKeyId = "LTAI5tEBb3cjLf8nRBJZmfD9";
|
||||
// // 您的AccessKey Secret
|
||||
// config.accessKeySecret = "Emzjl8SlqwlHP7xeFOrhwtXiJNfbkF";
|
||||
// //设置请求地址
|
||||
// config.endpoint = "ecs.aliyuncs.com";
|
||||
// // 设置连接超时为5000毫秒
|
||||
// config.connectTimeout = 5000;
|
||||
// // 设置读超时为5000毫秒
|
||||
// config.readTimeout = 5000;
|
||||
// // 2. 初始化客户端
|
||||
// com.aliyun.ecs20140526.Client client = new com.aliyun.ecs20140526.Client(config);
|
||||
|
||||
// 地域Id
|
||||
String addressId = "cn-shanghai";
|
||||
|
||||
Client client = ClientService.createEcsClient(addressId);
|
||||
java.util.List<String> regionIds = com.aliyun.darabonbastring.Client.split(addressId, ",", 50);
|
||||
|
||||
String regionId = regionIds.get(0);
|
||||
|
||||
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest()
|
||||
.setPageSize(100)
|
||||
.setRegionId(regionId);
|
||||
DescribeInstancesResponse resp = client.describeInstances(describeInstancesRequest);
|
||||
java.util.List<DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance> instances = resp.body.instances.instance;
|
||||
com.aliyun.teaconsole.Client.log("" + regionId + " 下 ECS 实例列表:");
|
||||
|
||||
//存储结果的List
|
||||
ArrayList<String> result = new ArrayList<>();
|
||||
for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : instances) {
|
||||
com.aliyun.teaconsole.Client.log("主机名:" + instance.hostName + " 实例ID:" + instance.instanceId + " CPU:" + instance.cpu + " 内存:" + instance.memory + " MB 规格:" + instance.instanceType + " 系统:" + instance.OSType + "(" + instance.OSName + ") 状态:" + instance.status + "");
|
||||
|
||||
result.add(instance.instanceId);
|
||||
}
|
||||
|
||||
log.info(result);
|
||||
|
||||
//返回结果
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,98 @@
|
|||
package com.lyh.openAPI.WorkCenter;
|
||||
|
||||
|
||||
import com.aliyun.ecs20140526.models.DescribeInstancesResponse;
|
||||
import com.aliyun.ecs20140526.models.DescribeInstancesResponseBody;
|
||||
import com.aliyun.tea.TeaException;
|
||||
import com.lyh.common.config.ClientService;
|
||||
import com.lyh.common.utils.UserUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @ProjectName: LoadCenter
|
||||
* @Author: LiuYunHu
|
||||
* @CreateTime: 2024/4/13
|
||||
* @Description: 查询一台或多台ECS实例的详细信息
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class GetInstanceProperties {
|
||||
static int i = 1;
|
||||
|
||||
/*
|
||||
* @Author: LiuYunHu
|
||||
* @Date: 2024/4/13 11:01
|
||||
* @Description: 实例ID 用 英文逗号拼接
|
||||
* @Param: [instanceIds] i-uf6chlqotgoc9h173alu
|
||||
* @Return: void
|
||||
**/
|
||||
public static List<DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance> startGet(String instanceIds) throws Exception {
|
||||
// 地域Id
|
||||
String regionId = "cn-shanghai";
|
||||
|
||||
com.aliyun.ecs20140526.Client client = ClientService.createEcsClient(regionId);
|
||||
com.aliyun.ecs20140526.models.DescribeInstancesRequest describeInstancesRequest = new com.aliyun.ecs20140526.models.DescribeInstancesRequest()
|
||||
.setRegionId(regionId)
|
||||
.setInstanceName("*")
|
||||
.setInstanceIds(com.aliyun.teautil.Common.toJSONString(com.aliyun.darabonbastring.Client.split(instanceIds, ",", 50)))
|
||||
.setPageSize(10);
|
||||
|
||||
|
||||
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||
|
||||
//初始化返回值
|
||||
List<DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance> instance = null;
|
||||
|
||||
try {
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
DescribeInstancesResponse describeInstancesResponse = client.describeInstancesWithOptions(describeInstancesRequest, runtime);
|
||||
DescribeInstancesResponseBody body = describeInstancesResponse.getBody();
|
||||
DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstances instances = body.getInstances();
|
||||
|
||||
//能返回的值
|
||||
instance = instances.getInstance();
|
||||
instance.forEach(item -> {
|
||||
log.info("实例{" + i + "}的ID:" + item.getInstanceId());
|
||||
log.info("名称:" + item.getInstanceName());
|
||||
log.info("地域ID:" + item.getRegionId());
|
||||
log.info("状态:" + item.getStatus());
|
||||
log.info("类型:" + item.getInstanceType());
|
||||
log.info("CPU核心数:" + item.getCpu());
|
||||
log.info("内存大小:" + item.getMemory() + "MB");
|
||||
log.info("磁盘大小:" + item.getLocalStorageCapacity() + "G");
|
||||
log.info("操作系统:" + item.getOSName());
|
||||
log.info("网络类型:" + item.getInstanceNetworkType());
|
||||
log.info("公网出带宽值:" + item.getInternetMaxBandwidthOut() + "Mbit/s");
|
||||
log.info("公网入带宽值:" + item.getInternetMaxBandwidthIn() + "Mbit/s");
|
||||
log.info("公网IP:" + UserUtil.removeBrackets(item.getPublicIpAddress().getIpAddress().toString()));
|
||||
log.info("私网IP:" + UserUtil.removeBrackets(item.getVpcAttributes().getPrivateIpAddress().ipAddress.toString()));
|
||||
log.info("专有网络VPCID:" + item.getVpcAttributes().getVpcId());
|
||||
log.info("安全组ID:" + UserUtil.removeBrackets(item.getSecurityGroupIds().getSecurityGroupId().toString()));
|
||||
log.info("创建时间:" + item.getCreationTime());
|
||||
log.info("到期时间:" + item.getExpiredTime());
|
||||
log.info("是否可以回收:" + (item.getRecyclable() ? "是" : "否") + "\n\n");
|
||||
log.info("---------------------");
|
||||
i++;
|
||||
});
|
||||
} catch (TeaException error) {
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
log.error(error.getMessage());
|
||||
// 诊断地址
|
||||
log.error(error.getData().get("Recommend").toString());
|
||||
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||
} catch (Exception _error) {
|
||||
TeaException error = new TeaException(_error.getMessage(), _error);
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
log.error(error.getMessage());
|
||||
// 诊断地址
|
||||
log.error(error.getData().get("Recommend").toString());
|
||||
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
// This file is auto-generated, don't edit it. Thanks.
|
||||
package com.lyh.openAPI.WorkCenter;
|
||||
|
||||
import com.aliyun.ecs20140526.models.*;
|
||||
import com.lyh.common.config.ClientService;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @ProjectName: five-groups-couplet
|
||||
* @Author: LiuYunHu
|
||||
* @CreateTime: 2024/4/12
|
||||
* @Description: 释放实例
|
||||
*/
|
||||
|
||||
@Component
|
||||
public class ReleaseInstance {
|
||||
public static DescribeInstancesResponse DescribeInstances(com.aliyun.ecs20140526.Client client, String regionId, String instanceIds, String instanceName) throws Exception {
|
||||
DescribeInstancesRequest req = new DescribeInstancesRequest()
|
||||
.setRegionId(regionId)
|
||||
.setInstanceName(instanceName);
|
||||
if (!com.aliyun.teautil.Common.empty(instanceIds)) {
|
||||
req.instanceIds = com.aliyun.teautil.Common.toJSONString(com.aliyun.darabonbastring.Client.split(instanceIds, ",", 50));
|
||||
}
|
||||
|
||||
DescribeInstancesResponse resp = client.describeInstances(req);
|
||||
com.aliyun.teaconsole.Client.log("--------------------查询需要删除的实例--------------------");
|
||||
return resp;
|
||||
}
|
||||
|
||||
public static void ModifyInstanceAttribute(com.aliyun.ecs20140526.Client client, String instatnceId) throws Exception {
|
||||
ModifyInstanceAttributeRequest req = new ModifyInstanceAttributeRequest()
|
||||
.setInstanceId(instatnceId)
|
||||
.setDeletionProtection(false);
|
||||
client.modifyInstanceAttribute(req);
|
||||
com.aliyun.teaconsole.Client.log("--------------------" + instatnceId + "释放保护取消成功--------------------");
|
||||
}
|
||||
|
||||
public static void DeleteInstances(com.aliyun.ecs20140526.Client client, String regionId, String instanceIds, String force) throws Exception {
|
||||
DeleteInstancesRequest req = new DeleteInstancesRequest()
|
||||
.setRegionId(regionId)
|
||||
.setInstanceId(com.aliyun.darabonbastring.Client.split(instanceIds, ",", 50))
|
||||
.setForce(com.aliyun.teautil.Common.equalString(force, "true"));
|
||||
DeleteInstancesResponse resp = client.deleteInstances(req);
|
||||
com.aliyun.teaconsole.Client.log("--------------------实例释放成功--------------------");
|
||||
com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(com.aliyun.teautil.Common.toMap(resp)));
|
||||
}
|
||||
|
||||
/*
|
||||
* @Author: LiuYunHu
|
||||
* @Date: 2024/4/13 11:02
|
||||
* @Description: 多个实例ID,用英文逗号分隔
|
||||
* @Param: [instanceIds] i-uf6chlqotgoc9h173alu
|
||||
* @Return: void
|
||||
**/
|
||||
public static void startRelease(String instanceIds) throws Exception {
|
||||
// 区域ID
|
||||
String regionId = "cn-shanghai";
|
||||
// 实例名称,支持使用通配符*进行模糊搜索
|
||||
String instanceName = "*";
|
||||
// 强制删除有删除保护的机器
|
||||
String deleteProtected = "true";
|
||||
// 强制删除运行中的机器
|
||||
String force = "true";
|
||||
com.aliyun.ecs20140526.Client client = ClientService.createEcsClient(regionId);
|
||||
if (com.aliyun.teautil.Common.equalString(deleteProtected, "true")) {
|
||||
DescribeInstancesResponse describeInstancesResp = ReleaseInstance.DescribeInstances(client, regionId, instanceIds, instanceName);
|
||||
instanceIds = "";
|
||||
for (DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance instance : describeInstancesResp.body.instances.instance) {
|
||||
instanceIds = "" + instance.instanceId + "," + instanceIds + "";
|
||||
if (instance.deletionProtection) {
|
||||
ReleaseInstance.ModifyInstanceAttribute(client, instance.instanceId);
|
||||
}
|
||||
|
||||
}
|
||||
instanceIds = com.aliyun.darabonbastring.Client.subString(instanceIds, 0, -1);
|
||||
}
|
||||
|
||||
if (com.aliyun.teautil.Common.empty(instanceIds)) {
|
||||
com.aliyun.teaconsole.Client.log("--------------------无有效实例可删除--------------------");
|
||||
return;
|
||||
}
|
||||
|
||||
ReleaseInstance.DeleteInstances(client, regionId, instanceIds, force);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.lyh.service;
|
||||
|
||||
import com.lyh.common.domain.resp.Result;
|
||||
|
||||
/**
|
||||
* @ProjectName: LoadCenter
|
||||
* @Author: LiuYunHu
|
||||
* @CreateTime: 2024/4/15
|
||||
* @Description:
|
||||
*/
|
||||
|
||||
public interface LoadCenterService {
|
||||
Result<String> getAssignedServer();
|
||||
|
||||
}
|
|
@ -0,0 +1,157 @@
|
|||
package com.lyh.service.impl;
|
||||
|
||||
import com.aliyun.ecs20140526.models.DescribeInstancesResponseBody;
|
||||
import com.lyh.common.domain.IpAndLoadCount;
|
||||
import com.lyh.common.domain.IpAndWeight;
|
||||
import com.lyh.common.domain.resp.Result;
|
||||
import com.lyh.common.redis.service.RedisService;
|
||||
import com.lyh.common.utils.UserUtil;
|
||||
import com.lyh.common.utils.mqtt.MqttUtil;
|
||||
import com.lyh.openAPI.WorkCenter.GetInstanceIDList;
|
||||
import com.lyh.openAPI.WorkCenter.GetInstanceProperties;
|
||||
import com.lyh.service.LoadCenterService;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @ProjectName: LoadCenter
|
||||
* @Author: LiuYunHu
|
||||
* @CreateTime: 2024/4/15
|
||||
* @Description:
|
||||
*/
|
||||
@Service
|
||||
@Log4j2
|
||||
public class LoadCenterServiceImpl implements LoadCenterService {
|
||||
@Autowired
|
||||
private MqttUtil mqttUtil;
|
||||
|
||||
@Autowired
|
||||
private RedisService redis;
|
||||
|
||||
/*
|
||||
* @Author: LiuYunHu
|
||||
* @Date: 2024/4/15 21:49
|
||||
* @Description: 获取 分配的一个服务器IP
|
||||
* @Param: []
|
||||
* @Return: com.lyh.common.domain.resp.Result<java.lang.String>
|
||||
**/
|
||||
@Override
|
||||
public Result<String> getAssignedServer() {
|
||||
|
||||
//存IP的List
|
||||
ArrayList<String> ipList = new ArrayList<>();
|
||||
|
||||
//获取上海区的实例ID列表
|
||||
try {
|
||||
List<String> idList = GetInstanceIDList.getIDList();
|
||||
idList.forEach(id -> {
|
||||
//调用方法,获取对应ID实例的IP
|
||||
try {
|
||||
List<DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance> result = GetInstanceProperties.startGet(id);
|
||||
|
||||
//获取集合第一个的属性
|
||||
DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance response = result.get(0);
|
||||
|
||||
//获取添加的实例的公网ip
|
||||
String instanceIp = UserUtil.removeBrackets(response.getPublicIpAddress().getIpAddress().toString());
|
||||
ipList.add(instanceIp);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
|
||||
log.info(ipList);
|
||||
|
||||
//将IP列表存入redis
|
||||
redis.deleteObject("服务器列表:");
|
||||
redis.setCacheList("服务器列表:", ipList);
|
||||
|
||||
//存各个 服务器的负载量
|
||||
ArrayList<IpAndLoadCount> ipAndLoadCounts = new ArrayList<>();
|
||||
|
||||
|
||||
ipList.forEach(ip -> {
|
||||
|
||||
//拿到IP后,获取各个IP的负载量
|
||||
int fetchLoad = mqttUtil.getFetchLoad(ip);
|
||||
ipAndLoadCounts.add(new IpAndLoadCount(ip, fetchLoad));
|
||||
});
|
||||
|
||||
|
||||
//求出空负载的总量
|
||||
Integer emptyLoadCount = 0;
|
||||
for (IpAndLoadCount ipAndLoadCount : ipAndLoadCounts) {
|
||||
|
||||
//假设使用2/8原则
|
||||
emptyLoadCount += (80 - ipAndLoadCount.getLoadCount());
|
||||
}
|
||||
|
||||
|
||||
//存储IP和对应的权重
|
||||
ArrayList<IpAndWeight> ipAndWeights = new ArrayList<>();
|
||||
for (IpAndLoadCount ipAndLoadCount : ipAndLoadCounts) {
|
||||
IpAndWeight ipAndWeight = new IpAndWeight(ipAndLoadCount.getIp(), (80 - ipAndLoadCount.getLoadCount()) * 100 / emptyLoadCount);
|
||||
|
||||
ipAndWeights.add(ipAndWeight);
|
||||
}
|
||||
|
||||
log.info(ipAndWeights);//[IpAndWeight(nodeIp=47.102.158.233, weight=55), IpAndWeight(nodeIp=47.102.123.209, weight=44)]
|
||||
|
||||
|
||||
//*******************************************以下为改老师的代码
|
||||
ArrayList<String> loadNodeList = new ArrayList<>();
|
||||
|
||||
int sum = ipAndWeights.stream().mapToInt(IpAndWeight::getWeight).sum();
|
||||
if (sum < 100) {
|
||||
List<IpAndWeight> list = ipAndWeights.stream().sorted(((o1, o2) -> o2.getWeight() - o1.getWeight())).toList();
|
||||
|
||||
//给权重高的节点 权重再加一个
|
||||
int countWeight = 0;
|
||||
for (int i = sum; i < 100; i++) {
|
||||
IpAndWeight ipAndWeight = list.get(countWeight++ % list.size());
|
||||
ipAndWeight.setWeight(ipAndWeight.getWeight() + 1);
|
||||
}
|
||||
}
|
||||
|
||||
whFor:
|
||||
while (true) {
|
||||
for (IpAndWeight ipAndWeight : ipAndWeights) {
|
||||
Integer weight = ipAndWeight.getWeight();
|
||||
if (weight > 0) {
|
||||
loadNodeList.add(ipAndWeight.getNodeIp());
|
||||
}
|
||||
ipAndWeight.setWeight(weight - 1);
|
||||
}
|
||||
|
||||
int sum1 = ipAndWeights.stream()
|
||||
.mapToInt(IpAndWeight::getWeight)
|
||||
.sum();
|
||||
if (sum1 <= 0) {
|
||||
break whFor;
|
||||
}
|
||||
}
|
||||
|
||||
//节点IP列表存入缓存
|
||||
redis.deleteObject("work:node:gateway");
|
||||
redis.setCacheList("work:node:gateway", loadNodeList);
|
||||
|
||||
|
||||
|
||||
//获取缓存里最后一个IP进行返回
|
||||
//最后一个IP进行返回
|
||||
String result = loadNodeList.get(loadNodeList.size() - 1);
|
||||
|
||||
return Result.success(result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
com.lyh.common.redis.configure.RedisConfig
|
||||
com.lyh.common.redis.service.RedisService
|
|
@ -0,0 +1,13 @@
|
|||
server:
|
||||
port: 9958
|
||||
|
||||
|
||||
spring:
|
||||
redis:
|
||||
# host: 39.103.132.68
|
||||
host: 127.0.0.1
|
||||
port: 6379
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
,--,
|
||||
,---.'| ,--,
|
||||
| | : ,--.'|
|
||||
: : | ,--, ,---, ,--, | :
|
||||
| ' : ,--.'| ,--, /_ ./| ,--, ,---, ,---.'| : ' ,--,
|
||||
; ; ' | |, ,'_ /| ,---, | ' : ,'_ /| ,-+-. / || | : _' | ,'_ /|
|
||||
' | |__ `--'_ .--. | | :/___/ \. : | .--. | | : ,--.'|' |: : |.' | .--. | | :
|
||||
| | :.'|,' ,'| ,'_ /| : . | . \ \ ,' ','_ /| : . || | ,"' || ' ' ; :,'_ /| : . |
|
||||
' : ;' | | | ' | | . . \ ; ` ,'| ' | | . .| | / | |' | .'. || ' | | . .
|
||||
| | ./ | | : | | ' | | | \ \ ' | | ' | | || | | | || | : | '| | ' | | |
|
||||
; : ; ' : |__ : | : ; ; | ' \ | : | : ; ; || | | |/ ' : | : ;: | : ; ; |
|
||||
| ,/ | | '.'|' : `--' \ \ ; ; ' : `--' \ | |--' | | ' ,/ ' : `--' \
|
||||
'---' ; : ;: , .-./ : \ \: , .-./ |/ ; : ;--' : , .-./
|
||||
| , / `--`----' \ ' ; `--`----' '---' | ,/ `--`----'
|
||||
---`-' `--` '---'
|
|
@ -0,0 +1,172 @@
|
|||
import com.lyh.LoadCenterApplication;
|
||||
import com.lyh.common.redis.service.RedisService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
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.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
/**
|
||||
* @ProjectName: LoadCenter
|
||||
* @Author: LiuYunHu
|
||||
* @CreateTime: 2024/4/14
|
||||
* @Description: 负载测试
|
||||
*/
|
||||
|
||||
@SpringBootTest(classes = LoadCenterApplication.class)
|
||||
@Log4j2
|
||||
public class LoadTest {
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
@Test
|
||||
public void load() {
|
||||
//初始化序列
|
||||
redisService.setCacheObject("cursor", 0);
|
||||
//初始化缓存
|
||||
redisService.deleteObject("work:node:gateway");
|
||||
|
||||
//模拟三台节点服务器 的ID(或IP) 权重值
|
||||
ArrayList<WorkGatewayNode> nodeIdList = new ArrayList<>() {{
|
||||
add(new WorkGatewayNode("node-A", 8));
|
||||
add(new WorkGatewayNode("node-B", 12));
|
||||
add(new WorkGatewayNode("node-C", 2));
|
||||
add(new WorkGatewayNode("node-D", 39));
|
||||
add(new WorkGatewayNode("node-E", 39));
|
||||
}};
|
||||
//100
|
||||
ArrayList<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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
whFor:
|
||||
while (true) {
|
||||
for (WorkGatewayNode workGatewayNode : nodeIdList) {
|
||||
int weight = workGatewayNode.getWeight();
|
||||
if (weight > 0) {
|
||||
loadNodeList.add(
|
||||
workGatewayNode.getNodeId()
|
||||
);
|
||||
workGatewayNode.setWeight(weight - 1);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int sum = nodeIdList.stream()
|
||||
.mapToInt(WorkGatewayNode::getWeight)
|
||||
.sum();
|
||||
if (sum <= 0) {
|
||||
break whFor;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
redisService.setCacheList("work:node:gateway", loadNodeList);
|
||||
|
||||
CountDownLatch countDownLatch = new CountDownLatch(3000);
|
||||
new Thread(() -> {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
Long cursor = redisService.increment("cursor", 1L);//游标
|
||||
String nodeId = redisService.getCacheList("work:node:gateway", cursor % 100);
|
||||
log.info(Thread.currentThread().getName() + ":" + cursor + "---" + nodeId);
|
||||
|
||||
//每次调通,进行统计
|
||||
stiNode.sti(nodeId);
|
||||
|
||||
countDownLatch.countDown();
|
||||
}
|
||||
|
||||
}).start();
|
||||
new Thread(() -> {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
Long cursor = redisService.increment("cursor", 1L);//游标
|
||||
String nodeId = redisService.getCacheList("work:node:gateway", cursor % 100);
|
||||
log.info(Thread.currentThread().getName() + ":" + cursor + "---" + nodeId);
|
||||
|
||||
//每次调通,进行统计
|
||||
stiNode.sti(nodeId);
|
||||
|
||||
countDownLatch.countDown();
|
||||
}
|
||||
}).start();
|
||||
new Thread(() -> {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
Long cursor = redisService.increment("cursor", 1L);//游标
|
||||
String nodeId = redisService.getCacheList("work:node:gateway", cursor % 100);
|
||||
log.info(Thread.currentThread().getName() + ":" + cursor + "---" + nodeId);
|
||||
|
||||
//每次调通,进行统计
|
||||
stiNode.sti(nodeId);
|
||||
|
||||
countDownLatch.countDown();
|
||||
}
|
||||
}).start();
|
||||
|
||||
try {
|
||||
countDownLatch.await();
|
||||
stiNode.show();
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
// for (int i = 0; i < 1000; i++) {
|
||||
// Long cursor = redisService.increment("cursor", 1L);//游标
|
||||
// String nodeId = redisService.getCacheList("work:node:gateway", cursor % 100);
|
||||
// log.info(cursor + "---" + nodeId);
|
||||
// }
|
||||
|
||||
log.info(nodeIdList);
|
||||
|
||||
log.info(loadNodeList);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Log4j2
|
||||
class stiNode {
|
||||
private static Map<String, Integer> stiNodeMap = new HashMap<>();
|
||||
|
||||
public synchronized static void sti(String nodeId) {
|
||||
Integer stiCount = stiNodeMap.getOrDefault(nodeId, 0);
|
||||
stiNodeMap.put(nodeId, stiCount + 1);
|
||||
}
|
||||
|
||||
public static void show() {
|
||||
stiNodeMap.forEach((key,val)->{
|
||||
log.info(key+"----"+val);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
class WorkGatewayNode {
|
||||
private String nodeId;
|
||||
private int weight;
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
import com.aliyun.ecs20140526.models.DescribeInstancesResponseBody;
|
||||
import com.lyh.LoadCenterApplication;
|
||||
import com.lyh.common.redis.service.RedisService;
|
||||
import com.lyh.common.utils.UserUtil;
|
||||
import com.lyh.openAPI.WorkCenter.GetInstanceProperties;
|
||||
import com.lyh.openAPI.WorkCenter.ReleaseInstance;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @ProjectName: LoadCenter
|
||||
* @Author: LiuYunHu
|
||||
* @CreateTime: 2024/4/13
|
||||
* @Description: 测试
|
||||
*/
|
||||
|
||||
//@SpringBootTest
|
||||
@SpringBootTest(classes = LoadCenterApplication.class)
|
||||
@Slf4j
|
||||
public class Test {
|
||||
@Autowired
|
||||
private RedisService redisService;
|
||||
|
||||
|
||||
@org.junit.jupiter.api.Test
|
||||
public void load() throws Exception {
|
||||
// //返回实例的ID
|
||||
// String instanceId = CreateAndRunInstance.startCreate();
|
||||
//
|
||||
// Thread.sleep(2000);
|
||||
|
||||
String instanceId = "i-uf6chlqotgoc9h173alu";
|
||||
|
||||
//通过实例ID获取实例的详细属性
|
||||
List<DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance> describeInstancesResponseBodyInstancesInstances = GetInstanceProperties.startGet(instanceId);
|
||||
|
||||
//获取集合第一个的属性
|
||||
DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance response = describeInstancesResponseBodyInstancesInstances.get(0);
|
||||
|
||||
//获取添加的实例的公网ip
|
||||
String instanceIp = UserUtil.removeBrackets(response.getPublicIpAddress().getIpAddress().toString());
|
||||
System.out.println("-----------------------");
|
||||
log.info(instanceIp);
|
||||
System.out.println("-----------------------");
|
||||
|
||||
}
|
||||
|
||||
|
||||
//获取指定节点的负载数
|
||||
|
||||
|
||||
/*
|
||||
* 释放实例
|
||||
* */
|
||||
@org.junit.jupiter.api.Test
|
||||
public void aaa() throws Exception {
|
||||
ReleaseInstance.startRelease("i-uf660jbffjg2mrrs4h91,i-uf6dac58ernhfgpjn6nn");
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue