Home  >  Article  >  Backend Development  >  Java backend development: Implementing a high-performance API server using Netty

Java backend development: Implementing a high-performance API server using Netty

WBOY
WBOYOriginal
2023-06-17 11:04:481793browse

In recent years, with the rapid development of Internet technology, the requirements for high performance, high concurrency and high availability on the server side are getting higher and higher. As a high-performance, asynchronous and non-blocking network communication framework, Netty is becoming more and more popular. Developer attention and use.

This article will introduce how to use the Netty framework to implement a high-performance API server.

1. What is Netty

Netty is an asynchronous event-driven network application framework based on Java NIO, used to quickly develop high-performance, high-reliability network communication programs, such as clients and server side.

Its core components include Buffer, Channel, EventLoop, Codec, etc. Buffer is Netty's buffer component, Channel provides an abstract network communication interface, EventLoop is Netty's event-driven model, and Codec is a codec. Through these components, the Netty framework can provide high-performance, high-concurrency, and low-latency network communication capabilities.

2. Basic use of Netty

First, we need to introduce Netty dependencies:

<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>4.1.42.Final</version>
</dependency>

Then, we need to create a Bootstrap object and use this object to start our Netty server:

EventLoopGroup bossGroup = new NioEventLoopGroup(); 
EventLoopGroup workerGroup = new NioEventLoopGroup(); 
try{
    ServerBootstrap bootstrap = new ServerBootstrap();
    bootstrap.group(bossGroup, workerGroup)
             .channel(NioServerSocketChannel.class)
             .childHandler(new ChannelInitializer<SocketChannel>() {
                 @Override
                 public void initChannel(SocketChannel ch) throws Exception {
                     ChannelPipeline pipeline = ch.pipeline();
                     pipeline.addLast(new HttpServerCodec());
                     pipeline.addLast(new HttpObjectAggregator(65536));
                     pipeline.addLast(new ChunkedWriteHandler());
                     pipeline.addLast(new HttpServerHandler());
                  }
              });
    ChannelFuture future = bootstrap.bind(port).sync();
    future.channel().closeFuture().sync();
}finally{
      bossGroup.shutdownGracefully();
      workerGroup.shutdownGracefully();
}

In the above code, we created two EventLoopGroup objects, a bossGroup for receiving client requests, and a workerGroup for processing client requests. Configure the parameters of the Netty server through the ServerBootstrap object, including the communication protocol (NioServerSocketChannel), processor (Handler), and Channel initialization and other operations.

We can also see that in the above code, we added the HttpServerCodec and HttpObjectAggregator components to implement encoding, decoding and aggregation of HTTP requests and responses. At the same time, we also added ChunkedWriteHandler to process big data streams.

Finally, we bind the port and start the Netty server through the bootstrap.bind method, and block the main thread and wait for the Netty server to shut down through the future.channel().closeFuture().sync() method.

3. Use Netty to implement high-performance API server

For an API server, we usually need to handle a large number of requests and responses while ensuring system availability and high-performance response time.

Here, we take the implementation of a simple API server as an example to introduce how to use the Netty framework to implement a high-performance API server.

1. Interface definition

Let’s first define a simple API interface. This interface is used to implement the function of obtaining user information:

GET /user/{id} HTTP/1.1
Host: localhost:8888

where {id} is the user The ID number, we need to query the user information based on this ID number and return it to the client.

2. Business processing

Next, we need to implement business logic processing, that is, query user information based on the ID number in the client request, and return the query results to the client.

First, let's create a processor HttpServerHandler, which inherits from SimpleChannelInboundHandler. We can implement our business logic in this processor.

public class HttpServerHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
        HttpServerRoute route = HttpServerRoute.builder()
                .addRoute("/user/{id}", new GetUserHandler())
                .build();
        HttpServerRequest request = new HttpServerRequest(msg);
        HttpServerResponse response = new HttpServerResponse(ctx, msg);
        route.route(request, response);
    }
}

As you can see, in the above code, we implement route matching through the HttpServerRoute object. When receiving a client request, we will convert the request into an HttpServerRequest object, wrap the response object HttpServerResponse in it, then match the routing rules through the HttpServerRoute object, and distribute the request to the corresponding processor for processing.

We need to implement the GetUserHandler processor, which is used to query user information based on the user ID:

public class GetUserHandler implements HttpServerHandlerInterface {
    @Override
    public void handle(HttpServerRequest request, HttpServerResponse response) throws Exception {
        String id = request.getPathParam("id");
        //查询用户信息
        User user = UserService.getUserById(id);
        if (user != null) {
            JSONObject json = new JSONObject();
            json.put("id", user.getId());
            json.put("name", user.getName());
            response.sendJSON(HttpResponseStatus.OK, json.toJSONString());
        } else {
            response.sendError(HttpResponseStatus.NOT_FOUND);
        }
    }
}

In the above code, we will query the user information based on the ID number in the request, and Use JSONObject to construct the JSON string data of the request response, and finally return the query results to the client.

We also need to implement the UserService class to provide the function of querying user information:

public class UserService {
    public static User getUserById(String id) {
        //查询数据库中的用户信息
    }
}

3. Performance test

Finally, let’s test the high performance of Netty we implemented API server response time and QPS (number of concurrent requests per second).

Through the Apache ab tool, we can simulate multiple client concurrent requests and collect statistics on response time and QPS information. Use the following command:

ab -n 10000 -c 100 -k http://localhost:8888/user/1

Parameter description:

-n: indicates the total number of requests

-c: indicates the number of concurrent requests

-k: indicates Enable Keep-alive connection

Through the test, we can get the response time and QPS information:

Server Software:
Server Hostname:        localhost
Server Port:            8888

Document Path:          /user/1
Document Length:        36 bytes

Concurrency Level:      100
Time taken for tests:   3.777 seconds
Complete requests:      10000
Failed requests:        0
Keep-Alive requests:    10000
Total transferred:      1460000 bytes
HTML transferred:       360000 bytes
Requests per second:    2647.65 [#/sec] (mean)
Time per request:       37.771 [ms] (mean)
Time per request:       0.378 [ms] (mean, across all concurrent requests)
Transfer rate:          377.12 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    2   1.2      2      10
Processing:     3   32  11.3     32      84
Waiting:        3   32  11.3     32      84
Total:          6   34  11.2     34      86

Percentage of the requests served within a certain time (ms)
  50%     34
  66%     38
  75%     40
  80%     42
  90%     49
  95%     55
  98%     64
  99%     71
 100%     86 (longest request)

As you can see, our API server can effectively handle 100 concurrent requests from the test The simulation can handle 2647.65 requests per second, and the average response time is only 37.771 milliseconds.

4. Summary

Through the above introduction and steps, we have learned how to use Netty as a network communication framework and use it to develop a high-performance API server. Using the Netty framework can greatly improve server performance, making our server have high concurrency, high reliability, low latency and other characteristics. At the same time, the Netty framework also has high scalability and flexibility and can be easily integrated into any application.

As part of the Java back-end development technology stack, using the Netty framework is also one of the skills that must be mastered.

The above is the detailed content of Java backend development: Implementing a high-performance API server using Netty. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn