Weblogic 12c:如何传递“活动配置文件"到 Spring Boot 应用程序? [英] Weblogic 12c : How to pass the "active profile" to a Spring Boot application?

查看:36
本文介绍了Weblogic 12c:如何传递“活动配置文件"到 Spring Boot 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 Spring Boot 应用程序,我必须让它在我们的 Weblogic 12c 服务器中运行,就像一场战争.

I'm developing a Spring Boot application and I have to make it run in our Weblogic 12c server, as a war.

我试图找到一种方法来传递默认的 Spring 活动配置文件(信息 这里here) 到应用程序,而不必在机器本身上设置环境变量.如果可能,我希望能够使用 Weblogic 管理控制台指定此配置文件.

I try to find a way to pass the default Spring active profile (info here and here) to the application, without having to set an environment variable on the machine itself. I'd like to be able to specify this profile using the Weblogic admin console, if possible.

我尝试使用Environment/Servers/MyServer/Server Start/Arguments"选项,但我一直无法让 Spring 以这种方式加载指定的配置文件.我试过了,没有成功:

I tried playing with the "Environment / Servers / MyServer / Server Start / Arguments" options, but I've been unable to make Spring load the specified profile this way. I've tried, without success :

spring.profiles.active=dev
-Dspring.profiles.active='dev'
-Dspring.profiles.active=dev
-spring.profiles.active=dev
--spring.profiles.active=dev

知道是否可以使用 Weblogic 将 spring.profiles.active 传递给 Spring?我需要传递dev"、acc"或prod".

Any idea if it's possible to pass the spring.profiles.active to Spring, using Weblogic? I need to pass "dev", "acc" or "prod".

更新:我会接受一个解释任何简单的方法来让不同的 .properties 文件用于应用程序配置,这取决于 Sprint Boot 应用程序运行的环境.那些 .properties 文件可以捆绑在应用程序本身中,也可以是外部的.但是我需要接触至少运行Weblogic的系统...没有环境变量,理想情况下也不要更改Weblogic文件!理想情况下,该解决方案将涉及使用 Weblogic 管理控制台完成的某种操作,例如将表示当前环境的参数传递给 Spring Boot 应用程序以便正确的 application.[ENVIRONMENT].properties 文件被使用.

UPDATE : I would accept an answer explaining any easy way to have different .properties files used for the application configurations, depending on the environment the Sprint Boot application runs on. Those .properties files could be bundled in the application itself, or can be external. But I need to touch the system Weblogic is running on at least as possible... No environment variables and ideally no change to the Weblogic files either! Ideally, the solution would involve some kind of manipulations done using the Weblogic Admin console, for example a way to pass a parameter representing the current environment to the Spring Boot application so the correct application.[ENVIRONMENT].properties file is used.

更新 2 :允许每个环境拥有自己的 application.properties 文件的一种方法是使用 部署计划.这可能是推荐的方法.我将添加它作为答案,但我仍然更喜欢更简单的方法......我无法相信在 Weblogic 中部署应用程序时,不可能简单地将参数传递给应用程序!如果Environment/Servers/MyServer/Server Start/Arguments"选项不能用于该目的,那么它是什么?

UPDATE 2 : One way of allowing each environment to have its own application.properties file , is by using Deployment plans. This is probably the recommended way of doing it. I'll add that as an answer, but I'd still prefere an easier approach... I can't believe it's not possible to simply pass a parameter to an application, when it's deployed in Weblogic! And what is the "Environment / Servers / MyServer / Server Start / Arguments option for if it can't be used for that purpose?

更新 3:相关线程.如果我理解正确,Environment/Servers/MyServer/Server Start/Arguments"选项可能仅在启用 NodeManager 时才有效?这将解释为什么我无法传递那个 spring.profiles.active=dev 变量!

UPDATE 3 : Related thread. If I understand correctly, the "Environment / Servers / MyServer / Server Start / Arguments" options may only work if a NodeManager is enabled? That would explain why I'm unable to pass that spring.profiles.active=dev variable!

