X Tutup
Skip to content

Commit 017ff24

Browse files
committed
add netty
1 parent 2a41d85 commit 017ff24

File tree

12 files changed

+345
-1
lines changed

12 files changed

+345
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ JdkLearn/learnjava.iml
66
Spring-Boot/learnsb.iml
77
Spring/learnspring.iml
88
Spring-AOP/learnaop.iml
9+
Spring-Netty/Spring-Netty.iml
910

1011
# target
1112
JdkLearn/target
1213
Spring/target
1314
Spring-AOP/target
1415
Spring-Boot/target
16+
Spring-Netty/target

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,4 @@
3838

3939
- [深入SpringBoot源码学习之——SpringFactoriesLoader](https://blog.csdn.net/CoderBruis/article/details/106559304)
4040
- [深入SpringBoot源码学习之——系统初始化器](https://blog.csdn.net/CoderBruis/article/details/106610007)
41-
)
4241

Spring-Netty/pom.xml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.2.1.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.bruis</groupId>
12+
<artifactId>learnnetty</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>learnnetty</name>
15+
<description>Project for Learn Netty</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
<netty-all.version>4.1.6.Final</netty-all.version>
20+
</properties>
21+
22+
<dependencies>
23+
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-web</artifactId>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-test</artifactId>
32+
<scope>test</scope>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>io.netty</groupId>
37+
<artifactId>netty-all</artifactId>
38+
<version>${netty-all.version}</version>
39+
</dependency>
40+
41+
</dependencies>
42+
43+
<build>
44+
<plugins>
45+
<plugin>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-maven-plugin</artifactId>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
52+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.bruis.learnnetty;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class LearnnettyApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(LearnnettyApplication.class, args);
11+
}
12+
13+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.bruis.learnnetty.bio;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.net.Socket;
6+
import java.util.Scanner;
7+
8+
/**
9+
* @author LuoHaiYang
10+
*
11+
* 在bio的例子中,主要有server端和client端:
12+
*
13+
* 1. Server端开启一个线程通过serverSocket.accept()来监听有没有socket连接进来,之列称之为线程A。
14+
* 2. 然后在ClientHandler中又开启了一个线程,称为B,用于从socket中获取服务器端传过来的消息,线程B中通过while-true循环来获取。
15+
* 3. 在client客户端中,还有一个线程称为C,一直去循环获取服务端的数据。
16+
*
17+
* 在nio中,NioEventLoop源码中的run方法就对应着线程A的while-true方法以及线程c的while-true方法。而NioEventLoop的select方法就对应着serverSocket的accept操作。
18+
*
19+
*/
20+
public class Client {
21+
private static final String HOST = "127.0.0.1";
22+
private static final int PORT = 8000;
23+
private static final int SLEEP_TIME = 5000;
24+
public static final int MAX_DATA_LEN = 1024;
25+
26+
public static void main(String[] args) throws IOException {
27+
28+
final Socket socket = new Socket(HOST, PORT);
29+
System.out.println("客户端启动成功!");
30+
31+
while (true) {
32+
try {
33+
Scanner scan = new Scanner(System.in);
34+
String clientMessage = scan.nextLine();
35+
System.out.println("客户端发送数据: " + clientMessage);
36+
socket.getOutputStream().write(clientMessage.getBytes());
37+
38+
InputStream inputStream = socket.getInputStream();
39+
40+
byte[] data = new byte[MAX_DATA_LEN];
41+
int len;
42+
while ((len = inputStream.read(data)) != -1) {
43+
String message = new String(data, 0, len);
44+
System.out.println("服务器传来消息: " + message);
45+
46+
}
47+
48+
} catch (Exception e) {
49+
System.out.println("写数据出错!");
50+
}
51+
}
52+
}
53+
54+
private static void sleep() {
55+
try {
56+
Thread.sleep(SLEEP_TIME);
57+
} catch (InterruptedException e) {
58+
e.printStackTrace();
59+
}
60+
}
61+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.bruis.learnnetty.bio;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.net.Socket;
6+
7+
/**
8+
* @author LuoHaiYang
9+
*/
10+
public class ClientHandler {
11+
public static final int MAX_DATA_LEN = 1024;
12+
private final Socket socket;
13+
14+
public ClientHandler(Socket socket) {
15+
this.socket = socket;
16+
}
17+
18+
public void start() {
19+
System.out.println("新客户端接入");
20+
new Thread(new Runnable() {
21+
@Override
22+
public void run() {
23+
doStart();
24+
}
25+
}).start();
26+
}
27+
28+
private void doStart() {
29+
try {
30+
InputStream inputStream = socket.getInputStream();
31+
while (true) {
32+
byte[] data = new byte[MAX_DATA_LEN];
33+
int len;
34+
while ((len = inputStream.read(data)) != -1) {
35+
String message = new String(data, 0, len);
36+
System.out.println("客户端传来消息: " + message);
37+
//socket.getOutputStream().write(data);
38+
}
39+
}
40+
41+
} catch (IOException e) {
42+
e.printStackTrace();
43+
}
44+
}
45+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.bruis.learnnetty.bio;
2+
3+
import java.io.IOException;
4+
import java.net.ServerSocket;
5+
import java.net.Socket;
6+
7+
/**
8+
* @author LuoHaiYang
9+
*/
10+
public class Server {
11+
private ServerSocket serverSocket;
12+
13+
public Server(int port) {
14+
try {
15+
this.serverSocket = new ServerSocket(port);
16+
System.out.println("服务端启动成功,端口:" + port);
17+
} catch (IOException exception) {
18+
System.out.println("服务端启动失败");
19+
}
20+
}
21+
22+
public void start() {
23+
new Thread(new Runnable() {
24+
@Override
25+
public void run() {
26+
doStart();
27+
}
28+
}).start();
29+
}
30+
31+
private void doStart() {
32+
while (true) {
33+
try {
34+
Socket client = serverSocket.accept();
35+
new ClientHandler(client).start();
36+
} catch (IOException e) {
37+
System.out.println("服务端异常");
38+
}
39+
}
40+
}
41+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.bruis.learnnetty.bio;
2+
3+
/**
4+
* @author LuoHaiYang
5+
*/
6+
public class ServerBoot {
7+
8+
private static final int PORT = 8000;
9+
10+
public static void main(String[] args) {
11+
Server server = new Server(PORT);
12+
server.start();
13+
}
14+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.bruis.learnnetty.netty.demo01;
2+
3+
import io.netty.bootstrap.ServerBootstrap;
4+
import io.netty.channel.ChannelFuture;
5+
import io.netty.channel.ChannelInitializer;
6+
import io.netty.channel.ChannelOption;
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+
import io.netty.util.AttributeKey;
12+
13+
/**
14+
* @author LuoHaiYang
15+
*/
16+
public class Server {
17+
public static void main(String[] args) throws Exception {
18+
19+
// bossGroup对应着socket编程中的服务端的Thread,用于监听是否有client连接
20+
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
21+
// workGroup对应着socket编程中的数据读取,读取server端读到的数据
22+
EventLoopGroup workGroup = new NioEventLoopGroup();
23+
24+
try {
25+
ServerBootstrap bootstrap = new ServerBootstrap();
26+
bootstrap.group(bossGroup, workGroup)
27+
// 给服务端channel设置SocketChannel类型
28+
.channel(NioServerSocketChannel.class)
29+
// 给每个客户端连接设置TCP基本属性
30+
.childOption(ChannelOption.TCP_NODELAY, true)
31+
// 绑定属性
32+
.childAttr(AttributeKey.newInstance("childAttr"), "childAttrValue")
33+
// handler表示客户端的一个处理逻辑
34+
.handler(new ServerHandler())
35+
.childHandler(new ChannelInitializer<SocketChannel>() {
36+
@Override
37+
public void initChannel(SocketChannel channel) {
38+
//channel.pipeline().addLast(new AuthHandler());
39+
}
40+
});
41+
ChannelFuture channelFuture = bootstrap.bind(8888).sync();
42+
channelFuture.channel().closeFuture().sync();
43+
} finally {
44+
bossGroup.shutdownGracefully();
45+
workGroup.shutdownGracefully();
46+
}
47+
48+
}
49+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.bruis.learnnetty.netty.demo01;
2+
3+
import io.netty.channel.ChannelHandlerContext;
4+
import io.netty.channel.ChannelInboundHandlerAdapter;
5+
import java.util.concurrent.TimeUnit;
6+
7+
/**
8+
* @author LuoHaiYang
9+
*/
10+
public class ServerHandler extends ChannelInboundHandlerAdapter {
11+
@Override
12+
public void channelActive(ChannelHandlerContext ctx) {
13+
System.out.println("channelActive");
14+
}
15+
16+
@Override
17+
public void channelRegistered(ChannelHandlerContext ctx) {
18+
System.out.println("channelRegistered");
19+
}
20+
21+
@Override
22+
public void handlerAdded(ChannelHandlerContext ctx) {
23+
System.out.println("handlerAdded");
24+
}
25+
26+
@Override
27+
public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
28+
super.channelRead(ctx, msg);
29+
30+
new Thread(new Runnable() {
31+
@Override
32+
public void run() {
33+
// 耗时的操作
34+
String result = loadFromDB();
35+
36+
ctx.channel().writeAndFlush(result);
37+
ctx.executor().schedule(new Runnable() {
38+
@Override
39+
public void run() {
40+
// ...
41+
}
42+
}, 1, TimeUnit.SECONDS);
43+
44+
}
45+
}).start();
46+
}
47+
48+
private String loadFromDB() {
49+
return "hello world!";
50+
}
51+
}

0 commit comments

Comments
 (0)
X Tutup