Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JedisConnectionFactory setHostName is deprecated

This will be my first time connecting Spring to Redis. The documentation for jedis connection factory: http://www.baeldung.com/spring-data-redis-tutorial

Offers the following code:

@Bean JedisConnectionFactory jedisConnectionFactory() {     JedisConnectionFactory jedisConFactory             = new JedisConnectionFactory();      jedisConFactory.setHostName("localhost");     jedisConFactory.setPort(6379);     return jedisConFactory; } 

Looks great, but my IDE is telling me that the setHostName and setPort methods have been deprecated (even though I'm using the versions from the tutorial).

I was wondering if anyone had a simple "get spring data connected to redis" example that uses the non-deprecated API calls?

like image 875
Michael Draper Avatar asked Feb 28 '18 04:02

Michael Draper


People also ask

What is JedisConnectionFactory?

public class JedisConnectionFactory extends Object implements InitializingBean, DisposableBean, RedisConnectionFactory. Connection factory creating Jedis based connections. JedisConnectionFactory should be configured using an environmental configuration and the client configuration .

What is LettuceConnectionFactory?

public class LettuceConnectionFactory extends Object implements InitializingBean, DisposableBean, RedisConnectionFactory, ReactiveRedisConnectionFactory. Connection factory creating Lettuce-based connections. This factory creates a new LettuceConnection on each call to getConnection() .

What is Spring Data Redis?

Spring Data Redis, part of the larger Spring Data family, provides easy configuration and access to Redis from Spring applications. It offers both low-level and high-level abstractions for interacting with the store, freeing the user from infrastructural concerns.


1 Answers

With Spring Data Redis 2.0, those methods have been deprecated. You now need to configure using RedisStandaloneConfiguration

Reference: https://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/connection/jedis/JedisConnectionFactory.html#setHostName-java.lang.String-

Example:

JedisConnectionFactory jedisConnectionFactory() {     RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration("localhost", 6379);     redisStandaloneConfiguration.setPassword(RedisPassword.of("yourRedisPasswordIfAny"));     return new JedisConnectionFactory(redisStandaloneConfiguration); } 
like image 144
Tehnaz Avatar answered Sep 21 '22 15:09

Tehnaz