This commit is contained in:
liulu
2024-10-28 09:55:21 +08:00
parent 4c0524415c
commit e59811ced4
200 changed files with 18322 additions and 0 deletions
@@ -0,0 +1,188 @@
package com.sunyard.ssp.redis;
import com.alibaba.fastjson.JSONObject;
import com.sunyard.ssp.server.Config;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.util.HashSet;
import java.util.Set;
@Slf4j
@Configuration
public class JedisClusterConfig extends CachingConfigurerSupport {
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.cluster.nodes}")
private String redisNodes;
@Value("${spring.redis.isJq}")
private Boolean isJq;
@Value("${spring.redis.host}")
private String host2;
@Value("${spring.redis.port}")
private int port2;
private int connectionTimeout = 6000;
private int soTimeout = 2000;
private int maxAttempts = 10;
private int maxTotal = 800;
private int minIdle = 50;
private int maxIdle = 200;
private int maxWaitMillis = 3000;
/**
* JedisPoolConfig 连接池
* @return
*/
@Bean
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
// 最大空闲数
jedisPoolConfig.setMaxIdle(maxIdle);
// 连接池的最大数据库连接数
jedisPoolConfig.setMaxTotal(maxTotal);
jedisPoolConfig.setTestOnBorrow(false);
// 逐出连接的最小空闲时间 默认1800000毫秒(30分钟)
return jedisPoolConfig;
}
@Bean
public RedisClusterConfiguration redisClusterConfiguration(){
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
//Set<RedisNode> clusterNodes
String[] serverArray = redisNodes.split(",");
Set<RedisNode> nodes = new HashSet<RedisNode>();
for(String ipPort:serverArray){
String[] ipAndPort = ipPort.split(":");
nodes.add(new RedisNode(ipAndPort[0].trim(),Integer.valueOf(ipAndPort[1])));
}
redisClusterConfiguration.setClusterNodes(nodes);
return redisClusterConfiguration;
}
@Bean
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig, RedisClusterConfiguration redisClusterConfiguration){
log.info("jedisConnectionFactory isJq = {}", isJq);
log.info("jedisConnectionFactory host2 = {}", host2);
log.info("jedisConnectionFactory port2 = {}", port2);
log.info("jedisConnectionFactory password = {}", password);
log.info("jedisConnectionFactory redisNodes = {}", redisNodes);
//是否开启集群模式
if(isJq){
JedisConnectionFactory JedisConnectionFactory = new JedisConnectionFactory(redisClusterConfiguration,jedisPoolConfig);
JedisConnectionFactory.setPassword(password);
return JedisConnectionFactory;
}else{
JedisConnectionFactory JedisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig);
//连接池
JedisConnectionFactory.setPoolConfig(jedisPoolConfig);
//IP地址
JedisConnectionFactory.setHostName(host2);
//端口号
JedisConnectionFactory.setPort(port2);
//如果Redis设置有密码
JedisConnectionFactory.setPassword(password);
return JedisConnectionFactory;
}
}
// @Bean
// public JedisPool redisPoolFactory(){
// JSONObject node = Config.global.messenger.redisCluster.getJSONObject("node");
// String password = Config.global.messenger.redisCluster.getString("password");
// String host = node.getString("ip");
// Integer port = node.getInteger("port");
// //调试的时候的配置
// JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
// //连接池中的最大空闲连接
// jedisPoolConfig.setMaxTotal(10);
// jedisPoolConfig.setMinIdle(3);
// jedisPoolConfig.setMaxIdle(250);
// jedisPoolConfig.setMinIdle(100);
// jedisPoolConfig.setMaxWaitMillis(10 * 1000);
// // 是否启用pool的jmx管理功能, 默认true
// //jedisPoolConfig.setJmxEnabled(true);
// JedisPool jedisPool = new JedisPool(jedisPoolConfig,host,port,10000,password);
// return jedisPool;
// }
@Bean
public JedisPool redisPoolFactory(){
JSONObject node = Config.global.messenger.redisCluster.getJSONObject("node");
String password = Config.global.messenger.redisCluster.getString("password");
String host = node.getString("ip");
Integer port = node.getInteger("port");
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(10);
jedisPoolConfig.setMinIdle(3);
jedisPoolConfig.setMaxIdle(250);
jedisPoolConfig.setMinIdle(100);
jedisPoolConfig.setMaxWaitMillis(10 * 1000);
jedisPoolConfig.setTestWhileIdle(true);
jedisPoolConfig.setMinEvictableIdleTimeMillis(60000);
jedisPoolConfig.setTimeBetweenEvictionRunsMillis(30000);
jedisPoolConfig.setTestOnBorrow(true);
jedisPoolConfig.setTestOnReturn(true);
JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port, 10000, password);
return jedisPool;
}
@Bean
public JedisCluster jedisCluster() {
Set<HostAndPort> hostAndPorts = new HashSet<HostAndPort>();
String nodes[] = redisNodes.split(",");
for(String item : nodes){
String host = item.split(":")[0];
Integer port = Integer.valueOf(item.split(":")[1]);
hostAndPorts.add(new HostAndPort(host,port));
}
JedisCluster jedisCluster = null;
//if (!hostAndPorts.isEmpty() && isJq){
if (!hostAndPorts.isEmpty() ){
GenericObjectPoolConfig pool = new GenericObjectPoolConfig();
pool.setMaxTotal(maxTotal);
pool.setMinIdle(minIdle);
pool.setMaxIdle(maxIdle);
pool.setMaxWaitMillis(maxWaitMillis);
jedisCluster = new JedisCluster(hostAndPorts, connectionTimeout, soTimeout, maxAttempts, password, pool);
}
return jedisCluster;
}
}
@@ -0,0 +1,84 @@
package com.sunyard.ssp.redis;
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.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* @author:tsz
* @date:2020/3/6
* @description:自定义序列化器
* * 不定义的话默认采用的是 serializer.JdkSerializationRedisSerializer.serialize() 序列化为二进制字节码 存储在数据库中不直观
*/
@Configuration
@Slf4j
@EnableCaching
public class RedisConfig {
@Bean
MessageListenerAdapter messageListener() {
//abstract methods overwrite
return new MessageListenerAdapter((MessageListener) (message, pattern) -> {
System.out.println("Message received: " + message.toString());
});
}
@Bean
RedisMessageListenerContainer redisContainer(RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container
= new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.addMessageListener(messageListener(), topic());
return container;
}
@Bean
ChannelTopic topic() {
return new ChannelTopic("topic");
}
/* @Bean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}*/
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
// 使用 Jackson2JsonRedisSerialize 需要导入依赖 com.fasterxml.jackson.core jackson-databindz
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
// 第一个参数表示: 表示所有访问者都受到影响 包括 字段, getter / isGetter,setter,creator
// 第二个参数表示: 所有类型的访问修饰符都是可接受的,不论是公有还有私有表变量都会被序列化
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
// 设置 key,value 序列化规则
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setKeySerializer(new StringRedisSerializer());
// 开启事务支持
redisTemplate.setEnableTransactionSupport(false);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
@@ -0,0 +1,468 @@
package com.sunyard.ssp.redis;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import redis.clients.jedis.BinaryJedisPubSub;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* @author:tsz
* @date:2020/3/6
* @description: 操作任意类型的 Template
*/
@Component
@Slf4j
public class RedisObjectOperation {
@Autowired
private RedisTemplate<Object, Object> objectRedisTemplate;
@Value("${spring.redis.isJq}")
private Boolean isJq;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.cluster.nodes}")
private String redisNodes;
@Autowired
private JedisCluster jedisCluster;
@Autowired
private JedisPool jedisPool;
/***
* 操作对象
*/
public void ObjectSet(Object key, Object value) {
ValueOperations<Object, Object> valueOperations = objectRedisTemplate.opsForValue();
valueOperations.set(key, value);
}
/***
* 操作元素为对象列表
*/
public void ListSet(Object key, List<Object> values) {
ListOperations<Object, Object> listOperations = objectRedisTemplate.opsForList();
values.forEach(value -> listOperations.leftPush(key, value));
}
/**
* 对象存储到redis 的list 中
*
* @param key
* @param value
*/
public void ListSet(Object key, Object value) {
ListOperations<Object, Object> listOperations = objectRedisTemplate.opsForList();
listOperations.leftPush(key, value);
}
/**
* 移除list 中的数据
*
* @param key
* @param start
* @param end
*/
public void ListTrim(Object key, Long start, Long end) {
ListOperations<Object, Object> listOperations = objectRedisTemplate.opsForList();
listOperations.trim(key, start, end);
}
/**
* 获得list 对象
*
* @param key
* @param start
* @param end
* @return
*/
public List<Object> ListGet(Object key, Long start, Long end) {
ListOperations<Object, Object> listOperations = objectRedisTemplate.opsForList();
return listOperations.range(key, start, end);
}
/***
* 操作元素为对象集合
*/
public void SetSet(Object key, Set<Object> values) {
SetOperations<Object, Object> setOperations = objectRedisTemplate.opsForSet();
values.forEach(value -> setOperations.add(key, value));
}
/***
* 获取对象
*/
public Object ObjectGet(Object key) {
ValueOperations<Object, Object> valueOperations = objectRedisTemplate.opsForValue();
return valueOperations.get(key);
}
/***
* 列表弹出元素
*/
public Object ListLeftPop(Object key) {
ListOperations<Object, Object> listOperations = objectRedisTemplate.opsForList();
return listOperations.leftPop(key, 2, TimeUnit.SECONDS);
}
/***
* 集合弹出元素
*/
public Object SetPop(Object key) {
SetOperations<Object, Object> setOperations = objectRedisTemplate.opsForSet();
return setOperations.pop(key);
}
public void close() {
if (!isJq) {
if (null != jedisPool) {
try {
jedisPool.close();
} catch (Exception e) {
}
}
} else {
if (null != jedisCluster) {
try {
jedisCluster.close();
} catch (Exception e) {
}
}
jedisCluster = null;
}
}
public Set<String> keys(String pattern) {
Set<String> keys = new HashSet<>();
if (!isJq) {
Jedis jedis = jedisPool.getResource();
try {
jedis.auth(password);
keys = jedis.keys(pattern);
} finally {
if (jedis != null) {
jedis.close();
}
//close();
}
} else {
try {
//获取所有连接池节点
//Map<String, JedisPool> nodes = jedisCluster.getClusterNodes();
String nodes[] = redisNodes.split(",");
for(String node : nodes){
String host = node.split(":")[0];
Integer port = Integer.valueOf(node.split(":")[1]);
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(1000);
jedisPoolConfig.setBlockWhenExhausted(false);
jedisPoolConfig.setJmxEnabled(true);
log.debug("从【{}】获取keys", node);
JedisPool pool = new JedisPool(jedisPoolConfig,host,port,10000,password);
Jedis jedis = null;
try {
jedis = pool.getResource();
keys.addAll(jedis.keys(pattern));
} catch (Exception e) {
log.error("获取key异常", e);
} finally {
log.info("关闭连接");
//一定要关闭连接!
if (jedis != null) {
jedis.close();
}
}
}
//遍历所有连接池,逐个进行模糊查询
/*for (String k : nodes.keySet()) {
log.debug("从【{}】获取keys", k);
JedisPool pool = nodes.get(k);
//获取Jedis对象,Jedis对象支持keys模糊查询
Jedis jedis = null;
try {
jedis = pool.getResource();
keys.addAll(jedis.keys(pattern));
} catch (Exception e) {
log.error("获取key异常", e);
} finally {
log.info("关闭连接");
//一定要关闭连接!
if (jedis != null)
jedis.close();
}
}*/
} catch (Exception e) {
log.error("获取key异常", e);
}
}
return keys;
}
public String get(String pattern) {
String keys;
if (!isJq) {
Jedis jedis = jedisPool.getResource();
try {
jedis.auth(password);
keys = jedis.get(pattern);
} finally {
if (jedis != null) {
jedis.close();
}
//close();
}
} else {
keys = jedisCluster.get(pattern);
}
return keys;
}
public void set(String key, String value) {
if (!isJq) {
Jedis jedis = jedisPool.getResource();
try {
jedis.auth(password);
jedis.set(key, value);
} finally {
if (jedis != null) {
jedis.close();
}
//close();
}
} else {
jedisCluster.set(key, value);
}
}
public void delete(Set<String> keys) {
if (!isJq) {
Jedis jedis = jedisPool.getResource();
try {
jedis.auth(password);
for (String key : keys) {
jedis.del(key);
}
} finally {
if (jedis != null) {
jedis.close();
}
//close();
}
} else {
for (String key : keys) {
jedisCluster.del(key);
}
}
}
public void subscribe(BinaryJedisPubSub jedisPubSub, final byte[]... channels) {
if (!isJq) {
Jedis jedis = jedisPool.getResource();
try {
jedis.auth(password);
jedis.subscribe(jedisPubSub, channels);
} finally {
if (jedis != null) {
jedis.close();
}
//close();
}
} else {
jedisCluster.subscribe(jedisPubSub, channels);
}
}
public Long zrem(final String key, final String... members) {
if (!isJq) {
Jedis jedis = jedisPool.getResource();
try {
jedis.auth(password);
return jedis.zrem(key, members);
} finally {
if (jedis != null) {
jedis.close();
//close();
}
}
} else {
return jedisCluster.zrem(key, members);
}
}
public Long zremrangeByScore(final byte[] key, final double min, final double max) {
if (!isJq) {
Jedis jedis = jedisPool.getResource();
try {
jedis.auth(password);
return jedis.zremrangeByScore(key, min, max);
} finally {
if (jedis != null) {
jedis.close();
//close();
}
}
} else {
return jedisCluster.zremrangeByScore(key, min, max);
}
}
public Long zremrangeByScore(final String key, final double min, final double max) {
if (!isJq) {
Jedis jedis = jedisPool.getResource();
try {
jedis.auth(password);
return jedis.zremrangeByScore(key, min, max);
} finally {
if (jedis != null) {
jedis.close();
//close();
}
}
} else {
return jedisCluster.zremrangeByScore(key, min, max);
}
}
public Set<String> zrangeByScore(String key, double min, double max) {
if (!isJq) {
Jedis jedis = jedisPool.getResource();
try {
jedis.auth(password);
return jedis.zrangeByScore(key, min, max);
} finally {
if (jedis != null) {
jedis.close();
//close();
}
}
} else {
return jedisCluster.zrangeByScore(key, min, max);
}
}
public Long zadd(final String key, final double score, final String member) {
if (!isJq) {
Jedis jedis = jedisPool.getResource();
jedis.auth(password);
return jedis.zadd(key, score, member);
} else {
return jedisCluster.zadd(key, score, member);
}
}
public Long zadd(final byte[] key, final double score, final byte[] member) {
if (!isJq) {
Jedis jedis = jedisPool.getResource();
jedis.auth(password);
return jedis.zadd(key, score, member);
} else {
return jedisCluster.zadd(key, score, member);
}
}
public Set<String> zrange(final String key, final long start, final long stop) {
Set<String> zrange;
if (!isJq) {
Jedis jedis = jedisPool.getResource();
try {
jedis.auth(password);
zrange = jedis.zrange(key, start, stop);
} finally {
if (jedis != null) {
jedis.close();
//close();
}
}
} else {
zrange = jedisCluster.zrange(key, start, stop);
}
return zrange;
}
public Long sadd(final String key, final String... members) {
if (!isJq) {
Jedis jedis = jedisPool.getResource();
jedis.auth(password);
return jedis.sadd(key, members);
} else {
return jedisCluster.sadd(key, members);
}
}
public Long del(final String key) {
if (!isJq) {
Jedis jedis = jedisPool.getResource();
try {
jedis.auth(password);
return jedis.del(key);
} finally {
if (jedis != null) {
jedis.close();
//close();
}
}
} else {
return jedisCluster.del(key);
}
}
public Long publish(final byte[] channel, final byte[] message) {
Long publish = null;
if (!isJq) {
Jedis jedis = jedisPool.getResource();
log.info("jedis publish[{}]", jedis);
try {
jedis.auth(password);
publish = jedis.publish(channel, message);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (jedis != null) {
jedis.close();
//close();
}
}
} else {
publish = jedisCluster.publish(channel, message);
}
return publish;
}
}
@@ -0,0 +1,72 @@
package com.sunyard.ssp.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* @author:tsz
* @date:2020/3/6
* @description: redis String 基本操作
*/
@Component
public class RedisOperation {
@Autowired
private StringRedisTemplate redisTemplate;
/***
* 操作普通字符串
*/
public void StringSet(String key, String value) {
ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
valueOperations.set(key, value);
}
/***
* 操作列表
*/
public void ListSet(String key, List<String> values) {
ListOperations<String, String> listOperations = redisTemplate.opsForList();
values.forEach(value -> listOperations.leftPush(key, value));
}
/***
* 操作集合
*/
public void SetSet(String key, Set<String> values) {
SetOperations<String, String> setOperations = redisTemplate.opsForSet();
values.forEach(value -> setOperations.add(key, value));
}
/***
* 获取字符串
*/
public String StringGet(String key) {
ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
return valueOperations.get(key);
}
/***
* 列表弹出元素
*/
public String ListLeftPop(String key) {
ListOperations<String, String> listOperations = redisTemplate.opsForList();
return listOperations.leftPop(key, 2, TimeUnit.SECONDS);
}
/***
* 集合弹出元素
*/
public String SetPop(String key) {
SetOperations<String, String> setOperations = redisTemplate.opsForSet();
return setOperations.pop(key);
}
}