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
66
67
68
69
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.core.env.MapPropertySource;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
 
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
 
 
public class TestRedis {
 
    public static void main(String... args) {
//        RedisTemplate<String, Object> redisTemplate = new TestRedis().redisTemplate();
////        // System.out.println(redisTemplate.opsForValue().setIfAbsent("1", 1000));
////        // System.out.println(redisTemplate.opsForValue().setIfAbsent("2", 2000));
//        // System.out.println(redisTemplate.opsForValue().get("budgetLock_13_17_954"));
//        // System.out.println(System.currentTimeMillis());
//        // System.out.println(((Long)redisTemplate.opsForValue().get("budgetLock_13_17_954")) > System.currentTimeMillis());
        // System.out.println(URLDecoder.decode("/ubp//ubp-api-control/process/rest/check?companyId=17&data=%7B%22tenantId%22%3A%22100000023%22%2C%22companyId%22%3A%2217%22%2C%22serviceId%22%3A2%7D&id=128989920180405694362503&loginSecurity=%7B%22accessId%22%3A%221289899%22%2C%22openId%22%3A%2268c6f3148506b3f1%22%2C%22appKey%22%3A%225e933001a7784cf2b7f5c11f9fb0047e%22%2C%22token%22%3A%2238312cb13caebe8b41ea10ca59d9b37f%22%7D&serviceId=2&sign=3a0fcfd99df89755d54e9accc3a18d83&tenantId=100000023"));
    }
 
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> rt = new RedisTemplate<>();
        rt.setValueSerializer(getJacksonRedisSerializer());
        rt.setHashKeySerializer(getJacksonRedisSerializer());
        rt.setConnectionFactory(getConnectionFactory());
        rt.afterPropertiesSet();
        return rt;
    }
 
    public JedisConnectionFactory getConnectionFactory() {
        JedisConnectionFactory factory = new JedisConnectionFactory(getClusterConfiguration());
//        if(ObjectUtil.notEmpty(environment.getProperty("spring.redis.cluster.password"))){
//            factory.setPassword(environment.getProperty("spring.redis.cluster.password"));
//        }
        factory.afterPropertiesSet();
        return factory;
    }
 
    public RedisClusterConfiguration getClusterConfiguration() {
        Map<String, Object> source = new HashMap<String, Object>();
        source.put("spring.redis.cluster.nodes", "10.4.32.148:7000,10.4.32.148:7001,10.4.32.148:7002,10.4.32.148:7003,10.4.32.148:7004,10.4.32.148:7005");
        source.put("spring.redis.cluster.max-redirects", 8);
        return new RedisClusterConfiguration(new MapPropertySource("RedisClusterConfiguration", source));
    }
 
    /**
     * 获得redis数据转换对象
     * @Title: getJacksonRedisSerializer
     * @param @return    设定文件
     * @return Jackson2JsonRedisSerializer<?>    返回类型
     * @throws
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public Jackson2JsonRedisSerializer<?> getJacksonRedisSerializer(){
        Jackson2JsonRedisSerializer<?> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        return jackson2JsonRedisSerializer;
    }
 
}