Let me introduce you to Hippo4J dynamic thread pool infrastructure

Keywords: Java Network Protocol TCP/IP

Many partners know that Xiaobian has been submitting Hippo4J dynamic thread pool project since June this year

After 200 + Commit, the official version of 1.0.0 will be released soon. Today, I will write an article to formally introduce the project architecture of Hippo4J

Hippo4J GitHub: https://github.com/acmenlt/dynamic-threadpool

If you are slow to access GitHub, you can improve the access speed by changing the Host, Modify Host scheme


1. Architecture design

Simply put, Hippo4J is divided into two roles from the perspective of deployment: Server side and Client side

The Server side is a Java process packaged by the Hippo4J project. Its functions include user permissions, thread pool monitoring and persistent actions

The Client side refers to our SpringBoot application, which is responsible for interacting with the Server side by introducing the Hippo4J Starter Jar package

For example, pull Server end thread pool data, dynamically update thread pool configuration, and collect and report thread pool runtime data

2. Foundation components

2.1 configuration center (Config)

The configuration center is located on the Server side. Its main function is to monitor the configuration change of the thread pool on the Server side and notify the Client instance to execute the thread pool change process in real time

The code design is based on the implementation of long polling and asynchronous Servlet mechanism of Nacos version 1. X


2.2 Registry (Discovery)

It is responsible for managing instances registered from the Client side (stand-alone or cluster) to the Server side, including but not limited to instance registration, renewal, expiration elimination and other operations. The code is implemented based on Eureka source code

The above configuration center is easy to understand that the dynamic thread pool parameter changes are fundamental. But what is the registry for?

The registry manages the instances registered on the Client side. Through these instances, the runtime parameter information of the thread pool can be obtained in real time

The current design is such that it does not rule out further expansion based on Discovery


2.3 Console

Docking front-end projects, including but not limited to the following module management

2.4 Abstract Tools

As the name suggests, some tools are abstracted separately and presented in the form of modules. This splitting method has two advantages: first, it is more in line with the separation of responsibilities, and second, it needs to use a certain function to achieve ready to use

Two pieces of content have been integrated:

  1. Log record tool: Based on mzt-biz-log Operation log change logging component
  2. Open change tool: monitor the change of Hippo4J project in the Star Fork of GitHub. By default, any change will be notified within five minutes

3. Message notification (Notify)

Hippo4J has built-in many events that need to be notified, such as thread pool parameter change notification, thread pool activity alarm, reject policy execution alarm, blocking queue capacity alarm, etc

At present, Notify has been connected to nailing, and will continue to integrate enterprise wechat, email, SMS and other notification channels in the future; In addition, the Notify module provides SPI scheme for message events, which can accept push defined by three parties


4. Hippo4j-Spring-Boot-Starter

Friends familiar with SpringBoot should not be unfamiliar with Starter. Hippo4J is nested in the application in the form of Starter Jar package and is responsible for interacting with the Server side

The Starter Jar package is pushed to the Maven public warehouse. At present, there are jars of version 0.0.2 in the public warehouse


5. SpringBoot quick start

5.1 Server startup

Import Hippo4J initialize SQL statement

Hippo4J Pull the code to local and start Server ServerApplication application class under module


5.2 SpringBoot introduces Hippo4j Starter

The SpringBoot application introduces Hippo4j Starter Jar. Note: version 0.0.2 is only a transitional version. Please wait for release of 1.0.0

<dependency>
    <groupId>io.github.acmenlt</groupId>
    <artifactId>hippo4j-spring-boot-starter</artifactId>
    <version>0.0.2</version>
</dependency>

Add Hippo4J related configuration files to SpringBoot application:

spring:
  profiles:
    active: dev
  application:
    name: dynamic-threadpool-example
  dynamic:
    thread-pool:
      notifys:
        - type: DING
          url: https://oapi.dingtalk.com/robot/send?access_token=
          # Here you can select your own nail group
          token: 4a582a588a161d6e3a1bd1de7eea9ee9f562cdfcbe56b6e72029e7fd512b2eae
          # Notify @ person
          receives: '15601166691'
      # Alarm sending interval
      alarm-interval: 30
      # Server address
      server-addr: http://localhost:6691
      # Tenant id, corresponding to tenant table
      namespace: prescription
      # Item id, corresponding to item table
      item-id: ${spring.application.name}

