spring-boot-actuator 微服务监控

随着业务越来越复杂,从原来的单体架构向微服务架构跃进,微服务稍微一多,可控的因素就会变少,当出现问题的时候查起来的时候就比较麻烦,所以微服务监控就变得很有必要。一旦微服务出点差错,就会影响业务的进行,轻者流失部分利润,重则这个系统瘫痪,无法正常使用。因此,微服务的各项指标正常就至关重要。
像微服务的正常运行访问、使用内存情况、占用CPU多少,组件控件的使用情况等,监控各项指标,微服务现在主流还是Springboot,可以结合spring-boot-actuator框架来完成监控任务,spring-boot-actuator提供很多微服务的端点,可以通过Http接口访问来查看微服务的信息。

spring-actuator

项目依赖

Springboot项目可以使用spring-boot-starter-actuator包依赖
maven依赖如下:

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-actuator</artifactId>
	</dependency>
</dependencies>

gradle依赖如下:

dependencies {
	compile("org.springframework.boot:spring-boot-starter-actuator")
}

端点

引用spring-boot-actuator包后,会提供默认的一些端口给使用者访问,访问方式有JMX和HTTP。JMX现在用的比较少,所以就说说HTTP的一些端点情况。访问的方式HTTP默认是/actuator/health的方式进行访问,这个前缀是通过配置修改的。

endpoint端点介绍

actuator的核心就是endpoint,每项endpoint都代表监控着某一项数据

endpoint描述
auditevents显示当前应用程序的审计事件信息。
beans显示应用程序中所有Spring bean的完整列表。
caches显示可用的缓存
conditions显示在配置和自动配置类上评估的条件,以及它们匹配或不匹配的原因。
configprops显示所有@ConfigurationProperties的整理列表。
env显示spring的运行环境参数
flyway显示已应用的所有Flyway数据库迁移信息,需要一个或多个 Flyway Bean
health显示微服务的健康信息
httptrace显示HTTP跟踪信息(默认情况下,最后100个HTTP请求-响应交换)。
info显示应用程序信息
integrationgraph显示 Spring Integration 图。需要依赖 spring-integration-core
loggers显示和修改应用程序中日志的配置
logfile显示日志文件的内容(如果已设置logging.file.name或logging.file.path属性)
liquibase显示已应用的任何Liquibase数据库迁移。
metrics显示系统度量指标信息
mappings显示所有@RequestMapping路径的整理列表。
scheduledtasks显示系统中的定时任务
sessions允许从Spring会话支持的会话存储中检索和删除用户会话。当使用Spring Session对响应式web应用程序的支持时不可用。
shutdown让应用程序优雅地关闭。
threaddump显示系统线程占用内存信息
heapdump返回hprof堆转储文件
jolokia通过HTTP公开JMX bean(当Jolokia在类路径上时,不适用于WebFlux)。需要依赖 jolokia-core
prometheus以Prometheus服务器可以抓取的格式公开指标。需要依赖 micrometer-registry-prometheus

开启端点方式:
通过management.endpoint.<id>.enabled 方式进行开启或关闭,例如:

management.endpoint.shutdown.enabled=true

如果您希望端点启用为opt-in而不是opt-out,则设置management.endpoints。默认启用的属性为false,并使用单个端点启用的属性来选择返回。下面的示例启用info端点并禁用所有其他端点:

management.endpoints.enabled-by-default=false
management.endpoint.info.enabled=true

*可用于选择所有端点。例如,要通过HTTP公开除了env和bean端点之外的所有内容,请使用以下属性:

management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans

我使用的是Springboot2.0.2.release版本。启动微服务访问http://127.0.0.1:8080/actuator,返回的结果如下:

使用默认配置返回信息:

{
  "_links": {
    "self": {
      "href": "http://127.0.0.1:8080/actuator",
      "templated": false
    },
    "health": {
      "href": "http://127.0.0.1:8080/actuator/health",
      "templated": false
    },
    "info": {
      "href": "http://127.0.0.1:8080/actuator/info",
      "templated": false
    }
  }
}

修改配置之后返回结果如下:

