hello all i have Redis run as a docker images by following command
docker run -e REDIS_PASSWORD=password123 -v /home/abed/project/Redis-Cache-Using-Spring-Boot/volume:/bitnami/redis/data bitnami/redis:latest
and setup configuration inside spring boot application but i cant connection with Redis from spring boot i have this error when run application and call API, this API will use Redis
java.net.ConnectException: Connection refused
at java.base/sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[na:na]
at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:777) ~[na:na]
at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:330) ~[netty-transport-4.1.72.Final.jar:4.1.72.Final]
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334) ~[netty-transport-4.1.72.Final.jar:4.1.72.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:710) ~[netty-transport-4.1.72.Final.jar:4.1.72.Final]
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:658) ~[netty-transport-4.1.72.Final.jar:4.1.72.Final]
i used this configuration inside spring boot application
@Bean(name = "redisTemplate")
public RedisTemplate<String,String> redisTemplate(RedisConnectionFactory factory){
final RedisTemplate<String,String> template=new RedisTemplate<String, String>();
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new GenericToStringSerializer<String>(String.class));
template.setHashValueSerializer(new Jackson2JsonRedisSerializer<String>(String.class));
template.setValueSerializer(new Jackson2JsonRedisSerializer<String>(String.class));
RedisStandaloneConfiguration configuration=new RedisStandaloneConfiguration(host,port);
template.setConnectionFactory(factory);
return template;
}
and inject it RedisTemplate
inside RedisUtility
class look at the following code
@Service
public class RedisUtility {
private final RedisTemplate<String,String> redisTemplate;
private final Gson gson;
public RedisUtility(RedisTemplate<String,String> redisTemplate,Gson gson)
{
this.redisTemplate=redisTemplate;
this.gson=gson;
}
public void setValue(final String key, UserDto authUserDto)
{
redisTemplate.opsForValue().set(key,gson.toJson(authUserDto));
redisTemplate.expire(key,10, TimeUnit.MINUTES);
}
public UserDto getValue(final String key)
{
return gson.fromJson(redisTemplate.opsForValue().get(key),UserDto.class);
}
public void deleteKeyFormed(String key)
{
redisTemplate.delete(key);
}
}
and i forget application.yml
server.port=8088
spring.redis.database=0
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=password123
spring.redis.timeout=60000
Can you check if you can connect to your Redis docker container using the redis-cli on port 6379?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.