java actuator是什么,让我们一起了解一下?
actuator是springboot中的一个附加功能,SpringBoot包含了许多其他特性,可以选择使用HTTP端点或使用JMX来管理和监视应用程序。审计、健康和度量收集也可以自动应用于应用程序。
考虑是否使用actuator框架的核心因素是什么?
在应用运行过程中,Apollo服务器端的配置信息和实例本地缓存的配置信息并不是时时刻刻都相同的。
因此,我需要能够通过某种技术手段,可以在需要时准确获知应用本地的配置数据副本,而不是仅能通过Apollo的protal查看服务器端数据。
这就需要actuator的原因!
让我们来看看actuator具体是什么。
Spring Boot提供了名称为spring-boot-starter-actuator的starters。
在官方文档中是这样介绍的:
Starter for using Spring Boot’s Actuator which provides production ready features to help you monitor and manage your application
其实也不难理解,让我们实战操作一下。
首先,定义一个SchemaEndpoint类。当然,得用@Endpoint注解来标注这个类,还要再提供一个使用@ReadOperation标注的方法:
@Endpoint(id = "schema")
public class SchemaEndpoint {
protected ApolloConverterFactory factory;
public SchemaEndpoint(ApolloConverterFactory factory) {
this.factory = factory;
}
@ReadOperation
public SchemaDescriptor schema() {
return new SchemaDescriptor(factory.getSchemaList(), factory.getGroups());
}
public static final class SchemaDescriptor {
protected Properties schemaList;
protected Map groups;
public SchemaDescriptor(@Nullable Properties schemaList, Map groups) {
this.schemaList = schemaList;
this.groups = groups;
}
@Nullable public Properties getSchemaList() {
return this.schemaList;
}
@Nullable public Map getGroups() {
return this.groups;
}
}
} 这里面的ApolloConverterFactory是基于Apollo本地缓存的配置信息创建数据转换器的工厂类。它的getSchemaList方法,返回的是sec.insight.schema.list这个namespace的本地缓存数据,getGroups返回的是每一组schema的配置集合清单。
然后,再来编写一个配置类:
@Configuration
@ConditionalOnExpression("${apollo.boostrap.enabled:true} && ${spring.apollo.schema.enabled:true}")
public class ApolloSchemaConfiguration {
@Bean
public ApolloConverterFactory apolloConverterFactory() {
return new ApolloConverterFactory();
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnEnabledEndpoint(endpoint = SchemaEndpoint.class)
public SchemaEndpoint schemaEndpoint(ApolloConverterFactory factory) {
return new SchemaEndpoint(factory);
}
}最后直接测试就可以了。
以上就是小编今天的分享了,希望可以帮助到大家。