netty 测试redis 的resp协议密码认证问题
public class TestRedisResp {
public static void main(String[] args) throws InterruptedException {
byte[] limit = {13, 10};
ChannelFuture connect = new Bootstrap()
.group(new NioEventLoopGroup())
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel ch) throws Exception {
ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ByteBuf buf = ctx.alloc().buffer();
buf.writeBytes("*2".getBytes());
buf.writeBytes(limit);
buf.writeBytes("$4".getBytes());
buf.writeBytes(limit);
buf.writeBytes("auth".getBytes());
buf.writeBytes(limit);
buf.writeBytes("$4".getBytes());
buf.writeBytes(limit);
buf.writeBytes("root".getBytes());
buf.writeBytes(limit);
buf.writeBytes("*3".getBytes(StandardCharsets.UTF_8));
buf.writeBytes(limit);
buf.writeBytes("$3".getBytes(StandardCharsets.UTF_8));
buf.writeBytes(limit);
buf.writeBytes("set".getBytes(StandardCharsets.UTF_8));
buf.writeBytes(limit);
buf.writeBytes("$4".getBytes(StandardCharsets.UTF_8));
buf.writeBytes(limit);
buf.writeBytes("name".getBytes(StandardCharsets.UTF_8));
buf.writeBytes(limit);
buf.writeBytes("$4".getBytes(StandardCharsets.UTF_8));
buf.writeBytes(limit);
buf.writeBytes("tang".getBytes(StandardCharsets.UTF_8));
buf.writeBytes(limit);
ctx.writeAndFlush(buf);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf buf = (ByteBuf) msg;
String m = buf.toString(Charset.defaultCharset());
System.out.println(m);
super.channelRead(ctx, msg);
}
});
}
})
.connect(new InetSocketAddress("localhost", 6379));
connect.sync();
System.out.println(connect);
}
}