Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE #2781 Jedis Port Resolving Support #2780

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.lang.Nullable;
import redis.clients.jedis.HostAndPortMapper;

/**
* Default implementation of {@literal JedisClientConfiguration}.
Expand All @@ -38,6 +39,7 @@ class DefaultJedisClientConfiguration implements JedisClientConfiguration {
private final Optional<SSLSocketFactory> sslSocketFactory;
private final Optional<SSLParameters> sslParameters;
private final Optional<HostnameVerifier> hostnameVerifier;
private final Optional<HostAndPortMapper> hostAndPortMapper;
private final boolean usePooling;
private final Optional<GenericObjectPoolConfig> poolConfig;
private final Optional<String> clientName;
Expand All @@ -53,33 +55,74 @@ class DefaultJedisClientConfiguration implements JedisClientConfiguration {
this.sslSocketFactory = Optional.ofNullable(sslSocketFactory);
this.sslParameters = Optional.ofNullable(sslParameters);
this.hostnameVerifier = Optional.ofNullable(hostnameVerifier);
this.hostAndPortMapper = Optional.empty();
this.usePooling = usePooling;
this.poolConfig = Optional.ofNullable(poolConfig);
this.clientName = Optional.ofNullable(clientName);
this.readTimeout = readTimeout;
this.connectTimeout = connectTimeout;
}

DefaultJedisClientConfiguration(boolean useSsl, @Nullable SSLSocketFactory sslSocketFactory,
@Nullable SSLParameters sslParameters, @Nullable HostnameVerifier hostnameVerifier,
@Nullable HostAndPortMapper hostAndPortMapper, boolean usePooling,
@Nullable GenericObjectPoolConfig poolConfig, @Nullable String clientName, Duration readTimeout,
Duration connectTimeout) {

this.useSsl = useSsl;
this.sslSocketFactory = Optional.ofNullable(sslSocketFactory);
this.sslParameters = Optional.ofNullable(sslParameters);
this.hostnameVerifier = Optional.ofNullable(hostnameVerifier);
this.hostAndPortMapper = Optional.ofNullable(hostAndPortMapper);
this.usePooling = usePooling;
this.poolConfig = Optional.ofNullable(poolConfig);
this.clientName = Optional.ofNullable(clientName);
this.readTimeout = readTimeout;
this.connectTimeout = connectTimeout;
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.jedis.JedisClientConfiguration#useSsl()
*/
@Override
public boolean isUseSsl() {
return useSsl;
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.jedis.JedisClientConfiguration#getSslSocketFactory()
*/
@Override
public Optional<SSLSocketFactory> getSslSocketFactory() {
return sslSocketFactory;
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.jedis.JedisClientConfiguration#getSslParameters()
*/
@Override
public Optional<SSLParameters> getSslParameters() {
return sslParameters;
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.jedis.JedisClientConfiguration#getHostnameVerifier()
*/
@Override
public Optional<HostnameVerifier> getHostnameVerifier() {
return hostnameVerifier;
}

@Override
public Optional<HostAndPortMapper> getHostAndPortMapper() {
return hostAndPortMapper;
}


@Override
public boolean isUsePooling() {
return usePooling;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.data.redis.connection.jedis;

import redis.clients.jedis.HostAndPortMapper;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Protocol;

Expand Down Expand Up @@ -78,6 +79,11 @@ public interface JedisClientConfiguration {
*/
Optional<HostnameVerifier> getHostnameVerifier();

/**
* @return the optional {@link HostAndPortMapper}.
*/
Optional<HostAndPortMapper> getHostAndPortMapper();

/**
* @return {@literal true} to use connection-pooling. Applies only to single node Redis. Sentinel and Cluster modes
* use always connection-pooling regardless of the pooling setting.
Expand Down Expand Up @@ -168,6 +174,15 @@ interface JedisClientConfigurationBuilder {
*/
JedisClientConfigurationBuilder clientName(String clientName);

/**
* Configure a {@code hostAndPortMapper}
*
* @param hostAndPortMapper must not be {@literal null}.
* @return {@literal this} builder.
* @throws IllegalArgumentException if clientName is {@literal null}.
*/
JedisClientConfigurationBuilder hostAndPortMapper(HostAndPortMapper hostAndPortMapper);

/**
* Configure a read timeout.
*
Expand Down Expand Up @@ -273,6 +288,7 @@ class DefaultJedisClientConfigurationBuilder implements JedisClientConfiguration
private @Nullable SSLSocketFactory sslSocketFactory;
private @Nullable SSLParameters sslParameters;
private @Nullable HostnameVerifier hostnameVerifier;
private @Nullable HostAndPortMapper hostAndPortMapper;
private boolean usePooling;
private GenericObjectPoolConfig poolConfig = new JedisPoolConfig();
private @Nullable String clientName;
Expand All @@ -281,83 +297,131 @@ class DefaultJedisClientConfigurationBuilder implements JedisClientConfiguration

private DefaultJedisClientConfigurationBuilder() {}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.jedis.JedisClientConfiguration.JedisClientConfigurationBuilder#useSsl()
*/
@Override
public JedisSslClientConfigurationBuilder useSsl() {

this.useSsl = true;
return this;
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.jedis.JedisClientConfiguration.JedisSslClientConfigurationBuilder#sslSocketFactory(javax.net.ssl.SSLSocketFactory)
*/
@Override
public JedisSslClientConfigurationBuilder sslSocketFactory(SSLSocketFactory sslSocketFactory) {

Assert.notNull(sslSocketFactory, "SSLSocketFactory must not be null");
Assert.notNull(sslSocketFactory, "SSLSocketFactory must not be null!");

this.sslSocketFactory = sslSocketFactory;
return this;
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.jedis.JedisClientConfiguration.JedisSslClientConfigurationBuilder#sslParameters(javax.net.ssl.SSLParameters)
*/
@Override
public JedisSslClientConfigurationBuilder sslParameters(SSLParameters sslParameters) {

Assert.notNull(sslParameters, "SSLParameters must not be null");
Assert.notNull(sslParameters, "SSLParameters must not be null!");

this.sslParameters = sslParameters;
return this;
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.jedis.JedisClientConfiguration.JedisSslClientConfigurationBuilder#hostnameVerifier(javax.net.ssl.HostnameVerifier)
*/
@Override
public JedisSslClientConfigurationBuilder hostnameVerifier(HostnameVerifier hostnameVerifier) {

Assert.notNull(hostnameVerifier, "HostnameVerifier must not be null");
Assert.notNull(hostnameVerifier, "HostnameVerifier must not be null!");

this.hostnameVerifier = hostnameVerifier;
return this;
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.jedis.JedisClientConfiguration.JedisClientConfigurationBuilder#usePooling()
*/
@Override
public JedisPoolingClientConfigurationBuilder usePooling() {

this.usePooling = true;
return this;
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.jedis.JedisClientConfiguration.JedisPoolingClientConfigurationBuilder#poolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig)
*/
@Override
public JedisPoolingClientConfigurationBuilder poolConfig(GenericObjectPoolConfig poolConfig) {

Assert.notNull(poolConfig, "GenericObjectPoolConfig must not be null");
Assert.notNull(poolConfig, "GenericObjectPoolConfig must not be null!");

this.poolConfig = poolConfig;
return this;
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.jedis.JedisClientConfiguration.JedisPoolingClientConfigurationBuilder#and()
*/
@Override
public JedisClientConfigurationBuilder and() {
return this;
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.jedis.JedisClientConfiguration.JedisClientConfigurationBuilder#clientName(java.lang.String)
*/
@Override
public JedisClientConfigurationBuilder clientName(String clientName) {

Assert.hasText(clientName, "Client name must not be null or empty");
Assert.hasText(clientName, "Client name must not be null or empty!");

this.clientName = clientName;
return this;
}

@Override
public JedisClientConfigurationBuilder hostAndPortMapper(HostAndPortMapper hostAndPortMapper) {
Assert.notNull(hostAndPortMapper, "HostAndPortMapper can not be null");

this.hostAndPortMapper = hostAndPortMapper;
return this;
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.jedis.JedisClientConfiguration.JedisClientConfigurationBuilder#readTimeout(java.time.Duration)
*/
@Override
public JedisClientConfigurationBuilder readTimeout(Duration readTimeout) {

Assert.notNull(readTimeout, "Duration must not be null");
Assert.notNull(readTimeout, "Duration must not be null!");

this.readTimeout = readTimeout;
return this;
}

/*
* (non-Javadoc)
* @see org.springframework.data.redis.connection.jedis.JedisClientConfiguration.JedisClientConfigurationBuilder#connectTimeout(java.time.Duration)
*/
@Override
public JedisClientConfigurationBuilder connectTimeout(Duration connectTimeout) {

Assert.notNull(connectTimeout, "Duration must not be null");
Assert.notNull(connectTimeout, "Duration must not be null!");

this.connectTimeout = connectTimeout;
return this;
Expand All @@ -366,7 +430,7 @@ public JedisClientConfigurationBuilder connectTimeout(Duration connectTimeout) {
@Override
public JedisClientConfiguration build() {

return new DefaultJedisClientConfiguration(useSsl, sslSocketFactory, sslParameters, hostnameVerifier, usePooling,
return new DefaultJedisClientConfiguration(useSsl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMapper, usePooling,
poolConfig, clientName, readTimeout, connectTimeout);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import redis.clients.jedis.Connection;
import redis.clients.jedis.DefaultJedisClientConfig;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.HostAndPortMapper;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisClientConfig;
import redis.clients.jedis.JedisCluster;
Expand Down Expand Up @@ -635,6 +636,7 @@ private JedisClientConfig createClientConfig(int database, @Nullable String user
builder.user(username);
}
password.toOptional().map(String::new).ifPresent(builder::password);
this.clientConfiguration.getHostAndPortMapper().ifPresent(builder::hostAndPortMapper);

if (isUseSsl()) {

Expand Down Expand Up @@ -1068,6 +1070,11 @@ public Optional<HostnameVerifier> getHostnameVerifier() {
return Optional.ofNullable(hostnameVerifier);
}

@Override
public Optional<HostAndPortMapper> getHostAndPortMapper() {
return Optional.empty();
}

public void setHostnameVerifier(HostnameVerifier hostnameVerifier) {
this.hostnameVerifier = hostnameVerifier;
}
Expand Down