Redis - Connecting to Redis

Configuring the Lettuce Connector

Lettuce is a Netty based open source connection pool supported by Spring Data Redis.

	@Configuration
	class AppConfig {
		@Bean
		public LettuceConnectionFactory redisConnectionFactory() {
			return new LettuceConnectionFactory(new RedisStandaloneConfiguration("server", 6379);
		}
	}

By default, all lettucconnection instances created by lettucconnectionfactory share the same thread safe local connection for all non blocking and non transactional operations. If you want to use a dedicated connection every time, set shareNativeConnection to false. Letuconnectionfactory can also use letucepool to manage blocked and transactional connections, or all connections with shareNativeConnection false.

Constructor of lettucconnectionfactory:

private final LettuceClientConfiguration clientConfiguration;

private RedisConfiguration configuration;

private RedisStandaloneConfiguration standaloneConfig = new RedisStandaloneConfiguration("localhost", 6379);

private LettuceConnectionFactory(LettuceClientConfiguration clientConfig) {
	Assert.notNull(cientConfig, "LettuceClientConfiguration must not be null!");
	this.clientConfiguration = clientConfig;
	this.configuration = this.standaloneConfig
}
  1. RedisStandaloneConfiguration, LettuceClientConfiguration
	public LettuceConnectionFactory(RedisStandaloneConfiguration standaloneConfig,
			LettuceClientConfiguration clientConfig) {

		this(clientConfig);

		Assert.notNull(standaloneConfig, "RedisStandaloneConfiguration must not be null!");

		this.standaloneConfig = standaloneConfig;
		this.configuration = this.standaloneConfig;
	}

There are three constructors of RedisStandaloneConfiguration:

  • RedisStandaloneConfiguration()
  • RedisStandaloneConfiguration(String hostName)
  • RedisStandaloneConfiguration(String hostName, int port)

Lettucleclientconfiguration can be constructed in the following ways:

  • Lettucleclientconfigurationbuilder - builder mode
  • DefaultLettuceClientConfiguration - constructor of common multiple parameters
  • LettucePoolingClientConfigurationBuilder - builder mode
  • DefaultLettucePoolingClientConfiguration - (LettuceClientConfiguration clientConfiguration,
    GenericObjectPoolConfig poolConfig)
  • MutableLettuceClientConfiguration
  1. RedisStandaloneConfiguration
	public LettuceConnectionFactory(RedisStandaloneConfiguration configuration) {
		this(configuration, new MutableLettuceClientConfiguration());
	}
  1. String host, int port
	public LettuceConnectionFactory(String host, int port) {
		this(new RedisStandaloneConfiguration(host, port), new MutableLettuceClientConfiguration());
	}
  1. RedisConfiguration
	public LettuceConnectionFactory(RedisConfiguration redisConfiguration) {
		this(redisConfiguration, new MutableLettuceClientConfiguration());
	}
  1. RedisSentinelConfiguration
	public LettuceConnectionFactory(RedisSentinelConfiguration sentinelConfiguration) {
		this(sentinelConfiguration, new MutableLettuceClientConfiguration());
	}
  1. RedisClusterConfiguration
	public LettuceConnectionFactory(RedisClusterConfiguration clusterConfiguration) {
		this(clusterConfiguration, new MutableLettuceClientConfiguration());
	}
  1. LettucePool
	/**
	 * @param pool
	 * @deprecated since 2.0, use pooling via {@link LettucePoolingClientConfiguration}.
	 */
	@Deprecated
	public LettuceConnectionFactory(LettucePool pool) {

		this(new MutableLettuceClientConfiguration());
		this.pool = pool;
	}
  1. RedisStandaloneConfiguration, LettuceClientConfiguration
	public LettuceConnectionFactory(RedisStandaloneConfiguration standaloneConfig,
			LettuceClientConfiguration clientConfig) {

		this(clientConfig);

		Assert.notNull(standaloneConfig, "RedisStandaloneConfiguration must not be null!");

		this.standaloneConfig = standaloneConfig;
		this.configuration = this.standaloneConfig;
	}
  1. RedisConfiguration, LettuceClientConfiguration
	public LettuceConnectionFactory(RedisConfiguration redisConfiguration, LettuceClientConfiguration clientConfig) {

		this(clientConfig);

		Assert.notNull(redisConfiguration, "RedisConfiguration must not be null!");

		this.configuration = redisConfiguration;
	}
  1. RedisSentinelConfiguration, LettuceClientConfiguration
	public LettuceConnectionFactory(RedisSentinelConfiguration sentinelConfiguration,
			LettuceClientConfiguration clientConfig) {

		this(clientConfig);

		Assert.notNull(sentinelConfiguration, "RedisSentinelConfiguration must not be null!");

		this.configuration = sentinelConfiguration;
	}
  1. RedisClusterConfiguration, LettuceClientConfiguration
	public LettuceConnectionFactory(RedisClusterConfiguration clusterConfiguration,
			LettuceClientConfiguration clientConfig) {

		this(clientConfig);

		Assert.notNull(clusterConfiguration, "RedisClusterConfiguration must not be null!");

		this.configuration = clusterConfiguration;
	}

Configuring the Jedis Connector

Jedis is a community driven connector supported by Spring Data Redis module through org.springframework.data.redis.connection.jedis package.

@Configuration
class AppConfig {

  @Bean
  public JedisConnectionFactory redisConnectionFactory() {
    return new JedisConnectionFactory();
  }
}

For production use, to fine tune settings, such as host name or password, configure as follows:

@Configuration
class RedisConfiguration {

  @Bean
  public JedisConnectionFactory redisConnectionFactory() {

    RedisStandaloneConfiguration config = new RedisStandaloneConfiguration("server", 6379);
    return new JedisConnectionFactory(config);
  }
}

Write to Master, Read from Replica

Redis's Master-Slave mechanism - there is no automatic failover, which not only allows data to be stored safely on more nodes, but also allows data to be read from the slave node and written on the master node by using Lettuce. You can use lettucleclientconfiguration to configure read-write policies.

@Configuration
class WriteToMasterReadFromReplicaConfiguration {

  @Bean
  public LettuceConnectionFactory redisConnectionFactory() {

    LettuceClientConfiguration clientConfig = LettuceClientConfiguration.builder()
      .readFrom(ReadFrom.SLAVE_PREFERRED)
      .build();

    RedisStandaloneConfiguration serverConfig = new RedisStandaloneConfiguration("server", 6379);

    return new LettuceConnectionFactory(serverConfig, clientConfig);
  }
}

For non-public address environments (for example, using AWS) published through the INFO command, use RedisStaticMasterReplicaConfiguration instead of redisstandalonconfiguration.

RedisStandaloneConfiguration: applicable to single node.

RedisStaticMasterReplicaConfiguration: applicable to master-slave mode, and the host is fixed.

94 original articles published, 14 praised, 30000 visitors+
Private letter follow

Tags: Redis Jedis Spring Netty

Posted on Wed, 26 Feb 2020 03:01:06 -0500 by Stargate