第6次优化

master
liuyunhu 2024-04-17 21:05:48 +08:00
parent ff7dfdc1e4
commit e70a315159
5 changed files with 36 additions and 26 deletions

View File

@ -2,6 +2,7 @@ package com.lyh;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* @ProjectName: Default (Template) Project
@ -11,8 +12,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
*/
@SpringBootApplication
//@EnableScheduling
//@EnableFeignClients
@EnableScheduling
public class LoadCenterApplication {
public static void main(String[] args) {
SpringApplication.run(LoadCenterApplication.class, args);

View File

@ -154,7 +154,7 @@ public class RedisService {
}
public <T> T getCacheList(final String key, Long index) {
return (T) redisTemplate.opsForList().index(key, index );
return (T) redisTemplate.opsForList().index(key, index);
}
/**

View File

@ -31,59 +31,59 @@ public class Result<T> implements Serializable {
private T data;
public static <T> Result<T> success () {
public static <T> Result<T> success() {
return restResult(null, SUCCESS, "操作成功");
}
public static <T> Result<T> success (T data) {
public static <T> Result<T> success(T data) {
return restResult(data, SUCCESS, "操作成功");
}
public static <T> Result<T> success (T data, String msg) {
public static <T> Result<T> success(T data, String msg) {
return restResult(data, SUCCESS, msg);
}
public static <T> Result<T> error () {
public static <T> Result<T> error() {
return restResult(null, FAIL, "操作失败");
}
public static <T> Result<T> error (String msg) {
public static <T> Result<T> error(String msg) {
return restResult(null, FAIL, msg);
}
public static <T> Result<T> error (T data) {
public static <T> Result<T> error(T data) {
return restResult(data, FAIL, "操作失败");
}
public static <T> Result<T> error (T data, String msg) {
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) {
public static <T> Result<T> error(int code, String msg) {
return restResult(null, code, msg);
}
public static <T> Result<T> warn () {
public static <T> Result<T> warn() {
return restResult(null, WARN, "操作失败");
}
public static <T> Result<T> warn (String msg) {
public static <T> Result<T> warn(String msg) {
return restResult(null, WARN, msg);
}
public static <T> Result<T> warn (T data) {
public static <T> Result<T> warn(T data) {
return restResult(data, WARN, "操作失败");
}
public static <T> Result<T> warn (T data, String msg) {
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) {
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) {
private static <T> Result<T> restResult(T data, int code, String msg) {
Result<T> apiResult = new Result<>();
apiResult.setCode(code);
apiResult.setData(data);
@ -91,11 +91,11 @@ public class Result<T> implements Serializable {
return apiResult;
}
public static <T> Boolean isError (Result<T> ret) {
public static <T> Boolean isError(Result<T> ret) {
return !isSuccess(ret);
}
public static <T> Boolean isSuccess (Result<T> ret) {
public static <T> Boolean isSuccess(Result<T> ret) {
return Result.SUCCESS == ret.getCode();
}
}

View File

@ -12,6 +12,7 @@ import com.lyh.domain.resp.Result;
import com.lyh.service.LoadCenterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
@ -36,6 +37,19 @@ public class LoadCenterServiceImpl implements LoadCenterService {
@Autowired
private AliYunEcsService aliYunEcsService;
/*
* @Author: LiuYunHu
* @Date: 2024/4/17 20:31
* @Description: IP
* @Param: []
* @Return: void
**/
@Scheduled(cron = "0/2 * * * * ?")
public void refreshEcsIPListCache() {
getEcsIPList();
}
/*
* @Author: LiuYunHu
* @Date: 2024/4/15 21:49
@ -46,10 +60,6 @@ public class LoadCenterServiceImpl implements LoadCenterService {
@Override
public Result<String> getAssignedServer() {
//刷新一下 实例公网IP列 缓存
this.getEcsIPList();
//从缓存中获取实例公网IP列表
if (redis.getCacheList("实例IP列表:").isEmpty()) {
throw new RuntimeException("实例IP列表为空");

View File

@ -43,7 +43,7 @@ public class Test {
//通过实例ID获取实例的详细属性 多个实例用英文逗号隔开
List<DescribeInstancesResponseBody.DescribeInstancesResponseBodyInstancesInstance> describeInstancesResponseBodyInstancesInstances = aliYunEcsService.queryInstancesInformation(instanceId);
describeInstancesResponseBodyInstancesInstances.forEach(item->{
describeInstancesResponseBodyInstancesInstances.forEach(item -> {
//循环打印所有的实例IP
log.info(UserUtil.removeBrackets(item.getPublicIpAddress().getIpAddress().toString()));
});
@ -54,7 +54,7 @@ public class Test {
//获取添加的实例的公网ip
String instanceIp = UserUtil.removeBrackets(response.getPublicIpAddress().getIpAddress().toString());
log.info("-----------------------");
log.info("第一个实例的公网IP为"+instanceIp);
log.info("第一个实例的公网IP为" + instanceIp);
log.info("-----------------------");
}
@ -72,7 +72,7 @@ public class Test {
**/
@org.junit.jupiter.api.Test
public void releaseInstances() throws Exception {
aliYunEcsService.releaseInstances("i-uf624nmh7j2nzlzxnd1u");
aliYunEcsService.releaseInstances("i-uf6dmmscd3b64m0jqs9f");
}
/*