feat(新增): 🚀 netty使用Redis发送消息
This commit is contained in:
parent
26acd1c7c6
commit
cb1bdfd36e
|
@ -1,4 +1,4 @@
|
||||||
package cn.bunny.service.netty;
|
package cn.bunny.service.netty.demo4;
|
||||||
|
|
||||||
import io.netty.bootstrap.Bootstrap;
|
import io.netty.bootstrap.Bootstrap;
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
|
@ -1,4 +1,4 @@
|
||||||
package cn.bunny.service.netty;
|
package cn.bunny.service.netty.demo4;
|
||||||
|
|
||||||
import io.netty.bootstrap.ServerBootstrap;
|
import io.netty.bootstrap.ServerBootstrap;
|
||||||
import io.netty.channel.*;
|
import io.netty.channel.*;
|
|
@ -0,0 +1,65 @@
|
||||||
|
package cn.bunny.service.netty.demo5;
|
||||||
|
|
||||||
|
import io.netty.bootstrap.Bootstrap;
|
||||||
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import io.netty.channel.ChannelFuture;
|
||||||
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
|
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||||
|
import io.netty.channel.ChannelInitializer;
|
||||||
|
import io.netty.channel.nio.NioEventLoopGroup;
|
||||||
|
import io.netty.channel.socket.SocketChannel;
|
||||||
|
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||||
|
import io.netty.handler.logging.LoggingHandler;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
public class RedisSend {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final byte[] LINE = {13, 10};
|
||||||
|
NioEventLoopGroup work = new NioEventLoopGroup();
|
||||||
|
try {
|
||||||
|
Bootstrap bootstrap = new Bootstrap();
|
||||||
|
bootstrap.channel(NioSocketChannel.class);
|
||||||
|
bootstrap.group(work);
|
||||||
|
bootstrap.handler(new ChannelInitializer<SocketChannel>() {
|
||||||
|
@Override
|
||||||
|
protected void initChannel(SocketChannel socketChannel) throws Exception {
|
||||||
|
socketChannel.pipeline().addLast(new LoggingHandler());
|
||||||
|
socketChannel.pipeline().addLast(new ChannelInboundHandlerAdapter() {
|
||||||
|
// 在连接建立时发送
|
||||||
|
@Override
|
||||||
|
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||||
|
ByteBuf buf = ctx.alloc().buffer();
|
||||||
|
buf.writeBytes("*2".getBytes());
|
||||||
|
buf.writeBytes(LINE);
|
||||||
|
buf.writeBytes("$3".getBytes());
|
||||||
|
buf.writeBytes(LINE);
|
||||||
|
buf.writeBytes("get".getBytes());
|
||||||
|
buf.writeBytes(LINE);
|
||||||
|
buf.writeBytes("$3".getBytes());
|
||||||
|
buf.writeBytes(LINE);
|
||||||
|
buf.writeBytes("aaa".getBytes());
|
||||||
|
buf.writeBytes(LINE);
|
||||||
|
ctx.writeAndFlush(buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||||
|
ByteBuf buf = (ByteBuf) msg;
|
||||||
|
System.out.println(buf.toString(Charset.defaultCharset()));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
ChannelFuture channelFuture = bootstrap.connect("47.120.65.66", 6379);
|
||||||
|
channelFuture.channel().closeFuture().sync();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
log.error("服务器错误", e);
|
||||||
|
} finally {
|
||||||
|
work.shutdownGracefully();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue