设为首页 加入收藏

TOP

quarkus实战之八:profile(二)
2023-08-06 07:49:59 】 浏览:99
Tags:quarkus profile
rofile=dev -jar hello-quarkus-1.0-SNAPSHOT-runner.jar

不指定profile时的默认值

  • 不指定profile的时候,quarkus会给profile设置默认值,有三种可能:dev、test、prod,具体逻辑如下:
  1. 如果启动命令是mvn quarkus:dev,profile等于dev,如下图,大家应该见过多次了:
    在这里插入图片描述
  2. 单元测试期间,例如执行命令mvn test,profile等于test
    在这里插入图片描述
  3. 以上两种场景之外,profile等于prod,例如用命令java -jar hello-quarkus-1.0-SNAPSHOT-runner.jar启动应用
    在这里插入图片描述

每个profile对应一个配置文件

  • 如果您希望每个profile都有自己的配置文件,quarkus也支持,如下所示,src/main/resources/目录下同时存在两个配置文件:application.propertiesapplication-staging.properties
resources
├── META-INF
│   └── resources
│       └── index.html
├── application-staging.properties
└── application.properties
  • application.properties内容如下
greeting.message=hello
quarkus.http.port=8080
  • application-staging.properties内容如下
greeting.message=hello
quarkus.http.port=8081
  • 如果启动命令指定了profile,如mvn quarkus:dev -Dquarkus.profile=staging,此时只有application-staging.properties文件生效,如下图

image-20220309081432688

  • 还要注意一点:此时如果指定一个不存在的profile,例如mvn quarkus:dev -Dquarkus.profile=xxxxxxx,此时生效的是application.properties文件生效,如下图

image-20220309081901953

Parent Profile

  • parent profile解决的问题是:假设当前profile是aaa,那么配置项xxx对应的配置名应该是%dev.aaa,如果找不到%dev.aaa,就去找它的parent profile对应的配置项,来看个例子就清楚了,假设配置信息如下:
# 指定profile的名字
quarkus.profile=dev
# 指定parent的名字
quarkus.config.profile.parent=common

%common.quarkus.http.port=9090
%dev.quarkus.http.ssl-port=9443

quarkus.http.port=8080
quarkus.http.ssl-port=8443
  1. 当前profile已经指定为dev
  2. parent profile已经指定为common
  3. 对于配置项quarkus.http.port,由于没找到%dev.quarkus.http.port,就去找parent profile的配置,于是找到了%common.quarkus.http.port,所以值为9090
  4. 对于配置项quarkus.http.ssl-port,由于找到了%dev.quarkus.http.ssl-port,所以值为9443
  5. 对于配置项quarkus.http.port,如果%dev.quarkus.http.port%common.quarkus.http.port都不存在,会用quarkus.http.port,值为8080

修改默认profile

  • 前面曾说到,启动的时候如果不指定profile,quarkus会指定默认的profile:将应用制作成jar,以java -jar命令启动时,profile会被设置为prod
  • 如果您想让默认值从prod变为其他值,可以在构建的时候用-Dquarkus.profile去改变它,例如下面这个命令,jar包生成后,启动的时候默认profile是prod-aws
mvn clean package -U -Dquarkus.package.type=uber-jar -Dquarkus.profile=prod-aws
  • 启动jar的时候不指定profile,如下图,profile已被设定为prod-aws

image-20220309085425879

三个关键注意事项(重要)

  • quarkus官方给出了三个重点注意事项
  1. 应用在运行时,只会有一种profile生效
  2. 如果想在代码获取当前的profile,可以用此API
io.quarkus.runtime.configuration.ProfileManager#getActiveProfile
  1. 用注解的方式获取profile是无效的,下面这段代码无法得到当前的profile
 @ConfigProperty("quarkus.profile")
 String profile;

欢迎关注博客园:程序员欣宸

学习路上,你不孤单,欣宸原创一路相伴...

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Java8 函数式编程stream流 下一篇并发编程-CompletableFuture解析

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目