目录
完整参考资料 JBOSS4指南
http://docs.jboss.org/jbossas/jboss4guide/r5/jboss4guide.zip
1. 修改虚拟机的参数
run.bat或run..conf 文件中的jvm堆大小,如果是用Sun或HP的JDK需要设置MaxPermSize
如:
set JAVA_OPTS=%JAVA_OPTS% -Xms 256m -Xmx 1024m -XX:MaxPermSize= 128m
2. 启动
执行$JBOSS_HOME/bin/run.sh 或run.bat
执行 run.bat –h 显示帮助
usage: run.bat [options]
options:
-h, --help Show this help message
-V, --version Show version information
-- Stop processing options
-D<name>[=<value>] Set a system property
-d, --bootdir=<dir> Set the boot patch directory; Must be absolute or url
-p, --patchdir=<dir> Set the patch directory; Must be absolute or url
-n, --netboot=<url> Boot from net with the given url as base
-c, --configuration=<name> Set the server configuration name
-B, --bootlib=<filename> Add an extra library to the front bootclasspath
-L, --library=<filename> Add an extra library to the loaders classpath
-C, --classpath=<url> Add an extra url to the loaders classpath
-P, --properties=<url> Load system properties from the given url
-b, --host=<host or ip> Bind address for all JBoss services
-g, --partition=<name> HA Partition name (default=DefaultDomain)
-u, --udp=<ip> UDP multicast address
-l, --log=<log4j|jdk> Specify the logger plugin type
如启动所有配置并绑定IP为192.168.101.20
run.sh –c all –b 192.168.101.20
3. 停止
执行$JBOSS_HOME/bin/ shutdown.sh 或shutdown.bat
shutdown.sh -S
4. 配置控制台安全
5. 数据库连接池
将$JBOSS_HOME /docs/examples/jca/oracle-ds.xml复制为$JBOSS_HOME/server/default/deploy/trms-ds.xml并修改连接参数。
如下:
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>rmsdatasource</jndi-name>
<connection-url>jdbc:oracle:thin:@192.168.100.54:1529:trms</connection-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<user-name>rms</user-name>
<password>rms</password>
<!-- Uses the pingDatabase method to check a connection is still valid before handing it out from the pool -->
<!--valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker</valid-connection-checker-class-name-->
<!-- Checks the Oracle error codes and messages for fatal errors -->
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>
<!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) -->
<!-- The minimum connections in a pool/sub-pool. Pools are lazily constructed on first use -->
<min-pool-size>5</min-pool-size>
<!-- The maximum connections in a pool/sub-pool -->
<max-pool-size>20</max-pool-size>
<!-- The time before an unused connection is destroyed -->
<!-- NOTE: This is the check period. It will be destroyed somewhere between 1x and 2x this timeout after last use -->
<!-- TEMPORARY FIX! - Disable idle connection removal, HSQLDB has a problem with not reaping threads on closed connections -->
<idle-timeout-minutes>0</idle-timeout-minutes>
<metadata>
<type-mapping>Oracle9i</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
6. 配置服务器日志
$JBOSS_HOME/server/default/conf/jboss-log4j.xml
注释按日期滚动的日志
<!-- A time/date based rolling appender
打开按文件大小滚动的日志
<!-- A size based file rolling appender -->
<appender name="FILE" class="org.jboss.logging.appender.RollingFileAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="File" value="${jboss.server.log.dir}/server.log"/>
<param name="Append" value="false"/>
<param name="MaxFileSize" value="10240KB"/>
<param name="MaxBackupIndex" value="10"/>
<param name="Threshold" value="INFO"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
7. 配置服务端lib包
将应用程序的包复制到$JBOSS_HOME /server/default/lib下,并下删与JBOSS冲突的应用包
antlr- 2.7.6 .jar
cglib-nodep-2.1_3.jar
commons-collections-3.2.jar
commons-logging-1.1.jar
dom4j- 1.6.1 .jar
junit-4.1.jar
quartz- 1.5.2 .jar
r
8. 配置EJB调用
从容器外能够正常调用EJB,但是web 应用中spring bean调用EJB出现下面异常 <bean id="secuContainerBo" class="org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean"> <property name="jndiName" value="oss.service.security.business.SecuContainerBo"/> <property name="businessInterface" value="oss.service.security.business.ISecuContainerBo"/> <property name="lookupHomeOnStartup" value="false"/> </bean>
RemoteProxyFailureException: No matching RMI stub method found for: public abstract
从JSP页面直接调用EJB <% Hashtable hs = new Hashtable(); hs.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory"); hs.put("java.naming.provider.url","jnp://localhost:1099"); hs.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces"); Context iniCtx = new InitialContext(hs); Object ref = iniCtx.lookup("oss.service.security.business.SecuContainerBo"); oss.service.security.business.SecuContainerBoEJBHome ejbHomeObject = (oss.service.security.business.SecuContainerBoEJBHome) PortableRemoteObject.narrow(ref, oss.service.security.business.SecuContainerBoEJBHome.class); out.print((ejbHomeObject.create()).loadAllAuth(new oss.service.security.domain.SysStaffMember())); %> java.rmi.ServerException: EJBException:; nested exception is: javax.ejb.EJBException: Invalid invocation, check your deployment packaging, method=public abstract
解决办法:
1.修改 conf/jboss-service.xml 中NamingService 的CallByValue 参数值为'true'
<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">true</attribute> <!-- The listening port for the bootstrap JNP service. Set this to -1 to run the NamingService without the JNP invoker listening port. -->
2.修改 deploy/ear-deployer.xml,中的Isloated和CallByValue 为 true
<server> <!-- EAR deployer, remove if you are not using ear deployments --> <mbean code="org.jboss.deployment.EARDeployer" name="jboss.j2ee:service=EARDeployer"> <!-- A flag indicating if ear deployments should have their own scoped class loader to isolate their classes from other deployments. --> <attribute name="Isolated">true</attribute> <!-- A flag indicating if the ear components should have in VM call optimization disabled. --> <attribute name="CallByValue">true</attribute> <!-- A flag the enables the default behavior of the ee5 library-directory. If true, the lib contents of an ear are assumed to be the default value for library-directory in the absence of an explicit library-directory. If false, there must be an explicit library-directory. --> <attribute name="EnablelibDirectoryByDefault">true</attribute> </mbean> </server>
3.修改 deploy/jboss-web.deployer/META-INF/jboss-service.xml中的Java2ClassLoadingCompliance 和 UseJbossWebLoader为true
<!-- Get the flag indicating if the normal Java2 parent first class loading model should be used over the servlet 2.3 web container first model. --> <attribute name="Java2ClassLoadingCompliance">true</attribute> <!-- A flag indicating if the JBoss Loader should be used. This loader uses a unified class loader as the class loader rather than the tomcat specific class loader. The default is false to ensure that wars have isolated class loading for duplicate jars and jsp files. --> <attribute name="UseJBossWebLoader">true</attribute>
9. 应用部署
将EJB的jar包和WEB的 war 复制到$JBOSS_HOME/server/default/deplay
如果是集群环境则复制到$JBOSS_HOME/server/default/farm
10. 客户端调用
l 将$JBOSS_HOME/client/jbossall-client.jar复制到lib目录下
l 修改jndi.properties内容为
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099
11. JBOSS集群
EJB集群的结构图
Web负载均衡集群结构图
11.1. 部署JBOSS集群服务
用 $JBOSS_HOME /server/all的配置来部署集群则不需这一步,只有下面1、2两步是default配置要多做的工作。
1. 将$JBOSS_HOME /server/all/deploy 下的cluster-service.xml和jboss-web-cluster.sar复制到$JBOSS_HOME /server/default/deploy
2. 将$JBOSS_HOME /server/all/lib 下的jgroups.jar、jbossha.jar复制到$JBOSS_HOME /server/default/lib
11.2. 配置Jboss节点
session复制配置
jboss session复制是jboss session同步的一种实现。原理是在各Jboss节点间建立横向联系,每个节点都将本节点的session变化同步到其他所有节点上。
jboss的session复制与HTTP集群是相互配合、相互独立的两个系统。session复制是节点间的横向联系,HTTP集群是负载均衡器与节点的纵向联系。
l $JBOSS_HOME/ server/default/deploy/jboss-web-cluster.sar /META-INF/jboss-service.xml和 $JBOSS_HOME/ server/default/deploy/cluster-service.xml
注意:集群的各节点需要在同一网段.
两个文件都要修改,前者是web session复制的,后者是jboss EJB等集群
找到<config><udp,将><config>到</config>全部注释掉. jboss session复制有UDP和TCP两种方式.UDP采用多播方式,但问题比较多,所以采用TCP方式。
找到<config> <tcp,将><config>到</config>生效.并对该部分进行以下修改:
- 将全部down_thread和up_thread的false都改为true.
- 在<tcp bind_addr=” ${jboss.bind.address}”>
- 在<tcpping initial_hosts=”后填入本机和集群其他全部Jboss节点的IP[7810],比如<tcpping initial_hosts=” ${jboss.bind.address} [7810],192.168.101.20[7810]”></tcpping>
l $JBOSS_HOME/ server/default/deploy/jboss-web-cluster.sar /META-INF/jboss-service.xm中的其他参数
ClusterName是集群名称 在同一局域网内,可以存在多个jboss集群,根据集群名称区分它们.所以,集群中各节点配置的集群名称必须一致,而机器IP则没有特殊要求,只要它们能相互连通. 理论上,可以在一台机器上安装多个Jboss实例,分属于不同的集群.但这会极大地增加复杂度,是不好的配置方式.严重不建议给自己找麻烦。
IsolationLevel是隔离等级. 可选值包括:SERIALIZABLE, REPEATABLE_READ, READ_COMMITTED, READ_UNCOMMITTED, 和 NONE。这里的隔离级别和数据库的隔离级别有同样的含义,对于大多数WEB应用程序来讲通常设置为REPEATABLE_READ。
CacheMode是缓存模式。 由于session复制是通过缓存实现的,所以实际上是复制模式.可选值包括:REPL_SYNC 和REPL_ASYNC,确定改变是应该同步还是异步复制。缺省值是REPL_ASYNC.使用同步复制,确保在请求完成之前传播改变,session同步没有滞后,但效率低。
11.3. 配置应用程序
在应用程序的web.xml的<web-app>段中增加<distributable />。
在jboss-web.xml中增加以下内容:
<?xml version="1.0"?>
<jboss-web>
<context-root>trms</context-root>
<replication-config>
<replication-trigger>SET_AND_NON_PRIMITIVE_GET</replication-trigger>
<replication-granularity>SESSION</replication-granularity>
<replication-field-batch-mode>true</replication-field-batch-mode>
</replication-config>
</jboss-web>
l 客户端jndi.properties内容为
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=192.168.101.19:1099, 192.168.101.20:1099
11.4. Apache负载均衡
11.4.1. 下载mod_jk
http://apache.mirror.phpchina.com/tomcat/tomcat-connectors/jk/binaries/ 到%apache%/modules 下
11.4.2. 修改%apache%/conf/httpd.conf
在文件末尾添加: Include conf/mod_jk2.conf
11.4.3. 在%apache%/conf下新建文件mod_jk2.conf
文件内容如下:
# Load mod_jk module. Specify the filename
# of the mod_jk lib you’ve downloaded and
# installed in the previous section
LoadModule jk_module modules/mod_jk.so
# Where to find workers.properties
JkWorkersFile conf/workers2.properties
# Where to put jk logs
JkLogFile logs/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel info
# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"
JkMount /* router
其中JkMount /* router的意思是,把所有的请求都发给router处理。可以通过修改url来控制发送某些request。
11.4.4. 在%apache%/conf下新建文件workers2.properties
其内容为:
# The advanced router LB worker
worker.list=router
# Define the first node...
worker.server1.port=8009
worker.server1.host=192.168.101.19
worker.server1.type=ajp13
worker.server1.lbfactor=1
worker.server1.redirect=server2
# Define the first node...
worker.server2.port=8009
worker.server2.host=192.168.101.20
worker.server2.type=ajp13
worker.server2.lbfactor=1
# Define prefered failover node for server1
worker.server2.redirect=server1
# Now we define the load-balancing behaviour
worker.router.type=lb
worker.router.balance_workers=server2,server1
worker.router.sticky_session=0
# Add the status worker to the worker list
worker.list=jkstatus
# Define a 'jkstatus' worker using status
worker.jkstatus.type=status
# Add the jkstatus mount point
JkMount /jkmanager/* jkstatus
# Enable the JK manager access from localhost only
<Location /jkmanager/>
JkMount jkstatus
Order deny,allow
Deny from all
Allow from 127.0.0.1
</Location>
其中对于node的命名规则是worker.节点名.xxxx。所以上述文件定义了两个节点:server1和server2。8009端口是jboss默认的ajp端口,另外需要注意的是worker.server2.lbfactor参数,它是节点的负载加权,它的值越大,获得负载的机会就越大。可以根据node的硬件性能进行调整。worker. router.sticky_session参数是指定是否使用粘性session。配置了http session复制就可以不需要粘性session。上面配置的是不使用粘性session。worker.server1.redirect=为failover转移到哪个worker
11.4.5. 配置JBOSS支持粘性mod_jk
修改$JBOSS_HOME/ server/default/deploy/jboss-web.deployer/META-INF/jboss-service.xml
<attribute name="UseJK">false</attribute> 改为ture
11.4.6. 配置JBOSS支持粘性session
修改$JBOSS_HOME/ server/default/deploy/jboss-web.deployer/META-INF/jboss-service.xml
<Engine name="jboss.web" defaultHost="localhost"> 改为
<Engine name="jboss.web" defaultHost="localhost" jvmRoute="server1"
注意:jvmRoute的值必须和mod_jk中的节点名字正确对应,否则无法正确路由