Java 在 WebLogic 启动中设置环境变量的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23620163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:20:19  来源:igfitidea点击:

Best way to set environmental variables in WebLogic startup

javaweblogicenvironment-variablesstartup

提问by David Hergert

In Oracle WebLogic, what is the best way to set an environmental variable so that it can be accessed by your code? We have third-party apps running WebLogic that look for an environment variable.

在 Oracle WebLogic 中,设置环境变量以便您的代码可以访问它的最佳方法是什么?我们有运行 WebLogic 的第三方应用程序来查找环境变量。

Note: We start our managed servers using Node Manager.

注意:我们使用节点管理器启动我们的托管服务器。

I would prefer to be able to set it somewhere in the domain configuration, like in the Server Start tab in the Admin Console, but there seems no good place to do that.

我希望能够在域配置中的某处设置它,比如在管理控制台的服务器启动选项卡中,但似乎没有什么好地方可以这样做。

The only way I can see to do it would be

我能看到的唯一方法是

  1. Edit the bin/setDomainEnv.shto export the environmental variable
  2. Modify nodemanager.propertiesto have StartScriptEnabled=true
  1. 编辑bin/setDomainEnv.sh导出环境变量
  2. 修改nodemanager.properties为有StartScriptEnabled=true

What this does is, forces NodeManager to use the <ms_home>/bin/startManagedWebLogic.sh, which sources setDomainEnv.shand they will be picked up when NodeManager starts. But you also have to do this on every machine.

它的作用是强制 NodeManager 使用<ms_home>/bin/startManagedWebLogic.sh,setDomainEnv.sh当 NodeManager 启动时,它们将被拾取。但是您也必须在每台机器上都这样做。

Wondering if there is a cleaner way of doing this than mucking with Oracle's startup scripts.

想知道是否有比使用 Oracle 的启动脚本更简单的方法来做到这一点。

回答by Jason Alexander

If you know for certain that none of the common frameworks are in use, like the Spring Framework, and you have code that strictly looks for environment variables, then you mustset the environment variables outside of any of the usual configuration files, before the Java process that will be expecting it, is started. Once the Java process is started, environment variables are read-only and final for that process.

如果您确定没有使用任何通用框架,例如 Spring Framework,并且您有严格查找环境变量的代码,那么您必须在任何常用配置文件之外,在 Java 之前设置环境变量期待它的过程开始了。一旦 Java 进程启动,环境变量就是该进程的只读和最终变量。

Note:If you need Environment Variables for the entire system, use /etc/profile, /etc/bash_profile, /etc/environment, etc. Keep in mind, that setting the variables in these global locations requires that you restart the Node Manager from a fresh login. You do not need to reboot, but profile/environment files are usually only sourced on login.

注意:如果您需要整个系统的环境变量,请使用 /etc/profile、/etc/bash_profile、/etc/environment 等。请记住,在这些全局位置设置变量需要您从以下位置重新启动节点管理器全新登录。您不需要重新启动,但配置文件/环境文件通常仅在登录时提供。

For apps within just one domain or node, environment variables should be in the startup scripts for the server(s). Editing setDomainEnv.[sh|cmd]or start(Managed)Weblogic.[sh|cmd], is the best option for setting WebLogic environment variables.

对于仅在一个域或节点内的应用程序,环境变量应位于服务器的启动脚本中。编辑setDomainEnv.[sh|cmd]start(Managed)Weblogic.[sh|cmd]是设置 WebLogic 环境变量的最佳选择。

However, if the app is using Spring, system properties and environment variables are combined. System properties are highlyencouraged and easier to maintain and control.

但是,如果应用程序使用 Spring,则系统属性和环境变量会组合在一起。系统属性受到高度鼓励,并且更易于维护和控制。

Ref: What is best practice for setting java system properties, -D or System.setProperty()?

参考:设置 java 系统属性、-D 或 System.setProperty() 的最佳实践是什么?

Weblogic Domain environment variables

Weblogic 域环境变量

One of the places to set both system properties or environment variables, is to edit the domain environment script used to start all nodes or servers that share the same WebLogic server installation and domain. Inside < weblogic_domain >/bin/setDomainEnv.sh, (setDomainEnv.cmdon windows), for environment variables, just add them near the top and add comments to document their use.

设置系统属性或环境变量的地方之一是编辑用于启动共享相同 WebLogic 服务器安装和域的所有节点或服务器的域环境脚本。在< weblogic_domain >/bin/setDomainEnv.sh中(windows 上为setDomainEnv.cmd),对于环境变量,只需将它们添加到顶部附近并添加注释以记录它们的使用。

    export CUSTOM_VAR="test" # UNIX comment to describe environment variable.

For System Properties, you can add command line arguments that will be added to every server by adding a line for EXTRA_JAVA_PROPERTIES, near the top of the file, near the WL_HOME definition, but after the functions and comments.

对于系统属性,您可以通过为 EXTRA_JAVA_PROPERTIES 添加一行来添加将添加到每个服务器的命令行参数,靠近文件顶部,靠近 WL_HOME 定义,但在函数和注释之后。

    EXTRA_JAVA_PROPERTIES="-Denv=TEST"
    export EXTRA_JAVA_PROPERTIES

    WL_HOME="/appl/oracle/middleware/wls/12.1.2.0.0/wlserver"
    export WL_HOME

Weblogic Node-specific environment variables

Weblogic 节点特定的环境变量

If you need different Environment Variables for each node that is started up by the same Node Manager, you will have to customize the startup scripts a little more. In that case, edit the < weblogic_domain >/bin/startManagedWeblogic.[sh|cmd]and insert some scripting logic after _export SERVER_NAME_. This way, you can drive your settings based on SERVER_NAME, etc.

如果您需要为由同一个节点管理器启动的每个节点使用不同的环境变量,您将需要更多地自定义启动脚本。在这种情况下,编辑< weblogic_domain >/bin/startManagedWeblogic.[sh|cmd]并在 _export SERVER_NAME_ 之后插入一些脚本逻辑。这样,您可以根据 SERVER_NAME 等驱动您的设置。

Tip: WindowsEnvironment Variables are not case-sensitive with System.getenv(..).

提示:Windows环境变量对于 System.getenv(..) 不区分大小写。