• Springboot学习笔记——2


    一、打包与运行

    1.1、程序打包与运行(windows版)

    1. 对SpringBoot项目打包(执行Maven构建指令package)
      在这里插入图片描述
    2. 运行项目(在cmd中执行启动指令)
    java -jar springboot.jar
    
    • 1

    jar支持命令行启动需要依赖maven插件支持,请确认打包时是否具有SpringBoot对应的maven插件

    <build>
    	<plugins>
    		<plugin>
    			<groupId>org.springframework.bootgroupId>
    			<artifactId>spring-boot-maven-pluginartifactId>
    		plugin>
    	plugins>
    build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    1.2、打包插件

    • 使用SpringBoot提供的maven插件可以将工程打包成可执行jar包
    <build>
    	<plugins>
    		<plugin>
    			<groupId>org.springframework.bootgroupId>
    			<artifactId>spring-boot-maven-pluginartifactId>
    		plugin>
    	plugins>
    build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    jar包描述文件(MANIFEST.MF)

    • 普通工程
    Manifest-Version: 1.0
    Implementation-Title: springboot_08_ssmp
    Implementation-Version: 0.0.1-SNAPSHOT
    Build-Jdk-Spec: 1.8
    Created-By: Maven Jar Plugin 3.2.0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 基于spring-boot-maven-plugin打包的工程
    Manifest-Version: 1.0
    Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx
    Implementation-Title: springboot_08_ssmp
    Implementation-Version: 0.0.1-SNAPSHOT
    Spring-Boot-Layers-Index: BOOT-INF/layers.idx
    Start-Class: com.itheima. SSMPApplication
    Spring-Boot-Classes: BOOT-INF/classes/
    Spring-Boot-Lib: BO0T-INF/lib/
    Build-Jdk-Spec: 1.8
    Spring-Boot-Version: 2.5.4
    Created-By: Maven Jar Plugin 3.2.0
    Main-Class: org.springframework.boot.loader.JarLauncher
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • Windows端口被占用
    #查询端口
    netstat -ano
    #查询指定端口
    netstat -ano |findstr "端口号"
    #根据进程PID查询进程名称
    tasklist lfindstr "进程PID号"
    # 根据PID杀死任务
    taskkill /F /PID "进程PID号"
    # 根据进程名称杀死任务
    taskkill -f -t -im "进程名称"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    1.3、Boot工程快速启动(Linux版)

    • 基于Linux ( CenteroS7)
    • 安装JDK,且版本不低于打包时使用的JDK版本
    • 安装包保存在/usr/local/自定义目录中或$HOME下
    • 其他操作参照windows版进行

    二、配置高级

    2.1、临时属性设置

    • 带属性启动Springboot
    java -jar springboot.jar --server.port=80
    
    • 1
    • 携带多个属性启动Springboot,属性间使用空格分隔
    • 临时属性必须是当前boot工程支持的属性,否则设置无效
      属性加载优先顺序
      参看属性加载优先顺序文档
      在这里插入图片描述
    • 带属性启动SpringBoot程序,为程序添加运行属性
      在这里插入图片描述
    • 通过编程形式带参数启动SpringBoot程序,为程序添加运行参数
    @SpringBootApplication
    public class SsmpApplication {
    	public static void main(String[] args) {
    		//可以通过设置一个字符串数组的方式定义参数
    		String[] arg =  new  String[1];
    		arg[0] = "--server.port=8082";
    		SpringApplication.run(SsmpApplication.class, arg);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 不携带参数启动SpringBoot程序
    @SpringBootApplication
    public class SsmpApplication {
    	public static void main(String[] args) {
    		//可以在启动boot程序时断开读取外部临时配置对应的入口,也就是去掉读取外部参数的形参
    		SpringApplication.run(SsmpApplication.class);
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2.2、配置程序四级分类

    1. SpringBoot中4级配置文件
      1级: file : config/application. yml
      2级: file : application.yml
      3级: classpath: config/application.yml
      4级: classpath: application.yml
    2. 作用:
      • 1级与2级留做系统打包后设置通用属性,1级常用于运维经理进行线上整体项目部署方案调控
      • 3级与4级用于系统开发阶段设置通用属性,3级常用于项目经理进行整体项目属性调控

    2.3、自定义配置文件

    • 通过启动参数加载配置文件(无需书写配置文件扩展名)

    在这里插入图片描述

    • 通过启动参数加载指定文件路径下的配置文件时可以加载多个配置
      在这里插入图片描述
      注意事项:

    多配置文件常用于将配置进行分类,进行独立管理,或将可选配置单独制作便于上线更新维护

    重要说明

    • 单服务器项目:使用自定义配置文件需求较低
    • 多服务器项目:使用自定义配置文件需求较高,将所有配置放置在一个目录中,统一管理
    • 基于SpringCloud技术,所有的服务器将不再设置配置文件,而是通过配置中心进行设定,动态加载配置信息

    三、多环境开发

    3.1、多环境开发(YAML版)

    在这里插入图片描述

    #应用环境
    spring:
      profiles:
        active: pro
    ---
    #设置环境
    #生产环境
    spring:
      config:
        activate:
          on-profile: pro
    server:
      port: 80
    ---
    #开发环境
    spring:
      config:
        activate:
          on-profile: dev
    server:
      port: 81
    ---
    #测试环境
    spring:
      config:
        activate:
          on-profile: test
    server:
      port: 82
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    在这里插入图片描述

    3.2、多环境开发文件版(YAML版)

    1. 主启动配置文件application.yml
    #应用环境
    spring:
      profiles:
        active: dev
    
    • 1
    • 2
    • 3
    • 4
    1. 配置分类配置文件application-pro.yml
    server:
      port: 80
    
    • 1
    • 2
    1. 配置分类配置文件application-dev.yml
    server:
      port: 81
    
    • 1
    • 2
    1. 配置分类配置文件application-test.yml
    server:
      port: 82
    
    • 1
    • 2
    • 主配置文件中设置公共配置(全局)
    • 环境分类配置文件中常用于设置冲突属性(局部)

    3.3、多环境开发文件版(properties版)

    1. 主启动配置文件application.properties
    spring.profiles.active=pro
    
    • 1
    1. 配置分类配置文件application-pro.properties
    server.port=9080
    
    • 1
    1. 配置分类配置文件application-dev.properties
    server.port=9081
    
    • 1
    1. 配置分类配置文件application-test.properties
    server.port=9082
    
    • 1

    3.4、多环境分组管理

    • 根据功能对配置文件中的信息进行拆分,并制作成独立的配置文件,命名规则如下

      • application-devDB.yml
      • application-devRedis.yml
      • application-devMVC.yml
    • 使用include属性在激活指定环境的情况下,同时对多个环境进行加载使其生效,多个环境间使用逗号分隔

    spring:
    	profiles:
    		active:
    		include: devDB,devRedis,devMVC
    
    • 1
    • 2
    • 3
    • 4

    注意事项:

    当主环境dev与其他环境有相同属性时,主环境属性生效;其他环境中有相同属性时,最后加载的环境属性生效

    • 从SpringBoot2.4版开始使用group属性替代include属性,降低了配置书写量
    • 使用group属性定义多种主环境与子环境的包含关系
    spring:
      profiles:
        active: dev
        group:
          "dev": devMVC,devDB,devRedis
          "pro": proMVC,proDB,proRedis
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3.5、多环境开发控制

    1. Maven中设置多环境
    <profiles>
    	<profile>
    		<id>env_devid>
    		<properties>
    			<profile.active>devprofile.active>
    			<activation>
    				<activeByDefault>trueactiveByDefault>
    			activation>
    		properties>
    	profile>
    	<profile>
    		<id>env_proid>
    		<properties>
    			<profile.active>proprofile.active>
    		properties>
    	profile>
    	<profile>
    		<id>env_testid>
    		<properties>
    			<profile.active>testprofile.active>
    		properties>
    	profile>
    profiles>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    1. SpringBoot中引用Maven属性
    spring:
      profiles:
        active: @profile.active@
    
    • 1
    • 2
    • 3
    1. 执行Maven打包指令,并在生成的boot打包文件.jar文件中查看对应信息
    • 当Maven与SpringBoot同时对多环境进行控制时,以Maven为主,SpringBoot使用@…@占位符读取Maven对应的配置属性值
    • 基于SpringBoot读取Maven配置属性的前提下,如果在Idea下测试工程时pom.xml每次更新需要手动compile方可生效

    四、日志

    4.1、日志基础

    • 日志(log)作用
      • 编程期调试代码
      • 运行期记录信息
        • 记录日常运营重要信息(峰值流量、平均响应时长……)
        • 记录应用报错信息(错误堆栈)
        • 记录运维过程数据(扩容、宕机、报警……)
    1. 添加日志记录操作
    @RestController
    @RequestMapping("/books")
    public class bookController {
        //创建记录日志的对象
        private static final Logger log = LoggerFactory.getLogger(bookController.class);
        @GetMapping
        public String getById(){
            System.out.println("SpringBoot is running...");
            log.debug("debug.............");
            log.info("info.............");
            log.warn("warn.............");
            log.error("error.............");
            return "SpringBoot is running...";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 日志级别
      • TRACE:运行堆栈信息,使用率低
      • DEBUG:程序员调试代码使用
      • INFO:记录运维过程数据
      • WARN:记录运维过程报警数据
      • ERROR:记录错误堆栈信息
      • FATAL:灾难信息,合并计入ERROR
    1. 设置日志输入级别
    # 开启debug模式,输出调试信息。常用于检查系统运行情况
    debug:true
    # 设置日志级别,root表示根节点,即整体应用日志级别
    logging:
      level:
        root: info
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    1. 设置日志组,控制指定包对应的日志输出级别,也可以直接控制指定包对应的日志输出级别
    logging:
      #设置分组
      group:
        ebank: com.smulll.controller,com.smulll
        iservice: com.fasterxml
      level:
        root: info
        #设置某个包的日志级别
        com.smulll.controller: debug
        #设置分组,对某个分组设置日志级别
        ebank: debug
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    4.2、快速创建日志对象

    • 引入lombok的依赖
    <dependency>
    	<groupId>org.projectlombokgroupId>
    	<artifactId>lombokartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 使用lombok提供的注解@slf4j简化开发,减少日志对象的声明操作
    @Slf4j
    @RestController
    @RequestMapping("/books")
    public class bookController { @GetMapping
        public String getById(){
            System.out.println("SpringBoot is running...");
            log.debug("debug.............");
            log.info("info.............");
            log.warn("warn.............");
            log.error("error.............");
            return "SpringBoot is running...";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    4.3、日志输入格式控制

    在这里插入图片描述

    • PID:进程ID,用于表明当前操作所处的进程,当多服务同时记录日志时,该值可用于协助程序员调试程序
    • 所属类/接口名:当前显示信息为SpringBoot重写后的信息,名称过长时,简化包名书写为首字母,甚至直接删除
    • 设置日志输出格式
    logging:
      pattern:
        console: 
    
    • 1
    • 2
    • 3
    • %d:日期
    • %m:消息
    • %n:换行
    logging:
      pattern:
        console: "%d %clr(%5p) ---[%16t] %clr(%-40.40c){cyan} : %m %n"
    
    • 1
    • 2
    • 3

    4.4、文件记录日志

    • 设置日志文件
    logging:
    #设置日志文件
      file:
        name: server.log
    
    • 1
    • 2
    • 3
    • 4
    • 日志文件详细设置
    logging:
    #设置日志文件
      file:
        name: server.log
      logback:
        rollingpolicy:
          file-name-pattern: server.%d{yyyy-MM-dd}.%i.log
          max-file-size: 5KB
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 相关阅读:
    UA-DETRAC BITVehicle车辆检测数据集(含下载地址)
    游戏开发需不需要考研?
    麒麟软件副总裁李震宁:中国开源社区是操作系统破局的土壤
    MongoDB的日志目录被删除了,导致无法启动:(code=exited, status=1/FAILURE)
    基于Java+SpringBoot+vue+element实现汽车订票管理平台详细设计和实现
    teamtalk实现即时通讯
    HTML5期末考核大作业 简单的学生网页作业源码 基于html css javascript仿王者荣耀首页 游戏网站开发 游戏官网设计与实现
    django-restful框架-不能运行在主线程-错误
    JPA 中 LazyInitializationException 解决方案
    以32bit加法器为核心的加法、减法、乘法和除法计算器(ALU)
  • 原文地址:https://blog.csdn.net/weixin_74155781/article/details/133543548