spring-redis can't connect to remote host

前提是你 提交于 2019-12-11 12:59:24

问题


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.



来源:https://stackoverflow.com/questions/37681528/spring-redis-cant-connect-to-remote-host

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!