问题描述
在 Oracle WebLogic 中,设置环境变量以便您的代码可以访问它的最佳方法是什么?我们有运行 WebLogic 的第三方应用程序来寻找环境变量.
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.
注意:我们使用节点管理器启动托管服务器.
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.
我能想到的唯一方法是
- 编辑
bin/setDomainEnv.sh
以导出环境变量 - 修改
nodemanager.properties
以使StartScriptEnabled=true
- Edit the
bin/setDomainEnv.sh
to export the environmental variable - Modify
nodemanager.properties
to haveStartScriptEnabled=true
这样做是强制 NodeManager 使用 <ms_home>/bin/startManagedWebLogic.sh
,它是 setDomainEnv.sh
的来源,并且它们将在何时被拾取节点管理器启动.但您也必须在每台机器上执行此操作.
What this does is, forces NodeManager to use the <ms_home>/bin/startManagedWebLogic.sh
, which sources setDomainEnv.sh
and they will be picked up when NodeManager starts. But you also have to do this on every machine.
想知道是否有比使用 Oracle 的启动脚本更简洁的方法.
Wondering if there is a cleaner way of doing this than mucking with Oracle's startup scripts.
推荐答案
如果你确定没有一个常用框架在使用,比如 Spring 框架,并且你有严格查找环境变量的代码,那么您必须在任何通常的配置文件之外设置环境变量,然后启动期望它的 Java 进程.一旦 Java 进程启动,环境变量是只读的并且是最终的该进程.
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 must set 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.
注意:如果您需要整个系统的环境变量,请使用/etc/profile、/etc/bash_profile、/etc/environment 等.请记住,在这些环境变量中设置变量全局位置要求您从新登录重新启动节点管理器.您不需要重新启动,但配置文件/环境文件通常仅在登录时获取.
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.
对于仅在一个域或节点中的应用程序,环境变量应位于服务器的启动脚本中.编辑 setDomainEnv.[sh|cmd] 或 start(Managed)Weblogic.[sh|cmd],是设置 WebLogic 环境变量的最佳选择.
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.
但是,如果应用程序使用 Spring,则系统属性和环境变量会组合在一起.系统属性受到高度的鼓励,并且更易于维护和控制.
However, if the app is using Spring, system properties and environment variables are combined. System properties are highly encouraged and easier to maintain and control.
参考:什么设置 java 系统属性,-D 或 System.setProperty() 的最佳实践是什么?
Weblogic 域环境变量
设置系统属性或环境变量的地方之一是编辑域环境脚本,该脚本用于启动共享相同 WebLogic 服务器安装和域的所有节点或服务器.里面weblogic_domain >/bin/setDomainEnv.sh,(setDomainEnv.cmd在windows上),对于环境变量,只需将它们添加到顶部附近并添加注释以记录它们的使用.
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.cmd on windows), for environment variables, just add them near the top and add comments to document their use.
export CUSTOM_VAR="test" # UNIX comment to describe environment variable.
对于系统属性,您可以添加将添加到每个服务器的命令行参数,方法是在文件顶部附近、WL_HOME 定义附近但在函数和注释之后添加一行 EXTRA_JAVA_PROPERTIES.
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="-Denv=TEST"
export EXTRA_JAVA_PROPERTIES
WL_HOME="/appl/oracle/middleware/wls/12.1.2.0.0/wlserver"
export WL_HOME
Weblogic 节点特定的环境变量
如果您需要为由同一个节点管理器启动的每个节点设置不同的环境变量,您将不得不更多地自定义启动脚本.在这种情况下,编辑 <weblogic_domain >/bin/startManagedWeblogic.[sh|cmd] 并在 _export SERVER_NAME_ 之后插入一些脚本逻辑.这样,您可以根据 SERVER_NAME 等驱动您的设置.
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.
提示:Windows 环境变量在 System.getenv(..) 中不区分大小写.
Tip: Windows Environment Variables are not case-sensitive with System.getenv(..).
这篇关于在 WebLogic 启动中设置环境变量的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!