由于本人现在所在公司使用的技术实在太过陈旧,而且框架性能并不优于现有主流技术。所以打算帮公司改版,自己研究了一下敏捷负载架构,但是还没空加上ehcache的集群。本架构用来解决敏捷开发项目的分布式,选用主流的ssh。
此架构实用技术:struts2.1+spring3.0(rmi)+cluster4spring+jpa(hibernate3.3)+ehcache。strust就不做介绍了,spring的IOC也不做介绍了,jpa怎么配置也不做介绍了。先来说说为什么用spring的rmi+cluster4spring来解决中间件的分布。首先第一点:既然是敏捷开发,自然就不考虑EJB了,因为比较“重”,即使现在开发便捷了许多但是还是比较复杂而且受到容器的限制。最重要的一点就是没有rmi执行速度快。,webservice也太慢了不做考虑。下面来看一下配置文件,下面是server端项目的配置文件:
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">
<!-- jpa实体工厂配置 -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="JpaSpringPU" />
</bean>
<!-- jpa事务管理bean -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory"
ref="entityManagerFactory" />
</bean><import resource="serviceContext.xml"/>
</beans>
<!-- 事务管理,在业务方法上使用注解即可 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- ehcache-annotation -->
<ehcache:annotation-driven cache-manager="ehCacheManager" />
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml" />
</bean><!-- 泛型dao配置 -->
<bean id="facePriceDao" class="skyhi.shop.dao.BasicDAO">
<constructor-arg>
<value>skyhi.shop.pojo.FacePrice</value>
</constructor-arg>
<!-- 指定使用的jpa实体工厂(bean容器) -->
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
下面是ehcache.xml的配置:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false">
<diskStore path="java.io.tmpdir/EhCacheSpringAnnotationsExampleApp" />
<defaultCache eternal="false"
maxElementsInMemory="1000"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LRU"/>
<cache name="metaColumnCache" eternal="false"
maxElementsInMemory="100"
overflowToDisk="false"
diskPersistent="false"
timeToIdleSeconds="0"
timeToLiveSeconds="300"
memoryStoreEvictionPolicy="LRU" />
</ehcache>下面是server端业务类的配置:
<!-- 发布rmi服务的bean -->
<bean id="serviceExporter"
class="org.springframework.remoting.rmi.RmiServiceExporter" abstract="true">
<property name="registryPort">
<value>8888</value>
</property>
</bean>
<bean id="moneyImpl" class="skyhi.shop.bl.MoneyServiceImpl">
<constructor-arg>
<ref bean="facePriceDao"/>
</constructor-arg>
</bean><bean id="moneyService" parent="serviceExporter">
<property name="service">
<ref bean="moneyImpl"/>
</property>
<property name="serviceName">
<value>money</value>
</property>
<property name="serviceInterface">
<value>skyhi.shop.inter.IMoneyService</value>
</property>
</bean>服务端主要是要配置对rmi和ehcache,jpa比较简单。如果服务端数据需要缓存,就在业务方法上加上@Cacheable(cacheName="metaColumnCache")
接下来我们来看看客户端(web)配置:
首先是rmi-client端的配置
<!-- cluster4spring:rmi负载&单点故障恢复(客户端) -->
<bean name="_RmiEndpointFactory" class="org.softamis.cluster4spring.rmi.support.RmiEndpointFactory"/>
<bean name="REMOTE_SERVICE_CLIENT" class="org.softamis.cluster4spring.rmi.RmiUrlListProxyFactoryBean" abstract="true">
<property name="refreshEndpointsOnConnectFailure" value="true"/><!-- 很重要,用来指定请求rmi server失败后是否重新连接 -->
<property name="refreshEndpointsOnStartup" value="true"/><property name="registerTraceInterceptor" value="true"/>
<property name="switchEndpointOnFailure" value="true"/>
<property name="endpointFactory" ref="_RmiEndpointFactory"/>
<property name="endpointSelectionPolicy">
<bean class="org.softamis.cluster4spring.support.invocation.DefaultEndpointSelectionPolicy"/>
</property>
<!-- <property name="endpointSelectionPolicy" value="org.softamis.cluster4spring.support.EndpointSelectionPolicy"/> -->
</bean>
<!-- 使用客户端实例 -->
<bean name="moneyServiceLinker" parent="REMOTE_SERVICE_CLIENT">
<property name="serviceInterface" value="skyhi.shop.client.IMoneyService"/>
<property name="serviceURLs">
<list>
<bean class="org.softamis.cluster4spring.support.ServiceMoniker">
<constructor-arg>
<value>rmi://localhost:8888/money</value>
</constructor-arg>
</bean>
</list>
</property>
</bean>先写这么多吧,还有很多细节没有调整。以后在贴出来分享,困了...
敏捷开发之rmi负载
最新推荐文章于 2021-02-16 08:05:28 发布