- 监控管理
Actuator监控应用
Actuator是spring boot的一个附加功能,可帮助你在应用程序生产环境时监视和管理应用程序。可以使用HTTP的各种请求来监管,审计,收集应用的运行情况.特别对于微服务管理十分有意义.缺点:没有可视化界面。
Maven依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
</dependencies> |
YML配置
###通过下面的配置启用所有的监控端点,默认情况下,这些端点是禁用的; 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
|
Actuator访问路径
通过actuator/+端点名就可以获取相应的信息。
路径 | 作用 |
/actuator/beans | 显示应用程序中所有Spring bean的完整列表。 |
/actuator/configprops | 显示所有配置信息。 |
/actuator/env | 陈列所有的环境变量。 |
/actuator/mappings | 显示所有@RequestMapping的url整理列表。 |
/actuator/health | 显示应用程序运行状况信息 up表示成功 down失败 |
/actuator/info | 查看应用信息 |
演示案例:
/actuator/info 配置文件新增
Admin-UI分布式微服务监控中心
Admin-UI基于actuator实现能够返回界面展示监控信息
Admin-UI-Server
Maven依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <!-- Spring Boot Actuator对外暴露应用的监控信息,Jolokia提供使用HTTP接口获取JSON格式 的数据 --> <dependency> <groupId>org.jolokia</groupId> <artifactId>jolokia-core</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1</version> </dependency> </dependencies> |
application.yml配置文件
spring: application: name: spring-boot-admin-server |
启动方式
@Configuration @EnableAutoConfiguration @EnableAdminServer public class AdminServerApplication {
public static void main(String[] args) { SpringApplication.run(AdminServerApplication.class, args); } } |
Admin-UI-Client
Maven依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.jolokia</groupId> <artifactId>jolokia-core</artifactId> </dependency> <dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1</version> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
</dependencies> |
YML配置
spring: boot: admin: client: url: http://localhost:8080 server: port: 8081
management: endpoints: web: exposure: include: "*" endpoint: health: show-details: ALWAYS |
启动类、
@SpringBootApplication public class AppClinet {
public static void main(String[] args) { SpringApplication.run(AppClinet.class, args); }
} |