【j360-boot】Spring-boot系列四(運維福利,監控和管理生產環境)

j360-boot

spring-boot入門工程之j360-boot:(歡迎star、fork)java

https://github.com/xuminwlt/j360-bootgit

spring-boot官方地址github

http://projects.spring.io/spring-boot/web


【j360-boot】Spring-boot系列

【j360-boot】Spring-boot系列一(多是最好的quick start)redis

【j360-boot】Spring-boot系列二(困難模式,比簡單複雜那麼一點點)
spring

【j360-boot】Spring-boot系列三(崩潰模式,不是你崩就是電腦崩)docker

【j360-boot】Spring-boot系列四(運維福利,監控和管理生產環境)
shell

【j360-boot】Spring-boot系列五(docker、docker、docker)瀏覽器


介紹

j360-production

Spring Boot包含不少其餘的特性,它們能夠幫你監控和管理髮布到生產環境的應用。你能夠選擇使用HTTP端點,JMX或遠程shell(SSH或Telnet)來管理和監控應用。審計Auditing),健康(health)和數據採集(metrics gathering)會自動應用到你的應用。安全

spring-boot-actuator模塊提供了Spring Boot全部的production-ready特性。啓用該特性的最簡單方式就是添加對spring-bootstarter-actuator ‘Starter POM’的依賴。

執行器(Actuator)

定義:執行器是一個製造業術語,指的是用於移動或控制東西的一個機械裝置。一個很小的改變就能讓執行器產生大量的運動。

基於Maven的項目想要添加執行器只需添加下面的'starter'依賴:

<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包含不少內置的端點,你也能夠添加本身的。例如,health端點提供了應用的基本健康信息。

端點暴露的方式取決於你採用的技術類型。大部分應用選擇HTTP監控,端點的ID映射到一個URL。例如,默認狀況下,health端點將被映射到/health。

下面的端點都是可用的:

ID                                         描述                                                     敏感(Sensitive)
autoconfig 顯示一個auto-configuration的報告,該報告展現全部auto-configuration候選者及它們被應用或未被應用的緣由    true
beans 顯示一個應用中全部Spring Beans的完整列表                                true
configprops 顯示一個全部@ConfigurationProperties的整理列表                    true
dump 執行一個線程轉儲                true
env 暴露來自Spring ConfigurableEnvironment的屬性                true
health 展現應用的健康信息(當使用一個未認證鏈接訪問時顯示一個簡單的'status',使用認證鏈接訪問則顯示所有信息詳情) false
info 顯示任意的應用信息        false
metrics 展現當前應用的'指標'信息        true
mappings 顯示一個全部@RequestMapping路徑的整理列表        true
shutdown 容許應用以優雅的方式關閉(默認狀況下不啓用) true
trace 顯示trace信息(默認爲最新的一些HTTP請求) true
注:根據一個端點暴露的方式,sensitive參數可能會被用作一個安全提示。例如,在使用HTTP訪問sensitive端點時須要提供用戶名/密碼(若是沒有啓用web安全,可能會簡化爲禁止訪問該端點)。

啓用該執行器的工程 ->瀏覽器輸入:

http://localhost:8080/autoconfig


http://localhost:8080/health

自定義端點

使用Spring屬性能夠自定義端點。你能夠設置端點是否開啓(enabled),是否敏感(sensitive),甚至它的id。例如,下面的application.properties改變了敏感性和beans端點的id,也啓用了shutdown。

endpoints.beans.id=springbeans
endpoints.beans.sensitive=false
endpoints.shutdown.enabled=true

注:前綴 endpoints + . + name 被用來惟一的標識被配置的端點。

默認狀況下,除了shutdown外的全部端點都是啓用的。若是但願指定選擇端點的啓用,你可使用endpoints.enabled屬性。

例如,下面的配置禁用了除info外的全部端點:

endpoints.enabled=false
endpoints.info.enabled=true


若是你正在開發一個Spring MVC應用,Spring Boot執行器自動將全部啓用的端點經過HTTP暴露出去。默認約定使用端點的id做爲URL路徑,例如,health暴露爲/health。

若是你的項目中添加的有Spring Security,全部經過HTTP暴露的敏感端點都會受到保護。默認狀況下會使用基本認證(basic authentication,用戶名爲user,密碼爲應用啓動時在控制檯打印的密碼)。

你可使用Spring屬性改變用戶名,密碼和訪問端點須要的安全角色。例如,你可能會在application.properties中添加下列配置:

security.user.name=admin
security.user.password=secret
management.security.role=SUPERUSER

注:若是你不使用Spring Security,那你的HTTP端點就被公開暴露,你應該慎重考慮啓用哪些端點。


自定義管理服務器的上下文路徑

