背景:为了更好的测试,你就得更好地了解开发,为了更好的了解开发,你就知道开发常用框架,那就来吧,第一个springboot
目的:使用不同yaml文件配置不同的环境
组网图:不涉及
工具:java version “1.8.0_65” ;Apache Maven 3.6.3;IDEA版本 2018.3 (准备步骤见本人其它博文)
步骤:
使用不同yaml文件配置不同的环境(线上和测试环境),如下yaml文件
application.yml内容如下:
spring:
profiles:
active: dev
application-dev.yml(开发环境)内容如下:
server:
port: 8080
servlet:
context-path: /v1
woman:
age: 25
girl: 那个我喜欢的人
attribute: handsome
application-prod.yml(生产环境)内容如下:
server:
port: 8080
servlet:
context-path: /v1
woman:
age: 25
girl: 那个我喜欢的人
attribute: beautiful
启动springboot
打开链接:http://127.0.0.1:8080/v1/hello
更改application.yml内容如下:
spring:
profiles:
active: prod
那是不是我们每次发布生产的时候都需要改环境配置打包的,不是的。
下面我们更改application.yml内容如下:
spring:
profiles:
active: dev
使用命令mvn clean package 打包,(具体用法见本人springboot的三种启动方式:https://blog.csdn.net/zhiyinweini3/article/details/104285706)
再执行:java -jar target/myfirst-0.0.1-SNAPSHOT.jar
C:\Users\Administrator\Desktop\myfirst>java -jar target/myfirst-0.0.1-SNAPSHOT.j
ar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.3.RELEASE)
2020-02-16 22:32:02.433 INFO 6096 --- [ main] com.hua.myfirst.Myfirst
Application : Starting MyfirstApplication v0.0.1-SNAPSHOT on PC-20180516QJ
KJ with PID 6096 (C:\Users\Administrator\Desktop\myfirst\target\myfirst-0.0.1-SN
APSHOT.jar started by Administrator in C:\Users\Administrator\Desktop\myfirst)
2020-02-16 22:32:02.437 INFO 6096 --- [ main] com.hua.myfirst.Myfirst
Application : The following profiles are active: dev
2020-02-16 22:32:04.447 INFO 6096 --- [ main] o.s.b.w.embedded.tomcat
.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-02-16 22:32:04.492 INFO 6096 --- [ main] o.apache.catalina.core.
StandardService : Starting service [Tomcat]
2020-02-16 22:32:04.492 INFO 6096 --- [ main] org.apache.catalina.cor
e.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.16]
2020-02-16 22:32:04.509 INFO 6096 --- [ main] o.a.catalina.core.AprLi
fecycleListener : The APR based Apache Tomcat Native library which allows opti
mal performance in production environments was not found on the java.library.pat
h: [C:\ProgramData\Oracle\Java\javapath;C:\Windows\Sun\Java\bin;C:\Windows\syste
m32;C:\Windows;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Window
s;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Python
34;C:\Python34\Scripts;C:\strawberry\c\bin;C:\strawberry\perl\bin;C:\Program Fil
es\Java\jdk1.8.0_65\bin;C:\Program Files\Java\jdk1.8.0_65\lib;D:\mysql-5.7.25-wi
nx64\bin;D:\apache-maven-3.6.3\bin;.]
2020-02-16 22:32:04.641 INFO 6096 --- [ main] o.a.c.c.C.[Tomcat].[loc
alhost].[/v1] : Initializing Spring embedded WebApplicationContext
2020-02-16 22:32:04.641 INFO 6096 --- [ main] o.s.web.context.Context
Loader : Root WebApplicationContext: initialization completed in 2115
ms
2020-02-16 22:32:04.934 INFO 6096 --- [ main] o.s.s.concurrent.Thread
PoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-02-16 22:32:05.392 INFO 6096 --- [ main] o.s.b.w.embedded.tomcat
.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '/v
1'
2020-02-16 22:32:05.396 INFO 6096 --- [ main] com.hua.myfirst.Myfirst
Application : Started MyfirstApplication in 3.745 seconds (JVM running for
4.598)
2020-02-16 22:32:19.649 INFO 6096 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[loc
alhost].[/v1] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-02-16 22:32:19.650 INFO 6096 --- [nio-8080-exec-1] o.s.web.servlet.Dispatc
herServlet : Initializing Servlet 'dispatcherServlet'
2020-02-16 22:32:19.656 INFO 6096 --- [nio-8080-exec-1] o.s.web.servlet.Dispatc
herServlet : Completed initialization in 6 ms
启动成功,
打开链接:http://127.0.0.1:8080/v1/hello
那我们怎么切生产环境呢?
执行:java -jar -Dspring.profiles.active=prod target/myfirst-0.0.1-SNAPSHOT.jar
C:\Users\Administrator\Desktop\myfirst>java -jar -Dspring.profiles.active=prod t
arget/myfirst-0.0.1-SNAPSHOT.jar
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.3.RELEASE)
2020-02-16 22:35:26.977 INFO 7252 --- [ main] com.hua.myfirst.Myfirst
Application : Starting MyfirstApplication v0.0.1-SNAPSHOT on PC-20180516QJ
KJ with PID 7252 (C:\Users\Administrator\Desktop\myfirst\target\myfirst-0.0.1-SN
APSHOT.jar started by Administrator in C:\Users\Administrator\Desktop\myfirst)
2020-02-16 22:35:26.980 INFO 7252 --- [ main] com.hua.myfirst.Myfirst
Application : The following profiles are active: prod
2020-02-16 22:35:28.645 INFO 7252 --- [ main] o.s.b.w.embedded.tomcat
.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-02-16 22:35:28.676 INFO 7252 --- [ main] o.apache.catalina.core.
StandardService : Starting service [Tomcat]
2020-02-16 22:35:28.677 INFO 7252 --- [ main] org.apache.catalina.cor
e.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.16]
2020-02-16 22:35:28.695 INFO 7252 --- [ main] o.a.catalina.core.AprLi
fecycleListener : The APR based Apache Tomcat Native library which allows opti
mal performance in production environments was not found on the java.library.pat
h: [C:\ProgramData\Oracle\Java\javapath;C:\Windows\Sun\Java\bin;C:\Windows\syste
m32;C:\Windows;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Window
s;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Python
34;C:\Python34\Scripts;C:\strawberry\c\bin;C:\strawberry\perl\bin;C:\Program Fil
es\Java\jdk1.8.0_65\bin;C:\Program Files\Java\jdk1.8.0_65\lib;D:\mysql-5.7.25-wi
nx64\bin;D:\apache-maven-3.6.3\bin;.]
2020-02-16 22:35:28.811 INFO 7252 --- [ main] o.a.c.c.C.[Tomcat].[loc
alhost].[/v1] : Initializing Spring embedded WebApplicationContext
2020-02-16 22:35:28.811 INFO 7252 --- [ main] o.s.web.context.Context
Loader : Root WebApplicationContext: initialization completed in 1767
ms
2020-02-16 22:35:29.119 INFO 7252 --- [ main] o.s.s.concurrent.Thread
PoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-02-16 22:35:29.455 INFO 7252 --- [ main] o.s.b.w.embedded.tomcat
.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '/v
1'
2020-02-16 22:35:29.459 INFO 7252 --- [ main] com.hua.myfirst.Myfirst
Application : Started MyfirstApplication in 3.068 seconds (JVM running for
3.523)
2020-02-16 22:35:36.349 INFO 7252 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[loc
alhost].[/v1] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-02-16 22:35:36.350 INFO 7252 --- [nio-8080-exec-1] o.s.web.servlet.Dispatc
herServlet : Initializing Servlet 'dispatcherServlet'
2020-02-16 22:35:36.358 INFO 7252 --- [nio-8080-exec-1] o.s.web.servlet.Dispatc
herServlet : Completed initialization in 8 ms
启动成功,
打开链接:http://127.0.0.1:8080/v1/hello