`

Linux下Jboss启动、关闭、端口配置等常见问题FAQ

 
阅读更多
单台Linux服务器中如何部署多个独立应用,即多个应用不能run在一个jboss实例中?
换言之,这个问题也可以这样描述:在单台Linux服务器中服务启动多个Jboss实例?
默认情况下,jboss启动时加载server/default/目录下的配置,要实现多个实例的启动,主要是解决端口冲突的问题,因为一套端口只能被一个应用占有;
一般来说,我们可以在Jboss启动时通过-Djboss.server.home设置当前实例启动时加载不同的目录来实现;
比如Jboss主程序安装在/usr/xx/jboss/,应用放在/home/admin/app/;那此时就可以cp一份default目录到当前应用app目录下,通过-Djboss.server.home=/home/admin/app/default来启动该实例;
若直接在/usr/xx/jboss/server/下复制default目录并改名为yy,则可以用更简单的参数-c yy来启动;
对于多应用的情况,我们仍然要解决端口冲突的问题,有两种方式:
其一,直接修改各应用对应的default目录下的所有配置端口,主要涉及配置文件conf/jboss-service.xml、tomcat下的server.xml;该方法比较土,很容易出错,因为端口众多,只要有一项端口没有改,jboss就将无法正常启动;若应用较少,比如就只有2个,那这种方式勉强可以用用;
其二,在jboss-service.xml中启用jboss.system:service=ServiceBindingManager这个mbean服务,设置ServerName、StoreURL属性;将各套应用对应的端口全部配置在同一个文件中;推荐采用该方案,具体可参考:

[xhtml] view plaincopyprint?
<mbean code="org.jboss.services.binding.ServiceBindingManager" 
  name="jboss.system:service=ServiceBindingManager"> 
  <attribute name="ServerName">ports-01</attribute> 
  <attribute name="StoreURL">${jboss.home.url}/docs/examples/binding-manager/sample-bindings.xml</attribute> 
  <attribute name="StoreFactoryClassName"> 
    org.jboss.services.binding.XMLServicesStoreFactory 
  </attribute> 
</mbean> 


<mbean code="org.jboss.services.binding.ServiceBindingManager"
  name="jboss.system:service=ServiceBindingManager">
  <attribute name="ServerName">ports-01</attribute>
  <attribute name="StoreURL">${jboss.home.url}/docs/examples/binding-manager/sample-bindings.xml</attribute>
  <attribute name="StoreFactoryClassName">
    org.jboss.services.binding.XMLServicesStoreFactory
  </attribute>
</mbean>

Jboss无法正常启动,报异常javax.management.MBeanRegistrationException: preRegister() failed?
一般来说,上面的异常出现在4.2及其以后的版本中,比较详细的异常如下:
[java] view plaincopyprint?
javax.management.MBeanRegistrationException: preRegister() failed: [ObjectName='jboss.remoting:service=NetworkRegistry', Class=org.jboss.remoting.network.NetworkRegistry (org.jboss.remoting.network.NetworkRegistry@16b6c55)] 
        at org.jboss.mx.server.registry.BasicMBeanRegistry.invokePreRegister(BasicMBeanRegistry.java:713) 
        at org.jboss.mx.server.registry.BasicMBeanRegistry.registerMBean(BasicMBeanRegistry.java:211) 
        at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) 
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
        at java.lang.reflect.Method.invoke(Method.java:585) 
        at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155) 
        ... 51 more 
Caused by: java.lang.RuntimeException: Exception creating identity: mall_dev4: mall_dev4 
        at org.jboss.remoting.ident.Identity.get(Identity.java:211) 
        at org.jboss.remoting.network.NetworkRegistry.preRegister(NetworkRegistry.java:268) 
        at org.jboss.mx.server.AbstractMBeanInvoker.invokePreRegister(AbstractMBeanInvoker.java:966) 
        at org.jboss.mx.modelmbean.ModelMBeanInvoker.invokePreRegister(ModelMBeanInvoker.java:489) 
        at org.jboss.mx.server.AbstractMBeanInvoker.preRegister(AbstractMBeanInvoker.java:654) 
        at org.jboss.mx.server.registry.BasicMBeanRegistry.invokePreRegister(BasicMBeanRegistry.java:697) 
        ... 56 more 
javax.management.MBeanRegistrationException: preRegister() failed: [ObjectName='jboss.remoting:service=NetworkRegistry', Class=org.jboss.remoting.network.NetworkRegistry (org.jboss.remoting.network.NetworkRegistry@16b6c55)]
        at org.jboss.mx.server.registry.BasicMBeanRegistry.invokePreRegister(BasicMBeanRegistry.java:713)
        at org.jboss.mx.server.registry.BasicMBeanRegistry.registerMBean(BasicMBeanRegistry.java:211)
        at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
        at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:155)
        ... 51 more
Caused by: java.lang.RuntimeException: Exception creating identity: mall_dev4: mall_dev4
        at org.jboss.remoting.ident.Identity.get(Identity.java:211)
        at org.jboss.remoting.network.NetworkRegistry.preRegister(NetworkRegistry.java:268)
        at org.jboss.mx.server.AbstractMBeanInvoker.invokePreRegister(AbstractMBeanInvoker.java:966)
        at org.jboss.mx.modelmbean.ModelMBeanInvoker.invokePreRegister(ModelMBeanInvoker.java:489)
        at org.jboss.mx.server.AbstractMBeanInvoker.preRegister(AbstractMBeanInvoker.java:654)
        at org.jboss.mx.server.registry.BasicMBeanRegistry.invokePreRegister(BasicMBeanRegistry.java:697)
        ... 56 more
这主要是因为Jboss启动了一个jboss.remoting:service=NetworkRegistry的mbean服务,启动时却又无法根据当前hostname找到IP!
解决方案为在/etc/hosts中绑定当前服务器的主机名和IP,比如10.2.224.214 mall_dev4;
对于4.2系列之前的版本,如果没有绑定主机名和IP,虽然会出现以下的异常,但并不一定会影响应用的正常启动;
[java] view plaincopyprint?
11:32:12,427 WARN  [ServiceController] Problem starting service jboss:service=invoker,type=http 
java.net.UnknownHostException: mall_dev4: mall_dev4 
        at java.net.InetAddress.getLocalHost(InetAddress.java:1308) 
        at org.jboss.invocation.http.server.HttpInvoker.checkInvokerURL(HttpInvoker.java:204) 
        at org.jboss.invocation.http.server.HttpInvoker.startService(HttpInvoker.java:101) 
        at org.jboss.system.ServiceMBeanSupport.jbossInternalStart(ServiceMBeanSupport.java:289) 
        at org.jboss.system.ServiceMBeanSupport.jbossInternalLifecycle(ServiceMBeanSupport.java:245) 
        at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source) 
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
        at java.lang.reflect.Method.invoke(Method.java:585) 
        at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.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.system.ServiceController$ServiceProxy.invoke(ServiceController.java:978) 
        at $Proxy0.start(Unknown Source) 
        at org.jboss.system.ServiceController.start(ServiceController.java:417) 
11:32:12,427 WARN  [ServiceController] Problem starting service jboss:service=invoker,type=http
java.net.UnknownHostException: mall_dev4: mall_dev4
        at java.net.InetAddress.getLocalHost(InetAddress.java:1308)
        at org.jboss.invocation.http.server.HttpInvoker.checkInvokerURL(HttpInvoker.java:204)
        at org.jboss.invocation.http.server.HttpInvoker.startService(HttpInvoker.java:101)
        at org.jboss.system.ServiceMBeanSupport.jbossInternalStart(ServiceMBeanSupport.java:289)
        at org.jboss.system.ServiceMBeanSupport.jbossInternalLifecycle(ServiceMBeanSupport.java:245)
        at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
        at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.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.system.ServiceController$ServiceProxy.invoke(ServiceController.java:978)
        at $Proxy0.start(Unknown Source)
        at org.jboss.system.ServiceController.start(ServiceController.java:417)
Jboss已经正常启动,但是无法用IP或者绑定IP访问应用?
熟悉Linux相关知识的筒靴都知道一个端口是绑定在某个地址上的,可以是一个,也可以是多个,一个端口在同一个地址上不能被两个应用占有;
出现这个问题一般是因为你使用的是4.2系列及其之后的版本,因为从这个系列开始,Jboss默认绑定的端口由之前的0.0.0.0变成了127.0.0.1;前者表示绑定当前服务器下所有地址,包括自身的IP、127.0.0.1;而后者只绑定了127.0.0.1,而没有绑定本机IP,通过IP去访问应用自然也就无法成功了,因为访问的IP地址没有开放对应的端口;
对于默认的这种情况,一般来说jboss是要和apache搭配使用的,apache暴露应用外部访问的端口,然后apache再监听本地(也就是127.0.0.1)的端口;
当然这个默认配置也是可以修改的,方式也比较多,比如:
方式1,启动脚本中通过参数-b, --host=<host or ip>指定地址,比如-b 0.0.0.0就表示绑定所有地址;
方式2,启动脚本中通过参数-Djboss.bind.address指定地址,比如-Djboss.bind.address=0.0.0.0;
方式3,直接修改需要对外暴露的端口绑定地址,比如tomcat/server.xml中的http端口绑定address信息;
Jboss正常启动,但是用shutdown脚本无法关闭应用?
对于jboss的关闭,shutdown命令允许好几种方式的关闭,可以参考该命令的帮助:
[java] view plaincopyprint?
usage: shutdown [options] <operation> 
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 
usage: shutdown [options] <operation>
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参数来关闭,也即通过JNDI URL;
若是基于JNDI URL的方式出现这种情况一般有三种可能:
其一,当前应用所使用的default目录内的内容与当前运行jboss自身的default内容不一致,比如当前jboss版本为4.0.5,而启动时指定的default却是从jboss 4.2.1中cp过来的;这种情况在搭建环境时经常出现,因为一般直接从另外一台linux服务器中scp过来,但其实两台服务器自身安装的jboss版本不一致;
其二,shutdown命令中指定的JNDI端口与实际应用启动的JNDI端口不一致;
其三,/etc/hosts中对当前主机名绑定的IP地址不正确,比如当前服务器实际的IP地址为10.2.224.214,而hosts中绑定的却是10.0.0.1;
总之,出现此类情况都是因为jboss自身无法正确接收到正确的关闭命令所致,一般来说在jboss的server.log中会抛如下的异常信息:

[java] view plaincopyprint?
Exception in thread "main" javax.naming.CommunicationException [Root exception is java.rmi.ConnectException: Connection refused to host: 10.0.0.1; nested exception is:  
        java.net.ConnectException: Connection timed out]  
        at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:707)  
        at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:572)  
        at javax.naming.InitialContext.lookup(InitialContext.java:351)  
        at org.jboss.Shutdown.main(Shutdown.java:202)  
Caused by: java.rmi.ConnectException: Connection refused to host: 10.0.0.1; nested exception is:  
        java.net.ConnectException: Connection timed out  
        at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:574)  
        at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:185)  
        at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:171)  
        at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:94)  
        at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)  
        at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:610)  
        ... 3 more  
Exception in thread "main" javax.naming.CommunicationException [Root exception is java.rmi.ConnectException: Connection refused to host: 10.0.0.1; nested exception is:
        java.net.ConnectException: Connection timed out]
        at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:707)
        at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:572)
        at javax.naming.InitialContext.lookup(InitialContext.java:351)
        at org.jboss.Shutdown.main(Shutdown.java:202)
Caused by: java.rmi.ConnectException: Connection refused to host: 10.0.0.1; nested exception is:
        java.net.ConnectException: Connection timed out
        at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:574)
        at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:185)
        at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:171)
        at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:94)
        at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
        at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:610)
        ... 3 more 
如何对Linux下的Jboss应用进行debug?
其实这个问题和Jboss是否部署在Linux下没有直接关系,可以在jboss启动时指定远程debug端口即可,比如:

[java] view plaincopyprint?
-Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n 
-Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n
然后在eclipse中新建remote debug时指定对应的端口即可;
Linux下Jboss启动、关闭、端口配置等常见问题FAQ
关于JBOSS端口被占用的问题
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics