├── pom.xml ├── tcp_client ├── .gitignore ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── tcp │ │ ├── TcpClientApplication.java │ │ ├── controller │ │ └── HandController.java │ │ └── server │ │ ├── TcpClient.java │ │ └── TcpClientHandler.java │ └── resources │ └── application.yml └── tcp_server ├── .gitignore ├── pom.xml └── src └── main ├── java └── com │ └── tcp │ ├── TcpServerApplication.java │ └── server │ ├── TcpServer.java │ └── TcpServerHandler.java └── resources └── application.yml /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 2.1.5.RELEASE 9 | 10 | 11 | com.tcp 12 | tcp_project 13 | 0.0.1-SNAPSHOT 14 | tcp_project 15 | Demo project for Spring Boot 16 | pom 17 | 18 | 19 | tcp_server 20 | tcp_client 21 | 22 | 23 | 24 | UTF-8 25 | UTF-8 26 | 1.8 27 | 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-starter 33 | 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-starter-web 38 | 39 | 40 | 41 | io.netty 42 | netty-all 43 | 4.1.4.Final 44 | 45 | 46 | 47 | org.springframework.boot 48 | spring-boot-starter-amqp 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | org.springframework.boot 58 | spring-boot-maven-plugin 59 | 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /tcp_client/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | /target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | 5 | ### STS ### 6 | .apt_generated 7 | .classpath 8 | .factorypath 9 | .project 10 | .settings 11 | .springBeans 12 | .sts4-cache 13 | 14 | ### IntelliJ IDEA ### 15 | .idea 16 | *.iws 17 | *.iml 18 | *.ipr 19 | 20 | ### NetBeans ### 21 | /nbproject/private/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ 26 | /build/ 27 | 28 | ### VS Code ### 29 | .vscode/ 30 | -------------------------------------------------------------------------------- /tcp_client/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.tcp 7 | tcp_project 8 | 0.0.1-SNAPSHOT 9 | 10 | 11 | com.tcp 12 | tcp_client 13 | 0.0.1-SNAPSHOT 14 | tcp_client 15 | Demo project for Spring Boot 16 | jar 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-maven-plugin 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /tcp_client/src/main/java/com/tcp/TcpClientApplication.java: -------------------------------------------------------------------------------- 1 | package com.tcp; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class TcpClientApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(TcpClientApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /tcp_client/src/main/java/com/tcp/controller/HandController.java: -------------------------------------------------------------------------------- 1 | package com.tcp.controller; 2 | 3 | import com.tcp.server.TcpClient; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.web.bind.annotation.GetMapping; 6 | import org.springframework.web.bind.annotation.RestController; 7 | 8 | @RestController 9 | public class HandController { 10 | 11 | @Autowired 12 | private TcpClient tcpClient; 13 | 14 | @GetMapping("/sent") 15 | public String sentMsg(){ 16 | try { 17 | tcpClient.sendMsg("1111"); 18 | } catch (Exception e) { 19 | return e.toString(); 20 | } 21 | return "1111"; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /tcp_client/src/main/java/com/tcp/server/TcpClient.java: -------------------------------------------------------------------------------- 1 | package com.tcp.server; 2 | 3 | import io.netty.bootstrap.Bootstrap; 4 | import io.netty.channel.*; 5 | import io.netty.channel.nio.NioEventLoopGroup; 6 | import io.netty.channel.socket.nio.NioSocketChannel; 7 | import io.netty.handler.codec.LengthFieldBasedFrameDecoder; 8 | import io.netty.handler.codec.LengthFieldPrepender; 9 | import io.netty.handler.codec.string.StringDecoder; 10 | import io.netty.handler.codec.string.StringEncoder; 11 | import io.netty.util.CharsetUtil; 12 | import org.springframework.stereotype.Component; 13 | 14 | @Component 15 | public class TcpClient { 16 | 17 | public static String HOST = "127.0.0.1"; 18 | public static int PORT = 9999; 19 | 20 | public static Bootstrap bootstrap = getBootstrap(); 21 | public static Channel channel = getChannel(HOST, PORT); 22 | 23 | /** 24 | * 初始化Bootstrap 25 | */ 26 | public static final Bootstrap getBootstrap() { 27 | EventLoopGroup group = new NioEventLoopGroup(); 28 | Bootstrap b = new Bootstrap(); 29 | b.group(group).channel(NioSocketChannel.class); 30 | b.handler(new ChannelInitializer() { 31 | @Override 32 | protected void initChannel(Channel ch) throws Exception { 33 | ChannelPipeline pipeline = ch.pipeline(); 34 | pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); 35 | pipeline.addLast("frameEncoder", new LengthFieldPrepender(4)); 36 | pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8)); 37 | pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8)); 38 | pipeline.addLast("handler", new TcpClientHandler()); 39 | } 40 | }); 41 | 42 | b.option(ChannelOption.SO_KEEPALIVE, true); 43 | return b; 44 | } 45 | 46 | public static final Channel getChannel(String host, int port) { 47 | Channel channel = null; 48 | try { 49 | channel = bootstrap.connect(host, port).sync().channel(); 50 | System.out.println("TcpClient启动成功"); 51 | } catch (Exception e) { 52 | System.out.println("连接Server失败" + e); 53 | return null; 54 | } 55 | return channel; 56 | } 57 | 58 | public void sendMsg(String msg) throws Exception { 59 | if (channel != null) { 60 | channel.writeAndFlush(msg).sync(); 61 | } else { 62 | System.out.println("消息发送失败,连接尚未建立!"); 63 | } 64 | } 65 | 66 | /*public static void main(String[] args) throws Exception { 67 | try { 68 | long t0 = System.nanoTime(); 69 | for (int i = 0; i < 100; i++) { 70 | TcpClient.sendMsg(i + "你好1"); 71 | } 72 | long t1 = System.nanoTime(); 73 | System.out.println("time used:" + (t1 - t0)); 74 | } catch (Exception e) { 75 | System.out.println("main err:" + e); 76 | } 77 | }*/ 78 | } 79 | -------------------------------------------------------------------------------- /tcp_client/src/main/java/com/tcp/server/TcpClientHandler.java: -------------------------------------------------------------------------------- 1 | package com.tcp.server; 2 | 3 | import io.netty.channel.ChannelHandlerContext; 4 | import io.netty.channel.SimpleChannelInboundHandler; 5 | 6 | public class TcpClientHandler extends SimpleChannelInboundHandler { 7 | 8 | @Override 9 | protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { 10 | System.out.println("client接收到服务器返回的消息:" + msg); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /tcp_client/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 9091 3 | 4 | spring: 5 | rabbitmq: 6 | host: 127.0.0.1 7 | port: 5672 8 | username: finance 9 | password: finance -------------------------------------------------------------------------------- /tcp_server/.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | /target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | 5 | ### STS ### 6 | .apt_generated 7 | .classpath 8 | .factorypath 9 | .project 10 | .settings 11 | .springBeans 12 | .sts4-cache 13 | 14 | ### IntelliJ IDEA ### 15 | .idea 16 | *.iws 17 | *.iml 18 | *.ipr 19 | 20 | ### NetBeans ### 21 | /nbproject/private/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ 26 | /build/ 27 | 28 | ### VS Code ### 29 | .vscode/ 30 | -------------------------------------------------------------------------------- /tcp_server/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | com.tcp 7 | tcp_project 8 | 0.0.1-SNAPSHOT 9 | 10 | 11 | com.tcp 12 | tcp_server 13 | 0.0.1-SNAPSHOT 14 | tcp_server 15 | Demo project for Spring Boot 16 | jar 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-maven-plugin 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /tcp_server/src/main/java/com/tcp/TcpServerApplication.java: -------------------------------------------------------------------------------- 1 | package com.tcp; 2 | 3 | import com.tcp.server.TcpServer; 4 | import org.springframework.boot.SpringApplication; 5 | import org.springframework.boot.autoconfigure.SpringBootApplication; 6 | 7 | //@SpringBootApplication 8 | public class TcpServerApplication { 9 | 10 | public static void main(String[] args) { 11 | // SpringApplication.run(TcpServerApplication.class, args); 12 | TcpServer.run(); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /tcp_server/src/main/java/com/tcp/server/TcpServer.java: -------------------------------------------------------------------------------- 1 | package com.tcp.server; 2 | 3 | import io.netty.bootstrap.ServerBootstrap; 4 | import io.netty.channel.ChannelInitializer; 5 | import io.netty.channel.ChannelPipeline; 6 | import io.netty.channel.EventLoopGroup; 7 | import io.netty.channel.nio.NioEventLoopGroup; 8 | import io.netty.channel.socket.SocketChannel; 9 | import io.netty.channel.socket.nio.NioServerSocketChannel; 10 | import io.netty.handler.codec.LengthFieldBasedFrameDecoder; 11 | import io.netty.handler.codec.LengthFieldPrepender; 12 | import io.netty.handler.codec.string.StringDecoder; 13 | import io.netty.handler.codec.string.StringEncoder; 14 | import io.netty.util.CharsetUtil; 15 | 16 | public class TcpServer { 17 | 18 | private static final String IP = "127.0.0.1"; 19 | private static final int PORT = 9999; 20 | /** 用于分配处理业务线程的线程组个数 */ 21 | protected static final int BIZGROUPSIZE = Runtime.getRuntime().availableProcessors() * 2; // 默认 22 | /** 业务出现线程大小 */ 23 | protected static final int BIZTHREADSIZE = 4; 24 | /* 25 | * NioEventLoopGroup实际上就是个线程池, 26 | * NioEventLoopGroup在后台启动了n个NioEventLoop来处理Channel事件, 27 | * 每一个NioEventLoop负责处理m个Channel, 28 | * NioEventLoopGroup从NioEventLoop数组里挨个取出NioEventLoop来处理Channel 29 | */ 30 | private static final EventLoopGroup bossGroup = new NioEventLoopGroup(BIZGROUPSIZE); 31 | private static final EventLoopGroup workerGroup = new NioEventLoopGroup(BIZTHREADSIZE); 32 | 33 | public static void run(){ 34 | try { 35 | ServerBootstrap b = new ServerBootstrap(); 36 | b.group(bossGroup, workerGroup); 37 | b.channel(NioServerSocketChannel.class); 38 | b.childHandler(new ChannelInitializer() { 39 | @Override 40 | public void initChannel(SocketChannel ch) throws Exception { 41 | ChannelPipeline pipeline = ch.pipeline(); 42 | pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); 43 | pipeline.addLast("frameEncoder", new LengthFieldPrepender(4)); 44 | pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8)); 45 | pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8)); 46 | pipeline.addLast(new TcpServerHandler()); 47 | } 48 | }); 49 | 50 | b.bind(IP, PORT).sync(); 51 | System.out.println("TCP服务器已启动"); 52 | } catch (InterruptedException e) { 53 | System.out.println(e); 54 | } 55 | } 56 | 57 | protected static void shutdown() { 58 | workerGroup.shutdownGracefully(); 59 | bossGroup.shutdownGracefully(); 60 | } 61 | 62 | /*public static void main(String[] args) throws Exception { 63 | System.out.println("启动TCP服务器..."); 64 | TcpServer.run(); 65 | // TcpServer.shutdown(); 66 | }*/ 67 | 68 | } 69 | -------------------------------------------------------------------------------- /tcp_server/src/main/java/com/tcp/server/TcpServerHandler.java: -------------------------------------------------------------------------------- 1 | package com.tcp.server; 2 | 3 | import io.netty.channel.ChannelHandlerContext; 4 | import io.netty.channel.SimpleChannelInboundHandler; 5 | 6 | public class TcpServerHandler extends SimpleChannelInboundHandler { 7 | 8 | @Override 9 | protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { 10 | System.out.println("SERVER接收到消息:" + msg); 11 | ctx.channel().writeAndFlush("server accepted msg:" + msg); 12 | } 13 | 14 | @Override 15 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { 16 | System.out.println("exceptionCaught!" + cause); 17 | ctx.close(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tcp_server/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | server: 2 | port: 9090 --------------------------------------------------------------------------------