This is the multi-page printable view of this section. Click here to print.

Return to the regular view of this page.

Spring Boot Actuator支持

Restlight适配了Spring Boot Actuator, 提供详细的健康检查以及监控等接口

Quick Start

引入Maven依赖:

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
	<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
	<groupId>io.esastack</groupId>
	<artifactId>restlight-starter-actuator</artifactId>
	<version>${restlight.version}</version>
</dependency>

访问 Get localhost:8080/actuator/info返回{}

1 - Restlight Endpoint扩展

Spring Boot Actuator基础之上Restlight提供额外的功能扩展

业务线程池Metrics

1、Json 格式

Get actuator/bizthreadpool

Example:

curl -X GET localhost:8080/actuator/bizthreadpool

返回

{
    "corePoolSize": 4,
    "maxPoolSize": 4,
    "queueLength": 1024,
    "keepAliveTimeSeconds": 180,
    "activeCount": 1,
    "poolSize": 4,
    "largestPoolSize": 4,
    "taskCount": 6,
    "queueCount": 0,
    "completedTaskCount": 5
}

2、Prometheus 格式

Get actuator/bizthreadpool4prometheus

Example:

curl -X GET localhost:8080/actuator/bizthreadpool4prometheus

返回

# HELP core_pool_size  
# TYPE core_pool_size gauge
core_pool_size 1.0
# HELP active_count  
# TYPE active_count gauge
active_count 0.0
# HELP queue_count  
# TYPE queue_count gauge
queue_count 0.0
# HELP largest_pool_size  
# TYPE largest_pool_size gauge
largest_pool_size 0.0
# HELP reject_task_count  
# TYPE reject_task_count gauge
reject_task_count -1.0
# HELP max_pool_size  
# TYPE max_pool_size gauge
max_pool_size 2.0
# HELP pool_size  
# TYPE pool_size gauge
pool_size 0.0
# HELP queue_length  
# TYPE queue_length gauge
queue_length 1024.0
# HELP keep_alive_time_seconds  
# TYPE keep_alive_time_seconds gauge
keep_alive_time_seconds 60.0
# HELP task_count  
# TYPE task_count gauge
task_count 0.0
# HELP completed_task_count  
# TYPE completed_task_count gauge
completed_task_count 0.0

业务线程池扩缩容

Post actuator/bizthreadpool

curl -X POST -H "Content-Type:application/json" -d "{\"corePoolSize\":\"1\",\"maxPoolSize\":\"2\"}" localhost:8080/actuator/bizthreadpool

IO线程池Metrics

1、Json 格式

Get actuator/ioexecutor

Example:

curl -X GET localhost:8080/actuator/ioexecutor

返回

{
    "childExecutors": [
        {
            "pendingTasks": 0,
            "maxPendingTasks": 2147483647,
            "ioRatio": 50,
            "taskQueueSize": 0,
            "tailTaskQueueSize": 0,
            "threadName": "Netty-I/O-1#0",
            "threadPriority": 5,
            "threadState": "RUNNABLE"
        },
        {
            "pendingTasks": 0,
            "maxPendingTasks": 2147483647,
            "ioRatio": 50,
            "taskQueueSize": 0,
            "tailTaskQueueSize": 0,
            "threadName": "Netty-I/O-1#1",
            "threadPriority": 5,
            "threadState": "RUNNABLE"
        }
    ],
    "threadCount": 2,
    "pendingTasks": 0,
    "threadStates": {
        "RUNNABLE": 2
    },
    "terminated": false,
    "shutDown": false
}

2、Prometheus 格式

Get actuator/ioexecutor4prometheus

Example:

curl -X GET localhost:8080/actuator/ioexecutor4prometheus

返回

# HELP pending_tasks_netty_io_1_0  
# TYPE pending_tasks_netty_io_1_0 gauge
pending_tasks_netty_io_1_0 0.0
# HELP thread_state_netty_io_1_1  
# TYPE thread_state_netty_io_1_1 gauge
thread_state_netty_io_1_1 1.0
# HELP thread_states_runnable  
# TYPE thread_states_runnable gauge
thread_states_runnable 2.0
# HELP task_queue_size_netty_io_1_1  
# TYPE task_queue_size_netty_io_1_1 gauge
task_queue_size_netty_io_1_1 0.0
# HELP thread_priority  
# TYPE thread_priority gauge
thread_priority 5.0
# HELP pending_tasks_netty_io_1_1  
# TYPE pending_tasks_netty_io_1_1 gauge
pending_tasks_netty_io_1_1 0.0
# HELP pending_tasks  
# TYPE pending_tasks gauge
pending_tasks 0.0
# HELP terminated  
# TYPE terminated gauge
terminated 0.0
# HELP max_pending_tasks  
# TYPE max_pending_tasks gauge
max_pending_tasks 2.147483647E9
# HELP thread_state_netty_io_1_0  
# TYPE thread_state_netty_io_1_0 gauge
thread_state_netty_io_1_0 1.0
# HELP io_ratio  
# TYPE io_ratio gauge
io_ratio 50.0
# HELP shutdown  
# TYPE shutdown gauge
shutdown 0.0
# HELP task_queue_size_netty_io_1_0  
# TYPE task_queue_size_netty_io_1_0 gauge
task_queue_size_netty_io_1_0 0.0
# HELP thread_count  
# TYPE thread_count gauge
thread_count 2.0

