如何设置JBOSS通过IP网络访问,如何自动启动/关闭JBoss基于jboss
发布网友
发布时间:2022-05-05 00:20
我来回答
共1个回答
热心网友
时间:2023-10-09 05:46
一、启动JBoss:
启动的话,直接运行run.bat文件就可以了!至于启动这点,本人建议把JAVA_HOME设置在run.bat文件中,如打开run.bat文件,在开始行写入set JAVA_HOME=JDK的安装目录,呵呵.
补充一点知识:JBOSS 4.2以上版本服务启动,如果不加任何参数的话,只能监听127.0.0.1,就是说只能用127.0.0.1或者localhost访问,用本机的对外地址访问不了,也就是同一网络内别的机子没有办法访问你机器上的服务器。除非你用参数-b ip地址 来绑定监听的地址才可以。这和以前版本的JBOSS不一样,以前版本的不加任何参数是监听本机所有的IP地址,现在必须使用参数-b 0.0.0.0才可以监听全部地址。
如:启动JBOSS的时候输入:run -b xxx.xxx.xxx.xxx,其中xxx.xxx.xxx.xxx为本机的IP地址。
或者使用另外一种方式:JBOSS_HOME\server\default\deploy\jboss-web.deployer\server.xml 把Connector (修改两个地方,一个是http中的,一个AJP中的)下面的address 改成对应的 IP 或者 0.0.0.0 就可以用IP访问了。(用0.0.0.0代表本机IP地址)
二、关闭JBoss:
首先,默认不改任何配置,如果直接运行shutdown.bat文件是不能停止JBoss服务的,必须要带选项-操作....(usage: shutdown [options] <operation>),以下是JBoss shutdown.bat常用的参数与操作(其实进入JBOSS_HOME\bin\目录运行shutdown.bat就可以得到这样信息了):
options:
-h, --help Show this help message (default)
-D<name>[=<value>] Set a system property
-- Stop processing options
-s, --server=<url> Specify the JNDI URL of the remote server
-n, --serverName=<url> Specify the JMX name of the ServerImpl
-a, --adapter=<name> Specify JNDI name of the MBeanServerConnection to use
-u, --user=<name> Specify the username for authentication
-p, --password=<name> Specify the password for authentication
operations:
-S, --shutdown Shutdown the server
-e, --exit=<code> Force the VM to exit with a status code
-H, --halt=<code> Force the VM to halt with a status code
从上面这些参数可以知道,用-S(大写)可以停止服务,所以我就运行了如下命令:
JBOSS_HOME\bin\shutdown.bat -S
谁知道报了一大堆错误信息,如下:
Exception in thread "main" javax.naming.CommunicationException: Could not obtain connection to any of these urls: localhost:1099 [Root exception is javax.naming
.CommunicationException: Failed to connect to server localhost:1099 [Root exception is javax.naming.ServiceUnavailableException: Failed to connect to server loc
alhost:1099 [Root exception is java.net.ConnectException: Connection refused: connect]]]
at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1562)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:634)
at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:627)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at org.jboss.Shutdown.main(Shutdown.java:214)
Caused by: javax.naming.CommunicationException: Failed to connect to server loca
lhost:1099 [Root exception is javax.naming.ServiceUnavailableException: Failed t
o connect to server localhost:1099 [Root exception is java.net.ConnectException:
Connection refused: connect]]
at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:274)
at org.jnp.interfaces.NamingContext.checkRef(NamingContext.java:1533)
... 4 more
Caused by: javax.naming.ServiceUnavailableException: Failed to connect to server
localhost:1099 [Root exception is java.net.ConnectException: Connection refused
: connect]
at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:248)
... 5 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.<init>(Socket.java:366)
at java.net.Socket.<init>(Socket.java:267)
at org.jnp.interfaces.TimedSocketFactory.createSocket(TimedSocketFactory
.java:84)
at org.jnp.interfaces.TimedSocketFactory.createSocket(TimedSocketFactory
.java:77)
at org.jnp.interfaces.NamingContext.getServer(NamingContext.java:244)
... 5 more
从上面信息得知,连接JBOSS命名服务不可达,也就是JNDI连接不上,换句话说,是要通过停止JNDI端口来停止JBOSS服务!
好吧,那就去看下服务器配置的JNDI端口是什么(默认端口为1099),找到JBOSS_HOME\server\default\conf\jboss-service.xml,找到
<mbean code="org.jboss.naming.NamingService"
name="jboss:service=Naming"
xmbean-dd="resource:xmdesc/NamingService-xmbean.xml">
<!-- The call by value mode. true if all lookups are unmarshalled using
the caller's TCL, false if in VM lookups return the value by reference.
-->
<attribute name="CallByValue">false</attribute>
<!-- The listening port for the bootstrap JNP service. Set this to -1
to run the NamingService without the JNP invoker listening port.
-->
<attribute name="Port">2099</attribute>
<!-- The bootstrap JNP server bind address. This also sets the default
RMI service bind address. Empty == all addresses
-->
原来公司以前的人把端口改成2099了,好了,现在再来运行shutdown.bat来关闭jboss(这时候-s是小写),运行如下命令:
shutdown.bat -s localhost:2099,会报如下的错误,
Exception in thread "main" java.lang.SecurityException: Failed to authenticate principal=null, securityDomain=jmx-console at org.jboss.jmx.connector.invoker.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:97)
at org.jboss.mx.server.Invocation.invoke(Invocation.java:88)
at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.
java:264)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
at org.jboss.invocation.jrmp.server.JRMPProxyFactory.invoke(JRMPProxyFac
tory.java:180)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatch
er.java:155)
at org.jboss.mx.server.Invocation.dispatch(Invocation.java:94)
at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.
java:264)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:659)
at org.jboss.invocation.jrmp.server.JRMPInvoker$MBeanServerAction.invoke
(JRMPInvoker.java:818)
at org.jboss.invocation.jrmp.server.JRMPInvoker.invoke(JRMPInvoker.java:
419)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:294)
at sun.rmi.transport.Transport$1.run(Transport.java:153)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:149)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:4
60)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport
.java:701)
at java.lang.Thread.run(Thread.java:595)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(Stream
RemoteCall.java:247)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:
223)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:126)
at org.jboss.invocation.jrmp.server.JRMPInvoker_Stub.invoke(Unknown Sour
ce) at org.jboss.invocation.jrmp.interfaces.JRMPInvokerProxy.invoke(JRMPInvo
kerProxy.java:133) at org.jboss.invocation.InvokerInterceptor.invokeInvoker(InvokerIntercep
tor.java:365) at org.jboss.invocation.InvokerInterceptor.invoke(InvokerInterceptor.jav
a:197) at org.jboss.jmx.connector.invoker.client.InvokerAdaptorClientIntercepto
r.invoke(InvokerAdaptorClientInterceptor.java:66) at org.jboss.proxy.SecurityInterceptor.invoke(SecurityInterceptor.java:7
0) at org.jboss.proxy.ClientMethodInterceptor.invoke(ClientMethodIntercepto
r.java:74)
at org.jboss.proxy.ClientContainer.invoke(ClientContainer.java:100)
at $Proxy0.invoke(Unknown Source)
at org.jboss.Shutdown$ServerProxyHandler.invoke(Shutdown.java:266)
at $Proxy1.shutdown(Unknown Source)
at org.jboss.Shutdown.main(Shutdown.java:237)
哇唔,报错了,真好,只有在错误中才能成长,呵呵!
好了,根据错误信息,可以定位到是由于要设置jmx-console的用户名和密码来通过验证才能关闭的,
出现这个问题的原因是:
是因为在deploy目录下的jmx-invoker-service.xml中有下面这么一句:
<!-- Interceptor to require authenticated users -->
<interceptor code="org.jboss.jmx.connector.invoker.AuthenticationInterceptor"
securityDomain="java:/jaas/jmx-console"/>
这个*在jboss4.x中默认是关闭的(我公司配置是开启了),也就是说默认不需要设置jmx-console的用户名和密码,然后用shutdown.bat -s localhost:1099 关闭 ,嘿嘿! 当然,如果想安全管理JBoss,可以释放这个*, 并且用shutdown.bat -s locahost:1099 -u admin -p admin才能关闭(-u -p 后面的就是jmx-console的用户名和密码)
那在哪里设置jmx-console-users用户和密码呢?
找到JBOSS_HOME\server\default\conf\props\jmx-console-users.properties,添加一个用户名和密码,如:admin=admin
这样后,重启JBOSS服务器后,就可以使用shutdown.bat -s localhost:2099 -u admin -p admin关闭服务器了,或者加入window系统计划任务中让它自动定时启动与关闭了...或者可以使用这样的命令(shutdown.bat -s jnp://localhost:2099 -u admin -p admin等同于上面所有的关闭命令操作),以上所有的操作同样适用于unix/linux环境!
但是还有一个小问题没有完善,什么问题呢?就是自动启动关闭时,dos窗口始终不能关闭,提示“按任意键继续”,嘿嘿,这个问题好办!
那怎么解决呢?更改两个地方,找到JBOSS_HOME\bin\run.bat和shutdown.bat两个文件,打开后,找到if "%NOPAUSE%" == "" pause这样一行代码,把它注释掉或直接删除,这样窗口就关闭了.