zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
commit | author | age
a18bfa 1 package cn.autoform.fw.config;
Z 2
3 import org.apache.commons.lang3.StringUtils;
4 import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
5 import org.springframework.beans.factory.annotation.Value;
6 import org.springframework.context.annotation.Bean;
7 import redis.clients.jedis.HostAndPort;
8 import redis.clients.jedis.JedisCluster;
9
10 import java.util.Arrays;
11 import java.util.Set;
12 import java.util.stream.Collectors;
13
14 //@Configuration
15 public class RedisConfig {
16
17     @Value("${redis.nodes}")
18     private String redisNodes;
19     
20     @Value("${redis.pass}")
21     private String pass;
22     
23     @Bean
24     public GenericObjectPoolConfig getGenericObjectPoolConfig() {
25         GenericObjectPoolConfig gopc = new GenericObjectPoolConfig();
26         gopc.setMaxTotal(1000);
27         gopc.setMaxIdle(10);
28         gopc.setMaxWaitMillis(30000);
29         gopc.setTestOnBorrow(true);
30         return gopc;
31     }
32     
33     @Bean
34     public JedisCluster getJedisCluster() {
35         Set<HostAndPort> hosts = 
36         Arrays.asList(redisNodes.split(","))
37             .stream()
38             .map(node->new HostAndPort(node.split(":")[0], Integer.parseInt(node.split(":")[1])))
39             .collect(Collectors.toSet());
40         if(StringUtils.isNotBlank(pass)) {
41             return new JedisCluster(hosts, 30000, 30000, 8, this.pass, getGenericObjectPoolConfig());
42         }else {
43             return new JedisCluster(hosts, getGenericObjectPoolConfig());
44         }
45     }
46     public static void main(String[] args) {
47         RedisConfig rc = new RedisConfig();
48         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";
49         JedisCluster j = rc.getJedisCluster();
50 //        FormLockImpl f = new FormLockImpl();
51 //        f.setJedisCluster(j);
52 //        f.setLockTime(1000L);
53 //        String formId = "123456";
54 //        Integer dataRowNum = 10, tenantId = 13, companyId = 17;
55 //        System.out.println("获得锁:"+f.getLock(formId, dataRowNum, tenantId, companyId));
56 //        System.out.println("获得锁:"+f.getLock(formId, dataRowNum, tenantId, companyId));
57 //        System.out.println("获得锁:"+f.getLock(formId, dataRowNum, tenantId, companyId));
58 //        String key;
59 //        System.out.println("获得锁钥匙:"+ (key = f.getKey(formId, dataRowNum, tenantId, companyId, "1299849792120990")));
60 //        System.out.println("获得锁钥匙:"+ f.getKey(formId, dataRowNum, tenantId, companyId, "7487724823554911"));
61 //        System.out.println("获得锁数量:"+ f.getLockSize(formId, dataRowNum, tenantId, companyId));
62 //        System.out.println("解锁:"); 
63 //        f.unLock(formId, dataRowNum, tenantId, companyId, "1299849792120990", key);
64     }
65 }