在Springboot的开发中,我们经常要切换各种各样的环境配置,比如现在是开发环境,然后又切换到生产环境,这个时候用多环境配置就是一个明智的选择。接下来我们沿用上一章的工程来配置多环境配置工程。
1.准备多环境配置文件
这里我们准备三个配置文件
application.properties是通用统一的配置文件,内容如下
server.port=8088
spring.profiles.active=@profile.active@
application-dev.properties是开发用的配置文件,内容如下
server.port=8088
first.web.welcome.name="now is dev"
spring.datasource.dynamic.hikari.pool-name=example-cp
spring.datasource.dynamic.hikari.max-pool-size=20
spring.datasource.dynamic.hikari.max-lifetime=17170000
spring.datasource.dynamic.hikari.connectionInitSql=set names utf8mb4
spring.datasource.dynamic.primary=test
#test???
spring.datasource.dynamic.datasource.test.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.dynamic.datasource.test.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
spring.datasource.dynamic.datasource.test.username=root
spring.datasource.dynamic.datasource.test.password=root
spring.datasource.dynamic.datasource.test.hikari.max-pool-size=12
spring.datasource.dynamic.datasource.test.hikari.max-lifetime=15000000
info.application.name="@project.name@"
info.application.description="@project.description@"
info.application.version="@project.version@"
management.server.port=52001
management.endpoints.web.exposure.include=*
management.info.env.enabled=true
#spring.elasticsearch.rest.uris=http://10.10.52.155:9200
#spring.elasticsearch.rest.username=
#spring.elasticsearch.rest.password=
#spring.elasticsearch.rest.connection-timeout=10000
#spring.elasticsearch.rest.read-timeout=30000
spring.elasticsearch.uris=http://10.10.52.155:9200
spring.data.elasticsearch.repositories.cluster-name=elasticsearch
spring.data.elasticsearch.repositories.cluster-nodes=10.10.52.155:9300
spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER
application-pro.properties作为生产配置,和上面的一样的。
2.修改POM.xml文件配置
1.增加profiles和resources的多配置属性的修改,整个pom文件的内容如下。profiles增加了dev,pro两个配置。resoucces包含了编译要包含的文件,配置文件按照,你选择的哪一版本配置加入配置文件。
<?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.7.14</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>firstweb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>firstweb</name>
<description>firstweb</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-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
<!-- 数据库动态连接池-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>2.5.4</version>
</dependency>
<!-- thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- tds -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- swagger api接口服务 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<!-- log4j的日志服务 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- ES 默认对应springboot的版本 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
</dependencies>
<profiles>
<profile>
<id>dev</id>
<properties>
<profile.active>dev</profile.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault> <!--默认启动环境-->
</activation>
</profile>
<profile>
<id>pro</id>
<properties>
<profile.active>pro</profile.active>
</properties>
</profile>
</profiles>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application.properties</include>
<include>application-${profile.active}.properties</include>
<include>log4j.properties</include>
<include>**/*.html</include>
<include>**/*.txt</include>
<include>/static/</include>
<include>/templates/</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.修改完配置记得更新idea的maven配置,这样就出现了profiles,你可以选择是dev安装发包,还是pro安装发包。
选择好pro版本后,我们直接install。然后maven就把与pro相关的配置全部打包成一个包。
这样按照多版本配置去打包就实现了。如果springboot要按版本运行,可以直接把配置加入这里
程序的源码在这里可以下载到链接: https://pan.baidu.com/s/1e3QSc9COQ-ioJiAxvDlXMg 提取码: cqnw