8000 添加netty示例 · JavaCourse00/JavaCourseCodes@bd91507 · GitHub
[go: up one dir, main page]

Skip to content

Commit bd91507

Browse files
committed
添加netty示例
1 parent 42a4f1a commit bd91507

File tree

5 files changed

+92
-5
lines changed

5 files changed

+92
-5
lines changed

07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/utils/NettyClientUtils.java renamed to 07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/client/NettyClient.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.kimmking.rpcfx.utils;
1+
package io.kimmking.rpcfx.client;
22

33
import io.kimmking.rpcfx.handler.ClientHandler;
44
import io.netty.bootstrap.Bootstrap;
@@ -10,7 +10,7 @@
1010
import io.netty.channel.socket.nio.NioSocketChannel;
1111
import io.netty.util.CharsetUtil;
1212

13-
public class NettyClientUtils {
13+
public class NettyClient {
1414

1515
private final String host;
1616

@@ -19,7 +19,7 @@ public class NettyClientUtils {
1919
private final String req;
2020

2121

22-
public NettyClientUtils(String host, Integer port, String req) {
22+
public NettyClient(String host, Integer port, String req) {
2323
this.host = host;
2424
this.port = port;
2525
this.req = req;
@@ -50,4 +50,8 @@ protected void initChannel(SocketChannel socketChannel) throws Exception {
5050
return resp;
5151
}
5252

53+
public static void main(String[] args) throws InterruptedException {
54+
new NettyClient("127.0.0.1", 8888, "hello netty").start();
55+
}
56+
5357
}

07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/client/Rpcfx.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import io.kimmking.rpcfx.param.RpcfxRequest;
99
import io.kimmking.rpcfx.param.RpcfxResponse;
1010
import io.kimmking.rpcfx.utils.ClientUtils;
11-
import io.kimmking.rpcfx.utils.NettyClientUtils;
1211
import io.kimmking.rpcfx.utils.XStreamUtils;
1312
import okhttp3.MediaType;
1413
import okhttp3.Request;

07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/handler/ClientHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public ClientHandler(String req) {
1919
@Override
2020
protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {
2121

22-
System.out.println(byteBuf.toString(CharsetUtil.UTF_8));
22+
System.out.println("Client receiver:" + byteBuf.toString(CharsetUtil.UTF_8));
2323

2424
}
2525

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package io.kimmking.rpcfx.handler;
2+
3+
import io.netty.buffer.ByteBuf;
4+
import io.netty.buffer.Unpooled;
5+
import io.netty.channel.*;
6+
import io.netty.util.CharsetUtil;
7+
8+
@ChannelHandler.Sharable
9+
public class ServerHandler extends ChannelInboundHandlerAdapter {
10+
11+
/**
12+
* 读取消息,写到回给发送者
13+
* @param ctx
14+
* @param msg
15+
* @throws Exception
16+
*/
17+
@Override
18+
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
19+
ByteBuf buf = (ByteBuf) msg;
20+
System.out.println("Server receiver:" + buf.toString(CharsetUtil.UTF_8));
21+
ctx.write(buf);
22+
}
23+
24+
25+
@Override
26+
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
27+
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
28+
}
29+
30+
@Override
31+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
32+
cause.printStackTrace();
33+
ctx.close();
34+
}
35+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.kimmking.rpcfx.server;
2+
3+
import io.kimmking.rpcfx.handler.ServerHandler;
4+
import io.netty.bootstrap.ServerBootstrap;
5+
import io.netty.channel.ChannelFuture;
6+
import io.netty.channel.ChannelInitializer;
7+
import io.netty.channel.EventLoopGroup;
8+
import io.netty.channel.nio.NioEventLoopGroup;
9+
import io.netty.channel.socket.SocketChannel;
10+
import io.netty.channel.socket.nio.NioServerSocketChannel;
11+
12+
public class NettyServer {
13+
14+
private final Integer port;
15+
16+
public NettyServer(Integer port) {
17+
this.port = port;
18+
}
19+
20+
public void start() {
21+
22+
EventLoopGroup group = new NioEventLoopGroup();
23+
try {
24+
ServerBootstrap bootstrap = new ServerBootstrap();
25+
bootstrap.group(group).
26+
channel(NioServerSocketChannel.class).
27+
localAddress(port).
28+
childHandler(new ChannelInitializer<SocketChannel>() {
29+
@Override
30+
protected void initChannel(SocketChannel socketChannel) throws Exception {
31+
socketChannel.pipeline().addLast(new ServerHandler());
32+
}
33+
});
34+
ChannelFuture future = bootstrap.bind().sync();
35+
future.channel().closeFuture().sync();
36+
} catch (InterruptedException e) {
37+
e.printStackTrace();
38+
} finally {
39+
group.shutdownGracefully();
40+
}
41+
42+
43+
}
44+
45+
public static void main(String[] args) {
46+
new NettyServer(8888).start();
47+
}
48+
49+
}

0 commit comments

Comments
 (0)
0