In ColdFusion, is there a way to determine what server the code is running on?

£可爱£侵袭症+ 提交于 2019-12-13 12:24:12

问题


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.

The servers are Windows 2003/IIS, if that matters. I'd love to know how to do it in Linux/Apache too. :-)


回答1:


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 />



回答2:


You can use Server Variables like

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.

You can get the hostname with a Java call:

<cfscript>
machineName = createObject("java", "java.net.InetAddress").localhost.getHostName();
instanceName = createObject("java", "jrunx.kernel.JRun").getServerName();
</cfscript>



回答3:


I believe that CGI.SERVER_NAME will get you what you want.

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).




回答4:


Another place to look for information about the executing JRun process is to instance the following:

<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.




回答5:


Use the below piece of code to get the domain name.

<cfoutput>#cgi.server_name#</cfoutput>

Hoping this is what you are expecting.




回答6:


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:

<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>



回答7:


<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 />


来源:https://stackoverflow.com/questions/830782/in-coldfusion-is-there-a-way-to-determine-what-server-the-code-is-running-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!