欢送拜访我的GitHub
这里分类和汇总了欣宸的全副原创(含配套源码):https://github.com/zq2599/blog_demos
本篇概览
- 本文是《quarkus实战》系列的第八篇,通过后面的学习,咱们对配置有了足够理解,但问题也随之而来:如何让利用以最小的改变同时运行在不同环境(如本地、测试、生产等)
举个例子,上面是个简化版配置文件,有两个配置项,第一个固定不变,第二个随环境变动各不相同:
# 这个配置信息在各个环境中都是雷同的greeting.message=hello# 这个配置信息在各个环境中都不一样quarkus.http.port=9090
- 在理论部署的时候,如何达到要求呢?<font color="blue">quarkus.http.port</font>的值随着环境变动
- 不同环境用不同配置文件是一种办法,但会导致配置文件数量回升,并且<font color="blue">greeting.message</font>在各环境都是一样的,这就呈现了冗余,除了保护成本增加,在治理过程中容易出错
除了多个配置文件,还有种办法能够满足要求,并且不须要多个配置文件,这就是明天要聊的profile
演示代码
创立一个demo工程,参考上面的命令,这样的工程会自带一个web服务类HobbyResource.java
mvn "io.quarkus:quarkus-maven-plugin:create" \-DprojectGroupId="com.bolingcavalry" \-DprojectArtifactId="hello-quarkus" \-DprojectVersion="1.0-SNAPSHOT" \-DclassName="HobbyResource" \-Dpath="actions"
用上面这段代码来演示配置是否失效,可见用了一个配置项<font color="blue">greeting.message</font>,所以咱们须要配置它的值才行
package com.bolingcavalry;import org.eclipse.microprofile.config.inject.ConfigProperty;import javax.ws.rs.GET;import javax.ws.rs.Path;import javax.ws.rs.Produces;import javax.ws.rs.core.MediaType;import java.time.LocalDateTime;@Path("/actions")public class HobbyResource { @ConfigProperty(name = "greeting.message") String message; @GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return "Hello RESTEasy, " + LocalDateTime.now() + " [" + message + "]"; }}
配置文件是<font color="blue">hello-quarkus/src/main/resources/application.properties</font>
# 这个配置信息在各个环境中都是雷同的greeting.message=hello
设定profile
profile本人是个一般的配置项,例如在application.properties文件中,是这样设置profile的
# 这个配置信息在各个环境中都是雷同的quarkus.profile=dev# 如果不指定profile,就应用此配置quarkus.http.port=8080
也能够在System properties中设置,如下所示,如此以来,不同环境只有启动命令不同,配置文件能够齐全不必批改:
java -Dquarkus.profile="dev" -jar hello-quarkus-1.0-SNAPSHOT-runner.jar
同一个配置项在不同profile时的值
- profile的格局是<font color="blue">%{profile-name}.config.name</font>
以方才的配置为例,<font color="red">quarkus.http.port</font>配置项共呈现三次,前两次带有前缀,格局是<font color="blue">百分号+profile名称+点号</font>,如下所示
# 指定以后profilequarkus.profile=dev# 这个配置信息在各个环境中都是雷同的greeting.message=hello# 如果profile为dev,就是用此配置%dev.quarkus.http.port=8081# 如果profile为production,就是用此配置%production.quarkus.http.port=8082# 如果不指定profile,或者profile既不是dev也不是production,就应用此配置quarkus.http.port=8080
- 应用上述配置后,因为profile等于<font color="blue">dev</font>,会应用不同配置项<font color="blue">%dev.quarkus.http.port</font>,也就是说服务端口是<font color="red">8081</font>,另外两个配置<font color="blue">%production.quarkus.http.port</font>和<font color="blue">quarkus.http.port</font>都有效
- 启动利用验证,我这是用<font color="blue">mvn quarkus:dev</font>命令启动的,如下图红框:
- 浏览器拜访地址<font color="blue">http://localhost:</font><font color="blue">8081</font><font color="blue">/actions</font>,服务失常,配置项greeting.message的值也合乎预期:
- 再试试另一种配置,先在application.properties文件中删除配置项<font color="blue">quarkus.profile=dev</font>,再改用<font color="red">mvn quarkus:dev -Dquarkus.profile=production</font>启动利用,这次失效的配置项是<font color="blue">%production.quarkus.http.port</font>,如下图:
- 拜访地址也变成了<font color="blue">http://localhost:</font><font color="blue">8082</font><font color="blue">/actions</font>
须要大写的场景
在《quarkus实战之六:配置》一文中,曾提到过配置形式有六种,有几种要求配置项大写,例如在<font color="blue">.env</font>中的配置,此时格局变成了<font color="red">\_{PROFILE}_CONFIG_KEY=value</font>,举例如下
# 这个配置信息在各个环境中都是雷同的GREETING_MESSAGE=hello# 如果profile为dev,就是用此配置_DEV_QUARKUS_HTTP_PORT=8081# 如果profile为production,就是用此配置_PRODUCTION_QUARKUS_HTTP_PORT=8082# 如果不指定profile,就应用此配置QUARKUS_HTTP_PORT=8080
留神,实测发现在.env中配置<font color="blue">QUARKUS_PROFILE=dev</font>有效,也就是说不能在.env中指定profile,此时应该在启动命令中指定profile,例如:
java -Dquarkus.profile=dev -jar hello-quarkus-1.0-SNAPSHOT-runner.jar
不指定profile时的默认值
- 不指定profile的时候,quarkus会给profile设置默认值,有三种可能:dev、test、prod,具体逻辑如下:
- 如果启动命令是<font color="blue">mvn quarkus:dev</font>,profile等于<font color="red">dev</font>,如下图,大家应该见过屡次了:
- 单元测试期间,例如执行命令<font color="blue">mvn test</font>,profile等于<font color="red">test</font>
- 以上两种场景之外,profile等于<font color="red">prod</font>,例如用命令<font color="blue">java -jar hello-quarkus-1.0-SNAPSHOT-runner.jar</font>启动利用
每个profile对应一个配置文件
- 如果您心愿每个profile都有本人的配置文件,quarkus也反对,如下所示,<font color="blue">src/main/resources/</font>目录下同时存在两个配置文件:<font color="red">application.properties</font>和<font color="red">application-staging.properties</font>
resources├── META-INF│ └── resources│ └── index.html├── application-staging.properties└── application.properties
- application.properties内容如下
greeting.message=helloquarkus.http.port=8080
- application-staging.properties内容如下
greeting.message=helloquarkus.http.port=8081
- 如果启动命令指定了profile,如<font color="blue">mvn quarkus:dev -Dquarkus.profile=staging</font>,此时只有<font color="red">application-staging.properties</font>文件失效,如下图
- 还要留神一点:此时如果指定一个不存在的profile,例如<font color="blue">mvn quarkus:dev -Dquarkus.profile=xxxxxxx</font>,此时失效的是<font color="red">application.properties</font>文件失效,如下图
Parent Profile
- parent profile解决的问题是:假如以后profile是aaa,那么配置项xxx对应的配置名应该是<font color="blue">%dev.aaa</font>,如果找不到<font color="blue">%dev.aaa</font>,就去找它的parent profile对应的配置项,来看个例子就分明了,假如配置信息如下:
# 指定profile的名字quarkus.profile=dev# 指定parent的名字quarkus.config.profile.parent=common%common.quarkus.http.port=9090%dev.quarkus.http.ssl-port=9443quarkus.http.port=8080quarkus.http.ssl-port=8443
- 以后profile曾经指定为dev
- parent profile曾经指定为common
- 对于配置项<font color="blue">quarkus.http.port</font>,因为没找到<font color="red">%dev.quarkus.http.port</font>,就去找parent profile的配置,于是找到了<font color="red">%common.quarkus.http.port</font>,所以值为<font color="blue">9090</font>
- 对于配置项<font color="blue">quarkus.http.ssl-port</font>,因为找到了<font color="red">%dev.quarkus.http.ssl-port</font>,所以值为<font color="blue">9443</font>
- 对于配置项<font color="blue">quarkus.http.port</font>,如果<font color="red">%dev.quarkus.http.port</font>和<font color="red">%common.quarkus.http.port</font>都不存在,会用<font color="red">quarkus.http.port</font>,值为<font color="blue">8080</font>
批改默认profile
- 后面曾说到,启动的时候如果不指定profile,quarkus会指定默认的profile:将利用制作成jar,以<font color="blue">java -jar</font>命令启动时,profile会被设置为<font color="red">prod</font>
- 如果您想让默认值从prod变为其余值,能够在构建的时候用<font color="blue">-Dquarkus.profile</font>去扭转它,例如上面这个命令,jar包生成后,启动的时候默认profile是<font color="red">prod-aws</font>
mvn clean package -U -Dquarkus.package.type=uber-jar -Dquarkus.profile=prod-aws
- 启动jar的时候不指定profile,如下图,profile已被设定为<font color="blue">prod-aws</font>
三个要害注意事项(重要)
- quarkus官网给出了三个重点注意事项
- 利用在运行时,只会有一种profile失效
如果想在代码获取以后的profile,能够用此API
io.quarkus.runtime.configuration.ProfileManager#getActiveProfile
用注解的形式获取profile是<font color="red">有效的</font>,上面这段代码无奈失去以后的profile
@ConfigProperty("quarkus.profile") String profile;
欢送关注思否:程序员欣宸
学习路上,你不孤独,欣宸原创一路相伴...