From e70a315159dc4446d69ccc3d26c5a0aadd43b45f Mon Sep 17 00:00:00 2001 From: liuyunhu <3286117488@qq.com> Date: Wed, 17 Apr 2024 21:05:48 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC6=E6=AC=A1=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/lyh/LoadCenterApplication.java | 4 +-- .../common/redis/service/RedisService.java | 2 +- src/main/java/com/lyh/domain/resp/Result.java | 32 +++++++++---------- .../service/impl/LoadCenterServiceImpl.java | 18 ++++++++--- src/test/java/Test.java | 6 ++-- 5 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/lyh/LoadCenterApplication.java b/src/main/java/com/lyh/LoadCenterApplication.java index 3d7185a..b8003a1 100644 --- a/src/main/java/com/lyh/LoadCenterApplication.java +++ b/src/main/java/com/lyh/LoadCenterApplication.java @@ -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); diff --git a/src/main/java/com/lyh/common/redis/service/RedisService.java b/src/main/java/com/lyh/common/redis/service/RedisService.java index 0dfb554..c9f9e05 100644 --- a/src/main/java/com/lyh/common/redis/service/RedisService.java +++ b/src/main/java/com/lyh/common/redis/service/RedisService.java @@ -154,7 +154,7 @@ public class RedisService { } public T getCacheList(final String key, Long index) { - return (T) redisTemplate.opsForList().index(key, index ); + return (T) redisTemplate.opsForList().index(key, index); } /** diff --git a/src/main/java/com/lyh/domain/resp/Result.java b/src/main/java/com/lyh/domain/resp/Result.java index 193c1e2..b00b4b8 100644 --- a/src/main/java/com/lyh/domain/resp/Result.java +++ b/src/main/java/com/lyh/domain/resp/Result.java @@ -31,59 +31,59 @@ public class Result implements Serializable { private T data; - public static Result success () { + public static Result success() { return restResult(null, SUCCESS, "操作成功"); } - public static Result success (T data) { + public static Result success(T data) { return restResult(data, SUCCESS, "操作成功"); } - public static Result success (T data, String msg) { + public static Result success(T data, String msg) { return restResult(data, SUCCESS, msg); } - public static Result error () { + public static Result error() { return restResult(null, FAIL, "操作失败"); } - public static Result error (String msg) { + public static Result error(String msg) { return restResult(null, FAIL, msg); } - public static Result error (T data) { + public static Result error(T data) { return restResult(data, FAIL, "操作失败"); } - public static Result error (T data, String msg) { + public static Result error(T data, String msg) { return restResult(data, FAIL, msg); } - public static Result error (int code, String msg) { + public static Result error(int code, String msg) { return restResult(null, code, msg); } - public static Result warn () { + public static Result warn() { return restResult(null, WARN, "操作失败"); } - public static Result warn (String msg) { + public static Result warn(String msg) { return restResult(null, WARN, msg); } - public static Result warn (T data) { + public static Result warn(T data) { return restResult(data, WARN, "操作失败"); } - public static Result warn (T data, String msg) { + public static Result warn(T data, String msg) { return restResult(data, WARN, msg); } - public static Result warn (int code, String msg) { + public static Result warn(int code, String msg) { return restResult(null, code, msg); } - private static Result restResult (T data, int code, String msg) { + private static Result restResult(T data, int code, String msg) { Result apiResult = new Result<>(); apiResult.setCode(code); apiResult.setData(data); @@ -91,11 +91,11 @@ public class Result implements Serializable { return apiResult; } - public static Boolean isError (Result ret) { + public static Boolean isError(Result ret) { return !isSuccess(ret); } - public static Boolean isSuccess (Result ret) { + public static Boolean isSuccess(Result ret) { return Result.SUCCESS == ret.getCode(); } } diff --git a/src/main/java/com/lyh/service/impl/LoadCenterServiceImpl.java b/src/main/java/com/lyh/service/impl/LoadCenterServiceImpl.java index c9aa193..28a91c8 100644 --- a/src/main/java/com/lyh/service/impl/LoadCenterServiceImpl.java +++ b/src/main/java/com/lyh/service/impl/LoadCenterServiceImpl.java @@ -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 getAssignedServer() { - - //刷新一下 实例公网IP列 缓存 - this.getEcsIPList(); - //从缓存中获取实例公网IP列表 if (redis.getCacheList("实例IP列表:").isEmpty()) { throw new RuntimeException("实例IP列表为空!"); diff --git a/src/test/java/Test.java b/src/test/java/Test.java index 0baa530..eb60e62 100644 --- a/src/test/java/Test.java +++ b/src/test/java/Test.java @@ -43,7 +43,7 @@ public class Test { //通过实例ID获取实例的详细属性 多个实例用英文逗号隔开 List 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"); } /*