Add thread pool configuration class. Dynamic thread pool supports two creation methods

  1. The DynamicThreadPoolWrapper wrapper is created to specify the thread pool ID
  2. @The DynamicThreadPool annotation modifies spring beans

The Spring post processor will scan the beans created in these two ways, get the thread pool ID, and call the Server side to obtain the configuration

If obtaining the Server side configuration fails, create an instance according to the default thread pool

@Configuration
public class ThreadPoolConfig {
    public static final String MESSAGE_PRODUCE = "message-produce";
    public static final String MESSAGE_CONSUME = "message-consume";

    @Bean
    // {@ link DynamicThreadPoolWrapper} completes the Server side subscription configuration function
    public DynamicThreadPoolWrapper messageCenterDynamicThreadPool() {
        return new DynamicThreadPoolWrapper(MESSAGE_CONSUME);
    }

    @Bean
    @DynamicThreadPool
    // The Server-side subscription configuration function is completed by modifying {@ link DynamicThreadPool} with {@ link DynamicThreadPool}
    // Modified by the dynamic thread pool annotation, {@ link DynamicThreadPoolExecutor} is saved in the IOC container
    public ThreadPoolExecutor dynamicThreadPoolExecutor() {
        return ThreadPoolBuilder.builder().threadFactory(MESSAGE_PRODUCE).dynamicPool().build();
    }
}

After starting the SpringBoot application, the preparation of the dynamic thread pool is completed


5.3 dynamic change of test line process pool

Modify the configuration in the thread pool through the interface. HTTP POST path: http://localhost:6691/v1/cs/configs , the Body request Body is as follows:

{
    "ignore": "tenantId,itemId,tpId Represents the unique thread pool. Please do not modify it",
    "tenantId": "prescription",
    "itemId": "dynamic-threadpool-example",
    "tpId": "message-produce",
    "coreSize": 10,
    "maxSize": 15,
    "queueType": 9,
    "capacity": 100,
    "keepAliveTime": 10,
    "rejectedType": 3,
    "isAlarm": 0,
    "capacityAlarm": 81,
    "livenessAlarm": 82
}

After the interface call is successful, observe the log output of the IDEA Client console. The log output includes but is not limited to this information

[🔥 MESSAGE-PRODUCE] Changed thread pool. coreSize :: [11=>10], maxSize :: [15=>15], queueType :: [9=>9], capacity :: [100=>100], keepAliveTime :: [10000=>10000], rejectedType :: [7=>7]

In addition, when the Client cluster is deployed, you can choose to modify all instances or one instance. Modify request path: http://localhost:6691/v1/cs/configs?identify=xxx The Body is the same as above

How to get the identify parameter? Each Client will be assigned a unique value and printed at startup

Client identity :: xxxxxx

If the identify parameter is not passed or empty, all instance parameters of the thread pool under the Client cluster of the thread pool will be modified


5.4 alarm notification

If you join a small partner in the nail group (No.: 31764717), you can receive a push notification of the nail robot. An example is as follows:


Configuration change


Nail plus group


6. Finally

On GitHub, the number of stars accounts for a certain factor in the quality of inspection items; Since Hippo4J boarded GitHub trend last time, 400 + stars have been harvested, which proves the project quality of Hippo4J

With the passage of time, more and more small partners pay attention to Hippo4J project, put forward relevant functional suggestions, and hope to participate in the co construction of the project, which is full of vitality as a whole

The questions and suggestions from the partners in the figure below are very good 👍👍👍

If you are interested in Hippo4J after reading the above introduction, you can not only answer your doubts about Hippo4J project, but also accept positive function suggestions by contacting Xiaobian in the following ways

For a long time, Hippo4J's goal has always been enterprise applications, and Xiaobian is constantly aligning in this direction. The release time of 1.0 will not be too far

If the small partners in front of the screen think the project function planning and code design are good, work hard 🚀 Star for easy follow-up

Do you have any different views on this project? Welcome to communicate in the comment area ~

Posted by madhu on Sun, 07 Nov 2021 16:35:02 -0800