转自:http://blog.csdn.net/sfdev/article/details/4266728
单台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属性;将各套应用对应的端口全部配置在同一个文件中;推荐采用该方案,具体可参考:
<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及其以后的版本中,比较详细的异常如下:
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,虽然会出现以下的异常,但并不一定会影响应用的正常启动;
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)
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
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