当使用的Springboot版本为2.3.X及以上时,需要在pom文件中引入micrometer-registry-prometheus 1.5.1及以上版本,此处以1.5.1版本为例,直接引入即可覆盖原有版本,具体操作如下:

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
    <version>1.5.1</version>
</dependency>

获取Restlight所有配置信息

Get actuator/restlightconfigs

Example:

curl -X GET localhost:8080/actuator/restlightconfigs

返回:

{
    "http2Enable": false,
    "useNativeTransports": false,
    "connectorThreads": 1,
    "ioThreads": 2,
    "coreBizThreads": 4,
    "maxBizThreads": 4,
    "blockingQueueLength": 1024,
    "keepAliveTimeSeconds": 180,
    "executorTerminationTimeoutSeconds": 60,
    "compress": false,
    "decompress": false,
    "maxContentLength": 4194304,
    "maxInitialLineLength": 4096,
    "maxHeaderSize": 8192,
    "soRcvbuf": 0,
    "soSendbuf": 0,
    "soBacklog": 128,
    "writeBufferHighWaterMark": 65536,
    "idleTimeSeconds": 60,
    "keepAliveEnable": true,
    "https": {
        "enable": false,
        "handshakeTimeoutMillis": 3000,
        "certificatePath": null,
        "privateKeyPath": null,
        "sessionTicketKeyPath": null,
        "sessionTimeoutSeconds": 0,
        "sessionCacheEnable": false,
        "sessionCacheSize": 0,
        "enabledCipherSuites": [],
        "enabledProtocols": []
    },
    "scheduling": {
        "defaultStrategy": "BIZ",
        "bufferSize": 4096,
        "batchingSize": 50
    },
    "route": {
        "useCachedRouting": true,
        "cacheRatio": 10,
        "computeRate": 1
    },
    "contextPath": null,
    "validationMessageFile": null,
    "serialize": {
        "request": {
            "negotiation": false,
            "negotiationParam": "format"
        },
        "response": {
            "negotiation": false,
            "negotiationParam": "format"
        }
    },
    "ext": {},
    "host": null,
    "port": 8080,
    "unixDomainSocketFile": null,
    "printBanner": true,
    "warmUp": {
        "enable": false,
        "delay": 0
    }
}

强制Full GC

Post actuator/forcefgc

Example:

curl -X POST localhost:8080/actuator/forcefgc

修改优雅停机等待时间

Post actuator/terminationtimeout

Example:

curl -X POST -H "Content-Type:application/json" -d "{\"timeout\": 120}" localhost:8080/actuator/terminationtimeout

返回

Success

2 - 自定义Endpoint

用户可以自己定义Endpoint实现定制化的健康检查接口

eg

@Endpoint(id = "appId")
public class AppIdEndpoint {

    @ReadOperation
    public String appId() {
        return "esa-restlight";
    }
}

上面的代码自定义了一个Endpoint接口并返回appid

将上面接口注入Spring容器

@Bean
public AppIdEndpoint endpoint() {
    return new AppIdEndpoint();
}

启动之后访问curl -X GET localhost:8080/actuator/appId

返回

esa-restlight

自定义异步EndPoint

用户可以自己定义基于CompletableftureEndpoint实现定制化的健康检查接口

eg

@Endpoint(id = "appId")
public class AppIdEndpoint {

    @ReadOperation
    public CompletableFuture<String> appId() {
        return CompletableFuture.supplyAsync(() -> {

            // do something...

            return "esa-restlight";
        });
    }
}

上面的代码自定义了一个异步的Endpoint接口并返回appid

将上面接口注入Spring容器

@Bean
public AppIdEndpoint endpoint() {
    return new AppIdEndpoint();
}

启动之后访问curl -X GET localhost:8080/actuator/appId

返回

esa-restlight

3 - 使用独立端口

默认情况下健康检查的接口都将与Restight使用同一个HttpServer服务, 如果需要将健康检查接口与业务接口分别使用不同的端口则需要添加自定义配置

详细配置如下:

#配置健康检查暴露的端口
management.server.port=8081

启动后看到日志打印

Started Restlight(Actuator) server in 386 millis on port:8081

辅助配置

SpringBoot场景下大多数的配置可通过application.properties(或者yaml)配置文件即可完成配置,但是配置文件配置还是会有其缺陷

  • 无法动态配置(这里的动态指的是通过代码计算等方式决定配置)
  • 语法表达能力有限
  • 配置过多变得冗杂

等问题。

ManagementConfigure

用于支持SpringBoot场景显式配置

eg.

@Bean
public ManagementConfigure configure() {
    return restlight -> {
        restlight.address(8081)
                .addFilter((request, response, chain) -> {
                    // biz logic
                    return chain.doFilter(request, response);
                });
        restlight.options().setCoreBizThreads(16);
        restlight.options().setMaxBizThreads(32);
        // more...
    };
}