說明
spring boot提供的actuator插件提供了大量的生產級特性,通過配置進行使用
<!-- 監(jiān)控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
endpoint
我們可以通過一系列的HTTP請求獲得應用程序的相關信息,這些請求就是endpoint,具體有如下幾個
autoconfig : 獲取自動配置信息
beans :獲取Spring Bean基本信息
configprops :獲取配置項信息
dump : 獲取當前線程基本信息
env :獲取環(huán)境變量信息
health : 獲取健康檢查信息
info : 獲取應用的基本信息
metrics : 獲取性能指標信息
mappings : 獲取請求映射信息
trace : 獲取請求調用信息
可以通過application.properties配置文件進行精準控制
# 放棄安全限制
management.security.enabled=false
# 關閉一個endpoint:beans
endpoints.beans.enabled=false
# 關閉所有endpoint,僅打開beans
endpoints.enable=false
endpoints.beans.enabled=false
# 修改endpoint名稱
endpoints.beans.id=mybeans
# 修改請求路徑
endpoints.beans.path=/endpoints/mybeans
信息聚合
POM.xml文件中增加如下內容
<!-- actuator聚合插件 /actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
可以通過/actuator 來訪問聚合信息
可以通過圖形化插件進行顯示,在POM.xml文件中增加如下內容
<!-- actuator聚合圖形化插件 /actuator -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-hal-browser</artifactId>
</dependency>
可以通過/actuator 來訪問圖形化聚合信息
應用信息
通過/info可以查看應用信息,信息可以在application.properties文件中配置,如果產生亂碼,請戳這里,有解決方案
info.app.name=應用名稱,使用POM中的信息,@project.modelVersion@
info.app.description=對應用的描述
info.app.version=1.0.0
# 自定義用戶信息
info.user.name=hutou
info.user.sex=男
info.user.age=22
健康檢查
通過/health可以進行監(jiān)控檢查,下面是一些配置項
# 暴露磁盤空間信息
endpoints.health.sensitive=true
# 健康信息緩存時間,設置太短會影響性能
endpoints.health.time-to-live=500
spring boot提供了很多內置的監(jiān)控檢查功能,都放置在:org.springframework.boot.actuate.health包下
ApplicationHealthIndicator
DiskSpaceHealthIndicator
DataSourceHealthIndicator 檢查數(shù)據(jù)庫連接
MailHealthIndicator 檢查郵件服務器
MongoHealthIndicator 檢查MongoDB數(shù)據(jù)庫
....
可以自行擴展健康檢查,一篇好文章
跨域訪問
CORS實現(xiàn)跨域訪問是比較好的方案!Spring boot有很好的支持!
- 在配置文件中進行配置
## 跨域訪問
endpoints.cors.allowed-origins=http://www.baidu.com
endpoints.cors.allowed-methods=GET,PUT,POST,DELETE
- 在應用中增加@CrossOrigin注解來實現(xiàn)跨域
- 自定義配置
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter{
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://www.baidu.com")
.allowedMethods("GET","PUT","POST","DELETE");
}
}