05 Spring Cloud 准备 - Spring Boot神器介绍

393 阅读2分钟

这是我参与8月更文挑战的第5天,活动详情查看:8月更文挑战
有些地方描述为四大组件,有些地方描述为四大核心原理,有些地方描述为四大神器。个人认为称呼神器更为合适。现在描述Actuator监控管理、Spring Boot CLI 命令行工具。

1.Actuator监控管理

1.1 介绍

Actuator 是 Spring Boot 的一个非常强大的功能。它可以实现应用程序的监控管理,比如帮助我们收集应用程序的运行情况、监测应用程序的健康状况以及显示HTTP跟踪请求信息等。它是我们搭建微服务架构必不可少的环节,是整个系统稳定运行的保障。

1.2 集成步骤

  1. 在springboot 中集成Actuator,只需要在pom.xml中添加以下依赖:
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  1. 配置 Actuator 内置有很多端点(endpoint),我们通过这些端点可以获得不同的监控信息。但Actautor默认只开启health和info两个端点,需要对application.yml文件进行配置:
management:
  endpoints:
    web:
      exposure:
        include: '*'

其中'*'标识开启所有端点。
也可以开启指定端点:

management:
  endpoints:
    web:
      exposure:
        include: health,info,httptrace

上述配置表示开启health、info、httptrace端点。此时启动程序访问http://localhost:8080/actuator/health ,会看到如下结果。

{"status":"UP"}

UP说明当前系统正常运行。

2. Spring Boot CLI命令行工具

Spring Boot CLI(Command Line Interface)是一款用于快速搭建基于Spring原型的命令行工具。它支持运行Groovy脚本,这意味着你可以拥有一个与Java语言类似的没有太多样板代码的语法。通过CLI来使用Spring Boot不是唯一方式,但它是让Spring应用程序搭建的最快速方式。

2.1 安装

下载地址
repo.spring.io/release/org…
配置环境变量

  1. 将spring.bat设置到环境变量中

image.png

  1. 打开cmd,输入spring --version,查看当前版本号,完成安装 image.png

2.2 使用

如果对Groovy不熟悉,可以直接编写java的Controller。

@RestController
public class AdviceController {
	@RequestMapping("/")
	public String hello(){
		return "hello world!"
	}
}