springboot部署测试环境和生产环境

开发环境

IDEA
Maven

配置文件

由于springboot会默认加载application配置文件,所以我们需要在application修改配置参数。
以下为application.yml文件格式

spring
  profiles
    active: dev

这段配置代码的意思是,spingboot会加载项目中的名字为application-dev的配置文件。
所以如果需要在打包时打包生产环境的包,那么创建一个名为application-prod.yml的配置文件,然后修改application.yml如下:

spring
  profiles
    active: prod

这样就可以区分打包生产环境和测试环境了

Maven

由于每次打包都需要手动修改application配置文件,会很麻烦并且不安全,并且大多数项目都是使用maven,所以集成maven可以使我们方便很多。

先配置pom.xml:

<!-- 在maven中添加如下配置 -->
<profiles>
        <profile>
            <!-- 测试环境 -->
            <id>dev</id>
            <properties>
                <profiles.active>dev</profiles.active>
            </properties>
        </profile>
        <profile>
            <!-- 生产环境 -->
            <id>prod</id>
            <properties>
                <profiles.active>prod</profiles.active>
            </properties>
        </profile>
    </profiles>

对这段代码做下说明,首先在maven中配置了两个环境的配置文件,一个测试环境test,一个生产环境prod;
其中
<profiles.active></profiles.active>是变量的key,test是变量的value

接下来在application中引用该变量

spring
  profiles
    active: @profiles.active@   <!-- 这里引用的是pom.xml中配置的key -->

问题

1.=='@' that cannot start any token. (Do not use @ for indentation)
在本地启动该项目时有时候会报如下错误

found character '@' that cannot start any token. (Do not use @ for indentation)
 in 'reader', line 4, column 11:
        name: @profiles.active@

 意思是识别不了@profiles.active@这个变量,这是因为这个变量没有被替换成我们需要的参数,如test,prod等,所以在本地启动时要加上参数启动,这样springboot会自动替换掉这个变量。

作者使用的是idea,所以启动springboot时在右上Edit Configurations-->Active Profiles 增加一个参数,参数值为你需要运行的环境名称,如dev

但是好像并没有什么用!!!

我的解决办法是:在application.yml中的active: @profiles.active@'改成active: '@profiles.active@' ,是的加个单引号。

spring
  profiles
    active: '@profiles.active@'   <!-- 这里引用的是pom.xml中配置的key -->

当然,网上好像还有其它的解决办法:(下面的两种方法对我来说没用)

《SpringBoot中出现'@' that cannot start any token. (Do not use @ for indentation)....》

《SpringBoot的yaml配置文件,提示Do not use @ for indentation》

那么,如何部署阿里切换启动:

java -jar -Dspring.profiles.active=prod XXXX.jar

你是技术宅么?那就加入我们吧~本博主不一定长期在线,可以进群大家一起解决问题~

商务合作@群主,谢谢!

欢迎加入 CSDN技术交流群 一起学习交流~

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值