推荐答案

虽然 Spring Profiles 允许我们使用属于同一个名为 profile 的逻辑组的属性集填充 Spring Environment,但 Spring Profiles 不是目标,而是实现目标的方法之一.

Although Spring Profiles let us to populate the Spring Environment with the set of properties that belong to the same logical group named as profile, Spring Profiles is not the goal but one of the ways to achieve the goal.

目标是能够从外部化的配置中填充 Spring Environment,即目标是能够在不同的环境中部署相同的应用程序代码,并为每个单独的环境加载特定的配置.

The goal is to be able to populate the Spring Environment from externalized configuration, that is, the goal is to be able to deploy the same application code in different environments and load the specific configuration for each single environment.

Spring Boot 提供了一种实现该目标的方法,该方法基于拥有自己的 PropertySource,该 PropertySource 旨在允许合理地覆盖值.

Spring Boot offers a way to achieve that goal based on having an own PropertySource that designed to allow sensible overriding of values.

如果你阅读了http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config 您将看到特定于配置文件的应用程序属性只是可用来源之一属性,并且 JNDI 属性将覆盖特定于配置文件的属性.

If you read http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config you will see that the profile-specific application properties is just one of the available sources of properties and that JNDI attributes will override the profile-specific properties.

分析 Spring Boot 属性覆盖顺序(http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config) 对于 JEE 环境,JNDI 属性实际上采用最优先:

Analyzing the Spring Boot properties overriding order (http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config) for JEE environments, the JNDI attributes take in fact the most precedence:

  1. 我建议不要使用全局设置,例如全局属性文件、命令行参数、系统环境变量等,因为它们由部署在同一应用程序服务器中的所有应用程序共享.
    • 您将获得附带好处:无需修改 Weblogic 启动文件

注意要激活配置文件,您必须使用全局变量,因此在同一应用服务器中部署的所有应用程序将激活相同的配置文件.

Note to activate a profile you must use global variable, so all the deployed applications in the same app server will activate the same profile.

因此,实际上,在纯 JEE 环境的 Spring Boot 应用程序中,您只有两个配置源:

So, in practice, in Spring Boot applications for pure JEE environments you have only two configuration sources:

  1. JNDI 属性
  2. 打包的 application.properties

请注意,您应该避免使用打包的 application-{profile}.properties 文件,因为配置文件激活意味着在部署在同一应用服务器中的所有应用中激活相同的配置文件.

Note you should avoid the usage of packaged application-{profile}.properties files because profile activation means to activate the same profile in all the apps deployed in the same app server.

在这一点上,您可以想象在纯 JEE* 中拥有的唯一外部配置源是 JNDI.

In that point you imagine the unique external configuration source you have in pure JEE* is JNDI.

[*] 纯 JEE"是指不使用 Spring Cloud Config 等外部配置服务器 (https://cloud.spring.io/spring-cloud-config/).

[*] By "pure JEE" I mean without using external configuration servers like Spring Cloud Config (https://cloud.spring.io/spring-cloud-config/).

也就是说,Weblogic 部署计划为您提供了为每个部署加载 JNDI 属性的最简单和舒适的方法,并且 Spring Boot 将从 JNDI 填充 Spring Environment.

That said, the Weblogic deployment plans give you the easiest and comfortable way to load the JNDI attributes for each deployment and Spring Boot will populate the Spring Environment from JNDI.

解决方案非常简单:

  1. 部署您的应用程序和为您的环境(开发、预制、专业、...)定制的 Weblogic 部署计划
  2. 在您的应用程序中使用 JNDI 属性值作为任何其他 Spring Environment 属性:在 application.properties 中的 ${jndi.var}、@Value(${jndi.var}) 等.
    • 请注意,Spring Boot 将使用 JNDI 属性填充 Spring Environment,因此您可以像使用 application.properties 文件中的属性一样使用它们.

希望有帮助!

这篇关于Weblogic 12c:如何传递“活动配置文件"到 Spring Boot 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