目录
springboot入门知识
/  

springboot入门知识

来自老师的笔记

1、工程的创建

    在创建时要导入一个spring web的依赖,即导入了  

|

org.springframework.boot spring-boot-starter-web

|

2、工程的运行

有两种运行方式:  

1)在Idea下直接运行

2)打为jar包后的运行

3、工程的结构

- .mvn                 ---------- maven包装器,用于方便不同版本maven间的切换  

   --wrapper

        --- maven-wrapper.jar

        --- maven-wrapper.properties

        --- MavenWrapperDownloader.java

- src                 ---------- 我们要写的代码就写在这里  

        -- main  

            -- resources  

                    -- static              ---------- tymeleaf文件所需要用到的像.css、.js、.jpg等静态资源存放目录              

                    -- templates       --------- Spring Boot对于前端页面,建议使用tymeleaf模板引擎,该文

                                                                  件的扩展名为.html

- target           ----------- maven中对项目进行构建所生成的内容存放目录

- .gitignore       ---------- 将当前工程push到git远程库时,并不是所有文件都需要上传的,不需要上传的 

                                          文件,就将其文件名写入到该文件中,那么,这些文件将不会被git管理

 - xxx.iml          ----------- 这是idea工程生成的文件,是idea需要的工程信息

- HELP.md       ------------ 这是一个markdown文件,是一个当前工程的帮助文档,需要开发人员来编写

- mvnw               --------- maven包装器相关内容

- mvnw.cmd       --------- maven包装器相关内容

- pom.xml          ---------- maven工程的核心配置文件

4、pom文件详解

first-0.0.1-SNAPSHOT.jar.original 文件是原来的maven打包的结果

first-0.0.1-SNAPSHOT.jar 文件是spring boot进行repackage后的结果

spring-boot-starter-web等依赖的版本是继承自父pom文件的

5、在线创建Spring Boot工程

在浏览器中打开[https://start.spring.io站点,在其中创建Spring](https://start.spring.xn--io%2Cspring-sl6n820av2bg44a5zz362d537a/) Boot工程。

Spring Boot配置文件分类

Spring Boot配置文件根据文件的扩展名的不同,有两种类型:  

1、properties文件

   该文件名必须是application.properties  

|

指定当前应用的端口号

server.port=9000

指定当前应用的上下文路径

server.servlet.context-path=/aynu

|

2、yml文件

    *YAML*是"YAML Ain't a Markup Language"(YAML不是一种标记语言)的递归缩写。在开发的这种语言时,*YAML *的意思其实是:"Yet Another Markup Language"(仍是一种标记语言),但为了强调这种语言以数据做为中心,而不是以标记语言为重点,而用反向缩略语重命名。

注意:

(1)yaml,通常情况下缩写为yml。Spring Boot 配置文件名称必须是application.yml。

(2)在yml文件中空格是有效字符,不能多写也不能少写。

|

server:
port: 8888 # 指定当前应用的端口号
servlet:
context-path: /rjxy # 指定当前应用的上下文路径

|

项目监控

1、什么是Actuator?

1)acturator是Spring Boot提供的一个可插拔模块,用于对工程进行监控。

2)其可通过不同的监控终端实现不同的监控功能。

3)Actuator可以部署在每一个Spring Boot工程中,对当前工程进行监控。

2、怎么使用Acturator?

1)导入actuator依赖

|

org.springframework.boot

spring-boot-starter-actuator

|

2)在浏览器地址栏中输入如下地址

http://localhost:8888/rjxy/actuator/health

3)为actuator指定专门的端口、上下文路径与basePath

在主配置文件中写入如下配置:  

|

修改actuator的相关配置

management:
server:
port: 9999 # 指定actuator的端口号
servlet:
context-path: /aynu # 指定actuator的上下文路径
endpoints:
web:
base-path: /java # 指定监控终端的基本路径,用于替换默认的/actuator路径

|

此时在浏览器地址栏中需要输入如下地址才可看到监控结果

http://localhost:9999/aynu/java/health

4)开户其它监控终端

默认情况下,actuator仅开启了两个监控终端:health与info。其它监控终端需要专门开启:在主配置文件中添加如下配置:  

|

management:
endpoints:
web:
base-path: /java # 指定监控终端的基本路径,用于替换默认的/actuator路径
exposure:
include: "*"

|

5)关闭指定的监控终端

|

management:
endpoints:
web:
base-path: /java # 指定监控终端的基本路径,用于替换默认的/actuator路径
exposure:
include: "*" # 开启所有actuator的监控终端
# include: ["env", "health"]
#include:
# - env
# - health
exclude: ["info", "env"] # 排除指定的监控终端

|

3、监控终端

1)health

用于显示当前应用的状态:UP与Down  

2)info

用于显示当前应用的相关信息,这些信息是配置在主配置文件中的,例如:  

|

:

:

:

: 17
Java

:

:

:

:

: @project.groupId@

: @project.artifactId@

: @project.version@

|

3) shutdown

用于关闭当前应用。其使用需要首先启用该监控终端,然后再提交POST请求。  

在主配置文件中启用该终端。  

|

:

:

:

:

|


标题:springboot入门知识
作者:gitsilence
地址:https://blog.lacknb.cn/articles/2020/02/20/1582166992695.html