spring-redis can't connect to remote host

2019-07-26 23:20发布

问题:

I have the following camel which polls Redis:

                from("timer://pollredis?fixedRate=true&period=5")
                    // poll redis
                    .setHeader("CamelRedis.Command", constant("LPOP"))
                    .setHeader("CamelRedis.Key", constant("shipments"))
                    // from redis, it is a producer, fetch with .to() !
                    .to(redisUri)
                    //
                    .choice().when(simple("${in.body} == null")).stop().otherwise()
                    //
                    .to("direct:internal").end();


    JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
    jedisConnectionFactory.afterPropertiesSet();

    RedisTemplate<?, ?> redisTemplate = new RedisTemplate();
    redisTemplate.setConnectionFactory(jedisConnectionFactory);
    redisTemplate.setDefaultSerializer(new StringRedisSerializer());
    redisTemplate.afterPropertiesSet();

    SimpleRegistry registry = new SimpleRegistry();
    registry.put("redisTemplate", redisTemplate);

And it works great. However, when I change the redisUri from

redisUri = spring-redis://localhost:6379?redisTemplate=#redisTemplate

to

redisUri = spring-redis://[stuff].xavwv8.ng.0001.euw1.cache.amazonaws.com:6379?redisTemplate=#redisTemplate

I get the following error:

11:42:49.754 INFO  Failed delivery for (MessageId: ID-ip-10-12-22-168-43293-1465299763162-0-1 on ExchangeId: ID-ip-10-12-22-168-43293-1465299763162-0-2). On delivery attempt: 0 caught: org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool | org.apache.camel.util.CamelLogger.log(CamelLogger.java:159) [Camel (camel-1) thread #0 - timer://pollredis] 

I have checked that I have access to the elasticache by telneting to it and by using redis-cli.

What is this Could not get a resource from the pool error that I'm getting when connecting to a remote host?

Both my local redis and the elasticache redis is running 2.8.24. Running camel 2.17.1.

回答1:

Here's how I got it working:

    JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
    jedisConnectionFactory.setHostName(redisHost);
    jedisConnectionFactory.setPort(Integer.parseInt(redisPort));
    jedisConnectionFactory.afterPropertiesSet();

    RedisTemplate<String, Object> redisTemplate = new RedisTemplate();
    redisTemplate.setConnectionFactory(jedisConnectionFactory);
    redisTemplate.setDefaultSerializer(new StringRedisSerializer());
    redisTemplate.afterPropertiesSet();

    SimpleRegistry registry = new SimpleRegistry();
    registry.put("redisTemplate", redisTemplate);

properties-file:

redisUri = spring-redis://notused?redisTemplate=#redisTemplate
redisHost = [stuff].xavwv8.ng.0001.euw1.cache.amazonaws.com
redisPort = 6379

Camel route same as before.

So apparently when you use a connection factory you can't set the host to use in the URI later.