您现在的位置是:首页 >技术交流 >Springboot配置Yml多环境网站首页技术交流
Springboot配置Yml多环境
简介Springboot配置Yml多环境
文章目录
学习链接
easypan
Spring Boot手把手教学(2):使用yml多环境配置和创建多环境profile打包
springboot 配置多环境使用yml配置 @profileActive@ 启动报错Do not use @ for indentation
Spring application使用@ 使用的问题:‘@’ that cannot start any token. (Do not use @ for indentation)
关于java:Maven:无法执行目标org.apache.maven.plugins:maven-resources-plugin:2.7:resources
前言
在开发的时候,用的是一套配置,但是项目部署到线上运行的时候,需要修改为线上的配置,不可能每次等到需要打包部署的时候,改成线上配置,开发的时候又恢复成开发环境的配置(虽然也可以通过git的分支
来玩,但是合并分支的时候还得小心别覆盖了),但是springboot提供了profiles这个多环境配置功能
,只需要在idea的maven面板切换即可,比较方便,除此之外,还可以使用@Profile这个注解定义bean在何种环境下配置生效
(如:@Profile(‘dev’)标识该配置仅在dev环境下生效)
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zzhua</groupId>
<artifactId>boot-blog</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>boot-blog</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>p6spy</groupId>
<artifactId>p6spy</artifactId>
<version>3.9.1</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--swagger2依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<!--swagger-ui第三方依赖-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.2</version>
</dependency>
<!--Lettuce是 一 个 基 于 Netty的 NIO方 式 处 理 Redis的 技 术 -->
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.6.0</version>
</dependency>
<!--邮件依赖-->
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- 验证码 -->
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
<!-- UserAgentUtils -->
<!-- https://mvnrepository.com/artifact/eu.bitwalker/UserAgentUtils -->
<dependency>
<groupId>eu.bitwalker</groupId>
<artifactId>UserAgentUtils</artifactId>
<version>1.20</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<profiles>
<profile>
<!-- 开发环境 -->
<id>dev</id>
<properties>
<profileActive>dev</profileActive>
</properties>
<!-- 默认激活的环境 -->
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 生产环境 -->
<id>prod</id>
<properties>
<profileActive>prod</profileActive>
</properties>
</profile>
</profiles>
<build>
<resources>
<!--<resource>
<directory>src/main/resources</directory>
<includes>
<include>application-${profileActive}.yml</include>
<include>application.yml</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>-->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<!-- 为使用@profileActive@而添加 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<configuration>
<delimiters>
<delimiter>@</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
<!-- 处理maven-resources-plugin:2.7报错而添加 -->
<dependencies>
<dependency>
<groupId>org.apache.maven.shared</groupId>
<artifactId>maven-filtering</artifactId>
<version>1.3</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- maven 打包时跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.yml
spring:
profiles:
# 使用@...@来引用maven中定义的变量
active: @profileActive@
# 以下是公共配置
servlet:
multipart:
max-file-size: 10MB
max-request-size: 10MB
mail:
host: smtp.qq.com
port: 587
username: 1255112011@qq.com
password: 填入自己的授权码
default-encoding: UTF-8
jackson:
time-zone: GMT+8
security:
ignore-urls:
- /**/*.css
- /**/*.js
- /doc.html
- /webjars/**
- /**/*swagger*/**
- /v2/api-docs
- /favicon.ico
- /test/**
- /static/**
application-dev.yml 开发环境配置
server:
port: 9091
file:
static-resource:
path: D:Projectsoot-blogsrcmain
esourcesstatic
spring:
datasource:
type: org.springframework.jdbc.datasource.DriverManagerDataSource
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
url: jdbc:p6spy:mysql://127.0.0.1:3306/boot-blog?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
username: root
password: root
redis:
host: 127.0.0.1
port: 6379
password:
database: 0
lettuce:
pool:
min-idle: 0
max-idle: 8
max-active: 8
max-wait: 100ms
application-prod.yml 线上环境配置
server:
port: 9091
file:
static-resource:
path: /usr/local/blog/bootblog/res/
spring:
datasource:
type: org.springframework.jdbc.datasource.DriverManagerDataSource
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
url: jdbc:mysql://ip:port/boot-blog?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
username: root
password: 填入自己的数据库密码
redis:
host: 127.0.0.1
port: 6379
password: 填入自己的reids密码
database: 0
lettuce:
pool:
min-idle: 0
max-idle: 8
max-active: 8
max-wait: 100ms
logback.xml 日志配置
日志文件配置
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<!-- 引入spirng boot默认的logback配置文件 -->
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<!--定义日志文件的存储地址 -->
<springProfile name="prod">
<!-- 打印在当前项目运行目录下的logs文件夹中 -->
<property name="LOG_BASE_PATH" value="./logs" />
</springProfile>
<springProfile name="dev">
<!-- 此处使用绝对路径, 须注意这里要用斜杠, 不能使用反斜杠 -->
<property name="LOG_BASE_PATH" value="D:/usr" />
</springProfile>
<property name="maxFileSize" value="5MB"/>
<property name="maxHistory" value="30"/>
<!-- 控制台日志打印格式 -->
<property name="consolePattern" value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(-%5p) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n" />
<!-- 线上日志打印格式 -->
<property name="filePattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS}-${PID}-[%t] %-5level %logger{30} [%file:%line] - %msg%n"/>
<!--控制台日志, 控制台输出 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度,%msg:日志消息,%n是换行符-->
<pattern>${consolePattern}</pattern>
</encoder>
</appender>
<!--文件日志, 按照每天生成日志文件 -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_BASE_PATH}/bootblog.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!--日志文件输出的文件名-->
<FileNamePattern>${LOG_BASE_PATH}/%d/bootblog.log.%d{yyyy-MM-dd}-%i.log</FileNamePattern>
<!--日志文件保留天数-->
<MaxHistory>${maxHistory}</MaxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${maxFileSize}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
<pattern>${filePattern}</pattern>
</encoder>
</appender>
<!-- 生产环境打印sql到日志中, 开发环境直接使用p6spy -->
<springProfile name="prod">
<!-- additivity指示为false,即不要传给父logger, debug打印sql -->
<logger name="com.zzhua.blog.mapper" level="debug" additivity="false">
<appender-ref ref="FILE" />
</logger>
</springProfile>
<!-- 日志输出级别 -->
<root level="INFO">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE"/>
</root>
</configuration>
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。