Skip to content

Commit 84200f3

Browse files
authored
Merge pull request #7 from JavaCourse00/main
update
2 parents 101d6cc + dbd14b1 commit 84200f3

125 files changed

Lines changed: 10599 additions & 7 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

02nio/nio01/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,13 @@
2020
</plugins>
2121
</build>
2222

23+
<dependencies>
24+
<dependency>
25+
<groupId>io.netty</groupId>
26+
<artifactId>netty-all</artifactId>
27+
<version>4.1.51.Final</version>
28+
</dependency>
29+
</dependencies>
30+
2331

2432
</project>

02nio/nio01/src/main/java/java0/nio01/HttpServer01.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.net.ServerSocket;
66
import java.net.Socket;
77

8+
// 单线程的socket程序
89
public class HttpServer01 {
910
public static void main(String[] args) throws IOException{
1011
ServerSocket serverSocket = new ServerSocket(8801);
@@ -20,7 +21,7 @@ public static void main(String[] args) throws IOException{
2021

2122
private static void service(Socket socket) {
2223
try {
23-
Thread.sleep(20);
24+
// Thread.sleep(5);
2425
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
2526
printWriter.println("HTTP/1.1 200 OK");
2627
printWriter.println("Content-Type:text/html;charset=utf-8");
@@ -30,7 +31,7 @@ private static void service(Socket socket) {
3031
printWriter.write(body);
3132
printWriter.close();
3233
socket.close();
33-
} catch (IOException | InterruptedException e) {
34+
} catch (IOException e) { // | InterruptedException e) {
3435
e.printStackTrace();
3536
}
3637
}

02nio/nio01/src/main/java/java0/nio01/HttpServer02.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.net.ServerSocket;
66
import java.net.Socket;
77

8+
// 每个请求一个线程
89
public class HttpServer02 {
910
public static void main(String[] args) throws IOException{
1011
ServerSocket serverSocket = new ServerSocket(8802);
@@ -22,7 +23,7 @@ public static void main(String[] args) throws IOException{
2223

2324
private static void service(Socket socket) {
2425
try {
25-
Thread.sleep(20);
26+
// Thread.sleep(5);
2627
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
2728
printWriter.println("HTTP/1.1 200 OK");
2829
printWriter.println("Content-Type:text/html;charset=utf-8");
@@ -32,7 +33,7 @@ private static void service(Socket socket) {
3233
printWriter.write(body);
3334
printWriter.close();
3435
socket.close();
35-
} catch (IOException | InterruptedException e) {
36+
} catch (IOException e) { // | InterruptedException e) {
3637
e.printStackTrace();
3738
}
3839
}

02nio/nio01/src/main/java/java0/nio01/HttpServer03.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
import java.util.concurrent.ExecutorService;
88
import java.util.concurrent.Executors;
99

10+
// 创建了一个固定大小的线程池处理请求
1011
public class HttpServer03 {
1112
public static void main(String[] args) throws IOException{
12-
ExecutorService executorService = Executors.newFixedThreadPool(40);
13+
14+
ExecutorService executorService = Executors.newFixedThreadPool(
15+
Runtime.getRuntime().availableProcessors() + 2);
1316
final ServerSocket serverSocket = new ServerSocket(8803);
1417
while (true) {
1518
try {
@@ -23,7 +26,7 @@ public static void main(String[] args) throws IOException{
2326

2427
private static void service(Socket socket) {
2528
try {
26-
Thread.sleep(20);
29+
// Thread.sleep(5);
2730
PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true);
2831
printWriter.println("HTTP/1.1 200 OK");
2932
printWriter.println("Content-Type:text/html;charset=utf-8");
@@ -33,7 +36,7 @@ private static void service(Socket socket) {
3336
printWriter.write(body);
3437
printWriter.close();
3538
socket.close();
36-
} catch (IOException | InterruptedException e) {
39+
} catch (IOException e) { // | InterruptedException e) {
3740
e.printStackTrace();
3841
}
3942
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package java0.nio01.netty;
2+
3+
import io.netty.buffer.Unpooled;
4+
import io.netty.channel.ChannelFutureListener;
5+
import io.netty.channel.ChannelHandlerContext;
6+
import io.netty.channel.ChannelInboundHandlerAdapter;
7+
import io.netty.handler.codec.http.DefaultFullHttpResponse;
8+
import io.netty.handler.codec.http.FullHttpRequest;
9+
import io.netty.handler.codec.http.FullHttpResponse;
10+
import io.netty.handler.codec.http.HttpUtil;
11+
import io.netty.util.ReferenceCountUtil;
12+
13+
import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION;
14+
import static io.netty.handler.codec.http.HttpHeaderValues.KEEP_ALIVE;
15+
import static io.netty.handler.codec.http.HttpResponseStatus.NO_CONTENT;
16+
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
17+
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
18+
19+
public class HttpHandler extends ChannelInboundHandlerAdapter {
20+
21+
@Override
22+
public void channelReadComplete(ChannelHandlerContext ctx) {
23+
ctx.flush();
24+
}
25+
26+
@Override
27+
public void channelRead(ChannelHandlerContext ctx, Object msg) {
28+
try {
29+
//logger.info("channelRead流量接口请求开始,时间为{}", startTime);
30+
FullHttpRequest fullRequest = (FullHttpRequest) msg;
31+
String uri = fullRequest.uri();
32+
//logger.info("接收到的请求url为{}", uri);
33+
if (uri.contains("/test")) {
34+
handlerTest(fullRequest, ctx);
35+
}
36+
37+
} catch(Exception e) {
38+
e.printStackTrace();
39+
} finally {
40+
ReferenceCountUtil.release(msg);
41+
}
42+
}
43+
44+
private void handlerTest(FullHttpRequest fullRequest, ChannelHandlerContext ctx) {
45+
FullHttpResponse response = null;
46+
try {
47+
String value = "hello,kimmking";
48+
response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(value.getBytes("UTF-8")));
49+
response.headers().set("Content-Type", "application/json");
50+
response.headers().setInt("Content-Length", response.content().readableBytes());
51+
52+
} catch (Exception e) {
53+
System.out.println("处理出错:"+e.getMessage());
54+
response = new DefaultFullHttpResponse(HTTP_1_1, NO_CONTENT);
55+
} finally {
56+
if (fullRequest != null) {
57+
if (!HttpUtil.isKeepAlive(fullRequest)) {
58+
ctx.write(response).addListener(ChannelFutureListener.CLOSE);
59+
} else {
60+
response.headers().set(CONNECTION, KEEP_ALIVE);
61+
ctx.write(response);
62+
}
63+
}
64+
}
65+
}
66+
67+
@Override
68+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
69+
cause.printStackTrace();
70+
ctx.close();
71+
}
72+
73+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package java0.nio01.netty;
2+
3+
import io.netty.channel.ChannelInitializer;
4+
import io.netty.channel.ChannelPipeline;
5+
import io.netty.channel.socket.SocketChannel;
6+
import io.netty.handler.codec.http.HttpObjectAggregator;
7+
import io.netty.handler.codec.http.HttpServerCodec;
8+
9+
public class HttpInitializer extends ChannelInitializer<SocketChannel> {
10+
11+
@Override
12+
public void initChannel(SocketChannel ch) {
13+
ChannelPipeline p = ch.pipeline();
14+
p.addLast(new HttpServerCodec());
15+
//p.addLast(new HttpServerExpectContinueHandler());
16+
p.addLast(new HttpObjectAggregator(1024 * 1024));
17+
p.addLast(new HttpHandler());
18+
}
19+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package java0.nio01.netty;
2+
3+
import io.netty.bootstrap.ServerBootstrap;
4+
import io.netty.buffer.PooledByteBufAllocator;
5+
import io.netty.channel.Channel;
6+
import io.netty.channel.ChannelOption;
7+
import io.netty.channel.EventLoopGroup;
8+
import io.netty.channel.epoll.EpollChannelOption;
9+
import io.netty.channel.nio.NioEventLoopGroup;
10+
import io.netty.channel.socket.nio.NioServerSocketChannel;
11+
import io.netty.handler.logging.LogLevel;
12+
import io.netty.handler.logging.LoggingHandler;
13+
14+
public class NettyHttpServer {
15+
public static void main(String[] args) throws InterruptedException {
16+
17+
int port = 8808;
18+
19+
EventLoopGroup bossGroup = new NioEventLoopGroup(2);
20+
EventLoopGroup workerGroup = new NioEventLoopGroup(16);
21+
22+
try {
23+
ServerBootstrap b = new ServerBootstrap();
24+
b.option(ChannelOption.SO_BACKLOG, 128)
25+
.option(ChannelOption.TCP_NODELAY, true)
26+
.option(ChannelOption.SO_KEEPALIVE, true)
27+
.option(ChannelOption.SO_REUSEADDR, true)
28+
.option(ChannelOption.SO_RCVBUF, 32 * 1024)
29+
.option(ChannelOption.SO_SNDBUF, 32 * 1024)
30+
.option(EpollChannelOption.SO_REUSEPORT, true)
31+
.childOption(ChannelOption.SO_KEEPALIVE, true)
32+
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
33+
34+
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
35+
.handler(new LoggingHandler(LogLevel.INFO))
36+
.childHandler(new HttpInitializer());
37+
38+
Channel ch = b.bind(port).sync().channel();
39+
System.out.println("开启netty http服务器,监听地址和端口为 http://127.0.0.1:" + port + '/');
40+
ch.closeFuture().sync();
41+
} finally {
42+
bossGroup.shutdownGracefully();
43+
workerGroup.shutdownGracefully();
44+
}
45+
46+
47+
}
48+
}

08cache/cache/pom.xml

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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.0.9.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>io.kimmking.08cache</groupId>
12+
<artifactId>cache</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>cache</name>
15+
<description>Demo project for Spring Boot</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-jdbc</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-web</artifactId>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.mybatis.spring.boot</groupId>
36+
<artifactId>mybatis-spring-boot-starter</artifactId>
37+
<version>2.1.4</version>
38+
</dependency>
39+
<dependency>
40+
<groupId>mysql</groupId>
41+
<artifactId>mysql-connector-java</artifactId>
42+
<version>5.1.47</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.projectlombok</groupId>
46+
<artifactId>lombok</artifactId>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-cache</artifactId>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-starter-data-redis</artifactId>
55+
</dependency>
56+
<dependency>
57+
<groupId>io.lettuce</groupId>
58+
<artifactId>lettuce-core</artifactId>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.apache.commons</groupId>
62+
<artifactId>commons-pool2</artifactId>
63+
</dependency>
64+
<dependency>
65+
<groupId>net.sf.ehcache</groupId>
66+
<artifactId>ehcache</artifactId>
67+
<version>2.8.3</version>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.mybatis</groupId>
71+
<artifactId>mybatis-ehcache</artifactId>
72+
<version>1.0.0</version>
73+
</dependency>
74+
75+
<dependency>
76+
<groupId>org.springframework.boot</groupId>
77+
<artifactId>spring-boot-starter-test</artifactId>
78+
<scope>test</scope>
79+
<exclusions>
80+
<exclusion>
81+
<groupId>org.junit.vintage</groupId>
82+
<artifactId>junit-vintage-engine</artifactId>
83+
</exclusion>
84+
</exclusions>
85+
</dependency>
86+
</dependencies>
87+
88+
<build>
89+
<plugins>
90+
<plugin>
91+
<groupId>org.springframework.boot</groupId>
92+
<artifactId>spring-boot-maven-plugin</artifactId>
93+
</plugin>
94+
</plugins>
95+
</build>
96+
</project>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.kimmking.cache;
2+
3+
import org.mybatis.spring.annotation.MapperScan;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.cache.annotation.EnableCaching;
7+
8+
@SpringBootApplication(scanBasePackages = "io.kimmking.cache")
9+
@MapperScan("io.kimmking.cache.mapper")
10+
@EnableCaching
11+
public class CacheApplication {
12+
13+
public static void main(String[] args) {
14+
SpringApplication.run(CacheApplication.class, args);
15+
}
16+
17+
}

0 commit comments

Comments
 (0)