Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

redis - Will lettuce resend AUTH after reconnecting?

Our redis server has a password. My application encountered "NOAUTH Authentication required" errors. There were reconnecting before those errors. I'm wondering if lettuce resend AUTH <password> after reconnecting.

If so, is there a way to insert AUTH <password> just after reconnected and before sending any command.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I did a simple test and prove lettuce does send AUTH after reconnection.

Just run config set 2 in redis cli, and set LOG level to TRACE so that all commands will be printed out.

@Test
public void should_reconnect_send_auth() {
    RedisURI redisURI = RedisURI.builder().withHost("localhost").build();

    RedisClient client = RedisClient.create(redisURI);

    StatefulRedisConnection<String, String> conn = client.connect();

    RedisCommands<String, String> sync = conn.sync();

    sync.auth("pwd4unittests");

    sync.hmset("my_id", ImmutableMap.of("feature_1", "value_1"));

    try {
        Thread.sleep(5000L);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    List<KeyValue<String, String>> results = sync.hmget("my_id", "feature_1");

    System.out.println(results);
}

The log shows that

[2021-01-12T18:38:30,121Z](lettuce-eventExecutorLoop-1-1)([]) INFO - ConnectionWatchdog - Reconnecting, last destination was localhost/127.0.0.1:6379
[2021-01-12T18:38:30,124Z](lettuce-eventExecutorLoop-1-1)([]) DEBUG - RedisClient - Resolved SocketAddress localhost:6379 using RedisURI [host='localhost', port=6379]
[2021-01-12T18:38:30,125Z](lettuce-eventExecutorLoop-1-1)([]) DEBUG - ReconnectionHandler - Reconnecting to Redis at localhost:6379
[2021-01-12T18:38:30,136Z](lettuce-nioEventLoop-4-2)([]) DEBUG - CommandHandler - [channel=0xc3ac6a89, [id: 0x51428352] (inactive), chid=0x2] channelRegistered()
[2021-01-12T18:38:30,138Z](lettuce-nioEventLoop-4-2)([]) DEBUG - CommandHandler - [channel=0xc3ac6a89, /127.0.0.1:60446 -> localhost/127.0.0.1:6379, chid=0x2] channelActive()
[2021-01-12T18:38:30,138Z](lettuce-nioEventLoop-4-2)([]) DEBUG - DefaultEndpoint - [channel=0xc3ac6a89, /127.0.0.1:60446 -> localhost/127.0.0.1:6379, epid=0x1] activateEndpointAndExecuteBufferedCommands 0 command(s) buffered
[2021-01-12T18:38:30,138Z](lettuce-nioEventLoop-4-2)([]) DEBUG - DefaultEndpoint - [channel=0xc3ac6a89, /127.0.0.1:60446 -> localhost/127.0.0.1:6379, epid=0x1] activating endpoint
[2021-01-12T18:38:30,139Z](lettuce-nioEventLoop-4-2)([]) DEBUG - RedisChannelHandler - dispatching command AsyncCommand [type=AUTH, output=StatusOutput [output=null, error='null'], commandType=io.lettuce.core.protocol.Command]
[2021-01-12T18:38:30,140Z](lettuce-nioEventLoop-4-2)([]) DEBUG - DefaultEndpoint - [channel=0xc3ac6a89, /127.0.0.1:60446 -> localhost/127.0.0.1:6379, epid=0x1] write() writeAndFlush command ActivationCommand [type=AUTH, output=StatusOutput [output=null, error='null'], commandType=io.lettuce.core.protocol.AsyncCommand]
[2021-01-12T18:38:30,140Z](lettuce-nioEventLoop-4-2)([]) DEBUG - CommandHandler - [channel=0xc3ac6a89, /127.0.0.1:60446 -> localhost/127.0.0.1:6379, chid=0x2] write(ctx, ActivationCommand [type=AUTH, output=StatusOutput [output=null, error='null'], commandType=io.lettuce.core.protocol.AsyncCommand], promise)
[2021-01-12T18:38:30,141Z](lettuce-nioEventLoop-4-2)([]) DEBUG - CommandEncoder - [channel=0xc3ac6a89, /127.0.0.1:60446 -> localhost/127.0.0.1:6379] writing command ActivationCommand [type=AUTH, output=StatusOutput [output=null, error='null'], commandType=io.lettuce.core.protocol.AsyncCommand]
[2021-01-12T18:38:30,141Z](lettuce-nioEventLoop-4-2)([]) TRACE - CommandEncoder - [channel=0xc3ac6a89, /127.0.0.1:60446 -> localhost/127.0.0.1:6379] Sent: *2
$4
AUTH
$13
my_password
[2021-01-12T18:38:30,141Z](lettuce-nioEventLoop-4-2)([]) DEBUG - DefaultEndpoint - [channel=0xc3ac6a89, /127.0.0.1:60446 -> localhost/127.0.0.1:6379, epid=0x1] write() done
[2021-01-12T18:38:30,142Z](lettuce-nioEventLoop-4-2)([]) DEBUG - DefaultEndpoint - [channel=0xc3ac6a89, /127.0.0.1:60446 -> localhost/127.0.0.1:6379, epid=0x1] flushCommands()
[2021-01-12T18:38:30,142Z](lettuce-nioEventLoop-4-2)([]) DEBUG - DefaultEndpoint - [channel=0xc3ac6a89, /127.0.0.1:60446 -> localhost/127.0.0.1:6379, epid=0x1] flushCommands() Flushing 0 commands
[2021-01-12T18:38:30,142Z](lettuce-nioEventLoop-4-2)([]) DEBUG - ConnectionWatchdog - [channel=0xc3ac6a89, /127.0.0.1:60446 -> localhost/127.0.0.1:6379, last known addr=localhost/127.0.0.1:6379] channelActive()
[2021-01-12T18:38:30,142Z](lettuce-nioEventLoop-4-2)([]) DEBUG - CommandHandler - [channel=0xc3ac6a89, /127.0.0.1:60446 -> localhost/127.0.0.1:6379, chid=0x2] channelActive() done
[2021-01-12T18:38:30,142Z](lettuce-nioEventLoop-4-2)([]) INFO - ReconnectionHandler - Reconnected to localhost:6379, Channel channel=0xc3ac6a89, /127.0.0.1:60446 -> localhost/127.0.0.1:6379

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...