diff --git a/config/nacos/dk-job.yml b/config/nacos/dk-job.yml index 6af577b..09f6fdc 100644 --- a/config/nacos/dk-job.yml +++ b/config/nacos/dk-job.yml @@ -6,7 +6,7 @@ spring: seata: true datasource: # 主库数据源 - master: + sample: type: ${spring.datasource.type} driver-class-name: com.mysql.cj.jdbc.Driver url: ${datasource.job.url} diff --git a/config/nacos/dk-snailjob-server.yml b/config/nacos/dk-snailjob-server.yml index cc9c1ed..6c5c2f5 100644 --- a/config/nacos/dk-snailjob-server.yml +++ b/config/nacos/dk-snailjob-server.yml @@ -13,6 +13,7 @@ spring: idle-timeout: 600000 max-lifetime: 900000 keepaliveTime: 30000 + cloud: nacos: discovery: diff --git a/dk-common/common-redis/src/main/java/org/dromara/common/redis/config/RedisConfigurationTemplate.java b/dk-common/common-redis/src/main/java/org/dromara/common/redis/config/RedisConfigurationTemplate.java new file mode 100644 index 0000000..d88e005 --- /dev/null +++ b/dk-common/common-redis/src/main/java/org/dromara/common/redis/config/RedisConfigurationTemplate.java @@ -0,0 +1,63 @@ +package org.dromara.common.redis.config; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.databind.MapperFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.PropertyNamingStrategy; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; +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.repository.configuration.EnableRedisRepositories; +import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +/** + * @author sean + * @version 1.0 + * @date 2022/4/19 + */ +@Configuration +@EnableRedisRepositories +public class RedisConfigurationTemplate { + + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory factory) { + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setConnectionFactory(factory); + + ObjectMapper objectMapper = new ObjectMapper(); + JavaTimeModule timeModule = new JavaTimeModule(); + timeModule.addDeserializer(LocalDateTime.class, + new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); + timeModule.addSerializer(LocalDateTime.class, + new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); + objectMapper.disable(MapperFeature.IGNORE_DUPLICATE_MODULE_REGISTRATIONS); + objectMapper.registerModules(timeModule); + objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(), + ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY); + + objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); + objectMapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); + objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + + + StringRedisSerializer serializer = new StringRedisSerializer(); + redisTemplate.setKeySerializer(serializer); + redisTemplate.setHashKeySerializer(serializer); + + GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer(objectMapper); + redisTemplate.setValueSerializer(jsonRedisSerializer); + redisTemplate.setHashValueSerializer(jsonRedisSerializer); + redisTemplate.afterPropertiesSet(); + return redisTemplate; + + } +} diff --git a/dk-common/common-redis/src/main/java/org/dromara/common/redis/utils/RedisOpsUtils.java b/dk-common/common-redis/src/main/java/org/dromara/common/redis/utils/RedisOpsUtils.java deleted file mode 100644 index 1be68dc..0000000 --- a/dk-common/common-redis/src/main/java/org/dromara/common/redis/utils/RedisOpsUtils.java +++ /dev/null @@ -1,263 +0,0 @@ -package org.dromara.common.redis.utils; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.stereotype.Component; -import org.springframework.util.CollectionUtils; - -import java.util.List; -import java.util.Set; -import java.util.concurrent.TimeUnit; - -/** - * @author sean - * @version 1.0 - * @date 2022/4/19 - */ -@Component -public class RedisOpsUtils { - - private static RedisTemplate redisTemplate; - - @Autowired - public void setRedisTemplate(RedisTemplate redisTemplate) { - RedisOpsUtils.redisTemplate = redisTemplate; - } - - /** - * HSET - * @param key - * @param field - * @param value - */ - public static void hashSet(String key, String field, Object value) { - redisTemplate.opsForHash().put(key, field, value); - } - - /** - * HGET - * @param key - * @param field - * @return - */ - public static Object hashGet(String key, String field) { - return redisTemplate.opsForHash().get(key, field); - } - - /** - * HKEYS - * @param key - * @return - */ - public static Set hashKeys(String key) { - return redisTemplate.opsForHash().keys(key); - } - - /** - * HEXISTS - * @param key - * @param field - * @return - */ - public static boolean hashCheck(String key, String field) { - return redisTemplate.opsForHash().hasKey(key, field); - } - - /** - * HDEL - * @param key - * @param fields - * @return - */ - public static boolean hashDel(String key, Object[] fields) { - return redisTemplate.opsForHash().delete(key, fields) > 0; - } - - /** - * HLEN - * @param key - * @return - */ - public static long hashLen(String key) { - return redisTemplate.opsForHash().size(key); - } - - /** - * EXPIRE - * @param key - * @param timeout - * @return - */ - public static boolean expireKey(String key, long timeout) { - return redisTemplate.expire(key, timeout, TimeUnit.SECONDS); - } - - /** - * SET - * @param key - * @param value - */ - public static void set(String key, Object value) { - redisTemplate.opsForValue().set(key, value); - } - - /** - * GET - * @param key - * @return - */ - public static Object get(String key) { - return redisTemplate.opsForValue().get(key); - } - - /** - * SETEX - * @param key - * @param value - * @param expire - */ - public static void setWithExpire(String key, Object value, long expire) { - redisTemplate.opsForValue().set(key, value, expire, TimeUnit.SECONDS); - } - - /** - * TTL - * @param key - * @return - */ - public static long getExpire(String key) { - return redisTemplate.getExpire(key, TimeUnit.SECONDS); - } - - /** - * EXISTS - * @param key - * @return - */ - public static boolean checkExist(String key) { - return redisTemplate.hasKey(key); - } - - /** - * DEL - * @param key - * @return - */ - public static boolean del(String key) { - return RedisOpsUtils.checkExist(key) && redisTemplate.delete(key); - } - - /** - * KEYS - * @param pattern - * @return - */ - public static Set getAllKeys(String pattern) { - return redisTemplate.keys(pattern); - } - - /** - * RPUSH - * @param key - * @param value - */ - public static void listRPush(String key, Object... value) { - if (value.length == 0) { - return; - } - for (Object val : value) { - redisTemplate.opsForList().rightPush(key, val); - } - } - - /** - * LRANGE - * @param key - * @param start - * @param end - * @return - */ - public static List listGet(String key, long start, long end) { - return redisTemplate.opsForList().range(key, start, end); - } - - /** - * LRANGE - * @param key - * @return - */ - public static List listGetAll(String key) { - return redisTemplate.opsForList().range(key, 0, -1); - } - - /** - * LLen - * @param key - * @return - */ - public static Long listLen(String key) { - return redisTemplate.opsForList().size(key); - } - - /** - * ZADD - * @param key - * @param value - * @param score - */ - public static Boolean zAdd(String key, Object value, double score) { - return redisTemplate.opsForZSet().add(key, value, score); - } - - /** - * ZREM - * @param key - * @param value - */ - public static Boolean zRemove(String key, Object... value) { - return redisTemplate.opsForZSet().remove(key, value) > 0; - } - /** - * ZRANGE - * @param key - * @param start - * @param end - * @return - */ - public static Set zRange(String key, long start, long end) { - return redisTemplate.opsForZSet().range(key, start, end); - } - - /** - * ZRANGE - * @param key - * @return - */ - public static Object zGetMin(String key) { - Set objects = zRange(key, 0, 0); - if (CollectionUtils.isEmpty(objects)) { - return null; - } - return objects.iterator().next(); - } - - /** - * ZSCORE - * @param key - * @param value - * @return - */ - public static Double zScore(String key, Object value) { - return redisTemplate.opsForZSet().score(key, value); - } - - /** - * ZINCRBY - * @param key - * @param value - * @param delta - */ - public static Double zIncrement(String key, Object value, double delta) { - return redisTemplate.opsForZSet().incrementScore(key, value, delta); - } -} diff --git a/dk-modules/sample/pom.xml b/dk-modules/sample/pom.xml index 5368199..93c7b26 100644 --- a/dk-modules/sample/pom.xml +++ b/dk-modules/sample/pom.xml @@ -18,10 +18,6 @@ org.dromara common-nacos - - org.dromara - common-core - org.dromara common-cloudsdk diff --git a/dk-modules/sample/src/main/java/org/dromara/sample/CloudApiSampleApplication.java b/dk-modules/sample/src/main/java/org/dromara/sample/CloudApiSampleApplication.java index a58256a..fef6810 100644 --- a/dk-modules/sample/src/main/java/org/dromara/sample/CloudApiSampleApplication.java +++ b/dk-modules/sample/src/main/java/org/dromara/sample/CloudApiSampleApplication.java @@ -4,10 +4,11 @@ import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; -@MapperScan("org.dromara.sample.*.mapper") @EnableDubbo -@SpringBootApplication(scanBasePackages = {"org.dromara.common.sdk.**","org.dromara.sample.**"}) +@SpringBootApplication +@ComponentScan("org.dromara") public class CloudApiSampleApplication { public static void main(String[] args) { diff --git a/dk-modules/sample/src/main/java/org/dromara/sample/component/ApplicationBootInitial.java b/dk-modules/sample/src/main/java/org/dromara/sample/component/ApplicationBootInitial.java index 9480f33..4b2af3d 100644 --- a/dk-modules/sample/src/main/java/org/dromara/sample/component/ApplicationBootInitial.java +++ b/dk-modules/sample/src/main/java/org/dromara/sample/component/ApplicationBootInitial.java @@ -27,6 +27,8 @@ public class ApplicationBootInitial implements CommandLineRunner { @Autowired private IDeviceRedisService deviceRedisService; + @Autowired + private RedisOpsUtils redisOpsUtils; /** * Subscribe to the devices that exist in the redis when the program starts, @@ -38,7 +40,7 @@ public class ApplicationBootInitial implements CommandLineRunner { public void run(String... args) throws Exception { int start = RedisConst.DEVICE_ONLINE_PREFIX.length(); - RedisOpsUtils.getAllKeys(RedisConst.DEVICE_ONLINE_PREFIX + "*") + redisOpsUtils.getAllKeys(RedisConst.DEVICE_ONLINE_PREFIX + "*") .stream() .map(key -> key.substring(start)) .map(deviceRedisService::getDeviceOnline) diff --git a/dk-modules/sample/src/main/java/org/dromara/sample/component/GlobalExceptionHandler.java b/dk-modules/sample/src/main/java/org/dromara/sample/component/GlobalExceptioSampleHandler.java similarity index 96% rename from dk-modules/sample/src/main/java/org/dromara/sample/component/GlobalExceptionHandler.java rename to dk-modules/sample/src/main/java/org/dromara/sample/component/GlobalExceptioSampleHandler.java index 5451562..fbfc502 100644 --- a/dk-modules/sample/src/main/java/org/dromara/sample/component/GlobalExceptionHandler.java +++ b/dk-modules/sample/src/main/java/org/dromara/sample/component/GlobalExceptioSampleHandler.java @@ -14,7 +14,7 @@ import org.springframework.web.bind.annotation.ResponseBody; */ @ControllerAdvice @ResponseBody -public class GlobalExceptionHandler { +public class GlobalExceptioSampleHandler { /** * Please do not return directly like this, there is a risk. diff --git a/dk-modules/sample/src/main/java/org/dromara/sample/component/GlobalScheduleService.java b/dk-modules/sample/src/main/java/org/dromara/sample/component/GlobalScheduleService.java index 2def2b5..1a07f5e 100644 --- a/dk-modules/sample/src/main/java/org/dromara/sample/component/GlobalScheduleService.java +++ b/dk-modules/sample/src/main/java/org/dromara/sample/component/GlobalScheduleService.java @@ -33,6 +33,9 @@ public class GlobalScheduleService { @Autowired private ObjectMapper mapper; + + @Autowired + private RedisOpsUtils redisOpsUtils; /** * Check the status of the devices every 30 seconds. It is recommended to use cache. */ @@ -40,10 +43,10 @@ public class GlobalScheduleService { private void deviceStatusListen() { int start = RedisConst.DEVICE_ONLINE_PREFIX.length(); - RedisOpsUtils.getAllKeys(RedisConst.DEVICE_ONLINE_PREFIX + "*").forEach(key -> { - long expire = RedisOpsUtils.getExpire(key); + redisOpsUtils.getAllKeys(RedisConst.DEVICE_ONLINE_PREFIX + "*").forEach(key -> { + long expire = redisOpsUtils.getExpire(key); if (expire <= 30) { - DeviceDTO device = (DeviceDTO) RedisOpsUtils.get(key); + DeviceDTO device = (DeviceDTO) redisOpsUtils.get(key); if (null == device) { return; } @@ -52,7 +55,7 @@ public class GlobalScheduleService { } else { deviceService.gatewayOffline(key.substring(start)); } - RedisOpsUtils.del(key); + redisOpsUtils.del(key); } }); diff --git a/dk-modules/sample/src/main/java/org/dromara/sample/manage/mapper/IDeviceDictionaryMapper.java b/dk-modules/sample/src/main/java/org/dromara/sample/manage/mapper/IDeviceDictionaryMapper.java index 1c04c7a..22e021d 100644 --- a/dk-modules/sample/src/main/java/org/dromara/sample/manage/mapper/IDeviceDictionaryMapper.java +++ b/dk-modules/sample/src/main/java/org/dromara/sample/manage/mapper/IDeviceDictionaryMapper.java @@ -1,5 +1,6 @@ package org.dromara.sample.manage.mapper; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.dromara.common.mybatis.core.mapper.BaseMapperPlus; import org.dromara.sample.manage.model.entity.DeviceDictionaryEntity; @@ -9,5 +10,5 @@ import org.dromara.sample.manage.model.entity.DeviceDictionaryEntity; * @date 2021/11/15 * @version 0.1 */ -public interface IDeviceDictionaryMapper extends BaseMapperPlus { +public interface IDeviceDictionaryMapper extends BaseMapper { } diff --git a/dk-modules/sample/src/main/java/org/dromara/sample/manage/mapper/IDeviceFirmwareMapper.java b/dk-modules/sample/src/main/java/org/dromara/sample/manage/mapper/IDeviceFirmwareMapper.java index ac3dd19..c4ea1f3 100644 --- a/dk-modules/sample/src/main/java/org/dromara/sample/manage/mapper/IDeviceFirmwareMapper.java +++ b/dk-modules/sample/src/main/java/org/dromara/sample/manage/mapper/IDeviceFirmwareMapper.java @@ -1,6 +1,7 @@ package org.dromara.sample.manage.mapper; import com.baomidou.mybatisplus.core.conditions.Wrapper; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.toolkit.Constants; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.dromara.common.mybatis.core.mapper.BaseMapperPlus; @@ -13,7 +14,7 @@ import org.apache.ibatis.annotations.Select; * @version 1.2 * @date 2022/8/16 */ -public interface IDeviceFirmwareMapper extends BaseMapperPlus { +public interface IDeviceFirmwareMapper extends BaseMapper { String sql = "