有時候將全部的管理端口劃分到一個路徑下是有用的。例如,你的應用可能已經將 /info 做爲他用。你能夠用 management.contextPath 屬性爲管理端口設置一個前綴:

management.context-path=/manage

上面的application.properties示例將把端口從 /{id} 改成 /manage/{id} (好比,/manage/info)。

自定義管理服務器的端口

對於基於雲的部署,使用默認的HTTP端口暴露管理端點(endpoints)是明智的選擇。然而,若是你的應用是在本身的數據中心運行,那你可能傾向於使用一個不一樣的HTTP端口來暴露端點。

management.port 屬性能夠用來改變HTTP端口:

management.port=8081

因爲你的管理端口常常被防火牆保護,不對外暴露也就不須要保護管理端點,即便你的主要應用是安全的。在這種狀況下,classpath下會存在Spring Security庫,你能夠設置下面的屬性來禁用安全管理策略(management security):

management.security.enabled=false

(若是classpath下不存在Spring Security,那也就不須要顯示的以這種方式來禁用安全管理策略,它甚至可能會破壞應用程序。)

自定義管理服務器的地址

你能夠經過設置 management.address 屬性來定義管理端點可使用的地址。這在你只想監聽內部或面向生產環境的網絡,或

只監聽來自localhost的鏈接時很是有用。

下面的application.properties示例不容許遠程管理鏈接:

management.port=8081
management.address=127.0.0.1

禁用HTTP端點

若是不想經過HTTP暴露端點,你能夠將管理端口設置爲-1: management.port=-1

基於JMX的監控和管理

Java管理擴展(JMX)提供了一種標準的監控和管理應用的機制。默認狀況下,Spring Boot在 org.springframework.boot 域下將管理端點暴露爲JMX MBeans。

使用遠程shell來進行監控和管理

Spring Boot支持集成一個稱爲'CRaSH'的Java shell。你能夠在CRaSH中使用ssh或telnet命令鏈接到運行的應用。爲了啓用遠程shell支持,你只需添加 spring-boot-starter-remote-shell 的依賴:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-remote-shell</artifactId>
</dependency>

度量指標(Metrics)

Spring Boot執行器包括一個支持'gauge'和'counter'級別的度量指標服務。'gauge'記錄一個單一值;'counter'記錄一個增量(增長或減小)。同時,Spring Boot提供一個PublicMetrics接口,你能夠實現它,從而暴露以上兩種機制不能記錄的指標。

