赞
踩
/actuator
,/actuator/info
,/actuator/health
】,需要添加properties中的启动端点配置/actuator/
作为前缀,比如/actuator/beans
在2.0之前就是/beans
引入依赖[build.gradle]
//web组件依赖
implementation 'org.springframework.boot:spring-boot-starter-web'
//Actuator依赖
implementation 'org.springframework.boot:spring-boot-starter-actuator'
配置文件[application.yml]
# 通过下面的配置启动所有的监控端点,默认情况下这些断点是禁用的[启动项目的时候监控的端点] management: endpoints: web: exposure: include: * #在properties中是management.endpoints.web.exposure.include=* spring: profiles: active:prod datasource: driver-class-name:com.mysql.jdbc.Driver url:jdbc:mysql://127.0.0.1:3306/test username:root password:root
运行效果如下,只有REST方式访问形式
通过actuator/+端点名获取相应的信息
路径 | 作用 |
---|---|
/actuator/beans | 显示应用程序中所有Springbean的完成列表 |
/actuator/configprops | 显示所有配置信息 |
/actuator/env | 陈列所有的环境变量 |
/actuator/mappings | 显示所有@RequestMapping url整理列表 |
/actuator/health | 显示应用程序运行状况信息,up表示成功,down失败 |
/actuator/info | 查看自定义应用信息【相当于在配置文件中配置info开头的配置信息】 |
当访问/actuator/health检测服务器配置返回为down时,表示相关配置信息有错误,为up时表示配置信息都可以跑通【原理:实际就是读取配置文件,模拟发送信息访问】
当访问/actuator/configprops显示系统的所有配置信息
当访问/actuator/info是查看自定义应用信息,在配置文件中配置的info开头的配置信息,如:
添加properties信息
info.name=jack
访问/actuator/info获得的返回信息
{"name":"jack"}
将所有服务(都继承Actuator监控)的监控中心管理存放在adminUI上
服务分布图
应用中添加依赖
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'de.codecentric:spring-boot-admin-starter-server'
implementation 'org.jolokia:jolokia-core'
implementation 'com.googlecode.json-simple:json-simple:1.1'
启动文件添加注解
package com.example.AdminUIServer; import de.codecentric.boot.admin.server.config.EnableAdminServer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Configuration; @Configuration @EnableAutoConfiguration @EnableAdminServer public class AdminUiServerApplication { public static void main(String[] args) { SpringApplication.run(AdminUiServerApplication.class, args); } }
运行项目截图
application.properties[将服务启动在8080端口上,并通过management.endpoints.web.exposure.include=*
的设置定义所有端点内容都被暴露,通过spring.boot.admin.client.url=http://localhost:8081
的设置定义server的路径]
spring.boot.admin.client.url=http://localhost:8081
management.endpoints.web.exposure.include=*
server.port=8080
build.gradle引入admin-starter-client和starter-actuator
plugins { id 'org.springframework.boot' version '2.1.3.RELEASE' id 'java' } apply plugin: 'io.spring.dependency-management' group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1.8' repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'de.codecentric:spring-boot-admin-starter-client:2.1.3' testImplementation 'org.springframework.boot:spring-boot-starter-test' }
application.java启动项目
package com.example.actuatorDemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ActuatorDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ActuatorDemoApplication.class, args);
}
}
application.properties
server.port=8081
build.gradle引入spring-boot-admin-starter-server
plugins { id 'org.springframework.boot' version '2.1.3.RELEASE' id 'java' } apply plugin: 'io.spring.dependency-management' group = 'com.example' version = '0.0.1-SNAPSHOT' sourceCompatibility = '1.8' repositories { mavenCentral() } ext { set('springBootAdminVersion', '2.1.3') } dependencies { implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'de.codecentric:spring-boot-admin-starter-server' implementation 'de.codecentric:spring-boot-admin-server-ui' testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'io.projectreactor:reactor-test' } dependencyManagement { imports { mavenBom "de.codecentric:spring-boot-admin-dependencies:${springBootAdminVersion}" } }
启动类上添加@EnableAdminServer注解启动Server
package com.example.AdminUIServer;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableAdminServer
public class AdminUiServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminUiServerApplication.class, args);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。