|
|
@@ -0,0 +1,73 @@
|
|
|
+package com.weblux.launchredis.config;
|
|
|
+
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.context.annotation.Primary;
|
|
|
+import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|
|
+import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
|
|
+import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
+import redis.clients.jedis.JedisPoolConfig;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author: volta
|
|
|
+ * @Date: 2022/3/10 17:36
|
|
|
+ * @Description: 中台redis配置 类
|
|
|
+ */
|
|
|
+@Configuration
|
|
|
+public class GlobalRedisConfig {
|
|
|
+
|
|
|
+ @Value("${spring.data.redis.host}")
|
|
|
+ private String privateHost;
|
|
|
+ @Value("${spring.data.redis.port}")
|
|
|
+ private String privatePort;
|
|
|
+ @Value("${spring.data.redis.password}")
|
|
|
+ private String privatePassword;
|
|
|
+ @Value("${spring.data.redis.database}")
|
|
|
+ private Integer privateDB;
|
|
|
+
|
|
|
+ private static final int MAX_IDLE = 200; // 最大空闲连接数
|
|
|
+ private static final int MAX_TOTAL = 5024; // 最大连接数
|
|
|
+ private static final long MAX_WAIT_MILLIS = 10000; // 建立连接最长等待时间
|
|
|
+
|
|
|
+ @Primary
|
|
|
+ @Bean("stringRedisTemplate")
|
|
|
+ public StringRedisTemplate stringRedisTemplate() {
|
|
|
+ StringRedisTemplate privateStringRedisTemplate = new StringRedisTemplate();
|
|
|
+ privateStringRedisTemplate.setConnectionFactory(
|
|
|
+ connectionFactory(privateHost, Integer.parseInt(privatePort), privatePassword, MAX_IDLE, MAX_TOTAL, MAX_WAIT_MILLIS, privateDB));
|
|
|
+ return privateStringRedisTemplate;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 配置工厂
|
|
|
+ public RedisConnectionFactory connectionFactory(String host, int port, String password, int maxIdle,
|
|
|
+ int maxTotal, long maxWaitMillis, int index) {
|
|
|
+ JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
|
|
|
+ jedisConnectionFactory.setHostName(host);
|
|
|
+ jedisConnectionFactory.setPort(port);
|
|
|
+
|
|
|
+ if (!StrUtil.isEmpty(password)) {
|
|
|
+ jedisConnectionFactory.setPassword(password);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (index != 0) {
|
|
|
+ jedisConnectionFactory.setDatabase(index);
|
|
|
+ }
|
|
|
+
|
|
|
+ jedisConnectionFactory.setPoolConfig(poolConfig(maxIdle, maxTotal, maxWaitMillis, false));
|
|
|
+ jedisConnectionFactory.afterPropertiesSet();
|
|
|
+ return jedisConnectionFactory;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 连接池配置
|
|
|
+ public JedisPoolConfig poolConfig(int maxIdle, int maxTotal, long maxWaitMillis, boolean testOnBorrow) {
|
|
|
+ JedisPoolConfig poolConfig = new JedisPoolConfig();
|
|
|
+ poolConfig.setMaxIdle(maxIdle);
|
|
|
+ poolConfig.setMaxTotal(maxTotal);
|
|
|
+ poolConfig.setMaxWaitMillis(maxWaitMillis);
|
|
|
+ poolConfig.setTestOnBorrow(testOnBorrow);
|
|
|
+ return poolConfig;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|