I have a Spring Boot API Gateway service and a Redis service running under the same network in Docker. I plan to connect the API Gateway to Redis, but I encountered an error:
"Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.redisson.api.RedissonClient]: Factory method 'redissionClient' threw an exception with the message: Unable to connect to the Redis server: 127.0.0.1/127.0.0.1:6379."
If I run the API Gateway from my local machine, I am able to connect to the Redis service running in Docker.
24 connections initialized for 127.0.0.1/127.0.0.1:6379
Here is my Redis configuration:
@Configuration
public class RedisConfig {
@Bean
public RedissonClient redissionClient() {
Config config = new Config();
config.useSingleServer().setAddress("redis://127.0.0.1:6379");
return Redisson.create(config);
}
}
I also edit etc/host added redis
127.0.0.1 api-gateway db redis
trying to connect api-gateway to redis when api-gateway run inside docker
I have the experience that
localhost
does not work when containers need to talk to eachother. I would advise you to try and use the IPv4 address of your DHCP by your local network i.e.192.168.1.1XX
. You can check through cmd withipconfig
what address the machine has been allocated by your DHCP.