diff --git a/src/main/java/com/mobai/controller/FluxGetInfoController.java b/src/main/java/com/mobai/controller/FluxGetInfoController.java index 975568c..f8bda06 100644 --- a/src/main/java/com/mobai/controller/FluxGetInfoController.java +++ b/src/main/java/com/mobai/controller/FluxGetInfoController.java @@ -18,17 +18,17 @@ import java.util.List; */ @RestController @Log4j2 -@RequestMapping("fluxmq/") +@RequestMapping("/fluxmq") public class FluxGetInfoController { @Autowired private FluxGetInfoService fluxGetInfoService; - @GetMapping("/getInfo/") + @GetMapping("/getInfo") public Result getInfo(){ return fluxGetInfoService.getInfo(null); } - @GetMapping("getIps/") + @GetMapping("/getIps") public Result> getIps(){ return fluxGetInfoService.getIps(); } diff --git a/src/main/java/com/mobai/openApi/SelectInstances.java b/src/main/java/com/mobai/openApi/SelectInstances.java index 513bd23..f5269d5 100644 --- a/src/main/java/com/mobai/openApi/SelectInstances.java +++ b/src/main/java/com/mobai/openApi/SelectInstances.java @@ -38,10 +38,14 @@ public class SelectInstances { // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。 // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html。 com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config() - // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。 - .setAccessKeyId("LTAI5t7vsLXtqTJKve7JipnX") - // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。 - .setAccessKeySecret("AIyMaSTKQtsB1V5sVqlNAGbQzxgXnS"); + //自己的 +// // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。 +// .setAccessKeyId("LTAI5t7vsLXtqTJKve7JipnX") +// // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。 +// .setAccessKeySecret("AIyMaSTKQtsB1V5sVqlNAGbQzxgXnS"); + // 杜云飞 + .setAccessKeyId("LTAI5tPTk3MFkmCGBbnQgmrM") + .setAccessKeySecret("q7rLjxrI0SLBXlvNT4VmYcHCNCY2p6"); // Endpoint 请参考 https://api.aliyun.com/product/Ecs config.endpoint = "ecs.cn-zhangjiakou.aliyuncs.com"; return new com.aliyun.ecs20140526.Client(config); @@ -84,7 +88,7 @@ public class SelectInstances { } //1分钟 - @Scheduled(cron = "* 0/5 * * * ? ") + @Scheduled(cron = "0 0/5 * * * ? ") //10秒 // @Scheduled(cron = "0/10 * * * * ? ") public void saveIps() { diff --git a/src/main/java/com/mobai/service/impl/FluxGetInfoServiceImpl.java b/src/main/java/com/mobai/service/impl/FluxGetInfoServiceImpl.java index d05128c..5dcdcf3 100644 --- a/src/main/java/com/mobai/service/impl/FluxGetInfoServiceImpl.java +++ b/src/main/java/com/mobai/service/impl/FluxGetInfoServiceImpl.java @@ -6,6 +6,7 @@ import com.mobai.domain.*; import com.mobai.domain.flux.ApifoxModel; import com.mobai.openApi.SelectInstances; import com.mobai.service.FluxGetInfoService; +import lombok.Synchronized; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; @@ -19,6 +20,7 @@ import org.springframework.web.client.RestTemplate; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.locks.Lock; import java.util.stream.Collectors; /** @@ -80,29 +82,34 @@ public class FluxGetInfoServiceImpl implements FluxGetInfoService { // "timestamp": "11111" new Date().getMillis() // "username": "你好" Vin + timestamp // "nonce": "33" 随机 + log.warn("参数为:{}", req); // String string = redis.opsForList().range("fluxMq", 0, -1).get(0); // List mqtts = JSON.parseArray(string, MqttServerModel.class); // log.info("集合:{}",mqtts); // tcp://192.168.1.1:1883 - if (redis.hasKey("fluxMqIndex")) { - redis.opsForValue().increment("fluxMqIndex", 1); - } else { - redis.opsForValue().set("fluxMqIndex", 0 + ""); - } - int index = Integer.valueOf(redis.opsForValue().get("fluxMqIndex")); - log.info("下标:{}", index); + synchronized(this){ + if (redis.hasKey("fluxMqIndex")) { + redis.opsForValue().increment("fluxMqIndex", 1); + } else { + redis.opsForValue().set("fluxMqIndex", 0 + ""); + } + int index = Integer.valueOf(redis.opsForValue().get("fluxMqIndex")); + log.info("下标:{}", index); // List fluxmq = redis.opsForList().range("mqttIp", 0, 5); - String mqttIp = redis.opsForList().index("mqttIp", index); + String mqttIp = redis.opsForList().index("mqttIp", index); // log.info(fluxmq); - log.info(mqttIp); - MqttServerModel mqttServerModel = JSON.parseObject(mqttIp, MqttServerModel.class); - if (index + 1 == 100) { - redis.opsForValue().set("fluxMqIndex", 0 + ""); + log.info(mqttIp); + MqttServerModel mqttServerModel = JSON.parseObject(mqttIp, MqttServerModel.class); + if (index + 1 >= 80) { + redis.opsForValue().set("fluxMqIndex", 0 + ""); + } + log.info("已获取到对象:{}", mqttServerModel); + + return Result.success(mqttServerModel); } - log.info("已获取到对象:{}", mqttServerModel); - return Result.success(mqttServerModel); + } @Override diff --git a/src/main/java/com/mobai/util/RedisService.java b/src/main/java/com/mobai/util/RedisService.java index b8cf38c..58366ff 100644 --- a/src/main/java/com/mobai/util/RedisService.java +++ b/src/main/java/com/mobai/util/RedisService.java @@ -1,6 +1,7 @@ package com.mobai.util; import com.alibaba.fastjson2.JSON; +import lombok.extern.log4j.Log4j2; import org.apache.poi.ss.formula.functions.T; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; @@ -16,6 +17,7 @@ import java.util.concurrent.TimeUnit; * @author muyu **/ @SuppressWarnings(value = {"unchecked", "rawtypes"}) +@Log4j2 @Component @Lazy public class RedisService { @@ -270,7 +272,7 @@ public class RedisService { public void increment(String s,int i) { if (Boolean.TRUE.equals(stringRedisTemplate.hasKey(s))){ String o = stringRedisTemplate.opsForValue().get(s); - System.out.println(o); + log.info("当前游标::"+o); stringRedisTemplate.opsForValue().increment(s,i); }else { stringRedisTemplate.opsForValue().set(s,i+""); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 722e2c6..6fd72d1 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -2,7 +2,7 @@ server: port: 8081 spring: redis: - host: 175.24.138.82 + host: 127.0.0.1 port: 6379 password: rabbitmq: