赞
踩
在我们开发测试过程中,因为线上线下环境的差异,比如数据库数据量或者准确性的原因,通常需要在不同的环境进行测试。
我们可以在测试的时候修改配置文件来达到对应的效果,但是这样有不足的地方:
复制三份配置文件分别命名为:
然后在配置里面配置不同环境的参数:
# application.yml
# 通用配置 | 本地环境
server:
port: 8080
# application-dev.yml
# 测试环境
server:
port: 8081
# application-product.yml
# 线上环境
server:
port: 8082
然后我们在application.yml 通过spring.profile.active 来指定到底使用那个配置文件:
# application.yml
# 默认使用配置
spring:
profiles:
active: dev
我们启动项目,因为指定了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)
多配置文件可以解决环境切换的需求,但是我们还是需要去手动的改spring.profile.active 参数,还是会出现忘修改导致发布后环境变更的问题。
那么我们需要一种不修改文件,又可以随意切换项目环境的方式:
java jar 包运行可以通过传参,来指定配置值,所以我们修改一下application.yml 来接受传参:
# 默认使用配置
spring:
profiles:
active: ${spring.profiles.active}
然后在通过运行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)
这样我们就可以通过穿参数来解决这个问题,如果不想写那么多命令可以通过脚本来执行:
# ./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
测试执行线上环境
$ ./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)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。