diff --git a/02nio/netty-server/.classpath b/02nio/netty-server/.classpath new file mode 100644 index 00000000..f0257c5a --- /dev/null +++ b/02nio/netty-server/.classpath @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/02nio/netty-server/.project b/02nio/netty-server/.project new file mode 100644 index 00000000..31cf8bba --- /dev/null +++ b/02nio/netty-server/.project @@ -0,0 +1,34 @@ + + + netty-server + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + + + 1604144598701 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + + diff --git a/02nio/netty-server/.settings/org.eclipse.core.resources.prefs b/02nio/netty-server/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 00000000..e9441bb1 --- /dev/null +++ b/02nio/netty-server/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,3 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding/=UTF-8 diff --git a/02nio/netty-server/.settings/org.eclipse.jdt.apt.core.prefs b/02nio/netty-server/.settings/org.eclipse.jdt.apt.core.prefs new file mode 100644 index 00000000..d4313d4b --- /dev/null +++ b/02nio/netty-server/.settings/org.eclipse.jdt.apt.core.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.apt.aptEnabled=false diff --git a/02nio/netty-server/.settings/org.eclipse.jdt.core.prefs b/02nio/netty-server/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 00000000..951833c6 --- /dev/null +++ b/02nio/netty-server/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,10 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.methodParameters=generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore +org.eclipse.jdt.core.compiler.processAnnotations=disabled +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/02nio/netty-server/.settings/org.eclipse.m2e.core.prefs b/02nio/netty-server/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 00000000..f897a7f1 --- /dev/null +++ b/02nio/netty-server/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/02nio/netty-server/.vscode/launch.json b/02nio/netty-server/.vscode/launch.json new file mode 100644 index 00000000..328be1a1 --- /dev/null +++ b/02nio/netty-server/.vscode/launch.json @@ -0,0 +1,21 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "java", + "name": "Debug (Launch) - Current File", + "request": "launch", + "mainClass": "${file}" + }, + { + "type": "java", + "name": "Debug (Launch)-NettyServerApplication", + "request": "launch", + "mainClass": "io.github.kimmking.netty.server.NettyServerApplication", + "projectName": "netty-server" + } + ] +} \ No newline at end of file diff --git a/02nio/netty-server/pom.xml b/02nio/netty-server/pom.xml new file mode 100644 index 00000000..68d92054 --- /dev/null +++ b/02nio/netty-server/pom.xml @@ -0,0 +1,71 @@ + + + 4.0.0 + + io.github.kimmking + netty-server + 0.0.1-SNAPSHOT + jar + + netty-server + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 2.0.4.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + + io.netty + netty-all + 4.1.45.Final + + + + org.slf4j + slf4j-api + 1.7.25 + + + org.slf4j + slf4j-log4j12 + 1.7.25 + + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/02nio/netty-server/src/main/java/io/github/kimmking/netty/server/HttpHandler.java b/02nio/netty-server/src/main/java/io/github/kimmking/netty/server/HttpHandler.java new file mode 100644 index 00000000..85503868 --- /dev/null +++ b/02nio/netty-server/src/main/java/io/github/kimmking/netty/server/HttpHandler.java @@ -0,0 +1,74 @@ +package io.github.kimmking.netty.server; + +import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelFutureListener; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.ChannelInboundHandlerAdapter; +import io.netty.handler.codec.http.DefaultFullHttpResponse; +import io.netty.handler.codec.http.FullHttpRequest; +import io.netty.handler.codec.http.FullHttpResponse; +import io.netty.handler.codec.http.HttpUtil; +import io.netty.util.ReferenceCountUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static io.netty.handler.codec.http.HttpHeaderValues.KEEP_ALIVE; +import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION; +import static io.netty.handler.codec.http.HttpResponseStatus.NO_CONTENT; +import static io.netty.handler.codec.http.HttpResponseStatus.OK; +import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; + +public class HttpHandler extends ChannelInboundHandlerAdapter { + + private static Logger logger = LoggerFactory.getLogger(HttpHandler.class); + + @Override + public void channelReadComplete(ChannelHandlerContext ctx) { + ctx.flush(); + } + + @Override + public void channelRead(ChannelHandlerContext ctx, Object msg) { + try { + //logger.info("channelRead流量接口请求开始,时间为{}", startTime); + FullHttpRequest fullRequest = (FullHttpRequest) msg; + String uri = fullRequest.uri(); + //logger.info("接收到的请求url为{}", uri); + if (uri.contains("/test")) { + handlerTest(fullRequest, ctx); + } + } finally { + ReferenceCountUtil.release(msg); + } + } + + private void handlerTest(FullHttpRequest fullRequest, ChannelHandlerContext ctx) { + FullHttpResponse response = null; + try { + String value = "hello,kimmking"; + response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(value.getBytes("UTF-8"))); + response.headers().set("Content-Type", "application/json"); + response.headers().setInt("Content-Length", response.content().readableBytes()); + + } catch (Exception e) { + logger.error("处理测试接口出错", e); + response = new DefaultFullHttpResponse(HTTP_1_1, NO_CONTENT); + } finally { + if (fullRequest != null) { + if (!HttpUtil.isKeepAlive(fullRequest)) { + ctx.write(response).addListener(ChannelFutureListener.CLOSE); + } else { + response.headers().set(CONNECTION, KEEP_ALIVE); + ctx.write(response); + } + } + } + } + + @Override + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { + cause.printStackTrace(); + ctx.close(); + } + +} diff --git a/02nio/netty-server/src/main/java/io/github/kimmking/netty/server/HttpInitializer.java b/02nio/netty-server/src/main/java/io/github/kimmking/netty/server/HttpInitializer.java new file mode 100644 index 00000000..ec82a258 --- /dev/null +++ b/02nio/netty-server/src/main/java/io/github/kimmking/netty/server/HttpInitializer.java @@ -0,0 +1,28 @@ +package io.github.kimmking.netty.server; + +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelPipeline; +import io.netty.channel.socket.SocketChannel; +import io.netty.handler.codec.http.HttpObjectAggregator; +import io.netty.handler.codec.http.HttpServerCodec; +import io.netty.handler.ssl.SslContext; + +public class HttpInitializer extends ChannelInitializer { + private final SslContext sslCtx; + + public HttpInitializer(SslContext sslCtx) { + this.sslCtx = sslCtx; + } + + @Override + public void initChannel(SocketChannel ch) { + ChannelPipeline p = ch.pipeline(); + if (sslCtx != null) { + p.addLast(sslCtx.newHandler(ch.alloc())); + } + p.addLast(new HttpServerCodec()); + //p.addLast(new HttpServerExpectContinueHandler()); + p.addLast(new HttpObjectAggregator(1024 * 1024)); + p.addLast(new HttpHandler()); + } +} diff --git a/02nio/netty-server/src/main/java/io/github/kimmking/netty/server/HttpServer.java b/02nio/netty-server/src/main/java/io/github/kimmking/netty/server/HttpServer.java new file mode 100644 index 00000000..85441dda --- /dev/null +++ b/02nio/netty-server/src/main/java/io/github/kimmking/netty/server/HttpServer.java @@ -0,0 +1,65 @@ +package io.github.kimmking.netty.server; + +import io.netty.bootstrap.ServerBootstrap; +import io.netty.buffer.PooledByteBufAllocator; +import io.netty.channel.Channel; +import io.netty.channel.ChannelOption; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.epoll.EpollChannelOption; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioServerSocketChannel; +import io.netty.handler.logging.LogLevel; +import io.netty.handler.logging.LoggingHandler; +import io.netty.handler.ssl.SslContext; +import io.netty.handler.ssl.util.SelfSignedCertificate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class HttpServer { + private static Logger logger = LoggerFactory.getLogger(HttpServer.class); + + private boolean ssl; + private int port; + + public HttpServer(boolean ssl,int port) { + this.port=port; + this.ssl=ssl; + } + + public void run() throws Exception { + final SslContext sslCtx; + if (ssl) { + SelfSignedCertificate ssc = new SelfSignedCertificate(); + sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey()); + } else { + sslCtx = null; + } + + EventLoopGroup bossGroup = new NioEventLoopGroup(3); + EventLoopGroup workerGroup = new NioEventLoopGroup(1000); + + try { + ServerBootstrap b = new ServerBootstrap(); + b.option(ChannelOption.SO_BACKLOG, 128) + .option(ChannelOption.TCP_NODELAY, true) + .option(ChannelOption.SO_KEEPALIVE, true) + .option(ChannelOption.SO_REUSEADDR, true) + .option(ChannelOption.SO_RCVBUF, 32 * 1024) + .option(ChannelOption.SO_SNDBUF, 32 * 1024) + .option(EpollChannelOption.SO_REUSEPORT, true) + .childOption(ChannelOption.SO_KEEPALIVE, true); + //.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); + + b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) + .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new HttpInitializer(sslCtx)); + + Channel ch = b.bind(port).sync().channel(); + logger.info("开启netty http服务器,监听地址和端口为 " + (ssl ? "https" : "http") + "://127.0.0.1:" + port + '/'); + ch.closeFuture().sync(); + } finally { + bossGroup.shutdownGracefully(); + workerGroup.shutdownGracefully(); + } + } +} diff --git a/02nio/netty-server/src/main/java/io/github/kimmking/netty/server/NettyServerApplication.java b/02nio/netty-server/src/main/java/io/github/kimmking/netty/server/NettyServerApplication.java new file mode 100644 index 00000000..96b34264 --- /dev/null +++ b/02nio/netty-server/src/main/java/io/github/kimmking/netty/server/NettyServerApplication.java @@ -0,0 +1,14 @@ +package io.github.kimmking.netty.server; + + +public class NettyServerApplication { + + public static void main(String[] args) { + HttpServer server = new HttpServer(false,8808); + try { + server.run(); + }catch (Exception ex){ + ex.printStackTrace(); + } + } +} diff --git a/02nio/nio01/.classpath b/02nio/nio01/.classpath new file mode 100644 index 00000000..f0257c5a --- /dev/null +++ b/02nio/nio01/.classpath @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/02nio/nio01/.project b/02nio/nio01/.project new file mode 100644 index 00000000..f5709381 --- /dev/null +++ b/02nio/nio01/.project @@ -0,0 +1,34 @@ + + + nio01 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + + + 1604141944224 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + + diff --git a/02nio/nio01/.settings/org.eclipse.jdt.apt.core.prefs b/02nio/nio01/.settings/org.eclipse.jdt.apt.core.prefs new file mode 100644 index 00000000..d4313d4b --- /dev/null +++ b/02nio/nio01/.settings/org.eclipse.jdt.apt.core.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.apt.aptEnabled=false diff --git a/02nio/nio01/.settings/org.eclipse.jdt.core.prefs b/02nio/nio01/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 00000000..1b6e1ef2 --- /dev/null +++ b/02nio/nio01/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,9 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore +org.eclipse.jdt.core.compiler.processAnnotations=disabled +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/02nio/nio01/.settings/org.eclipse.m2e.core.prefs b/02nio/nio01/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 00000000..f897a7f1 --- /dev/null +++ b/02nio/nio01/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/02nio/nio02/.classpath b/02nio/nio02/.classpath new file mode 100644 index 00000000..f0257c5a --- /dev/null +++ b/02nio/nio02/.classpath @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/02nio/nio02/.gitignore b/02nio/nio02/.gitignore new file mode 100644 index 00000000..b83d2226 --- /dev/null +++ b/02nio/nio02/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/02nio/nio02/.project b/02nio/nio02/.project new file mode 100644 index 00000000..92a4b3ca --- /dev/null +++ b/02nio/nio02/.project @@ -0,0 +1,34 @@ + + + netty-gateway + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + + + 1604141944221 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + + diff --git a/02nio/nio02/.settings/org.eclipse.core.resources.prefs b/02nio/nio02/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 00000000..e9441bb1 --- /dev/null +++ b/02nio/nio02/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,3 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding/=UTF-8 diff --git a/02nio/nio02/.settings/org.eclipse.jdt.apt.core.prefs b/02nio/nio02/.settings/org.eclipse.jdt.apt.core.prefs new file mode 100644 index 00000000..d4313d4b --- /dev/null +++ b/02nio/nio02/.settings/org.eclipse.jdt.apt.core.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.apt.aptEnabled=false diff --git a/02nio/nio02/.settings/org.eclipse.jdt.core.prefs b/02nio/nio02/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 00000000..951833c6 --- /dev/null +++ b/02nio/nio02/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,10 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.methodParameters=generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore +org.eclipse.jdt.core.compiler.processAnnotations=disabled +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/02nio/nio02/.settings/org.eclipse.m2e.core.prefs b/02nio/nio02/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 00000000..f897a7f1 --- /dev/null +++ b/02nio/nio02/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/02nio/nio02/.vscode/settings.json b/02nio/nio02/.vscode/settings.json new file mode 100644 index 00000000..11331294 --- /dev/null +++ b/02nio/nio02/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "java.configuration.updateBuildConfiguration": "automatic" +} \ No newline at end of file diff --git a/02nio/nio02/pom.xml b/02nio/nio02/pom.xml index 6cbbeffd..2c0e36e5 100644 --- a/02nio/nio02/pom.xml +++ b/02nio/nio02/pom.xml @@ -52,6 +52,13 @@ httpasyncclient 4.1.4 + + + com.squareup.okhttp3 + okhttp + 3.6.0 + +