You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Can someone please help me with a code snippet for connecting to an AWS ElastiCache Redis cluster using the Redisson client with IAM Role authentication? I have already implemented the following code:
#5542
public class IamAuthTokenRequest {
private static final SdkHttpMethod REQUEST_METHOD = SdkHttpMethod.GET;
private static final String SERVICE_NAME = "elasticache";
private static final long TOKEN_EXPIRY_SECONDS = 900; // 5 minutes
private final String userId;
private final String clusterName;
private final Region region;
private final boolean isServerless;
public IamAuthTokenRequest(String userId, String clusterName, Region region, boolean isServerless) {
this.userId = userId;
this.clusterName = clusterName;
this.region = region;
this.isServerless = isServerless;
}
public String toSignedRequestUri(AwsCredentials credentials) throws URISyntaxException {
SdkHttpFullRequest request = getSingableRequest();
SdkHttpFullRequest signedRequest = sign(request, credentials);
String signedUri = signedRequest.getUri().toString();
// Remove the protocol (http:// or https://) if present
if (signedUri.startsWith("http://") || signedUri.startsWith("https://")) {
signedUri = signedUri.substring(signedUri.indexOf("//") + 2);
}
return signedUri;
}
private SdkHttpFullRequest getSingableRequest() {
URI uri = getRequestUri();
return SdkHttpFullRequest.builder()
.method(REQUEST_METHOD)
.uri(uri)
.putRawQueryParameter("Action", "connect")
.putRawQueryParameter("User", userId)
.applyMutation(builder -> {
if (isServerless) {
builder.putRawQueryParameter("ResourceType", "ServerlessCache");
}
})
.build();
}
private URI getRequestUri() {
return URI.create(String.format("http://%s/", clusterName));
}
private SdkHttpFullRequest sign(SdkHttpFullRequest request, AwsCredentials credentials) {
Aws4Signer signer = Aws4Signer.create();
Instant expirationTime = Instant.now().plus(Duration.ofSeconds(TOKEN_EXPIRY_SECONDS));
return signer.presign(request, Aws4PresignerParams.builder()
.expirationTime(expirationTime)
.awsCredentials(credentials)
.signingRegion(region)
.signingName(SERVICE_NAME)
.build());
}
@Bean
public RedissonClient redissonClient() throws IOException, URISyntaxException {
String USER_NAME = "elasticache-stg-test-iam";
String CLUSTER_NAME = "elasticache-stg-test";
Region REGION = Region.US_EAST_1;
IamAuthTokenRequest iamAuthTokenRequest = new IamAuthTokenRequest(USER_NAME, CLUSTER_NAME, REGION, false);
AwsCredentialsProvider awsCredentialsProvider = DefaultCredentialsProvider.create();
log.info("AWS Credentials resolved toString: {}", awsCredentialsProvider.resolveCredentials());
AwsCredentialsResolver awsCredentialsResolver = new AwsCredentialsResolver(USER_NAME, iamAuthTokenRequest, awsCredentialsProvider);
Config config = new Config();
SingleServerConfig serverConfig = config.useSingleServer()
.setAddress("rediss://elasticache-stg-rciq-primary-xxxxx.elb.us-east-1.amazonaws.com:6379")
.setCredentialsResolver(awsCredentialsResolver)
.setSslEnableEndpointIdentification(false)
.setDatabase(0)
.setConnectionMinimumIdleSize(3)
.setConnectionPoolSize(5)
.setIdleConnectionTimeout(300)
.setConnectTimeout(3000)
.setTimeout(1000);
log.info("Single Server URI: {}", serverConfig.getAddress());
log.info("Config: {}", config.toYAML());
try {
RedissonClient client = Redisson.create(config);
RBucket<String> bucket = client.getBucket("state");
bucket.set("Jharkhand");
RBucket<String> state = client.getBucket("state");
System.out.println("-----Redis Get ----" + state.get());
log.info("Successfully connected to Redis at {}", client.getConfig().useSingleServer().getAddress());
return client;
} catch (Exception e) {
log.error("Unexpected error during Redisson client creation", e);
throw new RuntimeException("Failed to create Redisson client due to unexpected error", e);
} finally {
log.info("RedissonClient creation attempt completed.");
}
}
}
`
As per the above code I am able to connect in my local machine with same nonprod aws redis cluster but when I am trying to deploy it in the nonprod environment getting the below issue
please someone help me to understand what I am missing the configurations
Caused by: org.redisson.client.RedisConnectionException: Unable to connect to Redis server: elasticache-stg-rciq-primary-xxxxxxx.elb.us-east-1.amazonaws.com/18.209.140.58:6379 at org.redisson.connection.pool.ConnectionPool.lambda$createConnection$0(ConnectionPool.java:129) ~[redisson-3.24.3.jar!/:3.24.3] at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863) ~[na:na] at java.base/java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:841) ~[na:na] at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510) ~[na:na] at java.base/java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:2162) ~[na:na] at org.redisson.connection.pool.ConnectionPool.promiseFailure(ConnectionPool.java:281) ~[redisson-3.24.3.jar!/:3.24.3] at org.redisson.connection.pool.ConnectionPool.lambda$createConnection$6(ConnectionPool.java:245) ~[redisson-3.24.3.jar!/:3.24.3] at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863) ~[na:na] at java.base/java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:841) ~[na:na] at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510) ~[na:na] at java.base/java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:2162) ~[na:na] at org.redisson.client.RedisClient$1$1.run(RedisClient.java:301) ~[redisson-3.24.3.jar!/:3.24.3] at io.netty.util.concurrent.AbstractEventExecutor.runTask(AbstractEventExecutor.java:173) ~[netty-common-4.1.110.Final.jar!/:4.1.110.Final] at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:166) ~[netty-common-4.1.110.Final.jar!/:4.1.110.Final] at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:469) ~[netty-common-4.1.110.Final.jar!/:4.1.110.Final] at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:569) ~[netty-transport-4.1.110.Final.jar!/:4.1.110.Final] at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:994) ~[netty-common-4.1.110.Final.jar!/:4.1.110.Final] at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.110.Final.jar!/:4.1.110.Final] at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.110.Final.jar!/:4.1.110.Final] at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na] Caused by: java.util.concurrent.CompletionException: org.redisson.client.RedisTimeoutException: Command execution timeout for command: (AUTH), params: (password masked), Redis client: [addr=redis://elasticache-stg-rciq-primary-xxxxx.elb.us-east-1.amazonaws.com:6379]
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
`package com.condenast.contentiq.consumer.redisson;
import lombok.extern.slf4j.Slf4j;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import org.redisson.config.Credentials;
import org.redisson.config.CredentialsResolver;
import java.net.InetSocketAddress;
import java.net.URISyntaxException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
@slf4j
public class AwsCredentialsResolver implements CredentialsResolver {
}
package com.condenast.contentiq.consumer.redisson;
import software.amazon.awssdk.auth.credentials.AwsCredentials;
import software.amazon.awssdk.auth.signer.Aws4Signer;
import software.amazon.awssdk.auth.signer.params.Aws4PresignerParams;
import software.amazon.awssdk.http.SdkHttpFullRequest;
import software.amazon.awssdk.http.SdkHttpMethod;
import software.amazon.awssdk.regions.Region;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Duration;
import java.time.Instant;
public class IamAuthTokenRequest {
private static final SdkHttpMethod REQUEST_METHOD = SdkHttpMethod.GET;
private static final String SERVICE_NAME = "elasticache";
private static final long TOKEN_EXPIRY_SECONDS = 900; // 5 minutes
}
package com.condenast.contentiq.consumer.redisson;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import java.io.IOException;
import java.net.URISyntaxException;
@slf4j
@configuration
public class RedissonConfig {
}
`
As per the above code I am able to connect in my local machine with same nonprod aws redis cluster but when I am trying to deploy it in the nonprod environment getting the below issue
please someone help me to understand what I am missing the configurations
Caused by: org.redisson.client.RedisConnectionException: Unable to connect to Redis server: elasticache-stg-rciq-primary-xxxxxxx.elb.us-east-1.amazonaws.com/18.209.140.58:6379 at org.redisson.connection.pool.ConnectionPool.lambda$createConnection$0(ConnectionPool.java:129) ~[redisson-3.24.3.jar!/:3.24.3] at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863) ~[na:na] at java.base/java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:841) ~[na:na] at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510) ~[na:na] at java.base/java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:2162) ~[na:na] at org.redisson.connection.pool.ConnectionPool.promiseFailure(ConnectionPool.java:281) ~[redisson-3.24.3.jar!/:3.24.3] at org.redisson.connection.pool.ConnectionPool.lambda$createConnection$6(ConnectionPool.java:245) ~[redisson-3.24.3.jar!/:3.24.3] at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863) ~[na:na] at java.base/java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:841) ~[na:na] at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510) ~[na:na] at java.base/java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:2162) ~[na:na] at org.redisson.client.RedisClient$1$1.run(RedisClient.java:301) ~[redisson-3.24.3.jar!/:3.24.3] at io.netty.util.concurrent.AbstractEventExecutor.runTask(AbstractEventExecutor.java:173) ~[netty-common-4.1.110.Final.jar!/:4.1.110.Final] at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:166) ~[netty-common-4.1.110.Final.jar!/:4.1.110.Final] at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:469) ~[netty-common-4.1.110.Final.jar!/:4.1.110.Final] at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:569) ~[netty-transport-4.1.110.Final.jar!/:4.1.110.Final] at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:994) ~[netty-common-4.1.110.Final.jar!/:4.1.110.Final] at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.110.Final.jar!/:4.1.110.Final] at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.110.Final.jar!/:4.1.110.Final] at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na] Caused by: java.util.concurrent.CompletionException: org.redisson.client.RedisTimeoutException: Command execution timeout for command: (AUTH), params: (password masked), Redis client: [addr=redis://elasticache-stg-rciq-primary-xxxxx.elb.us-east-1.amazonaws.com:6379]
Beta Was this translation helpful? Give feedback.
All reactions