Updated March 29, 2023
Definition of Spring Boot Netty
It is an event-driven application framework that was used in-network, it will provide the HTTP, UDP, and non-blocking server and client. As per the name spring boot, netty is based on the netty framework, it is also known as the non-blocking input and output (NIO) framework. We can use the framework as highly scalable, to build the low-level server is straightforward by using netty. Spring boot netty developer also works on socket level, an example to create communication protocol.
What is spring boot netty?
- It supports APIs of non-blocking and blocking also it will support the SSL and TLS.
- We can run our all-request synchronously on each thread. To run each thread synchronously it uses the blocking server model.
- It is basically designed to make the easy implementation of custom network protocols. We can write our own protocol.
How to use spring boot netty?
To use the netty in our spring boot application we need to follow the below steps as follows.
- We need to add the spring boot starter webflux dependency in the pom.xml file.
- After adding the spring boot starter webflux dependency it will automatically add the spring boot starter reactor dependency in our project.
- After adding the dependency in the application pom.xml file next step is to configure the server. We can configure the server by using two methods.
- The first method to configure the server is by using the properties file. Spring boot exposes the common configuration by using the application properties file. We need to define the server port in the properties file.
- We can also do the same configuration by using the application.yml file. Apart from the server port spring boot contains the other options of server configuration.
- The application properties which was starting from the server prefix which was overriding the configuration of the default server.
- We can also configure the server by using the programmatic configuration.
- To configure the server through code spring boot uses the NettyServerCustomizer and WebServerFactoryCustomizer classes.
- To configure the server by using the programmatic configuration spring boot uses the component of the factory customizer at the time of startup.
- Spring boot automatically configures the configuration of the server while adding starter dependency in pom.xml file so we need to skip auto configuration by defining the bean of NettyReactiveWebServerFactory.
- To skip auto-configuration of the server we need to define the bean in our configuration class and need to add the customizer.
- After configuring the server next step is to configure the SSL. To configure the SSL in the spring boot netty project we need to use SslServerCustomizer class in our project.
- After configuring SSL in our application next step is to access the log information. In the project, we cannot access the logging by using logback.
- Spring boot is configuring the access logging by using the file of application properties for tomcat and jetty server.
- To enable the logging we need to set the netty http server access log enables parameter value as true at the time of running the application.
Custom Method spring boot netty
- We can configure the application by using custom method. We need to create a project template by using a spring initializer.
- We can also create the project by creating a new project using eclipse or the spring tool suite.
- After creating the project using spring initializer next step is to add the dependency package in the pom.xml file.
- After adding the dependency package we are going to create a server by using a custom method.
- Custom method uses the netty to send and receive the object of java. Custom method is very useful to create the applications.
- Custom method is used in the property-based configuration of spring boot. We can configure the server in a fine-grained manner.
- Using custom method we can configure the netty server by using the code. We need to add some classes in our application.
Server Configuration
Below examples shows to create the server configuration are as follows.
1) Create project template using spring initializer and give name to project –
In the below step, we have provided project group name as com. example, artifact name as springboot-netty, project name as springboot-netty, and package as jar file.
Group – com.example
Artifact name – springboot-netty
Name – springboot-netty
Description – Project for springboot-netty
Spring boot – 2.5.5
Project – Maven project
Package name – com.example.springboot-netty
Packaging – Jar
Java – 11
Dependencies – spring native, spring web, spring reactive web.
2) After generating project extract files and open this project by using spring tool suite –
3) After opening project using spring tool suite check the project and its files –
4) Add dependency packages –
Code:
<dependency> -- Start of dependency tag.
<groupId>org.springframework.boot</groupId> -- Start and end of groupId tag.
<artifactId>spring-boot-starter-webflux</artifactId> -- Start and end of artifactId tag.
</dependency> -- End of dependency tag.
5) Configure the server using application.properties file.
Code:
Server.port = 8080
6) Configure the SSL –
Code:
@Component
public class NettyWebServerFactorySslCustomizer
implements WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {
@Override
public void customize(NettyReactiveWebServerFactory serverFactory) {
Ssl ssl = new Ssl();
ssl.setEnabled (true);
ssl.setKeyStore ("classpath:sample.jks");
ssl.setKeyAlias ("netty");
ssl.setKeyPassword ("pass");
ssl.setKeyStorePassword ("pass");
}
}
7) Run the application by using spring boot netty –
Conclusion
This framework supports APIs of non-blocking and blocking also it will support the SSL and TLS. It is basically designed to make the easy implementation of custom network protocols. We can write our own protocol.
Recommended Articles
This is a guide to Spring Boot Netty. Here we discuss the definition, What is spring boot netty, How to use spring boot netty, examples with code. You may also have a look at the following articles to learn more –