我在設(shè)置Tomcat for JMX時(shí)遇到了一些問題.我添加了以下屬性
CATALINA_OPTS="-Dcom.sun.management.jmxremote.port=18070 -Dcom.sun.management.jmxremote.password.file=$CATALINA_BASE/conf/jmxremote.password -Dcom.sun .management.jmxremote.ssl=false"
并將jmxremote.password文件添加到conf目錄中.我寫了一個(gè)連接到端口18070上運(yùn)行的JMX服務(wù)器的客戶端工具.當(dāng)我運(yùn)行客戶端程序時(shí),我收到以下錯(cuò)誤.
Exception in thread "main" java.lang.SecurityException: Authentication failed! Credentials required at com.sun.jmx.remote.security.JMXPluggableAuthenticator.authenticationFailure(JMXPluggableAuthenticator.java:193) at com.sun.jmx.remote.security.JMXPluggableAuthenticator.authenticate(JMXPluggableAuthenticator.java:145) at sun.management.jmxremote.ConnectorBootstrap$AccessFileCheckerAuthenticator.authenticate(ConnectorBootstrap.java:185) at javax.management.remote.rmi.RMIServerImpl.doNewClient(RMIServerImpl.java:213) at javax.management.remote.rmi.RMIServerImpl.newClient(RMIServerImpl.java:180) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:305) at sun.rmi.transport.Transport$1.run(Transport.java:159) at java.security.AccessController.doPrivileged(Native Method) at sun.rmi.transport.Transport.serviceCall(Transport.java:155) at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907) at java.lang.Thread.run(Thread.java:619) at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:255) at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:233) at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:142) at javax.management.remote.rmi.RMIServerImpl_Stub.newClient(Unknown Source) at javax.management.remote.rmi.RMIConnector.getConnection(RMIConnector.java:2312) at javax.management.remote.rmi.RMIConnector.connect(RMIConnector.java:277) at javax.management.remote.JMXConnectorFactory.connect(JMXConnectorFactory.java:248) at com.bt.c21sc.c21tkprobe.accessors.C21TkProbeJmxDAO.connect(Unknown Source) at com.bt.c21sc.c21tkprobe.service.C21TkProbeBD.execute(Unknown Source) at com.bt.c21sc.c21tkprobe.C21AppserverProbe.main(Unknown Source)
如果我將CATALINA_OPTS屬性更改為
CATALINA_OPTS="-Dcom.sun.management.jmxremote.port=18070 -Dcom.sun.management.jmxremote.password.file=$CATALINA_BASE/conf/jmxremote.password -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"
然后它工作正常.我認(rèn)為我感到困惑的是什么被歸類為遠(yuǎn)程訪問.我正在運(yùn)行遠(yuǎn)離Tomcat實(shí)例的客戶端程序,但Tomcat和客戶端工具都在同一臺(tái)機(jī)器上(即不同的虛擬機(jī),但環(huán)境相同).我想如果我從另一臺(tái)機(jī)器遠(yuǎn)程訪問JMX服務(wù)器,我必須配置遠(yuǎn)程驗(yàn)證.
通過遠(yuǎn)程訪問,它們是指從本地還是遠(yuǎn)程訪問任何VM的JMX服務(wù)器?
編輯
謝謝.我發(fā)現(xiàn)問題是即使我在jmxremort.password和jmxremote.access文件中提供用戶名和密碼,我仍然需要從客戶端提供用戶名和密碼.
你是對(duì)的,如果在本地運(yùn)行,我不必提供用戶憑據(jù).我已經(jīng)證明了這一點(diǎn),因?yàn)槲铱梢酝ㄟ^Jconsole訪問Tomcat的JVM.
我以編程方式訪問它,所以我必須提供一個(gè)URL,如下所示
??????服務(wù):JMX:RMI:/// JNDI / RMI://本地主機(jī):9004 / jmxrmi
然后我得到j(luò)mx服務(wù)器,如下所示.
url = new JMXServiceURL(urlString); Hashtable<String, String[]> env = new Hashtable<String, String[]>();String[] credentials = new String[] {user,pass};env.put(JMXConnector.CREDENTIALS, credentials); jmxc = JMXConnectorFactory.connect(url,env); mbsc = jmxc.getMBeanServerConnection();
如果我在本地訪問這個(gè)我怎么去這個(gè)?我知道它沒有用戶憑據(jù)就無法在本地工作,因?yàn)檫@就是我發(fā)現(xiàn)我需要提供的方式.如果Jconsole或visualvm可以連接到它,那么必須有一種不同的方法允許檢索Tomcat jmx服務(wù)器而不必提供端口號(hào).
URL包括端口號(hào).如果以編程方式訪問JMX服務(wù)器,如何在不指定端口號(hào)的情況下訪問它?
順便說一句,我使用的是Tomcat 5.5和JDK 1.6
謝謝
解決方法:
遠(yuǎn)程訪問
遠(yuǎn)程訪問意味著從另一臺(tái)計(jì)算機(jī)訪問,通過另一臺(tái)VM本地訪問don’t have to setup JMX [注1].您可以通過在沒有JMX的情況下啟動(dòng)Java應(yīng)用程序來嘗試此操作,然后在同一臺(tái)計(jì)算機(jī)上啟動(dòng)VisualVM.啟動(dòng)的應(yīng)用程序應(yīng)顯示在“Local”下(在VisualVM中).
正如異常所述:身份驗(yàn)證失?。⌒枰獞{證
你提供了用戶名和密碼嗎? ;)我猜你做到了……
可能解決方案
如果你這樣做,那么你應(yīng)該嘗試從CATALINA_OPTS中刪除$CATALINA_BASE并輸入一個(gè)硬編碼值,只是為了看看這是否是根本問題.
例:
CATALINA_OPTS=”-Dcom.sun.management.jmxremote.port=18070
-Dcom.sun.management.jmxremote.password.file=/opt/tomcat6/conf/jmxremote.password
-Dcom.sun.management.jmxremote.ssl=false”
如果它有效,請(qǐng)嘗試這樣:
CATALINA_OPTS="-Dcom.sun.management.jmxremote.port=18070-Dcom.sun.management.jmxremote.password.file=`echo $CATALINA_BASE`/conf/jmxremote.password-Dcom.sun.management.jmxremote.ssl=false"
[注1]:這里說:“在Java SE平臺(tái)的先前版本中,為了允許JMX客戶端訪問本地Java VM,您必須在啟動(dòng)時(shí)設(shè)置以下系統(tǒng)屬性(com.sun.management.jmxremote) Java VM或Java應(yīng)用程序.設(shè)置此屬性會(huì)注冊(cè)Java VM平臺(tái)的MBean并通過專用接口發(fā)布遠(yuǎn)程方法調(diào)用(RMI)連接器,以允許JMX客戶端應(yīng)用程序監(jiān)視本地Java平臺(tái),即運(yùn)行在其上的Java VM與Java J6客戶端相同的機(jī)器.在Java SE 6平臺(tái)中,不再需要設(shè)置此系統(tǒng)屬性.在Java SE 6平臺(tái)上啟動(dòng)的任何應(yīng)用程序都將支持Attach API,因此將自動(dòng)提供在需要時(shí)進(jìn)行本地監(jiān)控和管理.“
來源:https://www.icode9.com/content-1-285401.html聯(lián)系客服