├── spring-boot-netty
├── study-netty
│ ├── study-client
│ │ ├── src
│ │ │ ├── main
│ │ │ │ ├── resources
│ │ │ │ │ └── application.properties
│ │ │ │ └── java
│ │ │ │ │ └── org
│ │ │ │ │ └── yyx
│ │ │ │ │ └── netty
│ │ │ │ │ └── study
│ │ │ │ │ ├── websocket
│ │ │ │ │ ├── WebSocketConstant.java
│ │ │ │ │ ├── WebSocketMessage.java
│ │ │ │ │ ├── WebSocketHandlerClient.java
│ │ │ │ │ ├── WebSocketClient.java
│ │ │ │ │ ├── WebSocketUsers.java
│ │ │ │ │ └── WebSocketClientHandler.java
│ │ │ │ │ ├── StudyClientApplication.java
│ │ │ │ │ ├── codec
│ │ │ │ │ └── msgpack
│ │ │ │ │ │ ├── MsgPackDecoder.java
│ │ │ │ │ │ └── MsgPackEncoder.java
│ │ │ │ │ ├── echo
│ │ │ │ │ ├── delimiter
│ │ │ │ │ │ ├── EchoClientHandler.java
│ │ │ │ │ │ └── EchoClient.java
│ │ │ │ │ ├── fixlength
│ │ │ │ │ │ ├── EchoClientHandler.java
│ │ │ │ │ │ └── EchoClient.java
│ │ │ │ │ └── megpack
│ │ │ │ │ │ ├── MessagePackClientHandler.java
│ │ │ │ │ │ └── MessagePackClient.java
│ │ │ │ │ └── time
│ │ │ │ │ ├── demo1
│ │ │ │ │ ├── TimeClient.java
│ │ │ │ │ └── TimeClientHandler.java
│ │ │ │ │ ├── demo2
│ │ │ │ │ ├── TimeClient.java
│ │ │ │ │ └── TimeClientHandler.java
│ │ │ │ │ └── demo3
│ │ │ │ │ ├── TimeClient.java
│ │ │ │ │ └── TimeClientHandler.java
│ │ │ └── test
│ │ │ │ └── java
│ │ │ │ └── org
│ │ │ │ └── yyx
│ │ │ │ └── netty
│ │ │ │ └── study
│ │ │ │ └── StudyClientApplicationTests.java
│ │ └── pom.xml
│ ├── study-server
│ │ ├── src
│ │ │ ├── main
│ │ │ │ ├── resources
│ │ │ │ │ └── application.properties
│ │ │ │ └── java
│ │ │ │ │ └── org
│ │ │ │ │ └── yyx
│ │ │ │ │ └── netty
│ │ │ │ │ └── study
│ │ │ │ │ ├── websocket
│ │ │ │ │ ├── WebSocketConstant.java
│ │ │ │ │ ├── WebSocketMessage.java
│ │ │ │ │ ├── WebSocketChildHandler.java
│ │ │ │ │ ├── WebSocketServer.java
│ │ │ │ │ ├── WebSocketUsers.java
│ │ │ │ │ └── WebSocketServerHandler.java
│ │ │ │ │ ├── StudyServerApplication.java
│ │ │ │ │ ├── echo
│ │ │ │ │ ├── fixlength
│ │ │ │ │ │ ├── EchoServerHandler.java
│ │ │ │ │ │ └── EchoServer.java
│ │ │ │ │ ├── delimiter
│ │ │ │ │ │ ├── EchoServerHandler.java
│ │ │ │ │ │ └── EchoServer.java
│ │ │ │ │ └── megpack
│ │ │ │ │ │ ├── MessagePackServerHandler.java
│ │ │ │ │ │ └── MessagePackServer.java
│ │ │ │ │ ├── time
│ │ │ │ │ ├── demo1
│ │ │ │ │ │ ├── ChildChannelHandler.java
│ │ │ │ │ │ ├── TimeServer.java
│ │ │ │ │ │ └── TimeServerHandler.java
│ │ │ │ │ ├── demo2
│ │ │ │ │ │ ├── ChildChannelHandler.java
│ │ │ │ │ │ ├── TimeServer.java
│ │ │ │ │ │ └── TimeServerHandler.java
│ │ │ │ │ └── demo3
│ │ │ │ │ │ ├── ChildChannelHandler.java
│ │ │ │ │ │ ├── TimeServerHandler.java
│ │ │ │ │ │ └── TimeServer.java
│ │ │ │ │ └── codec
│ │ │ │ │ └── msgpack
│ │ │ │ │ ├── MsgPackEncoder.java
│ │ │ │ │ └── MsgPackDecoder.java
│ │ │ └── test
│ │ │ │ └── java
│ │ │ │ └── org
│ │ │ │ └── yyx
│ │ │ │ └── netty
│ │ │ │ └── study
│ │ │ │ ├── StudyServerApplicationTests.java
│ │ │ │ └── CodeCTest.java
│ │ └── pom.xml
│ └── pom.xml
├── netty-demo
│ ├── netty-server
│ │ ├── README.md
│ │ ├── src
│ │ │ ├── main
│ │ │ │ ├── resources
│ │ │ │ │ └── application.yml
│ │ │ │ └── java
│ │ │ │ │ └── org
│ │ │ │ │ └── yyx
│ │ │ │ │ └── netty
│ │ │ │ │ ├── NettyServerApplication.java
│ │ │ │ │ ├── rpc
│ │ │ │ │ └── service
│ │ │ │ │ │ └── DemoService.java
│ │ │ │ │ └── server
│ │ │ │ │ ├── config
│ │ │ │ │ └── NettyServerConfig.java
│ │ │ │ │ ├── impl
│ │ │ │ │ └── DemoServiceImpl.java
│ │ │ │ │ ├── adapter
│ │ │ │ │ └── ServerChannelHandlerAdapter.java
│ │ │ │ │ ├── listener
│ │ │ │ │ └── NettyServerListener.java
│ │ │ │ │ └── dispatcher
│ │ │ │ │ └── RequestDispatcher.java
│ │ │ └── test
│ │ │ │ └── java
│ │ │ │ └── org
│ │ │ │ └── yyx
│ │ │ │ └── netty
│ │ │ │ └── NettyServerApplicationTests.java
│ │ └── pom.xml
│ ├── netty-client
│ │ ├── src
│ │ │ ├── main
│ │ │ │ ├── resources
│ │ │ │ │ └── application.yml
│ │ │ │ └── java
│ │ │ │ │ └── org
│ │ │ │ │ └── yyx
│ │ │ │ │ └── netty
│ │ │ │ │ ├── NettyClientApplication.java
│ │ │ │ │ ├── config
│ │ │ │ │ ├── NettyConfig.java
│ │ │ │ │ └── NettyConfiguration.java
│ │ │ │ │ ├── rpc
│ │ │ │ │ ├── service
│ │ │ │ │ │ └── DemoService.java
│ │ │ │ │ └── util
│ │ │ │ │ │ ├── WrapMethodUtils.java
│ │ │ │ │ │ ├── RemoteMethodInvokeUtil.java
│ │ │ │ │ │ ├── NettyBeanScanner.java
│ │ │ │ │ │ ├── ChannelUtil.java
│ │ │ │ │ │ └── PackageClassUtils.java
│ │ │ │ │ ├── client
│ │ │ │ │ ├── NettyClientHandler.java
│ │ │ │ │ ├── NettyClientListener.java
│ │ │ │ │ ├── NettyClient.java
│ │ │ │ │ ├── NettyClientHandlerAdapter.java
│ │ │ │ │ └── RPCProxyFactoryBean.java
│ │ │ │ │ └── action
│ │ │ │ │ └── MainAction.java
│ │ │ └── test
│ │ │ │ └── java
│ │ │ │ └── org
│ │ │ │ └── yyx
│ │ │ │ └── netty
│ │ │ │ └── client
│ │ │ │ └── NettyClientApplicationTests.java
│ │ ├── README.md
│ │ └── pom.xml
│ └── pom.xml
├── netty-commons
│ ├── src
│ │ └── main
│ │ │ └── java
│ │ │ └── org
│ │ │ └── yyx
│ │ │ └── netty
│ │ │ ├── exception
│ │ │ ├── NoUseableChannel.java
│ │ │ └── ErrorParamsException.java
│ │ │ ├── entity
│ │ │ ├── NullWritable.java
│ │ │ ├── User.java
│ │ │ └── MethodInvokeMeta.java
│ │ │ └── util
│ │ │ ├── ObjectCodec.java
│ │ │ └── ObjectSerializerUtils.java
│ └── pom.xml
└── pom.xml
├── .gitignore
└── README.md
/spring-boot-netty/study-netty/study-client/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/spring-boot-netty/study-netty/study-server/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/spring-boot-netty/netty-demo/netty-server/README.md:
--------------------------------------------------------------------------------
1 | # netty-server
2 | >博文源码,基于SOA架构的服务端,使用Netty做RPC通信.
3 |
4 | 这是一个Netty服务端与SpringBoot集成的Demo.
--------------------------------------------------------------------------------
/spring-boot-netty/netty-demo/netty-client/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | netty:
2 | port: 12345
3 | basePackage: org.yyx.netty.rpc.service;
4 | clientName: nettyClient
5 | url: 127.0.0.1
--------------------------------------------------------------------------------
/spring-boot-netty/netty-demo/netty-server/src/main/resources/application.yml:
--------------------------------------------------------------------------------
1 | # netty配置
2 | netty:
3 | # 端口号
4 | port: 12345
5 | # 最大线程数
6 | maxThreads: 1024
7 | # 数据包的最大长度
8 | max_frame_length: 65535
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled class file
2 | *.class
3 |
4 | # Log file
5 | *.log
6 |
7 | # BlueJ files
8 | *.ctxt
9 |
10 | # Mobile Tools for Java (J2ME)
11 | .mtj.tmp/
12 |
13 | # Package Files #
14 | *.jar
15 | *.war
16 | *.ear
17 | *.zip
18 | *.tar.gz
19 | *.rar
20 |
21 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
22 | hs_err_pid*
23 | *.DS_Store
24 | *.iml
25 | .idea/
26 | target/
27 |
28 |
--------------------------------------------------------------------------------
/spring-boot-netty/netty-demo/netty-server/src/test/java/org/yyx/netty/NettyServerApplicationTests.java:
--------------------------------------------------------------------------------
1 | package org.yyx.netty;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class NettyServerApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/spring-boot-netty/netty-demo/netty-client/src/test/java/org/yyx/netty/client/NettyClientApplicationTests.java:
--------------------------------------------------------------------------------
1 | package org.yyx.netty.client;
2 |
3 | import org.junit.Test;
4 | import org.junit.runner.RunWith;
5 | import org.springframework.boot.test.context.SpringBootTest;
6 | import org.springframework.test.context.junit4.SpringRunner;
7 |
8 | @RunWith(SpringRunner.class)
9 | @SpringBootTest
10 | public class NettyClientApplicationTests {
11 |
12 | @Test
13 | public void contextLoads() {
14 | }
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/spring-boot-netty/netty-commons/src/main/java/org/yyx/netty/exception/NoUseableChannel.java:
--------------------------------------------------------------------------------
1 | package org.yyx.netty.exception;
2 |
3 | /**
4 | * 没有可用的通道异常
5 | *
6 | *
7 | * @author 叶云轩 at marquis_xuan@163.com
8 | * @date 2018/11/2-16:00
9 | */
10 | public class NoUseableChannel extends RuntimeException{
11 | private static final long serialVersionUID = 7762465537123947683L;
12 |
13 | public NoUseableChannel() {
14 | super();
15 | }
16 |
17 | public NoUseableChannel(String message) {
18 | super(message);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/spring-boot-netty/netty-demo/netty-client/src/main/java/org/yyx/netty/NettyClientApplication.java:
--------------------------------------------------------------------------------
1 | package org.yyx.netty;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | * @author 叶云轩 contact by tdg_yyx@foxmail.com
8 | * @date 2018/8/15 - 12:29
9 | */
10 | @SpringBootApplication
11 | public class NettyClientApplication {
12 |
13 | public static void main(String[] args) {
14 | SpringApplication.run(NettyClientApplication.class, args);
15 | }
16 | }
--------------------------------------------------------------------------------
/spring-boot-netty/netty-commons/src/main/java/org/yyx/netty/exception/ErrorParamsException.java:
--------------------------------------------------------------------------------
1 | package org.yyx.netty.exception;
2 |
3 | /**
4 | * 参数错误异常
5 | *
6 | *
7 | * @author 叶云轩 at tdg_yyx@foxmail.com
8 | * @date 2018/11/1-14:41
9 | */
10 | public class ErrorParamsException extends RuntimeException {
11 | private static final long serialVersionUID = -623198335011996153L;
12 |
13 | public ErrorParamsException() {
14 | super();
15 | }
16 |
17 | public ErrorParamsException(String message) {
18 | super(message);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/spring-boot-netty/netty-demo/netty-client/src/main/java/org/yyx/netty/config/NettyConfig.java:
--------------------------------------------------------------------------------
1 | package org.yyx.netty.config;
2 |
3 | import lombok.Data;
4 | import org.springframework.boot.context.properties.ConfigurationProperties;
5 | import org.springframework.stereotype.Component;
6 |
7 | /**
8 | * netty客户端配置
9 | *
10 | *
11 | * @author 叶云轩 at tdg_yyx@foxmail.com
12 | * @date 2018/11/1-17:13
13 | */
14 | @Component
15 | @ConfigurationProperties(prefix = "netty")
16 | @Data
17 | public class NettyConfig {
18 |
19 | private String url;
20 |
21 | private int port;
22 | }
23 |
--------------------------------------------------------------------------------
/spring-boot-netty/netty-commons/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 | spring-boot-netty
7 | org.yyx.netty
8 | 0.0.1-SNAPSHOT
9 |
10 | 4.0.0
11 |
12 | netty-commons
13 |
14 |
15 |
--------------------------------------------------------------------------------
/spring-boot-netty/study-netty/study-client/src/main/java/org/yyx/netty/study/websocket/WebSocketConstant.java:
--------------------------------------------------------------------------------
1 | package org.yyx.netty.study.websocket;
2 |
3 | /**
4 | * WebSocket常量
5 | *
6 | * create by 叶云轩 at 2018/5/18-下午1:29
7 | * contact by tdg_yyx@foxmail.com
8 | */
9 | public interface WebSocketConstant {
10 |
11 | /**
12 | * 点对点
13 | */
14 | String SEND_TO_USER = "send_to_user";
15 |
16 | /**
17 | * 群发 广播
18 | */
19 | String SEND_TO_USERS = "send_to_users";
20 |
21 | /**
22 | * 请求成功
23 | */
24 | String REQUEST_SUCCESS = "request_success";
25 | }
26 |
--------------------------------------------------------------------------------
/spring-boot-netty/study-netty/study-server/src/main/java/org/yyx/netty/study/websocket/WebSocketConstant.java:
--------------------------------------------------------------------------------
1 | package org.yyx.netty.study.websocket;
2 |
3 | /**
4 | * WebSocket常量
5 | *
6 | * create by 叶云轩 at 2018/5/18-下午1:29
7 | * contact by tdg_yyx@foxmail.com
8 | */
9 | public interface WebSocketConstant {
10 |
11 | /**
12 | * 点对点
13 | */
14 | String SEND_TO_USER = "send_to_user";
15 |
16 | /**
17 | * 群发 广播
18 | */
19 | String SEND_TO_USERS = "send_to_users";
20 |
21 | /**
22 | * 请求成功
23 | */
24 | String REQUEST_SUCCESS = "request_success";
25 | }
26 |
--------------------------------------------------------------------------------
/spring-boot-netty/study-netty/study-server/src/main/java/org/yyx/netty/study/StudyServerApplication.java:
--------------------------------------------------------------------------------
1 | package org.yyx.netty.study;
2 |
3 | import org.springframework.boot.CommandLineRunner;
4 | import org.springframework.boot.SpringApplication;
5 | import org.springframework.boot.autoconfigure.SpringBootApplication;
6 |
7 | @SpringBootApplication
8 | public class StudyServerApplication implements CommandLineRunner {
9 |
10 | public static void main(String[] args) {
11 | SpringApplication.run(StudyServerApplication.class, args);
12 | }
13 |
14 | @Override
15 | public void run(String... args) throws Exception {
16 | }
17 | }
--------------------------------------------------------------------------------
/spring-boot-netty/study-netty/study-client/src/main/java/org/yyx/netty/study/StudyClientApplication.java:
--------------------------------------------------------------------------------
1 | package org.yyx.netty.study;
2 |
3 | import org.springframework.boot.CommandLineRunner;
4 | import org.springframework.boot.SpringApplication;
5 | import org.springframework.boot.autoconfigure.SpringBootApplication;
6 |
7 | @SpringBootApplication
8 | public class StudyClientApplication implements CommandLineRunner {
9 |
10 | public static void main(String[] args) {
11 | SpringApplication.run(StudyClientApplication.class, args);
12 | }
13 |
14 | @Override
15 | public void run(String... args) throws Exception {
16 |
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/spring-boot-netty/study-netty/study-client/src/main/java/org/yyx/netty/study/websocket/WebSocketMessage.java:
--------------------------------------------------------------------------------
1 | package org.yyx.netty.study.websocket;
2 |
3 | import lombok.Data;
4 |
5 | import java.io.Serializable;
6 |
7 | /**
8 | * WebSocketMessage
9 | *
10 | * create by 叶云轩 at 2018/5/18-下午3:02
11 | * contact by tdg_yyx@foxmail.com
12 | */
13 | @Data
14 | public class WebSocketMessage implements Serializable {
15 | private static final long serialVersionUID = -4666429837358506065L;
16 |
17 | private String accept;
18 | private String content;
19 | private Type header;
20 |
21 | enum Type {
22 | send_user, send_users, request_success;
23 | }
24 |
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/spring-boot-netty/study-netty/study-server/src/main/java/org/yyx/netty/study/websocket/WebSocketMessage.java:
--------------------------------------------------------------------------------
1 | package org.yyx.netty.study.websocket;
2 |
3 | import lombok.Data;
4 |
5 | import java.io.Serializable;
6 |
7 | /**
8 | * WebSocketMessage
9 | *
10 | * create by 叶云轩 at 2018/5/18-下午3:02
11 | * contact by tdg_yyx@foxmail.com
12 | */
13 | @Data
14 | public class WebSocketMessage implements Serializable {
15 | private static final long serialVersionUID = -4666429837358506065L;
16 |
17 | private String accept;
18 | private String content;
19 | private Type header;
20 |
21 | enum Type {
22 | send_user, send_users, request_success;
23 | }
24 |
25 |
26 | }
27 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # spring boot netty
2 |
3 | ### 资源库说明
4 |
5 | 首先,不是一个项目.而是一个DEMO性质的代码分享.
6 |
7 | **当前资源库中包含了两大部分**
8 |
9 | 1. netty-demo下的可以在项目中使用的netty与SpringBoot集成的demo
10 | 2. 在study-netty下的netty相关的基础学习demo
11 |
12 | ## netty-demo
13 |
14 | #### 简述
15 | 这是我之前博客中的集成代码,当然,如今的代码已经不完全是博文中所述那样.这里做了相应的优化与升级.主要包含以下几点:
16 |
17 | 1. 性能优化,不再循环中与服务端创建连接.
18 | 2. 代码优化,客户端代码添加了更多的注释,以便理解这样的做法.
19 | 3. 功能优化,添加了保持长连接的心跳检测功能,并添加了测试类模拟测试.
20 |
21 | 原博文地址: [开源中国](https://my.oschina.net/yzwjyw/blog/1614889) [CSDN](http://blog.csdn.net/yuanzhenwei521/article/details/79194275) [博客园](http://www.cnblogs.com/tdg-yyx/p/8376842.html)
22 |
23 | #### PS
24 |
25 | 具体项目说明详见内部项目的说明文档
26 |
27 | ## study-netty
28 |
29 | #### 简述
30 |
31 | 当前目录下的项目为netty基础学习demo.
32 |
33 | #### PS
34 |
35 | 具体项目说明详见内部项目的说明文档
36 |
37 | ## End
38 |
39 | #### 作者QQ: 562638362
40 |
41 | #### 作者邮箱:marquis_xuan@163.com
42 |
43 |
--------------------------------------------------------------------------------
/spring-boot-netty/netty-demo/netty-client/README.md:
--------------------------------------------------------------------------------
1 | # Netty-client
2 |
3 | > 该项目源自博主项目中实际应用场景。主要用作RPC通信中的客户端处理。
4 |
5 | ## 阅读说明
6 |
7 | ### 阅读顺序:
8 |
9 | netty-client阅读顺序参考以下链条
10 |
11 | ```java
12 | NettyConfiguration -> NettyBeanScanner -> PackageClassUtils(工具) -> RPCProxyFactoryBean -> WrapMethodUtils(工具) -> NettyClient -> CustomChannelInitializerClient -> ClientChannelHandlerAdapter
13 | ```
14 |
15 | 由NettyConfiguration入手,获取配置文件中的相关配置。
16 |
17 | ### 项目阅读
18 |
19 | 主要见博客:
20 |
21 | [叶云轩的知识库](http://xuan.wp.happyqing.com/2018/09/17/netty%E4%B8%8Espring-boot%E7%9A%84%E6%95%B4%E5%90%88/)、[开源中国](https://my.oschina.net/yzwjyw/blog/1614889)、[CSDN](http://blog.csdn.net/yuanzhenwei521/article/details/79194275)、[博客园](http://www.cnblogs.com/tdg-yyx/p/8376842.html)
22 |
23 | **更直接的参见源码注释。**
24 |
25 | ### 更新说明
26 |
27 | ------
28 |
29 | #### 2018-11-01
30 |
31 | 1. 添加阅读思路,整理待优化细节
32 |
33 | ------
34 |
35 |
--------------------------------------------------------------------------------
/spring-boot-netty/netty-commons/src/main/java/org/yyx/netty/entity/NullWritable.java:
--------------------------------------------------------------------------------
1 | package org.yyx.netty.entity;
2 |
3 | import lombok.Data;
4 |
5 | import java.io.Serializable;
6 |
7 | /**
8 | * 服务器可能返回空的处理
9 | *
10 | *
11 | * @author 叶云轩 contact by tdg_yyx@foxmail.com
12 | * @date 2018/8/15 - 11:57
13 | */
14 | @Data
15 | public class NullWritable implements Serializable {
16 | /**
17 | * 序列化标识
18 | */
19 | private static final long serialVersionUID = 2123827169429254101L;
20 | /**
21 | * 单例
22 | */
23 | private static NullWritable instance = new NullWritable();
24 |
25 | /**
26 | * 私有构造器
27 | */
28 | private NullWritable() {
29 | }
30 |
31 | /**
32 | * 返回代表Null的对象
33 | *
34 | * @return {@link NullWritable} 当方法返回值为void时或返回值为null时返回此对象
35 | */
36 | public static NullWritable nullWritable() {
37 | return instance;
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/spring-boot-netty/study-netty/study-client/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 |
7 | study-client
8 | 0.0.1-SNAPSHOT
9 | jar
10 |
11 | study-client
12 | Demo project for Spring Boot
13 |
14 |
15 | org.yyx.netty
16 | study-netty
17 | 0.0.1-SNAPSHOT
18 |
19 |
20 |
21 |
22 |
23 |
24 | org.springframework.boot
25 | spring-boot-maven-plugin
26 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/spring-boot-netty/netty-demo/netty-server/src/main/java/org/yyx/netty/NettyServerApplication.java:
--------------------------------------------------------------------------------
1 | package org.yyx.netty;
2 |
3 | import org.springframework.boot.CommandLineRunner;
4 | import org.springframework.boot.SpringApplication;
5 | import org.springframework.boot.autoconfigure.SpringBootApplication;
6 | import org.yyx.netty.server.listener.NettyServerListener;
7 |
8 | import javax.annotation.Resource;
9 |
10 | /**
11 | * Netty服务器启动类
12 | *
13 | * @author 叶云轩 contact by tdg_yyx@foxmail.com
14 | * @date 2018/7/9 - 上午9:33
15 | */
16 | @SpringBootApplication
17 | public class NettyServerApplication implements CommandLineRunner {
18 |
19 | @Resource
20 | private NettyServerListener nettyServerListener;
21 |
22 | public static void main(String[] args) {
23 | SpringApplication.run(NettyServerApplication.class, args);
24 | }
25 |
26 | @Override
27 | public void run(String... args) {
28 | nettyServerListener.start();
29 | }
30 | }
--------------------------------------------------------------------------------
/spring-boot-netty/netty-demo/netty-server/src/main/java/org/yyx/netty/rpc/service/DemoService.java:
--------------------------------------------------------------------------------
1 | package org.yyx.netty.rpc.service;
2 |
3 |
4 | import org.yyx.netty.entity.User;
5 | import org.yyx.netty.exception.ErrorParamsException;
6 |
7 | /**
8 | * 测试Service
9 | *
10 | * create by 叶云轩 at 2018/3/3-下午1:46
11 | * contact by tdg_yyx@foxmail.com
12 | */
13 | public interface DemoService {
14 |
15 | /**
16 | * 除法运算
17 | *
18 | * @param numberA 第一个数
19 | * @param numberB 第二个数
20 | * @return 结果
21 | */
22 | double division(int numberA, int numberB) throws ErrorParamsException;
23 |
24 | /**
25 | * 获取用户信息
26 | *
27 | * @return 用户信息
28 | */
29 | User getUserInfo();
30 |
31 | /**
32 | * 打印方法
33 | *
34 | * @return 一个字符串
35 | */
36 | String print();
37 |
38 | /**
39 | * 求和方法
40 | *
41 | * @param numberA 第一个数
42 | * @param numberB 第二个数
43 | * @return 两数之和
44 | */
45 | int sum(int numberA, int numberB);
46 | }
47 |
--------------------------------------------------------------------------------
/spring-boot-netty/study-netty/study-server/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 | 4.0.0
5 |
6 | study-server
7 | 0.0.1-SNAPSHOT
8 | jar
9 |
10 | study-server
11 | Demo project for Spring Boot
12 |
13 |
14 | org.yyx.netty
15 | study-netty
16 | 0.0.1-SNAPSHOT
17 |
18 |
19 |
20 |
21 |
22 |
23 | org.springframework.boot
24 | spring-boot-maven-plugin
25 |
26 |
27 |
28 |
29 |
30 |
31 |
--------------------------------------------------------------------------------
/spring-boot-netty/netty-demo/netty-client/src/main/java/org/yyx/netty/config/NettyConfiguration.java:
--------------------------------------------------------------------------------
1 | package org.yyx.netty.config;
2 |
3 | import org.springframework.beans.factory.annotation.Value;
4 | import org.springframework.context.annotation.Bean;
5 | import org.springframework.context.annotation.Configuration;
6 | import org.yyx.netty.rpc.util.NettyBeanScanner;
7 |
8 | /**
9 | * Netty相关的初始化入口
10 | *
11 | *
12 | * @author 叶云轩 contact by tdg_yyx@foxmail.com
13 | * @date 2018/7/9 - 上午9:32
14 | */
15 | @Configuration
16 | public class NettyConfiguration {
17 |
18 | /**
19 | * 初始化加载Netty相关bean的配置方法
20 | *
21 | * @param basePackage 配置的包名
22 | * @param clientName 配置的Netty实例对象名
23 | * @return NettyBeanScanner
24 | */
25 | @Bean
26 | public static NettyBeanScanner initNettyBeanScanner(@Value("${netty.basePackage}") String basePackage,
27 | @Value("${netty.clientName}") String clientName) {
28 | // 创建对象
29 | return new NettyBeanScanner(basePackage, clientName);
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/spring-boot-netty/netty-demo/netty-client/src/main/java/org/yyx/netty/rpc/service/DemoService.java:
--------------------------------------------------------------------------------
1 | package org.yyx.netty.rpc.service;
2 |
3 | import org.yyx.netty.entity.User;
4 | import org.yyx.netty.exception.ErrorParamsException;
5 |
6 | /**
7 | * 测试Service
8 | *
9 | * create by 叶云轩 at 2018/3/3-下午1:46
10 | * contact by tdg_yyx@foxmail.com
11 | *
12 | * @author 叶云轩 contact by tdg_yyx@foxmail.com
13 | * @date 2018/8/15 - 12:29
14 | */
15 | public interface DemoService {
16 |
17 | /**
18 | * 除法运算
19 | *
20 | * @param numberA 第一个数
21 | * @param numberB 第二个数
22 | * @return 结果
23 | */
24 | double division(int numberA, int numberB) throws ErrorParamsException;
25 |
26 | /**
27 | * 获取用户信息
28 | *
29 | * @return 用户信息
30 | */
31 | User getUserInfo();
32 |
33 | /**
34 | * 打印方法
35 | *
36 | * @return 一个字符串
37 | */
38 | String print();
39 |
40 | /**
41 | * 求和方法
42 | *
43 | * @param numberA 第一个数
44 | * @param numberB 第二个数
45 | * @return 两数之和
46 | */
47 | int sum(int numberA, int numberB);
48 | }
49 |
--------------------------------------------------------------------------------
/spring-boot-netty/netty-commons/src/main/java/org/yyx/netty/util/ObjectCodec.java:
--------------------------------------------------------------------------------
1 | package org.yyx.netty.util;
2 |
3 | import io.netty.buffer.ByteBuf;
4 | import io.netty.buffer.Unpooled;
5 | import io.netty.channel.ChannelHandlerContext;
6 | import io.netty.handler.codec.MessageToMessageCodec;
7 |
8 | import java.util.List;
9 |
10 | /**
11 | *
12 | * create by 叶云轩 at 2018/3/3-下午1:42
13 | * contact by tdg_yyx@foxmail.com
14 | */
15 | public class ObjectCodec extends MessageToMessageCodec {
16 |
17 | @Override
18 | protected void encode(ChannelHandlerContext ctx, Object msg, List