|
|
@@ -0,0 +1,106 @@
|
|
|
+package com.poyee.config;
|
|
|
+
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import org.redisson.Redisson;
|
|
|
+import org.redisson.api.RedissonClient;
|
|
|
+import org.redisson.config.Config;
|
|
|
+import org.redisson.config.SentinelServersConfig;
|
|
|
+import org.redisson.config.SingleServerConfig;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.cache.annotation.EnableCaching;
|
|
|
+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.*;
|
|
|
+import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
|
|
+import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
|
|
+import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Redis配置
|
|
|
+ *
|
|
|
+ * @author zheng
|
|
|
+ */
|
|
|
+@Configuration
|
|
|
+@EnableCaching
|
|
|
+public class RedisConfig {
|
|
|
+ @Value("${spring.redis.host:127.0.0.1}")
|
|
|
+ private String host;
|
|
|
+ @Value("${spring.redis.port:6379}")
|
|
|
+ private String port;
|
|
|
+ @Value("${spring.redis.password}")
|
|
|
+ private String password;
|
|
|
+ @Value("${spring.redis.sentinel.nodes:127.0.0.1:6379}")
|
|
|
+ private String nodes;
|
|
|
+ @Value("${spring.redis.sentinel.master:master}")
|
|
|
+ private String master;
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
|
|
|
+ RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
|
|
|
+ redisTemplate.setKeySerializer(new StringRedisSerializer());
|
|
|
+ redisTemplate.setHashKeySerializer(new StringRedisSerializer());
|
|
|
+ redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
|
|
|
+ redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
|
|
|
+ // 开启事务
|
|
|
+ //redisTemplate.setEnableTransactionSupport(true);
|
|
|
+ redisTemplate.setConnectionFactory(factory);
|
|
|
+ return redisTemplate;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
|
|
|
+ return redisTemplate.opsForHash();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public ValueOperations<String, String> valueOperations(RedisTemplate<String, String> redisTemplate) {
|
|
|
+ return redisTemplate.opsForValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
|
|
|
+ return redisTemplate.opsForList();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
|
|
|
+ return redisTemplate.opsForSet();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
|
|
|
+ return redisTemplate.opsForZSet();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean(name = "redissonClient")
|
|
|
+ public RedissonClient redissonClientSingle() throws IOException {
|
|
|
+ Config config = new Config();
|
|
|
+ if (!"127.0.0.1:6379".equals(nodes) && !"master".equals(master)) {
|
|
|
+ SentinelServersConfig sentinelServers = config.useSentinelServers();
|
|
|
+ sentinelServers.addSentinelAddress("redis://" + nodes).setMasterName(master);
|
|
|
+ if (!StrUtil.isBlank(password)) {
|
|
|
+ sentinelServers.setPassword(password);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ SingleServerConfig singleServer = config.useSingleServer();
|
|
|
+ singleServer.setAddress("redis://" + host + ":" + port);
|
|
|
+ if (!StrUtil.isBlank((password))) {
|
|
|
+ singleServer.setPassword(password);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return Redisson.create(config);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public RedisMessageListenerContainer myRedisMessageListenerContainer(RedisConnectionFactory connectionFactory) {
|
|
|
+ RedisMessageListenerContainer container = new RedisMessageListenerContainer();
|
|
|
+ container.setConnectionFactory(connectionFactory);
|
|
|
+ return container;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|