当前位置: 首页 > article >正文

SpringBoot服务多环境配置

一个项目的的环境一般有三个:开发(dev)、测试(test)、生产(proc),一般对应三套环境,三套配置文件。
像下面这样直接写两个配置文件是不行的。

application.yml

server:
  port: 8080
application-dev.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
    username: root
    password: root

SpringBoot多环境配置有两种方式,一种是在配置文件中指定环境,一种是用maven中指定文件

通过配置文件

使用spring.profiles.active配置项

application.yml

server:
  port: 8080
spring:
  profiles:
    active: dev
application-dev.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
    username: root
    password: root

通过maven

通过maven控制环境有个好处就是可以直接通过idea右侧的maven管理功能控制项目环境

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>
...

	<build>
    	<resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>application*.yml</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>application.yml</include>
                    <include>application-${env}.yml</include>
                </includes>
            </resource>
        </resources>
	</build>

    <profiles>
        <profile>
            <id>dev</id>
            <activation>
            	<!-- 默认激活的环境 -->
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <env>dev</env>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <env>prod</env>
            </properties>
        </profile>
    </profiles>

</project>
application.yml

spring:
  profiles:
    active: @env@
server:
  port: 8080
application-dev.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
    username: root
    password: root

http://www.kler.cn/a/402228.html

相关文章:

  • 0017__多播,IP_MULTICAST_TTL,IP_ADD_MEMBERSHIP,IP_MULTICAST_IF,IP_DROP_MEMBERSHIP
  • 【主机游戏】犯罪现场清理工
  • 基于SSM的农家乐管理系统+论文示例参考
  • 【Redis】Redis实现的消息队列
  • Vue3插槽v-slot使用方式
  • kubesphere问题处理:devops
  • 【leetcode】LCR150.彩灯装饰记录Ⅲ
  • Windsurf:超越 Cursor 的下一代 AI 编程助手
  • 使用C#编写一个控制台应用程序,实现文件的复制功能。
  • 软件工程9、10章小测
  • JavaScript 中,.call()的使用详解
  • Android U 多任务启动分屏——SystemUI流程(更新中)
  • perf使用方法
  • .NET 9 运行时中的新增功能
  • go语言中的切片含义和用法详解
  • (计算机毕设)基于SpringBoot+Vue的房屋租赁系统的设计与实现
  • 共享门店模式:创新零售的新篇章
  • 11.18 Maven-SpringBootWeb入门
  • Spring Boot图书馆管理系统:疫情中的技术实现
  • R环境依赖的备份与恢复全攻略