From 319121f1b561d379584f889d8ea3d3a8c1e1ced8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com> Date: Tue, 12 Dec 2023 22:18:32 +0800 Subject: [PATCH] =?UTF-8?q?fix:redis=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 14 ++++ .../springbootkwan/config/RedisConfig.java | 84 +++++++++---------- .../config/RedisTestConfig.java | 34 ++++++++ .../controller/RedisController.java | 27 ++++-- 4 files changed, 112 insertions(+), 47 deletions(-) create mode 100644 src/main/java/com/kwan/springbootkwan/config/RedisTestConfig.java diff --git a/pom.xml b/pom.xml index af40b07..d508d92 100644 --- a/pom.xml +++ b/pom.xml @@ -249,6 +249,20 @@ poi 4.1.2 + + + + + + + + + + + org.redisson + redisson + 3.17.2 + diff --git a/src/main/java/com/kwan/springbootkwan/config/RedisConfig.java b/src/main/java/com/kwan/springbootkwan/config/RedisConfig.java index f69421f..c50bcf4 100644 --- a/src/main/java/com/kwan/springbootkwan/config/RedisConfig.java +++ b/src/main/java/com/kwan/springbootkwan/config/RedisConfig.java @@ -1,42 +1,42 @@ -package com.kwan.springbootkwan.config; - -import com.fasterxml.jackson.annotation.JsonAutoDetect; -import com.fasterxml.jackson.annotation.PropertyAccessor; -import com.fasterxml.jackson.databind.ObjectMapper; -import lombok.extern.slf4j.Slf4j; -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.Jackson2JsonRedisSerializer; -import org.springframework.data.redis.serializer.StringRedisSerializer; - -@Slf4j -@Configuration -public class RedisConfig { - - @Bean - @SuppressWarnings("all") - public RedisTemplate redisTemplate(RedisConnectionFactory factory) { - log.info("==================自定义的redisTemplate实例化了==================="); - RedisTemplate template = new RedisTemplate(); - template.setConnectionFactory(factory); - Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); - ObjectMapper om = new ObjectMapper(); - om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); - om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); - jackson2JsonRedisSerializer.setObjectMapper(om); - StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); - // key采用String的序列化方式 - template.setKeySerializer(stringRedisSerializer); - // hash的key也采用String的序列化方式 - template.setHashKeySerializer(stringRedisSerializer); - // value序列化方式采用jackson - template.setValueSerializer(jackson2JsonRedisSerializer); - // hash的value序列化方式采用jackson - template.setHashValueSerializer(jackson2JsonRedisSerializer); - template.afterPropertiesSet(); - return template; - } -} - +//package com.kwan.springbootkwan.config; +// +//import com.fasterxml.jackson.annotation.JsonAutoDetect; +//import com.fasterxml.jackson.annotation.PropertyAccessor; +//import com.fasterxml.jackson.databind.ObjectMapper; +//import lombok.extern.slf4j.Slf4j; +//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.Jackson2JsonRedisSerializer; +//import org.springframework.data.redis.serializer.StringRedisSerializer; +// +//@Slf4j +//@Configuration +//public class RedisConfig { +// +// @Bean +// @SuppressWarnings("all") +// public RedisTemplate redisTemplate(RedisConnectionFactory factory) { +// log.info("==================自定义的redisTemplate实例化了==================="); +// RedisTemplate template = new RedisTemplate(); +// template.setConnectionFactory(factory); +// Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); +// ObjectMapper om = new ObjectMapper(); +// om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); +// om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); +// jackson2JsonRedisSerializer.setObjectMapper(om); +// StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); +// // key采用String的序列化方式 +// template.setKeySerializer(stringRedisSerializer); +// // hash的key也采用String的序列化方式 +// template.setHashKeySerializer(stringRedisSerializer); +// // value序列化方式采用jackson +// template.setValueSerializer(jackson2JsonRedisSerializer); +// // hash的value序列化方式采用jackson +// template.setHashValueSerializer(jackson2JsonRedisSerializer); +// template.afterPropertiesSet(); +// return template; +// } +//} +// diff --git a/src/main/java/com/kwan/springbootkwan/config/RedisTestConfig.java b/src/main/java/com/kwan/springbootkwan/config/RedisTestConfig.java new file mode 100644 index 0000000..a14706c --- /dev/null +++ b/src/main/java/com/kwan/springbootkwan/config/RedisTestConfig.java @@ -0,0 +1,34 @@ +package com.kwan.springbootkwan.config; + +import lombok.Data; +import org.redisson.Redisson; +import org.redisson.api.RedissonClient; +import org.redisson.config.Config; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + + +@Configuration +@ConfigurationProperties(prefix = "spring.redis") +@Data +public class RedisTestConfig { + + private String host; + private String port; + private String password; + + @Bean + public RedissonClient redissonClient() { + Config config = new Config(); + config.useSingleServer() + .setAddress(String.format("redis://%s:%s", host, port)) + .setPassword(password) + .setConnectionPoolSize(50) + .setIdleConnectionTimeout(10000) + .setConnectTimeout(3000) + .setTimeout(3000) + .setDatabase(5); + return Redisson.create(config); + } +} \ No newline at end of file diff --git a/src/main/java/com/kwan/springbootkwan/controller/RedisController.java b/src/main/java/com/kwan/springbootkwan/controller/RedisController.java index 0d93d72..7abe09e 100644 --- a/src/main/java/com/kwan/springbootkwan/controller/RedisController.java +++ b/src/main/java/com/kwan/springbootkwan/controller/RedisController.java @@ -4,13 +4,14 @@ import com.kwan.springbootkwan.entity.Result; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; +import org.redisson.api.RMap; +import org.redisson.api.RedissonClient; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; -import java.util.concurrent.TimeUnit; import static com.kwan.springbootkwan.constant.CommonConstant.PREFIX_REDIS_KEY; @@ -30,14 +31,30 @@ public class RedisController { @Resource private RedisTemplate redisTemplate; + @Resource + private RedissonClient redissonClient; @ApiOperation(value = "redis添加数据", notes = "redis添加数据") @GetMapping("/add") public Result add(String key, String value) { - //向对应的key值添加数据 - redisTemplate.opsForValue().set(PREFIX_REDIS_KEY + key, value); - //设置过期时间 - redisTemplate.expire(PREFIX_REDIS_KEY, 2000000, TimeUnit.SECONDS); +// //向对应的key值添加数据 +// redisTemplate.opsForValue().set(PREFIX_REDIS_KEY + key, value); +// //设置过期时间 +// redisTemplate.expire(PREFIX_REDIS_KEY, 2000000, TimeUnit.SECONDS); + + // 获取或创建一个 Redisson Map 对象,它类似于 Java 的 Map + RMap map = redissonClient.getMap("myMap"); + + // 存储值 + map.put("key1", "value1"); + map.put("key2", "value2"); + + // 检索值 + String value1 = map.get("key1"); + String value2 = map.get("key2"); + + System.out.println("Value of key1: " + value1); + System.out.println("Value of key2: " + value2); return Result.ok(); } -- GitLab