使用aredis API向安全Redis服务器发送命令失败的技术求助
Hey there, let's figure out why your authenticated Redis connection with ARedis is failing when you try to run set commands. Based on the code snippet you shared, here are the most probable issues and their fixes:
1. Wrong Order of Authentication Setup
You're calling AsyncRedisFactory.setAuth(host, password) before creating the factory instance, but ARedis's static auth method doesn't play well with connection pools. The more reliable approach is to pass your authentication credentials directly to the RedisServerInfo object before creating the connection pool.
Corrected Code Example:
// Pass password directly to RedisServerInfo for per-server auth RedisServerInfo info = new RedisServerInfo(host, port, password); AsyncRedisFactory factory = new AsyncRedisFactory(null); factory.setPoolSize(100); asyncPool = factory.getConnectionPool(info); // Retrieve connection from pool and execute set command AsyncRedisConnection asyncCon = asyncPool.getConnection(); asyncCon.set("your-key", "your-value", new AsyncHandler<String>() { @Override public void onSuccess(String result) { System.out.println("Set command succeeded: " + result); asyncPool.returnConnection(asyncCon); // Always return connections to the pool! } @Override public void onFailure(Throwable throwable) { System.err.println("Set command failed: " + throwable.getMessage()); asyncPool.returnConnection(asyncCon); } });
2. Missing Error Handling & Connection Cleanup
Your code cuts off, but two critical things are easy to miss:
- Return connections to the pool: Failing to do this will exhaust your connection pool over time, leading to timeouts or failed commands.
- Catch errors explicitly: Add an
onFailurehandler to your async commands to see the exact root cause (like "NOAUTH Authentication required" which would confirm the auth step never completed properly).
3. Static setAuth Isn't Ideal for Connection Pools
The static AsyncRedisFactory.setAuth() method is designed for global auth configuration, but it can be unreliable with pools because the factory might initialize connections before the static auth is applied. Using RedisServerInfo with the password parameter is the recommended way to handle per-server authentication.
4. Double-Check Redis Server Settings
Before debugging code further, confirm these server-side details:
- The Redis server has
requirepassset correctly in its configuration file - The password you're using matches exactly (no typos, extra spaces, or case mismatches)
- You're connecting to the correct host and port for the authenticated Redis instance
Quick Debugging Step:
First test if authentication works on its own to rule out credential issues:
AsyncRedisConnection testCon = asyncPool.getConnection(); testCon.auth(password, new AsyncHandler<String>() { @Override public void onSuccess(String result) { System.out.println("Authentication successful!"); testCon.set("test-key", "test-val", new AsyncHandler<String>() { @Override public void onSuccess(String res) { System.out.println("Set worked post-auth!"); } @Override public void onFailure(Throwable t) { System.err.println("Set failed post-auth: " + t.getMessage()); } }); asyncPool.returnConnection(testCon); } @Override public void onFailure(Throwable throwable) { System.err.println("Authentication failed: " + throwable.getMessage()); asyncPool.returnConnection(testCon); } });
内容的提问来源于stack exchange,提问作者PravinKumar.R

