Spring Boot 必知必会
一、是什么?能做什么?
-
是一个快速开发的脚手架
-
作用:快速创建独立的、生产级的基于Spring的应用程序
SpringBoot是基于Spring的用来提升效率的框架,可以让编码更加简单,配置管理也更加简单,部署也更方便。
-
特性
- 无需部署WAR文件
- 提供starter简化配置
- 尽可能自动配置Spring以及第三方库
- 提供“生产就绪”功能,例如指标、健康检查、外部配置等
- 无代码生成&无XML
二、编写第一个SpringBoot应用
- 需求
- 整合Spring MVC
- /test路径(端点)
- 使用Spring Initializr快速创建Spring Boot应用
![image-20210929122815096](https://heling-picture.oss-cn-hangzhou.aliyuncs.com/img/image-20210929122815096.png)
左侧选择Spring Initializr,SDK选择JDK1.8,Custom使用http://start.aliyun.com可以加快下载速度,然后点击Next创建。
填写Group和项目唯一标识Artifact,打包方式选择Jar,这也是Sprint官方推荐的打包方式,Java版本选择8,点击Next。
选择最新版本的Spring Boot,添加Spring Web来整合Spring MVC。
填写项目名称以及项目地址,完成。
生成的项目其实已经整合了MVC。
下面写Test端点,在cn.cstube包下创建类TestController。
package cn.cstube;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @auther heling
* @date 2021/9/29
*/
@RestController
public class TestController {
@GetMapping("/test")
public String test(){
return "test";
}
}
在项目启动之前,使用mvn clean install
确保能够构建成功再去启动,主要是为了防止有Jar包没有下载完整,导致启动失败。
最后启动SpringBootDemoApplication类。
在浏览器访问localhost:8080/test,可以看到可以返回test。
以上是在Idea中的启动,在实际的项目部署中我们需要进到target
目录,找到可以执行的jar包,然后执行java -jar spring-boot-demo-0.0.1-SNAPSHOT.jar
,同样在浏览器中也可以返回正常结果。
三、Spring Boot的应用组成分析
- 依赖:
pom.xml
<dependencies>
<!--springmvc整合-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--单元测试支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- 启动类:注解
@SpringBootApplication
代表这个类为启动类
@SpringBootApplication
public class SpringBootDemoApplication {
//启动类
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
}
- 配置:application.properties
# 应用名称
spring.application.name=spring-boot-demo
# 应用服务 WEB 访问端口
server.port=8080
- static目录:静态文件
主要用于存放静态文件,比如html,css文件。
- templates目录:模板文件
用于存放模板文件,因为Spring MVC支持多种视图格式,比如jsp,freemarker等模板引擎,但是现在的应用大多基于前后端分离开发,以后基本用不到模板引擎。
四、Spring Boot开发三板斧
- 加依赖
如果你想在Spring Boot整合xxx,那么只需要在pom.xml
中上xxx的依赖即可。例如整合JPA和Mybatis。
<!-- boot官方提供:spring-boot-starter-xxx-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- mybatis 非官方提供starter:xxx-spring-boot-starter-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
- 写注解
在启动类中会添加各种各样的注解
- 写配置
在application.properties
中添加各种配置
五、Spring Boot Actuator
- 是什么?
Spring Boot Actuator是Spring Boot中非常重要的组件,它为我们的应用提供了强大的监控能力。如今的应用越来越复杂,我们需要一些监控工具来帮助我们记录问题。
- 如何整合?
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
启动应用,可以看到actuator暴露了两个端点。
打开localhost:8080/actuator
![image-20210929145852358](https://heling-picture.oss-cn-hangzhou.aliyuncs.com/img/image-20210929145852358.png)
点击http://localhost:8080/actuator/health
![image-20210929150133024](https://heling-picture.oss-cn-hangzhou.aliyuncs.com/img/image-20210929150133024.png)
health端点是非常重要的端点,以后会经常用到。作用是健康检查,也就是检查应用的资源。我们需要在配置文件中添加配置。
# 应用名称
spring.application.name=spring-boot-demo
# 应用服务 WEB 访问端口
server.port=8080
management.endpoint.health.show-details=always
启动应用。再次打开http://localhost:8080/actuator/health
![image-20210929150622468](https://heling-picture.oss-cn-hangzhou.aliyuncs.com/img/image-20210929150622468.png)
可以看到这里检查了磁盘资源,磁盘大小为325G,剩余空间为63.3G,如果剩余空间低于threshold,那么这个磁盘就是不健康的。
status取值:
- UP:正常
- DOWN: 遇到了问题,不正常
- OUT_OF_SERVICE: 资源未在使用,或者不该去使用
- UNKNOWN: 不知道
http://localhost:8080/actuator/info是一个描述端点,主要用来描述应用信息。例如在配置文件中添加配置:
# 描述应用
info.app-name=spring-boot-demo
info.author=heling
info.email=xxx@email
启动应用,然后访问info端点
![image-20210929151753941](https://heling-picture.oss-cn-hangzhou.aliyuncs.com/img/image-20210929151753941.png)
下面是Spring Boot Actuator常见的监控端点
除了health和info,其他的端点都是隐藏的。只需要在配置文件中添加配置即可激活所有端点。
# 激活所有的actuator端点
management.endpoints.web.exposure.include=*
重启应用,刷新localhost:8080/actuator就会发现已激活所有端点,如果只想激活个别端点,那么就更改一下配置,比如只激活metrics和health端点。
management.endpoints.web.exposure.include=metrics,health
六、Spring Boot配置管理
- 支持的配置格式
Spring Boot除了支持.properties
类型的配置文件外,还支持.yml
或者.yaml
的文件。
**Yet Anther Markup Language(.yml/.yaml)**是JSON子集,格式如下
info:
app-name: spring-boot-demo
author: heling
email: xxx@email
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: metrics,health
server:
port: 8080
spring:
application:
name: spring-boot-demo
需要注意的是:
后的空格一定不能缺少,另外每一行前有严格的缩进。
在实际项目中更推荐使用.yml
格式的配置文件,因为其可读性更强,在业界更受欢迎,而且它可以保证配置读取的顺序。
-
配置管理常用方式
-
配置文件
-
环境变量
例如在将
always
改为${SOME_ENV}
info: app-name: spring-boot-demo author: heling email: xxx@email management: endpoint: health: show-details: ${SOME_ENV} endpoints: web: exposure: include: metrics,health server: port: 8080 spring: application: name: spring-boot-demo
然后在应用启动的时候指定环境变量
启动应用发现依然可以展示详情,但是在重新构建应用的时候,会因为单元测试报错,因为单元测试拿不到idea中的配置,这时候可以删除测试类或者在使用
mvn clean install -DskipTests
跳过单元测试。构建应用之后,使用java -jar spring-boot-demo-0.0.1-SNAPSHOT.jar --SOME_ENV=always
启动应用。 -
外部配置文件
将Jar包和配置文件放在同一目录下,然后使用命令启动应用,Spring Boot会优先读取配置文件中的配置,也就是说外部配置文件的优先级比Jar包的配置文件更高。
-
命令行参数
比如我希望应用的Tomcat运行端口为8081,但我又不想写在配置文件中,只需要在运行配置里填写
--server.port=8081
就可以了。如果是使用命令的方式启动应用的话,使用
java -jar spring-boot-demo-0.0.1-SNAPSHOT.jar -- server.port=8081
启动即可。
-
七、Profile
- 如何实现不同环境不同配置
在实际项目中我们往往需要在不同的环境使用不同的配置,例如开发环境的Tomcat不需要性能调优,但是生产环境的Tomcat往往需要设置一些性能调优参数,比如最大连接数,最大限制数等,这个时候就可以使用Profile。
- 怎么使用
如果应用是yml
格式配置的,那么在配置文件中使用连字符---
将配置文件分成若干段,
# 所有环境公用的配置属性
info:
app-name: spring-boot-demo
author: heling
email: xxx@email
management:
endpoint:
health:
show-details: ${SOME_ENV}
endpoints:
web:
exposure:
include: metrics,health
spring:
application:
name: spring-boot-demo
---
spring:
config:
activate:
on-profile: dev
---
# profile=y的专用属性,也就是某个环境下的专用属性
# 生产环境
spring:
config:
activate:
on-profile: prod
server:
tomcat:
threads:
max: 300
max-connections: 1000
如果应用以dev
启动的话就会使用1,2两段,如果以prod
启动的话,就会使用1,3两段。例如使用prod
启动
如果想在配置文件中指定启动环境,则添加配置
![image-20210929162306832](https://heling-picture.oss-cn-hangzhou.aliyuncs.com/img/image-20210929162306832.png)