java 在 ColdFusion 中,有没有办法确定代码在哪个服务器上运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/830782/
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
In ColdFusion, is there a way to determine what server the code is running on?
提问by Patrick McElhaney
Is there any way in ColdFusion code to determine on what server the code is executing? I have few load-balanced ColdFusion servers. I want to be able to know on which server the code is running when I catch an exception, so I can include that information in the logging / reporting code.
ColdFusion 代码中是否有任何方法可以确定代码在哪个服务器上执行?我几乎没有负载平衡的 ColdFusion 服务器。当我捕获异常时,我希望能够知道代码在哪个服务器上运行,因此我可以将该信息包含在日志记录/报告代码中。
The servers are Windows 2003/IIS, if that matters. I'd love to know how to do it in Linux/Apache too. :-)
如果重要的话,服务器是 Windows 2003/IIS。我也很想知道如何在 Linux/Apache 中做到这一点。:-)
回答by Jayson
This may help you further...
这可能会帮助您进一步...
<cfscript>
machineName = createObject("java", "java.net.InetAddress").localhost.getCanonicalHostName();
hostaddress = createObject("java", "java.net.InetAddress").localhost.getHostAddress();
</cfscript>
<cfdump var="#machineName#"><br />
<cfdump var="#hostaddress#"><br />
回答by Vincent Buck
You can use Server Variableslike
您可以使用服务器变量,如
server.coldfusion.appserver
server.coldfusion.expiration
server.coldfusion.productlevel
server.coldfusion.productname
server.coldfusion.productversion
server.coldfusion.rootdir
server.coldfusion.serialnumber
server.coldfusion.supportedlocales
server.os.additionalinformation
server.os.arch
server.os.buildnumber
server.os.name
server.os.version
to tweak your code to specific platforms. Do a <cfdump var=”#SERVER#” />to see what's applicable to your version of Coldfusion.
将您的代码调整到特定平台。做一次<cfdump var=”#SERVER#” />,看看有什么适用于您的Coldfusion版本。
You can get the hostname with a Java call:
您可以通过 Java 调用获取主机名:
<cfscript>
machineName = createObject("java", "java.net.InetAddress").localhost.getHostName();
instanceName = createObject("java", "jrunx.kernel.JRun").getServerName();
</cfscript>
回答by JP Alioto
I believe that CGI.SERVER_NAMEwill get you what you want.
我相信CGI.SERVER_NAME会得到你想要的。
Edit per comment: You might be able to do something a bit more "low level" ...
每条评论编辑:您可能可以做一些更“低级”的事情......
<cfset inet = CreateObject("java", "java.net.InetAddress")>
<cfdump var = "#inet.getLocalhost().gethostname()#">
(No CF server here at work, so I can't test that).
(这里没有 CF 服务器在工作,所以我无法测试)。
回答by Matt Newby
Another place to look for information about the executing JRun process is to instance the following:
另一个查找有关正在执行的 JRun 进程的信息的地方是实例化以下内容:
<cfset oErrorJRun = createObject("java","jrunx.kernel.JRun")/>
<cfset strServerName = oErrorJRun.ServerName />
That will give you the name of the JRun instance where the code is being executed. We've run into occasions where in our cluster environment the IIS on one node will log the page hit, but the JRun on the other node will handle the request. Occasionally, we'll have one node's JRun stop responding, and we'll need to restart some services to get the traffic back to that node. I use the above code in my error handler plugin to stick the server name in an email I send to the admins, and to incorporate it in the filename where I write the debugging info.
这将为您提供正在执行代码的 JRun 实例的名称。我们遇到过这样的情况,在我们的集群环境中,一个节点上的 IIS 将记录页面命中,但另一个节点上的 JRun 将处理请求。有时,我们会发现一个节点的 JRun 停止响应,我们需要重新启动一些服务才能让流量返回到该节点。我在我的错误处理程序插件中使用上面的代码将服务器名称粘贴到我发送给管理员的电子邮件中,并将其合并到我编写调试信息的文件名中。
回答by Senthil Nathan
Use the below piece of code to get the domain name.
使用以下代码获取域名。
<cfoutput>#cgi.server_name#</cfoutput>
Hoping this is what you are expecting.
希望这是你所期待的。
回答by nathfy
For us using nodes behind a load balancing proxy I ended up calling the 'hostname' command, works on windows too - so here is the set:
对于在负载平衡代理后面使用节点的我们,我最终调用了“主机名”命令,它也适用于 Windows - 所以这里是一组:
<cfscript>
machineName = createObject("java", "java.net.InetAddress").localhost.getCanonicalHostName();
hostaddress = createObject("java", "java.net.InetAddress").localhost.getHostAddress();
</cfscript>
<cfdump var="#machineName#"><br />
<cfdump var="#hostaddress#"><br />
<cfdump var="#CGI.SERVER_NAME#"><br />
<cfexecute name = "hostname" timeout = "1"></cfexecute>
回答by Fronk
<cffunction name="getMachineName" returntype="string" access="private" output="false" hint="Server Name">
<cftry>
<cfexecute
name="hostname"
arguments=""
variable="local.machineNameResult"
timeout=10 />
<cfreturn Trim(local.machineNameResult)>
<cfcatch type="any">
<cfdump var="#cfcatch#">
<cfabort>
</cfcatch>
</cftry>
</cffunction>
<cfdump var="#getMachineName()#" />
<cfabort />

