zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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<HostAndPort> 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);
    }
}