web服务器一般都是一个服务器连接一个数据库的架构,可是随着系统的使用,该架构已经不能满足需求了,这里使用的是c3p0的数据库水平扩容。 Spring核心配置: !-- 配置文件的引入 -- bean id = "propertyConfigurer" class = "org.springframework.beans.fact
web服务器一般都是一个服务器连接一个数据库的架构,可是随着系统的使用,该架构已经不能满足需求了,这里使用的是c3p0的数据库水平扩容。
Spring核心配置:
<!-- 配置文件的引入 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:/jdbc.properties</value>
</property>
</bean>
<!-- 配置三组数据源(每一组分主和从) -->
<!-- 数据源1 -->
<bean id="dataSource1" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc1.url}" />
<property name="user" value="${jdbc1.username}" />
<property name="password" value="${jdbc1.password}" />
<property name="autoCommitOnClose" value="true"/>
<!-- <property name="checkoutTimeout" value="${cpool.checkoutTimeout}"/>-->
<property name="initialPoolSize" value="${cpool.minPoolSize}"/>
<property name="minPoolSize" value="${cpool.minPoolSize}"/>
<property name="maxPoolSize" value="${cpool.maxPoolSize}"/>
<property name="maxIdleTime" value="${cpool.maxIdleTime}"/>
<property name="acquireIncrement" value="${cpool.acquireIncrement}"/>
<property name="maxIdleTimeExcessConnections" value="${cpool.maxIdleTimeExcessConnections}"/>
</bean>
<!-- 数据源2 -->
<bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}" />
<property name="jdbcUrl" value="${jdbc2.url}" />
<property name="user" value="${jdbc2.username}" />
<property name="password" value="${jdbc2.password}" />
<property name="autoCommitOnClose" value="true"/>
<!-- <property name="checkoutTimeout" value="${cpool.checkoutTimeout}"/>-->
<property name="initialPoolSize" value="${cpool.minPoolSize}"/>
<property name="minPoolSize" value="${cpool.minPoolSize}"/>
<property name="maxPoolSize" value="${cpool.maxPoolSize}"/>
<property name="maxIdleTime" value="${cpool.maxIdleTime}"/>
<property name="acquireIncrement" value="${cpool.acquireIncrement}"/>
<property name="maxIdleTimeExcessConnections" value="${cpool.maxIdleTimeExcessConnections}"/>
</bean>
...
</bean>
<!-- 配置数据源 -->
<bean id="dataSources" class="com.caland.sun.client.datasources.DefaultDataSourceService">
<property name="dataSourceDescriptors">
<set>
<bean class="com.caland.sun.client.datasources.DataSourceDescriptor">
<property name="identity" value="partition1"/>
<!-- 指定数据源1 -->
<property name="targetDataSource" ref="dataSource1"/>
<!-- 对数据源1进行心跳监测 -->
<property name="targetDetectorDataSource" ref="dataSource1"/>
<!-- 指定备机数据源4 -->
<property name="standbyDataSource" ref="dataSource4"/>
<!-- 对备机进行心跳监测 -->
<property name="standbyDetectorDataSource" ref="dataSource4"/>
</bean>
<bean class="com.caland.sun.client.datasources.DataSourceDescriptor">
<property name="identity" value="partition2"/>
<property name="targetDataSource" ref="dataSource2"/>
<property name="targetDetectorDataSource" ref="dataSource2"/>
<property name="standbyDataSource" ref="dataSource5"/>
<property name="standbyDetectorDataSource" ref="dataSource5"/>
</bean>
<bean class="com.caland.sun.client.datasources.DataSourceDescriptor">
<property name="identity" value="partition3"/>
<property name="targetDataSource" ref="dataSource3"/>
<property name="targetDetectorDataSource" ref="dataSource3"/>
<property name="standbyDataSource" ref="dataSource6"/>
<property name="standbyDetectorDataSource" ref="dataSource6"/>
</bean>
</set>
</property>
<!-- HA配置,对数据库发送SQL语句:update caland set timeflag=CURRENT_TIMESTAMP()进行数据库状态检测 -->
<property name="haDataSourceCreator">
<bean class="com.caland.sun.client.datasources.ha.FailoverHotSwapDataSourceCreator">
<property name="detectingSql" value="update caland set timeflag=CURRENT_TIMESTAMP()"/>
</bean>
</property>
</bean>
<!-- 配置路由规则开始 -->
<!-- 该bean指定了路由算法的处理类 -->
<bean id="hashFunction" class="com.caland.core.dao.router.HashFunction"/>
<bean id="internalRouter"
class="com.caland.sun.client.router.config.InteralRouterXmlFactoryBean">
<!-- functionsMap是在使用自定义路由规则函数的时候使用 -->
<property name="functionsMap">
<map>
<entry key="hash" value-ref="hashFunction"></entry> 指定使用哪个方法
</map>
</property>
<property name="configLocations"> 指定配置路由的xml路径
<list>
<value>classpath:/dbRule/sharding-rules-on-namespace.xml</value>
</list>
</property>
</bean>
<!-- 配置路由规则结束 -->
路由的配置文件
<rules>
<rule>
<namespace>User</namespace>
<!--
表达式如果不使用自定义路由规则函数,而是直接使用 taobaoId%2==0这种的话就不用在文件
中配置<property name="functionsMap">中了
-->
<shardingExpression>hash.applyUser(username) == 1</shardingExpression>
<shards>partition1</shards>
</rule>
<rule>
<namespace>User</namespace>
<shardingExpression>hash.applyUser(username) == 2</shardingExpression>
<shards>partition2</shards>
</rule>
<rule>
<namespace>User</namespace>
<shardingExpression>hash.applyUser(username) == 3</shardingExpression>
<shards>partition3</shards>
</rule>
</rules>