[英]Use Spring Boot Actuator without a Spring Boot Application
具有生產信息端點的Spring Boot的Actuator庫對任何服務器應用程序都非常有用。 但問題是我找不到集成到傳統Spring應用程序(不是Spring BOOT應用程序)的方法。
必須有一些方法來使用執行器的端點,但我無法將它們連接起來。
我有一個JavaConfig類,如下所示
@Configuration
@ComponentScan(basePackages = { "com.company.helper", "org.springframework.boot" })
@EnableWebMvc
@Import({ DbConfig.class })
public class AppConfig extends WebMvcConfigurerAdapter {
}
但是此配置在部署期間會引發錯誤。
沒有Spring Boot應用程序可以完成這種連線嗎?
我在此博客文章中添加了有關如何在非啟動應用程序中添加spring boot執行器的信息
http://givenwhenthen.blogspot.com/2015/09/adding-spring-boot-actuator-to-non.html
在應用程序的build.gradle中,我添加了以下依賴項
compile('org.springframework.boot:spring-boot-actuator:1.2.5.RELEASE'){
exclude group: 'org.springframework.boot', module:'spring-boot-starter-logging'}
在應用程序的Spring Config類中,我添加了以下內容:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration;
import org.springframework.boot.actuate.endpoint.BeansEndpoint;
import org.springframework.boot.actuate.endpoint.HealthEndpoint;
import org.springframework.boot.actuate.endpoint.InfoEndpoint;
import org.springframework.boot.actuate.endpoint.RequestMappingEndpoint;
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;
@Configuration
@Import(EndpointAutoConfiguration.class)
public class MyAppSpringConfig {
@Bean
@Autowired
//Define the HandlerMapping similar to RequestHandlerMapping to expose the endpoint
public EndpointHandlerMapping endpointHandlerMapping(
Collection<? extends MvcEndpoint> endpoints
){
return new EndpointHandlerMapping(endpoints);
}
@Bean
@Autowired
//define the HealthPoint endpoint
public HealthMvcEndpoint healthMvcEndpoint(HealthEndpoint delegate){
return new HealthMvcEndpoint(delegate, false);
}
@Bean
@Autowired
//define the Info endpoint
public EndpointMvcAdapter infoMvcEndPoint(InfoEndpoint delegate){
return new EndpointMvcAdapter(delegate);
}
@Bean
@Autowired
//define the beans endpoint
public EndpointMvcAdapter beansEndPoint(BeansEndpoint delegate){
return new EndpointMvcAdapter(delegate);
}
@Bean
@Autowired
//define the mappings endpoint
public EndpointMvcAdapter requestMappingEndPoint(
RequestMappingEndpoint delegate
){
return new EndpointMvcAdapter(delegate);
}
}
如果你想擺脫一個額外的依賴,那么請參閱博客帖子。
UPDATE
此外,您需要確保為RequestMappingHandlerAdapter定義了一個bean,如果您沒有它,ServletDispatcher將無法為您的HealthMvcEndpoint的處理程序獲取適配器。
如果你沒有它只是將它添加到你的bean配置文件
xml配置:
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter"/>
</list>
</property>
</bean>
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
<property name="prettyPrint" value="true" />
</bean>
我正在研究的項目使用Spring,但是Spring-boot和Spring-MVC都沒有。 以下解決方案可能不像啟動執行器那樣自動化,但它以非常簡潔的方式暴露端點。
基本上,所有執行器端點都只是bean,因此您可以在端點中創建新組件並自動裝配,但您認為合適。
我的pom中唯一的附加依賴項是spring-boot-actuator和spring-webmvc:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
<version>1.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
然后,您需要做的就是創建一個單獨的組件類(如果需要,可以注冊它)。 確保使用@EnableAutoConfiguration進行注釋:
@Component
@EnableAutoConfiguration
@Path("/actuator/")
public class ActuatorResource {
private ObjectMapper mapper = new ObjectMapper();
@Autowired
private DumpEndpoint dumpEndpoint;
@GET
@Produces("application/json")
@Path("/dump")
@Transactional(readOnly = true)
public String getDump() throws JsonProcessingException {
return mapper.writeValueAsString(dumpEndpoint.invoke());
}
@Autowired
private EnvironmentEndpoint envEndpoint;
@GET
@Produces("application/json")
@Path("/environment")
@Transactional(readOnly = true)
public String getEnvironment() throws JsonProcessingException {
return mapper.writeValueAsString(envEndpoint.invoke());
}
}
在我們的項目中,我們使用了一個對我們有用的小黑客。 為了啟用執行器,我們在POM中使用了spring-boot的依賴關系。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
<version>1.2.3.RELEASE</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.2.Final</version>
</dependency>
並且只使用了其他配置類,如下所示:
@EnableConfigurationProperties
@Configuration
@EnableAutoConfiguration
@Import(EndpointAutoConfiguration.class)
public class SpringBootActuatorConfig {
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.