IDEA's profile combined with Maven build a different development environment

I. Overview

  During development, our project will have different development environments, such as the development environment, production environment, test environment, and our project some configuration is not the same in different environments, such as data source configuration, log files, configuration, etc. when if every time we deploy software to a different environment, we need to modify the appropriate configuration files, modify the to and fro, it is less prone to change places, and waste our workforce. Maven project with the profile to distinguish between different environment configuration, I have just come to learn about.

  In addition, the technology applied to my point I was a little demo, the address: https://github.com/Simple-Coder/student-demo

  In this paper if inappropriate, please correct me, I hope you leave a message to discuss the issue.

Second, the data preparation

  Data Preparation: Prepare three databases, database called test_db, table name for the student, but different data.

(1)192.168.229.134

  

 

 (2)192.168.229.133

  

 

(3)192.168.229.132

   

Third, the application demo

  In this presentation, IDEA binding profile maven used to implement the switching of the switching and database application port. Engineering structure is shown:

 

profile configuration (1) the parent pom file

 <!--配置不同的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

  

四、总结

   在使用期间,遇到了很多问题,比如如下异常,折腾好几个小时,百度了很久也不知道哪里问题,都怪自己学识太浅,Spring boot启动原理也不太了解,走了很多弯路,在此要特别感谢这个博客帮了我大忙,本来抱着试试看的态度,结果解决了!附上博客链接:https://blog.csdn.net/Colton_Null/article/details/82145467

(1)异常信息如下:

  

 (2)异常原因

  

 (3)异常解决方案

  

 五、项目地址

  本项目是我突发奇想的,由于一直看《小欢喜》电视剧,所以就想到用目前公司的这些技术点综合起来做一个小demo:项目地址:https://github.com/Simple-Coder/student-demo

Guess you like

Origin www.cnblogs.com/rmxd/p/11510894.html