public static RedisTemplate buildRedisTemplate(byte[] redisProperties){ JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory( RedisConfUtils.getClusterConfiguration( (RedisProperties) Serializer.INSTANCE.deserialize(redisProperties))); RedisTemplate<String, Long> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(jedisConnectionFactory); jedisConnectionFactory.afterPropertiesSet(); GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer(); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringRedisSerializer); redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer); redisTemplate.setHashKeySerializer(stringRedisSerializer); redisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer); redisTemplate.afterPropertiesSet(); return redisTemplate; }
@Bean @Primary public RedisProperties dataSourceProperties() { RedisProperties properties = new RedisProperties(); final String serviceId = environment.getProperty(REDIS_SERVICE_ID); if (loadBalancerClient != null && serviceId != null) { // also, we can choose redis sentinel system, or wait redis cluster (see https://jira.spring.io/browse/DATAREDIS-315) final ServiceInstance infos = loadBalancerClient.choose(serviceId); if (infos != null) { fillFields(infos, properties); LOGGER.info("registered redis from cloud {}:{}", infos.getHost(), infos.getPort()); } else { LOGGER.warn("there is no services with id {} in service discovery", serviceId); } } return properties; }
/** * Redis RelationshipDetector. * * @param redisProperties * Redis configuration properties (nullable). * @param redisConnectionFactories * Available RedisConnectionFactory instances (nullable). */ public RedisRelationshipDetector(RedisProperties redisProperties, List<RedisConnectionFactory> redisConnectionFactories) { this.redisProperties = redisProperties; this.connectionFactories = new ArrayList<>(); if (!CollectionUtils.isEmpty(redisConnectionFactories)) { this.connectionFactories.addAll(redisConnectionFactories); } }
@Test public void nullSentinelShouldReturnDefaultComponentName() { RedisConnectionFactory factory = Mockito.mock(RedisConnectionFactory.class); RedisRelationshipDetector detector = new RedisRelationshipDetector(new RedisProperties(), Arrays.asList(factory)); Set<Relationship> rels = new HashSet<>(); rels.add(Dependency.on(Component.of("default", ComponentType.REDIS))); Assertions.assertThat(detector.detect()).isEqualTo(rels); }
@Test public void shouldUseSentinelMasterName() { String name = "name"; RedisConnectionFactory factory = Mockito.mock(RedisConnectionFactory.class); RedisProperties props = new RedisProperties(); props.setSentinel(new Sentinel()); props.getSentinel().setMaster(name); RedisRelationshipDetector detector = new RedisRelationshipDetector(props, Arrays.asList(factory)); Set<Relationship> rels = new HashSet<>(); rels.add(Dependency.on(Component.of(name, ComponentType.REDIS))); Assertions.assertThat(detector.detect()).isEqualTo(rels); }
/** * {@link org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration#getClusterConfiguration} * @param redisProperties * @return */ public static RedisClusterConfiguration getClusterConfiguration(RedisProperties redisProperties) { if (redisProperties.getCluster() == null) { return null; } RedisProperties.Cluster clusterProperties = redisProperties.getCluster(); RedisClusterConfiguration config = new RedisClusterConfiguration( clusterProperties.getNodes()); if (clusterProperties.getMaxRedirects() != null) { config.setMaxRedirects(clusterProperties.getMaxRedirects()); } return config; }
@Bean @ConditionalOnMissingBean(RedisConnectionFactory.class) public RedisConnectionFactory redisConnectionFactory(RedisProperties redisProperties) { LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(); connectionFactory.setPort(redisProperties.getPort()); connectionFactory.setHostName(redisProperties.getHost()); connectionFactory.setPassword(redisProperties.getPassword()); connectionFactory.setShutdownTimeout(0); return connectionFactory; }
private void fillFields(ServiceInstance serviceInstance, RedisProperties properties) { properties.setHost(serviceInstance.getHost()); properties.setPort(serviceInstance.getPort()); }