'docker-compose with springboot and redis
I'm trying to connect a simple springboot app with redis using docker compose. However, I keep getting the below error.
java-service_1 | 2020-11-20 10:30:54.053 ERROR 1 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is 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] with root cause
java-service_1 |
java-service_1 | java.net.ConnectException: Connection refused (Connection refused)
java-service_1 | at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_252]
java-service_1 | at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_252]
java-service_1 | at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_252]
java-service_1 | at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_252]
java-service_1 | at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_252]
java-service_1 | at java.net.Socket.connect(Socket.java:607) ~[na:1.8.0_252]
java-service_1 | at redis.clients.jedis.DefaultJedisSocketFactory.createSocket(DefaultJedisSocketFactory.java:53) ~[jedis-3.3.0.jar!/:na]
java-service_1 | at redis.clients.jedis.Connection.connect(Connection.java:158) ~[jedis-3.3.0.jar!/:na]
java-service_1 | at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:109) ~[jedis-3.3.0.jar!/:na]
Config
@Bean
public RedisStandaloneConfiguration redisStandaloneConfiguration() {
System.out.println("connecting with redis");
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("redis", 6379);
return redisStandaloneConfiguration;
}
@Bean
public ClientOptions clientOptions() {
return ClientOptions.builder()
.disconnectedBehavior(ClientOptions.DisconnectedBehavior.REJECT_COMMANDS)
.autoReconnect(true)
.build();
}
@Bean
public RedisConnectionFactory connectionFactory(RedisStandaloneConfiguration redisStandaloneConfiguration) {
LettuceClientConfiguration configuration = LettuceClientConfiguration.builder()
.clientOptions(clientOptions()).build();
return new LettuceConnectionFactory(redisStandaloneConfiguration, configuration);
}
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
@Primary
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
StringRedisTemplate template = new StringRedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
docker-compose.yml
version: '3' services: redis-server: container_name: redis image: 'redis' ports: - "6379:6379" java-service: build: . links: - redis-server ports: - "8080:8080"
Solution 1:[1]
I think problem occurs because your application is not being able to connect to DB, because of
RedisStandaloneConfiguration redisStandaloneConfiguration
= new RedisStandaloneConfiguration("redis", 6379);
Now, you have used redis as hostname, which is not proper. You should use container name as host and make sure that both containers are on same network.
In order to make it work, you can do :
docker-compose.yml
version: '3'
services:
redis-server:
container_name: redis
image: 'redis'
ports:
- "6379:6379"
java-service:
build: .
links:
- redis-server
ports:
- "8080:8080"
Now other containers on same network will be able to communicate to redis using redis as hostname.
OR
If you don't want to set fix container name, you use hostname property like :
docker-compose.yml
version: '3'
services:
redis-server:
hostname: redis
image: 'redis'
ports:
- "6379:6379"
java-service:
build: .
links:
- redis-server
ports:
- "8080:8080"
Solution 2:[2]
It may be useful for others who seeks answer for same issue My issue resolved by removing the special character from Redis pass
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nimantha |
| Solution 2 | Peji Da |
