package cn.autoform.fw.config; import org.apache.commons.lang3.StringUtils; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.JedisCluster; import java.util.Arrays; import java.util.Set; import java.util.stream.Collectors; //@Configuration public class RedisConfig { @Value("${redis.nodes}") private String redisNodes; @Value("${redis.pass}") private String pass; @Bean public GenericObjectPoolConfig getGenericObjectPoolConfig() { GenericObjectPoolConfig gopc = new GenericObjectPoolConfig(); gopc.setMaxTotal(1000); gopc.setMaxIdle(10); gopc.setMaxWaitMillis(30000); gopc.setTestOnBorrow(true); return gopc; } @Bean public JedisCluster getJedisCluster() { Set hosts = Arrays.asList(redisNodes.split(",")) .stream() .map(node->new HostAndPort(node.split(":")[0], Integer.parseInt(node.split(":")[1]))) .collect(Collectors.toSet()); if(StringUtils.isNotBlank(pass)) { return new JedisCluster(hosts, 30000, 30000, 8, this.pass, getGenericObjectPoolConfig()); }else { return new JedisCluster(hosts, getGenericObjectPoolConfig()); } } public static void main(String[] args) { RedisConfig rc = new RedisConfig(); rc.redisNodes = "122.114.176.216:7000,122.114.176.216:7001,122.114.176.216:7002,122.114.176.216:7003,122.114.176.216:7004,122.114.176.216:7005"; JedisCluster j = rc.getJedisCluster(); // FormLockImpl f = new FormLockImpl(); // f.setJedisCluster(j); // f.setLockTime(1000L); // String formId = "123456"; // Integer dataRowNum = 10, tenantId = 13, companyId = 17; // System.out.println("获得锁:"+f.getLock(formId, dataRowNum, tenantId, companyId)); // System.out.println("获得锁:"+f.getLock(formId, dataRowNum, tenantId, companyId)); // System.out.println("获得锁:"+f.getLock(formId, dataRowNum, tenantId, companyId)); // String key; // System.out.println("获得锁钥匙:"+ (key = f.getKey(formId, dataRowNum, tenantId, companyId, "1299849792120990"))); // System.out.println("获得锁钥匙:"+ f.getKey(formId, dataRowNum, tenantId, companyId, "7487724823554911")); // System.out.println("获得锁数量:"+ f.getLockSize(formId, dataRowNum, tenantId, companyId)); // System.out.println("解锁:"); // f.unLock(formId, dataRowNum, tenantId, companyId, "1299849792120990", key); } }