springMVC中增加spring-boot-actuator
由於專案上需要,要在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才相容的
相關文章
- spring-boot-actuatorSpringboot
- 探索SpringBoot中的SpringMVCSpring BootSpringMVC
- SpringMVC中的攔截器SpringMVC
- Springmvc中轉發和重定向SpringMVC
- springMVC中controller的返回值SpringMVCController
- 【SpringMVC】SpringMVC搭建框架SpringMVC框架
- SpringMVC---IDEA 搭建SpringMVC工程SpringMVCIdea
- SpringMvc - SpringMvc的執行流程SpringMVC
- SpringMVCSpringMVC
- SpringMVC中的攔截器Interceptor實現SpringMVC
- springMVC簡單demo集合(持續更新中……)SpringMVC
- springmvc中Dispatchservlet繼承體系詳解SpringMVCServlet繼承
- SpringMVC原始碼分析1:SpringMVC概述SpringMVC原始碼
- SpringMVC-08-SpringMVC層編寫SpringMVC
- SpringMVC-01 什麼是SpringMVCSpringMVC
- SpringMVC中@RequestMapping註解中的return “redirect:/“;跟return““的用法。SpringMVCAPP
- SpringMVC系列知識:(二)什麼是springMVC,為什麼要使用springMVCSpringMVC
- SpringMVC IOCSpringMVC
- SpringMVC 流程SpringMVC
- SpringMVC 中 @ControllerAdvice 註解的三種使用場景!SpringMVCController
- 淺談 SpringMVC 中各層職責的設計SpringMVC
- 如何在rac環境中增加監聽
- HGDB中如何為大表增加新主鍵
- VuePress 中增加使用者登入功能Vue
- Xcode10.2中LLDB增加的新特性XCodeLLDB
- SpringMVC簡介SpringMVC
- springMVC @ResponseBody 原理SpringMVC
- SpringMVC:RESTful案例SpringMVCREST
- SpringMVC筆記SpringMVC筆記
- 【SpringMVC】傳參SpringMVC
- [SpringMVC] 傳參SpringMVC
- SpringMVC 簡單SpringMVC
- SpringMVC 入門SpringMVC
- 初識 SpringMVCSpringMVC
- SpringMVC整合MybatisSpringMVCMyBatis
- SpringMVC總結SpringMVC
- SpringMVC-6SpringMVC
- SpringMvc學習SpringMVC