Springboot Integrates Netty Initial Practice

Netty is a powerful communication framework, and has used MINA similar to him before. Although MINA has many functions, it needs to write its own encoder (filter data) when dealing with TCP's unpacking and sticking problem. Netty provides some methods to solve this problem. In this Demo, the author also uses MINA. One of them, as for the others, can be searched on the Internet or can view official documents. This paper mainly focuses on building projects. Don't say much, just go to the code.

First, let's prepare a project without integrating Netty, which consists of Springboot, Mybatis and MySQL. I'm sure you've already vomited about this kind of code, but the author still has to upload it, because the author will use Netty in it.

First, we need to introduce relevant jar packages, and the authors use Maven to manage these dependencies:

Configuration files have been completed, and then some configurations are loaded. The author divides the configurations into cross-domain, Dao and transaction, and creates config packages under the basic package path, which include three layers: web, service and dao; cross-domain configurations are completed in web, transaction configurations are completed in service, and database is completed in dao. Relevant configuration. The documents are as follows:

When these configurations are completed, you can start the project, but the console will print a warning message to remind you that there is no xml file under the mapper folder. At this time, we need to complete some basic CRUD. There are not too many explanations here, go directly to the code, students who do not understand can go to study first and read later. This paper:

Using IDEA shortcuts, lombok can also be introduced to reduce the writing of getter/setter methods and so on. At this point, the basic process is over and the Web interface can now be accessed through various Client s (e.g. Browser, Mobile Phone, etc.).

Next, enter the highlight of this article=> Netty, before reading this article, need to understand the use of Netty, Netty in this Demo is managed by spring, directly on the code:

This Java file contains the whole process of Netty. What needs to be changed by ourselves is the processor in the childHandler method. We inject the object into it automatically, and then ChildChannelHandler.java:

In this paper, I define two coders, one is to solve the problem of TCP unpacking and sticking, the other is to convert data into corresponding entity classes and do persistence processing. Delimiter BasedFrameDecoder is a decoder provided by Netty, which divides data according to the separator at the end of the message to solve the problem of sticky package. Other Netty decoders are not explained here, and they are similar in usage.

I encapsulated some similar parts and made them into tool classes. Reference is as follows. Because the author is not very good at reflective application, you can rewrite the binding object method by yourselves.

At this point, you can start the project (students who don't know CommandLine Runner can search for information online), start a network debugging assistant locally, send data, and then start a Chrome, call the interface, you can see that the data is updated in real time. So far, the basic functions have been realized.