zoukankan      html  css  js  c++  java
  • IDEA结合Maven的profile构建不同开发环境(SpringBoot)

    一、概述

      在开发过程中,我们的项目会存在不同的开发环境,比如开发环境、生产环境、测试环境,而我们的项目在不同的环境中有些配置也是不一样的,比如数据源配置、日志文件配置等,假如我们每次将软件部署到不同的环境时,都需要对相应的配置文件进行修改,来来回回修改,很容易出现少改的地方,而且浪费我们的劳动力。项目用的maven的profile来区别不同的环境配置,我也正好来学习一下。

    二、数据准备

      数据准备:准备3个数据库、数据库名为test_db、表名都为student,只是数据不同。

    (1)192.168.229.134

    (2)192.168.229.133

    (3)192.168.229.132

    三、应用演示

      在本次演示中,使用IDEA结合maven的profile来实现对数据库的切换以及应用端口的切换。工程结构如图:

    (1)父pom文件的profile配置

     <!--配置不同的profile,对应不同的生产环境-->
        <profiles>
            <profile>
                <!--开发-->
                <id>dev</id>
                <activation>
                    <!--默认开发环境-->
                    <activeByDefault>true</activeByDefault>
                </activation>
                <properties>
                    <activatedProperties>dev</activatedProperties>
                </properties>
            </profile>
            <profile>
                <!--生产-->
                <id>pro</id>
                <properties>
                    <activatedProperties>pro</activatedProperties>
                </properties>
            </profile>
            <profile>
                <!--生产-->
                <id>test</id>
                <properties>
                    <activatedProperties>test</activatedProperties>
                </properties>
            </profile>
        </profiles>

    (2)application.properties的配置

    spring.profiles.active=@activatedProperties@  //这里名称与profile中的标签名一致

    (3)application-xxx.properties的配置(3个基本一致,数据库地址和应用上下文根不同)

    (4)测试

    ①默认情况:父pom中已指定默认情况为开发环境,连接192.168.229.132数据,学生名应该为王五,上下文根为/dev-app,启动springboot测试如下:

     打开浏览器访问:localhost/dev-app/all?base=1

    ②指定测试环境,编辑启动配置添加:-Dspring.profiles.active=test

     启动程序,查看控制台日志如下

     打开浏览器访问:http://localhost/test-app/all?base=1,连接192.168.229.134数据,学生名应该为张三

    ③指定生产环境:-Dspring.profiles.active=test,控制日志如下:

     浏览器访问:http://localhost/pro-app/all?base=1

  • 相关阅读:
    frp穿透.md
    6_模板与渲染.md
    4_多数据格式返回请求结果.md
    3_请求参数绑定与多数据格式处理.md
    5_中间件.md
    1_go_gin介绍.md
    2_Gin网络请求与路由处理.md
    14_文件.md
    firefox油猴脚本编写.md
    js 四舍五入
  • 原文地址:https://www.cnblogs.com/qingmuchuanqi48/p/11939397.html
Copyright © 2011-2022 走看看