{
  "_links": {
    "self": {
      "href": "http://127.0.0.1:8080/actuator",
      "templated": false
    },
    "auditevents": {
      "href": "http://127.0.0.1:8080/actuator/auditevents",
      "templated": false
    },
    "beans": {
      "href": "http://127.0.0.1:8080/actuator/beans",
      "templated": false
    },
    "health": {
      "href": "http://127.0.0.1:8080/actuator/health",
      "templated": false
    },
    "conditions": {
      "href": "http://127.0.0.1:8080/actuator/conditions",
      "templated": false
    },
    "shutdown": {
      "href": "http://127.0.0.1:8080/actuator/shutdown",
      "templated": false
    },
    "configprops": {
      "href": "http://127.0.0.1:8080/actuator/configprops",
      "templated": false
    },
    "env": {
      "href": "http://127.0.0.1:8080/actuator/env",
      "templated": false
    },
    "env-toMatch": {
      "href": "http://127.0.0.1:8080/actuator/env/{toMatch}",
      "templated": true
    },
    "info": {
      "href": "http://127.0.0.1:8080/actuator/info",
      "templated": false
    },
    "loggers": {
      "href": "http://127.0.0.1:8080/actuator/loggers",
      "templated": false
    },
    "loggers-name": {
      "href": "http://127.0.0.1:8080/actuator/loggers/{name}",
      "templated": true
    },
    "heapdump": {
      "href": "http://127.0.0.1:8080/actuator/heapdump",
      "templated": false
    },
    "threaddump": {
      "href": "http://127.0.0.1:8080/actuator/threaddump",
      "templated": false
    },
    "metrics": {
      "href": "http://127.0.0.1:8080/actuator/metrics",
      "templated": false
    },
    "metrics-requiredMetricName": {
      "href": "http://127.0.0.1:8080/actuator/metrics/{requiredMetricName}",
      "templated": true
    },
    "scheduledtasks": {
      "href": "http://127.0.0.1:8080/actuator/scheduledtasks",
      "templated": false
    },
    "httptrace": {
      "href": "http://127.0.0.1:8080/actuator/httptrace",
      "templated": false
    },
    "mappings": {
      "href": "http://127.0.0.1:8080/actuator/mappings",
      "templated": false
    }
  }
}

像我们自己的微服务,确实需要监控微服务的运行情况,但是如果被其他不怀好意的人知道了,利用了这里面的信息,给我们造成了不必要的损失就不好了,所以我们对这些功能还是要做一下权限校验,结合 Spring Security,做安全防护。

加入spring-boot-starter-security包引用。不做任何配置启动微服务。再访问http://127.0.0.1:8080/actuator,就会出现如下界面:
在这里插入图片描述
默认用户是user,密码是微服务启动时控制台打印的密码信息:

Using generated security password: ae4febb7-2a8d-4a1b-ab2e-bc96318219bf

登录就可以完成请求查看。

如果存在Spring Security,还可以设置缓存失效的时间:

management.endpoint.<name>.cache.time-to-live=10s

失效时间为十秒,name是actuator提供的端点名。
跨域访问:
我们实际的应用过程中难免会碰到跨域问题,同时端点访问也存在这样的问题,所以要允许跨域只需要完成以下配置:

management.endpoints.web.cors.allowed-origins=https://example.com
management.endpoints.web.cors.allowed-methods=GET,POST

spring-boot-actuator也提供了很好的延展性,除了自身框架上带的这些端点,还可以自己定义端点信息。使用 @Endpoint@JmxEndpoint@WebEndpoint注解来暴露端点,使用@Endpoint注解的bean,可以通过JMX和HTTP两者访问,使用@JmxEndpoint注解的bean只能JMX方位,而使用@WebEndpoint的bean只能被HTTP访问。
以下是web端点的方法注解说明情况:

OperationHTTP Method
@ReadOperationGET
@WriteOperationPOST
@DeleteOperationDELETE

举个示例:

@Component
@WebEndpoint(id="sc")
public class TestHealthEndpoint{
    
    @ReadOperation
    public String get(@Selector String name) {
        return "happy";
    }
}

@Selector 的含义是让这个路径变成/actuator/sc/{name} 我们能从路径上获取一个入参。
监控微服务运行时检查信息:

我们简单地开启健康检查health,通过/actuator/health访问返回的结果只有一个UP。但是我们可以在这个返回结果里添加任何我们想监控的返回信息。运行内存信息、cpu运行信息,数据库连接信息等等。

actuator提供的一些监控信息:

NameDescription
CassandraHealthIndicator检测database cassandra是否是启动up状态
CouchbaseHealthIndicator检测 Couchbase cluster 是否启动up装填
DiskSpaceHealthIndicator检测磁盘空间是否不足
DataSourceHealthIndicator检查数据源连接是否正常
ElasticsearchHealthIndicator检测ES集群是否up
InfluxDbHealthIndicator检测InfluxDB服务是否up
JmsHealthIndicator检测JMS broker是否是up
LdapHealthIndicator检测LDAP服务是否是up
MailHealthIndicator检测mail服务是否是up
MongoHealthIndicator检测Mongo db是否是up
Neo4jHealthIndicator检测Neo4j db是否是up
RabbitHealthIndicator检测rabbit服务是否是up
RedisHealthIndicator检测Redis服务是否是up
SolrHealthIndicator检测Solr服务是否是up

也可以自定义去写这个健康检测:

自己编写实现类实现HealthIndicators。

示例:

@Component
public class MyHealthIndicator implements HealthIndicator {

	@Override
	public Health health() {
		int errorCode = check(); 
		if (errorCode != 0) {
			return Health.down().withDetail("Error Code", errorCode).build();
		}
		return Health.up().build();
	}

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值