springMVC中增加spring-boot-actuator

重名五次了發表於2020-11-04


由於專案上需要,要在MVC的框架程式上增加actuator,網上大部分都是boot下的新增方式,最終只在csdn中找到三篇相關的內容,但是涉及到的版本適配上還是有一些問題,這裡整理了一下相關的內容做一次記錄。

MVC版本說明

使用的mvc版本:spring-webmvc-4.3.12.RELEASE

actuator相關的版本

spring-boot-actuator:1.4.3.RELEASE
hibernate-validator:5.1.2.Final

	<dependency>
   		<groupId>org.springframework.boot</groupId>
   		<artifactId>spring-boot-actuator</artifactId>
   		<version>1.4.3.RELEASE</version>
	</dependency>
    <dependency>
		<groupId>org.hibernate</groupId>
		<artifactId>hibernate-validator</artifactId>
		<version>5.1.2.Final</version>
	</dependency>

第一步

pom中新增上面actuator的兩個依賴包

第二步

增加一個配置類,這裡需要注意必須將類放到掃描的範圍內,可以和其他比如容器初始化類,swagger初始化類放到一起,這裡參考了另外幾位大佬的程式碼。

import java.util.Collection;

import org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.HealthIndicatorAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.PublicMetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.TraceRepositoryAutoConfiguration;
import org.springframework.boot.actuate.endpoint.BeansEndpoint;
import org.springframework.boot.actuate.endpoint.DumpEndpoint;
import org.springframework.boot.actuate.endpoint.EnvironmentEndpoint;
import org.springframework.boot.actuate.endpoint.HealthEndpoint;
import org.springframework.boot.actuate.endpoint.InfoEndpoint;
import org.springframework.boot.actuate.endpoint.MetricsEndpoint;
import org.springframework.boot.actuate.endpoint.RequestMappingEndpoint;
import org.springframework.boot.actuate.endpoint.ShutdownEndpoint;
import org.springframework.boot.actuate.endpoint.TraceEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.EndpointHandlerMapping;
import org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter;
import org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.context.support.StandardServletEnvironment;


@Configuration
@Import({EndpointAutoConfiguration.class,
        PublicMetricsAutoConfiguration.class,
        HealthIndicatorAutoConfiguration.class,
        TraceRepositoryAutoConfiguration.class})

public class ActuatorSpringConfig {
    @Bean
    public EndpointHandlerMapping endpointHandlerMapping(
            Collection<? extends MvcEndpoint> endpoints) {
        EndpointHandlerMapping mapping = new EndpointHandlerMapping(endpoints);
        return mapping;
    }
 
    @Bean
    public HealthMvcEndpoint healthMvcEndpoint(HealthEndpoint delegate) {
        return new HealthMvcEndpoint(delegate, false);
    }
 
    @Bean
    public EndpointMvcAdapter metricsEndPoint(MetricsEndpoint delegate) {
        return new EndpointMvcAdapter(delegate);
    }
 
    @Bean
    public EndpointMvcAdapter infoEndPoint(InfoEndpoint delegate) {
        return new EndpointMvcAdapter(delegate);
    }
 
    @Bean
    public EndpointMvcAdapter traceEndPoint(TraceEndpoint delegate) {
        return new EndpointMvcAdapter(delegate);
    }
 
    @Bean
    public EndpointMvcAdapter dumpEndpoint() {
        return new EndpointMvcAdapter(new DumpEndpoint());
    }
 
    @Bean
    public EndpointMvcAdapter environmentEndpoint(StandardServletEnvironment environment) {
        EnvironmentEndpoint environmentEndpoint = new EnvironmentEndpoint();
        environmentEndpoint.setEnvironment(environment);
        return new EndpointMvcAdapter(environmentEndpoint);
    }
 
    @Bean
    public EndpointMvcAdapter beansEndpoint(ApplicationContext context) {
        BeansEndpoint endpoint = new BeansEndpoint();
        endpoint.setApplicationContext(context);
        return new EndpointMvcAdapter(endpoint);
    }
 
    @Bean
    public EndpointMvcAdapter requestMappingEndpoint(ApplicationContext context) {
        RequestMappingEndpoint requestMappingEndpoint = new RequestMappingEndpoint();
        requestMappingEndpoint.setApplicationContext(context);
        return new EndpointMvcAdapter(requestMappingEndpoint);
    }
 
    @Bean
    public EndpointMvcAdapter shutdownEndpoint(ApplicationContext context) {
        ShutdownEndpoint shutdownEndpoint = new ShutdownEndpoint();
        shutdownEndpoint.setApplicationContext(context);
        shutdownEndpoint.setEnabled(true);
        return new EndpointMvcAdapter(shutdownEndpoint);
    }
}

第三步

訪問地址
http://ip:port/專案名/health
返回的結果
http://ip:port/專案名/dump
在這裡插入圖片描述
actuator請求返回的json資料,具體請求如下:
在這裡插入圖片描述
圖片是複製自另外一名博主的介紹內容
詳細介紹請參考: http://blog.csdn.net/u011851478/article/details/55046918 順便感謝博主的圖片

第四部

確認一下請求的時候日誌中是否有記憶體不夠的報錯

The background cache eviction process was unable to free [10] percent of the cache for Context

如果有請修改tomcat的context.xml配置檔案

<Resources cachingAllowed="true" cacheMaxSize="100000" />

最後說幾句

引入的jar版本可能會和現有的一些包出現衝突,建議多試幾個版本,根據啟動的日誌調整對應架包的版本,hibernate-validator的包就從4.3的版本跳到5.1.2才相容的

相關文章