能夠看到基本的 memory , heap , class loading , processor 和 thread pool 信息,連同一些HTTP指標。在該實例中, root ('/'), /metrics URLs分別返回20次,3次 HTTP 200 響應。同時能夠看到 root URL返回了4次 HTTP401 (unauthorized)響應。雙asterix(star-star)來自於被Spring MVC /** 匹配到的一個請求(一般爲一個靜態資源)。

gauge 級別展現了一個請求的最後響應時間。因此, root 的最後請求被響應耗時2毫秒, /metrics 耗時3毫秒。


系統指標

Spring Boot暴露如下系統指標:

  1. 系統內存總量(mem),單位:Kb

  2. 空閒內存數量(mem.free),單位:Kb

  3. 處理器數量(processors)

  4. 系統正常運行時間(uptime),單位:毫秒

  5. 應用上下文(就是一個應用實例)正常運行時間(instance.uptime),單位:毫秒

  6. 系統平均負載(systemload.average)

  7. 堆信息(heap,heap.committed,heap.init,heap.used),單位:Kb

  8. 線程信息(threads,thread.peak,thead.daemon)

  9. 類加載信息(classes,classes.loaded,classes.unloaded)

  10. 垃圾收集信息(gc.xxx.count, gc.xxx.time)

數據源指標

Spring Boot會爲你應用中定義的支持的DataSource暴露如下指標:

  • 最大鏈接數(datasource.xxx.max)

  • 最小鏈接數(datasource.xxx.min)

  • 活動鏈接數(datasource.xxx.active)

  • 鏈接池的使用狀況(datasource.xxx.usage)

全部的數據源指標共用 datasoure. 前綴。該前綴對每一個數據源都很是合適:

  • 若是是主數據源(惟一可用的數據源或存在的數據源中被@Primary標記的)前綴爲datasource.primary

  • 若是數據源bean名稱以dataSource結尾,那前綴就是bean的名稱去掉dataSource的部分(例如,batchDataSource的前綴是datasource.batch)

  • 其餘狀況使用bean的名稱做爲前綴

Tomcat session指標

若是你使用Tomcat做爲內嵌的servlet容器,session指標將被自動暴露出去。 httpsessions.active 和 httpsessions.max 提供了活動的和最大的session數量。

追蹤(Tracing)

對於全部的HTTP請求Spring Boot自動啓用追蹤。你能夠查看 trace 端點,並獲取最近一些請求的基本信息:

附錄:執行器的詳細配置列表

# ----------------------------------------
# ACTUATOR PROPERTIES
# ----------------------------------------
# MANAGEMENT HTTP SERVER (ManagementServerProperties)
management.port= # defaults to 'server.port'
management.address= # bind to a specific NIC
management.context-path= # default to '/'
management.add-application-context-header= # default to true
management.security.enabled=true # enable security
management.security.role=ADMIN # role required to access the management endpoint
management.security.sessions=stateless # session creating policy to use (always, never, if_required, stateless)
# PID FILE (ApplicationPidFileWriter)
spring.pidfile= # Location of the PID file to write
# ENDPOINTS (AbstractEndpoint subclasses)
endpoints.autoconfig.id=autoconfig
endpoints.autoconfig.sensitive=true
endpoints.autoconfig.enabled=true
endpoints.beans.id=beans
endpoints.beans.sensitive=true
endpoints.beans.enabled=true
endpoints.configprops.id=configprops
endpoints.configprops.sensitive=true
endpoints.configprops.enabled=true
endpoints.configprops.keys-to-sanitize=password,secret,key # suffix or regex
endpoints.dump.id=dump
endpoints.dump.sensitive=true
endpoints.dump.enabled=true
endpoints.env.id=env
endpoints.env.sensitive=true
endpoints.env.enabled=true
endpoints.env.keys-to-sanitize=password,secret,key # suffix or regex
endpoints.health.id=health
endpoints.health.sensitive=true
endpoints.health.enabled=true
endpoints.health.mapping.*= # mapping of health statuses to HttpStatus codes
endpoints.health.time-to-live=1000
endpoints.info.id=info
endpoints.info.sensitive=false
endpoints.info.enabled=true
endpoints.mappings.enabled=true
endpoints.mappings.id=mappings
endpoints.mappings.sensitive=true
endpoints.metrics.id=metrics
endpoints.metrics.sensitive=true
endpoints.metrics.enabled=true
endpoints.shutdown.id=shutdown
endpoints.shutdown.sensitive=true
endpoints.shutdown.enabled=false
endpoints.trace.id=trace
endpoints.trace.sensitive=true
Spring Boot參考指南
附錄A. 常見應用屬性 393
endpoints.trace.enabled=true
# HEALTH INDICATORS (previously health.*)
management.health.db.enabled=true
management.health.elasticsearch.enabled=true
management.health.elasticsearch.response-timeout=100 # the time, in milliseconds, to wait for a response from the cluster
management.health.diskspace.enabled=true
management.health.diskspace.path=.
management.health.diskspace.threshold=10485760
management.health.mongo.enabled=true
management.health.rabbit.enabled=true
management.health.redis.enabled=true
management.health.solr.enabled=true
management.health.status.order=DOWN, OUT_OF_SERVICE, UNKNOWN, UP
# MVC ONLY ENDPOINTS
endpoints.jolokia.path=jolokia
endpoints.jolokia.sensitive=true
endpoints.jolokia.enabled=true # when using Jolokia
# JMX ENDPOINT (EndpointMBeanExportProperties)
endpoints.jmx.enabled=true
endpoints.jmx.domain= # the JMX domain, defaults to 'org.springboot'
endpoints.jmx.unique-names=false
endpoints.jmx.static-names=
# JOLOKIA (JolokiaProperties)
jolokia.config.*= # See Jolokia manual
# REMOTE SHELL
shell.auth=simple # jaas, key, simple, spring
shell.command-refresh-interval=-1
shell.command-path-patterns= # classpath*:/commands/**, classpath*:/crash/commands/**
shell.config-path-patterns= # classpath*:/crash/*
shell.disabled-commands=jpa*,jdbc*,jndi* # comma-separated list of commands to disable
shell.disabled-plugins=false # don't expose plugins
shell.ssh.enabled= # ssh settings ...
shell.ssh.key-path=
shell.ssh.port=
shell.telnet.enabled= # telnet settings ...
shell.telnet.port=
shell.auth.jaas.domain= # authentication settings ...
shell.auth.key.path=
shell.auth.simple.user.name=
shell.auth.simple.user.password=
shell.auth.spring.roles=
# SENDGRID (SendGridAutoConfiguration)
spring.sendgrid.username= # SendGrid account username
spring.sendgrid.password= # SendGrid account password
spring.sendgrid.proxy.host= # SendGrid proxy host
spring.sendgrid.proxy.port= # SendGrid proxy port
# GIT INFO
spring.git.properties= # resource ref to generated git info properties file
相關文章
相關標籤/搜索