|
@@ -0,0 +1,73 @@
|
|
|
|
|
+package cn.com.ctop.job.bytedance.utils;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+import org.redisson.Redisson;
|
|
|
|
|
+import org.redisson.api.RedissonClient;
|
|
|
|
|
+import org.redisson.config.Config;
|
|
|
|
|
+import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
|
|
|
|
|
+import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
|
|
|
|
+import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
|
+import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
|
|
|
|
|
+import org.springframework.data.redis.serializer.StringRedisSerializer;
|
|
|
|
|
+
|
|
|
|
|
+@Configuration
|
|
|
|
|
+public class RedisConfig {
|
|
|
|
|
+ @Value("${spring.redis.host}")
|
|
|
|
|
+ private String host;
|
|
|
|
|
+
|
|
|
|
|
+ @Value("${spring.redis.port}")
|
|
|
|
|
+ private int port;
|
|
|
|
|
+
|
|
|
|
|
+ @Value("${spring.redis.database}")
|
|
|
|
|
+ private int database;
|
|
|
|
|
+
|
|
|
|
|
+ @Value("${spring.redis.password}")
|
|
|
|
|
+ private String password;
|
|
|
|
|
+
|
|
|
|
|
+ @Bean
|
|
|
|
|
+ public RedissonClient redissonClient() {
|
|
|
|
|
+ Config config = new Config();
|
|
|
|
|
+ config.useSingleServer().setAddress("redis://" + host + ":" + port).setDatabase(database).setPassword(password);
|
|
|
|
|
+ return Redisson.create(config);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Bean(name = "redisTemplateDefault")
|
|
|
|
|
+ public RedisTemplate<String, Object> redisTemplateDefault() {
|
|
|
|
|
+ return createTemplate(database);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Bean(name = "redisTemplate_db10")
|
|
|
|
|
+ public RedisTemplate<String, Object> redisTemplate10() {
|
|
|
|
|
+ return createTemplate(10);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Bean(name = "redisTemplate_db2")
|
|
|
|
|
+ public RedisTemplate<String, Object> redisTemplate2() {
|
|
|
|
|
+ return createTemplate(2);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private RedisTemplate<String, Object> createTemplate(int dbIndex) {
|
|
|
|
|
+ RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
|
|
|
|
|
+ config.setHostName(host);
|
|
|
|
|
+ config.setPort(port);
|
|
|
|
|
+ config.setDatabase(dbIndex);
|
|
|
|
|
+ config.setPassword(password);
|
|
|
|
|
+
|
|
|
|
|
+ LettuceConnectionFactory factory = new LettuceConnectionFactory(config);
|
|
|
|
|
+ factory.afterPropertiesSet();
|
|
|
|
|
+
|
|
|
|
|
+ RedisTemplate<String, Object> template = new RedisTemplate<>();
|
|
|
|
|
+ template.setKeySerializer(new StringRedisSerializer());
|
|
|
|
|
+ template.setValueSerializer(new StringRedisSerializer());
|
|
|
|
|
+ template.setHashKeySerializer(new StringRedisSerializer());
|
|
|
|
|
+ template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
|
|
|
|
|
+ template.setConnectionFactory(factory);
|
|
|
|
|
+ template.afterPropertiesSet();
|
|
|
|
|
+ return template;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|