当前位置:   article > 正文

SpringBoot 多环境配置_replacing main artifact e:\salarycalculation-dev\s

replacing main artifact e:\salarycalculation-dev\salarycalculation-system\ta

为什么要配置多环境?

在我们开发测试过程中,因为线上线下环境的差异,比如数据库数据量或者准确性的原因,通常需要在不同的环境进行测试。

我们可以在测试的时候修改配置文件来达到对应的效果,但是这样有不足的地方:

  • 每次修改显得很麻烦
  • 如果忘记改回原来的配置,发布到线上就会造成事故

怎么做?

多配置文件

复制三份配置文件分别命名为:

  • application.yml
  • application-dev.yml
  • application-product.yml

然后在配置里面配置不同环境的参数:

# application.yml
# 通用配置 | 本地环境

server:
  port: 8080

# application-dev.yml
# 测试环境
server:
  port: 8081

# application-product.yml
# 线上环境
server:
  port: 8082
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

然后我们在application.yml 通过spring.profile.active 来指定到底使用那个配置文件:

# application.yml

# 默认使用配置
spring:
  profiles:
    active: dev
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

测试

我们启动项目,因为指定了dev 环境,那么开启的端口应该是8081 :

2019-12-26 18:01:37.845  INFO 9405 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-12-26 18:01:38.125  INFO 9405 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'taskScheduler'
2019-12-26 18:01:38.226  INFO 9405 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path ''
2019-12-26 18:01:38.229  INFO 9405 --- [           main] c.j.c.CertificationApplication           : Started CertificationApplication in 2.967 seconds (JVM running for 3.357)

  • 1
  • 2
  • 3
  • 4
  • 5

改进-通过脚本控制多环境

多配置文件可以解决环境切换的需求,但是我们还是需要去手动的改spring.profile.active 参数,还是会出现忘修改导致发布后环境变更的问题。

那么我们需要一种不修改文件,又可以随意切换项目环境的方式:

java jar 包运行可以通过传参,来指定配置值,所以我们修改一下application.yml 来接受传参:

# 默认使用配置
spring:
  profiles:
    active: ${spring.profiles.active}
  • 1
  • 2
  • 3
  • 4

然后在通过运行jar 包的方式来启动项目(需要用maven 打包):

mvn clean && mvn package

java -jar /Users/wentong/project/TheCentralCertification/target/certification-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev

2019-12-26 18:11:20.016  INFO 9627 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-12-26 18:11:20.284  INFO 9627 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'taskScheduler'
2019-12-26 18:11:20.380  INFO 9627 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path ''
2019-12-26 18:11:20.383  INFO 9627 --- [           main] c.j.c.CertificationApplication           : Started CertificationApplication in 2.891 seconds (JVM running for 3.282)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

这样我们就可以通过穿参数来解决这个问题,如果不想写那么多命令可以通过脚本来执行:

# ./start.sh
#!/bin/bash
path="target/"
fileName="certification"
version="0.0.1-SNAPSHOT"
environment="dev"

# 打包项目
mvn clean && mvn package

# 判断传参,默认为dev
if test $# -eq 1
then
        if test $1 = "product"
        then
                environment="product"
        fi
fi
echo "当前执行环境:$environment"
java -jar $path$fileName-$version.jar --spring.profiles.active=$environment

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

测试执行线上环境

$ ./start.sh product
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------------< com.jike:certification >-----------------------
[INFO] Building certification 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ certification ---
[INFO] Deleting /Users/wentong/project/TheCentralCertification/target
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.469 s
[INFO] Finished at: 2019-12-26T18:17:09+08:00
[INFO] ------------------------------------------------------------------------
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------------< com.jike:certification >-----------------------
[INFO] Building certification 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ certification ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 3 resources
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ certification ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 9 source files to /Users/wentong/project/TheCentralCertification/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ certification ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/wentong/project/TheCentralCertification/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ certification ---
[INFO] Changes detected - recompiling the module!
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ certification ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) @ certification ---
[INFO] Building jar: /Users/wentong/project/TheCentralCertification/target/certification-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.1.8.RELEASE:repackage (repackage) @ certification ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  3.312 s
[INFO] Finished at: 2019-12-26T18:17:14+08:00
[INFO] ------------------------------------------------------------------------
当前执行环境:product

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.8.RELEASE)

2019-12-26 18:17:14.909  INFO 9862 --- [           main] c.j.c.CertificationApplication           : Starting CertificationApplication v0.0.1-SNAPSHOT on wentongdeMacBook-Pro.local with PID 9862 (/Users/wentong/project/TheCentralCertification/target/certification-0.0.1-SNAPSHOT.jar started by wentong in /Users/wentong/project/TheCentralCertification)
2019-12-26 18:17:14.913  INFO 9862 --- [           main] c.j.c.CertificationApplication           : The following profiles are active: product
2019-12-26 18:17:16.193  INFO 9862 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8082 (http)
2019-12-26 18:17:16.218  INFO 9862 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-12-26 18:17:16.218  INFO 9862 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.24]
2019-12-26 18:17:16.312  INFO 9862 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-12-26 18:17:16.312  INFO 9862 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1335 ms
2019-12-26 18:17:16.574  INFO 9862 --- [           main] c.a.d.s.b.a.DruidDataSourceAutoConfigure : Init DruidDataSource
2019-12-26 18:17:16.765  INFO 9862 --- [           main] com.alibaba.druid.pool.DruidDataSource   : {dataSource-1} inited
2019-12-26 18:17:17.072  INFO 9862 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-12-26 18:17:17.375  INFO 9862 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'taskScheduler'
2019-12-26 18:17:17.470  INFO 9862 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8082 (http) with context path ''
2019-12-26 18:17:17.474  INFO 9862 --- [           main] c.j.c.CertificationApplication           : Started CertificationApplication in 2.996 seconds (JVM running for 3.378)


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/522498
推荐阅读
相关标签
  

闽ICP备14008679号