[英]Spring load prod profile
我有一個必須在生產中部署的 Spring 應用程序。 不幸的是,我無法打開 prod 配置文件。 我有一個 application-dev.yml 和 application-prod.yml 文件以及 application.yml。 我的application.yml如下:
# ===================================================================
# Spring Boot configuration.
#
# This configuration will be overridden by the Spring profile you use,
# for example application-dev.yml if you use the "dev" profile.
#
# More information on profiles: http://www.jhipster.tech/profiles/
# More information on configuration properties: http://www.jhipster.tech/common-application-properties/
# ===================================================================
# ===================================================================
# Standard Spring Boot properties.
# Full reference is available at:
# http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
# ===================================================================
eureka:
client:
enabled: true
healthcheck:
enabled: true
fetch-registry: true
register-with-eureka: true
instance-info-replication-interval-seconds: 10
registry-fetch-interval-seconds: 10
instance:
appname: majurca
instanceId: majurca:${spring.application.instance-id:${random.value}}
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 10
status-page-url-path: ${management.context-path}/info
health-check-url-path: ${management.context-path}/health
metadata-map:
zone: primary # This is needed for the load balancer
profile: ${spring.profiles.active}
version: ${info.project.version}
ribbon:
eureka:
enabled: true
management:
security:
roles: ADMIN
context-path: /management
info:
git:
mode: full
health:
mail:
enabled: false # When using the MailService, configure an SMTP server and set this to true
spring:
profiles:
default: prod
active: prod
application:
name: majurca
jackson:
serialization.write_dates_as_timestamps: false
jpa:
open-in-view: false
hibernate:
ddl-auto: none
naming:
physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
messages:
basename: i18n/messages
mvc:
favicon:
enabled: false
thymeleaf:
mode: XHTML
security:
basic:
enabled: false
server:
session:
cookie:
http-only: true
info:
project:
version: #project.version#
# ===================================================================
# JHipster specific properties
#
# Full reference is available at: http://www.jhipster.tech/common-application-properties/
# ===================================================================
jhipster:
async:
core-pool-size: 2
max-pool-size: 50
queue-capacity: 10000
# By default CORS is disabled. Uncomment to enable.
#cors:
#allowed-origins: "*"
#allowed-methods: "*"
#allowed-headers: "*"
#exposed-headers: "Authorization,Link,X-Total-Count"
#allow-credentials: true
#max-age: 1800
mail:
from: majurca@localhost
swagger:
default-include-pattern: /api/.*
title: majurca API
description: majurca API documentation
version: 0.0.1
terms-of-service-url:
contact-name:
contact-url:
contact-email:
license:
license-url:
ribbon:
display-on-active-profiles: dev
# ===================================================================
# Application specific properties
# Add your own application properties here, see the ApplicationProperties class
# to have type-safe configuration, like in the JHipsterProperties above
#
# More documentation is available at:
# http://www.jhipster.tech/common-application-properties/
# ===================================================================
application:
forms:
register:
autocomplete-cp-nbitems: 20
google:
recaptcha:
url: https://www.google.com/recaptcha/api/siteverify
secret: 6Lci63UUAAAAAIsluk6G3ueNiJ_ET0m4luMsC8O5
key: 6Lci63UUAAAAADFyeZFTkEaHshVK2LQgYV63PPD_
#key: 6LfUG3UUAAAAAMxScj7ZFY-OXedoWLRzl0wryrrF
#secret: 6LfUG3UUAAAAANsSIsCFOi3X9uYzS72De8EqKqNM
所以你可以看到我將spring.profiles.default
和spring.pofiles.active
設置為 prod。 我還嘗試使用 maven 和選項-Dspring.profiles.active=prod
構建應用程序,但盡管如此,每次運行生成的戰爭時,它都會The following profiles are active: swagger,dev
。
任何人都知道為什么沒有加載 prod 配置文件? 提前致謝。
我已經在 spring-boot 2.1.4 上驗證了以下配置:
應用程序.yaml:
spring:
profiles:
active: dev
應用程序-dev.yaml:
sample:
value: development
應用程序-prod.yaml:
sample:
value: production
以及以下(非常簡單的)spring boot 應用程序:
package com.example.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.event.EventListener;
@SpringBootApplication
public class DemoApplication {
@Value("${sample.value}")
private String sampleValue;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@EventListener
public void onAppStarted(ApplicationStartedEvent evt) {
System.out.println("Sample Value: " + sampleValue);
}
}
即使我不提供任何標志,此配置也有效: java -jar myapp.jar
所以這里肯定還有一些與您的代碼相關的東西。
沒有看到應用程序很難說有什么問題,但是我確實發現了一個“可疑”的陳述:
你說,無論你嘗試什么:
以下配置文件處於活動狀態:swagger,dev
現在“招搖”的個人資料從何而來? 我沒有看到任何參考。 如果您使用java -jar myapp.jar
運行應用程序,我可以看到的一種可能性是有一個spring.factories
文件定義了EnvironmentPostProcessor
- 它是一個鈎子,您可以在其中“擺弄”配置文件,並添加活動配置文件“手動”(在代碼中)。
所以請檢查這種可能性,但再次 - 你所做的在 spring 引導中是正確的(好吧,從技術上講,你不需要spring.profiles.default
條目,但它沒有害處)
Spring Boot 可以從許多位置加載相同的屬性,您可以在此處的文檔中參考其加載順序。
列表中的上半部分比下半部分具有更高的加載優先級。這意味着您在application-xxx.yml
定義application-xxx.yml
可以被同樣在 OS env 變量或 JVM 屬性等中定義的相同屬性覆蓋。 ....
由於命令行參數具有相當高的加載順序,這意味着基本上您可以嘗試使用它來設置配置文件,方法是將--spring.profiles.active=prod
添加到啟動應用程序的命令中,例如:
$ java -jar myproject.jar --spring.profiles.active=prod
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.