弹簧负载产品配置文件

Posted

技术标签:

【中文标题】弹簧负载产品配置文件【英文标题】:Spring load prod profile 【发布时间】:2020-05-28 03:12:55 【问题描述】:

我有一个必须在生产中部署的 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.defaultspring.pofiles.active 设置为 prod。我还尝试使用 maven 和选项 -Dspring.profiles.active=prod 构建应用程序,但尽管如此,每次我运行生成的战争时都会显示 The following profiles are active: swagger,dev

有人知道为什么没有加载 prod 配置文件吗?提前致谢。

【问题讨论】:

【参考方案1】:

我已经在 spring-boot 2.1.4 上验证了以下配置:

application.yaml:

spring:
 profiles:
   active: dev

应用程序-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 条目,但它没有害处)

【讨论】:

【参考方案2】:

Spring Boot 可以从多个位置加载相同的属性,您可以在 here 的文档中参考其加载顺序。

列表中上部的加载优先级高于下部。这意味着您在application-xxx.yml 中定义的内容可以被操作系统环境变量或JVM 属性等中定义的相同属性覆盖.....

由于命令行参数的加载顺序非常高,这意味着您基本上可以尝试使用它来设置配置文件,方法是在启动应用程序的命令中添加--spring.profiles.active=prod,例如:

$ java -jar myproject.jar --spring.profiles.active=prod

【讨论】:

以上是关于弹簧负载产品配置文件的主要内容,如果未能解决你的问题,请参考以下文章

配置文件的弹簧云配置模式匹配

如何强制弹簧只有一个活动配置文件?

弹簧配置文件和测试

使用 bootWar 设置活动弹簧配置文件

如何从环境变量中添加活动弹簧配置文件?

Maven 配置文件属性不会覆盖弹簧配置中的值