我有一个必须在生产中部署的Spring应用程序。不幸的是,我无法打开产品配置文件。我有一个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
。
任何人都有一个想法,为什么未加载产品概要文件?预先感谢。
答案 0 :(得分:1)
我已经在spring-boot 2.1.4上验证了以下配置:
application.yaml:
spring:
profiles:
active: dev
application-dev.yaml:
sample:
value: development
application-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
现在,“ swagger”简介来自何处?我没有看到任何参考。如果您使用java -jar myapp.jar
运行该应用程序,那么我看到的一种可能性是,某个地方的spring.factories
文件定义了一个EnvironmentPostProcessor
-它是一个钩子,您可以在其中“摆弄”配置文件,并且其他事情(以代码形式)“手动”添加活动配置文件。
因此,请检查这种可能性,不过,再次-您所做的操作在Spring Boot中是正确的(嗯,从技术上讲,您不需要输入spring.profiles.default
,但这无害)
答案 1 :(得分:0)
Spring Boot可以从许多位置加载相同的属性,您可以在here的文档中参考其加载顺序。
列表的上部比下部具有更高的加载优先级。这意味着您在application-xxx.yml
中定义的内容可以被OS env变量或JVM属性中定义的相同属性覆盖。或其他.....
由于命令行参数的加载顺序很高,这意味着基本上您可以尝试通过在启动应用程序的命令中添加--spring.profiles.active=prod
来使用它来设置配置文件,例如:
$ java -jar myproject.jar --spring.profiles.active=prod