implementation
Step 1 enable the monitoring function
2. Code Dubbo provides the RpcStatus class to read monitoring data. We can customize the Dubbo interceptor and print monitoring data in the interceptor.
configuration
Why configure it? Since Dubbo comes with built-in monitoring, all we need to do is use it. Many frameworks come with built-in monitoring (such as Alibaba’s Druid database connection pool), and generally just use it.
How to configure it?
Dubbo thread pool tuning ###
# Maximum number of concurrent requests received by a single method as a provider (all consumers)
dubbo.provider.executes=200
# Maximum number of concurrent requests received by a single method as a provider (single consumer)
dubbo.provider.actives=200
The maximum number of concurrent requests to invoke a remote method as a consumer
dubbo.consumer.actives=200
Copy the code
These parameters are used for tuning purposes, because normally you only need to configure the number of thread pools. However, if you want to limit the number of concurrent requests from a single method or a single client to prevent a single service or client from dragging the system down, you can configure these parameters. After the configuration, the monitoring function is automatically enabled.
code
Custom Dubbo interceptor class
if ("provider".equals(side)) {// Prints monitor data only at the provider, RpcStatus status = rpcStatus.getStatus (invoker.geturl (), inv.getName ())); URL url = invoker.getUrl(); String methodName = inv.getMethodName(); int max = url.getMethodParameter(methodName,"executes", 0);
if (max <= 0) {
max = url.getMethodParameter(methodName, "default.executes", 0);
}
int left = max - status.getActive();
logger.info("dubbo status:[method:{},status:{},max:{},left:{}]", new Object[]{methodName, JSONObject.toJSONString(status), max, left});
Copy the code
Fields that
The test data
The 2020-07-09 16:52:22. 286 | INFO | dw4Vq9rvpwaA - 61-0 | xxx.com mon. Filter. Dubbo. AccessLogExtFilter. Invoke: 167 | | dubbo status:[method:getQrcodeInfoById,status:{"active": 9,"averageElapsed": 1252,"averageTps": 0."failed": 0."failedAverageElapsed": 0."failedElapsed": 0."failedMaxElapsed": 0."maxElapsed": 1258,"succeeded": 42."succeededAverageElapsed": 1252,"succeededElapsed": 52611,"succeededMaxElapsed": 1258,"total": 42."totalElapsed": 52611}, Max: 200, left: 191] the 2020-07-09 16:52:22. 287 | INFO |xypAIqCmPDQ4-22-11|xxx.common.filter.dubbo.AccessLogExtFilter.invoke:167|| dubbo status:[method:getQrcodeInfoById,status:{"active":2, // Number of active connections currently requested"averageElapsed":1252, // Average time"averageTps":0, // Number of requests per second"failed":0, // Number of failures"failedAverageElapsed":0, // Average failure time"failedElapsed":0, // Total time for failed requests"failedMaxElapsed":0, // Maximum time for failed requests"maxElapsed":1258, // Maximum time"succeeded":49, // Number of successful requests"succeededAverageElapsed":1252, // Average time to success"succeededElapsed":61388, // Total request success time"succeededMaxElapsed":1258, // The maximum time for a successful request"total":49 // Total number of requests (current statistics do not include the current request, is the sum of previous requests)"totalElapsed":61388 // Total time}, Max :200, //execute number of interceptors left:198 // Number of available connections = max-Activce]Copy the code
Note Example of monitoring results (The monitoring data is the data before the current request, excluding the current request, the number of active connections and the number of successful connections is one less).
At the time of testing, the number of interceptor configurations was 200
Dubbo thread pool tuning ###
# Maximum number of concurrent requests received by a single method as a provider (all consumers)
dubbo.provider.executes=200
# Maximum number of concurrent requests received by a single method as a provider (single consumer)
dubbo.provider.actives=200
The maximum number of concurrent requests to invoke a remote method as a consumer
dubbo.consumer.actives=200
Copy the code
so
"total":49 // Total number of requests (the current statistics does not include the current requests and is the sum of previous requests). Max :200, // Execute number of interceptors left:198 // Number of connections left available = max-ActivceCopy the code
nature
Dubbo provides monitoring capabilities, which are essentially interceptor capabilities,
The principle of
To enable the monitoring function, set the maximum number of connections for the service consumer and the maximum number of connections for the service provider. ActiveLimitFilter is passed for each request to check whether the maximum number of concurrent requests is exceeded. If the maximum number of concurrent requests is exceeded, wait until timeout.
The service provider passes the ExecuteLimitFilter for each request and checks whether the number of concurrent requests exceeds the maximum number. If the number exceeds the maximum number, an exception occurs.
reference
My.oschina.net/liangxiao/b…