-
Notifications
You must be signed in to change notification settings - Fork 5
/
db-source.xml
224 lines (211 loc) · 10.7 KB
/
db-source.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<bean id="wall-config" class="com.alibaba.druid.wall.WallConfig">
<property name="multiStatementAllow" value="true"/>
</bean>
<bean id="wall-filter" class="com.alibaba.druid.wall.WallFilter">
<property name="config" ref="wall-config"/>
</bean>
<bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
<property name="slowSqlMillis" value="10000"/>
<property name="logSlowSql" value="true"/>
<property name="mergeSql" value="true"/>
</bean>
<!-- system -->
<bean id="dataSourceSystem" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.system.url}"/>
<property name="username" value="${jdbc.system.username}"/>
<property name="password" value="${jdbc.system.password}"/>
<property name="initialSize" value="${pool.initialPoolSize}"/>
<property name="minIdle" value="${pool.minPoolSize}"/>
<property name="maxActive" value="${pool.maxPoolSize}"/>
<property name="validationQuery" value="select 1"/>
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="minEvictableIdleTimeMillis" value="300000"/>
<property name="removeAbandoned" value="true"/>
<!-- 超时时间;单位为秒 -->
<property name="removeAbandonedTimeout" value="30"/>
<property name="filters" value="log4j2"/>
<property name="proxyFilters">
<list>
<ref bean="stat-filter"/>
<ref bean="wall-filter"/>
</list>
</property>
</bean>
<!-- student -->
<bean id="dataSourceStudent" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.student.url}"/>
<property name="username" value="${jdbc.student.username}"/>
<property name="password" value="${jdbc.student.password}"/>
<property name="initialSize" value="${pool.initialPoolSize}"/>
<property name="minIdle" value="${pool.minPoolSize}"/>
<property name="maxActive" value="${pool.maxPoolSize}"/>
<property name="validationQuery" value="select 1"/>
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="minEvictableIdleTimeMillis" value="300000"/>
<property name="removeAbandoned" value="true"/>
<!-- 超时时间;单位为秒 -->
<property name="removeAbandonedTimeout" value="30"/>
<property name="filters" value="log4j2"/>
<property name="proxyFilters">
<list>
<ref bean="stat-filter"/>
<ref bean="wall-filter"/>
</list>
</property>
</bean>
<!-- finance -->
<bean id="dataSourceFinance" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.finance.url}"/>
<property name="username" value="${jdbc.finance.username}"/>
<property name="password" value="${jdbc.finance.password}"/>
<property name="initialSize" value="${pool.initialPoolSize}"/>
<property name="minIdle" value="${pool.minPoolSize}"/>
<property name="maxActive" value="${pool.maxPoolSize}"/>
<property name="validationQuery" value="select 1"/>
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="minEvictableIdleTimeMillis" value="300000"/>
<property name="removeAbandoned" value="true"/>
<!-- 超时时间;单位为秒 -->
<property name="removeAbandonedTimeout" value="30"/>
<property name="filters" value="log4j2"/>
<property name="proxyFilters">
<list>
<ref bean="stat-filter"/>
<ref bean="wall-filter"/>
</list>
</property>
</bean>
<!-- biz -->
<bean id="dataSourceBiz" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.biz.url}"/>
<property name="username" value="${jdbc.biz.username}"/>
<property name="password" value="${jdbc.biz.password}"/>
<property name="initialSize" value="${pool.initialPoolSize}"/>
<property name="minIdle" value="${pool.minPoolSize}"/>
<property name="maxActive" value="${pool.maxPoolSize}"/>
<property name="validationQuery" value="select 1"/>
<property name="timeBetweenEvictionRunsMillis" value="60000"/>
<property name="minEvictableIdleTimeMillis" value="300000"/>
<property name="removeAbandoned" value="true"/>
<!-- 超时时间;单位为秒 -->
<property name="removeAbandonedTimeout" value="30"/>
<property name="filters" value="log4j2"/>
<property name="proxyFilters">
<list>
<ref bean="stat-filter"/>
<ref bean="wall-filter"/>
</list>
</property>
</bean>
<bean id="dataSource" class="common.aspect.ChooseDataSource" primary="true">
<!-- 默认数据源 -->
<property name="defaultTargetDataSource" ref="dataSourceSystem"/>
<!-- 下面的各个 key 需要配置到 ShardConfig 的 schemaKeyList,见下面的:<bean id="shardConfig" class="mis.api.common.shard.shard.ShardConfig" > -->
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="system" value-ref="dataSourceSystem"/>
<entry key="student" value-ref="dataSourceStudent"/>
<entry key="finance" value-ref="dataSourceFinance"/>
<entry key="biz" value-ref="dataSourceBiz"/>
</map>
</property>
</bean>
<!-- 以下配置,部分表名只是用于配置示例,仅为了更好的展示如何配置。
本项目没有用到的表名有:edu_class、biz_trade_order、biz_item、biz_item_sku
-->
<bean id="shardConfig" class="common.shard.ShardConfig" >
<!-- 列表值为 dataSource.targetDataSources 的 keys -->
<property name="schemaKeyList">
<list>
<value>system</value>
<value>student</value>
<value>finance</value>
<value>biz</value>
</list>
</property>
<!-- 基于服务接口分库策略,
把针对某个 schema 的接口配置在该数据源 key 对应的 list 下,没有就不配置
-->
<property name="shardSchemaInterfaceClassNameList">
<map>
<entry key="student">
<list>
<value>biz.service.facade.IEduStudentService</value>
</list>
</entry>
</map>
</property>
<!-- 分表策略
直接将 ShardRequest.shardKeyTable(优先级高于后者) 或 ShardRequest.shardKeyTableNumber 作为分表后缀的表。
需要配合 shardKeyTable 或 shardKeyTableNumber 使用,二选一,shardKeyTable 的优先级高于 shardKeyTableNumber,如 shardKeyTable=3,则下面的 edu_student 最终分表为 edu_student_3
ShardRequest 参见:https://github.com/uncleAndyChen/mybatis-plugin-shard/blob/master/common/common-shard/src/main/java/common/shard/ShardRequest.java
-->
<property name="shardTableDirectlyList">
<list>
<value>edu_student</value>
<value>edu_class</value>
</list>
</property>
<!-- 分表策略
通过两个数相除取余作为后缀的表,配合 ShardRequest.shardKeyTableNumber 使用
ShardRequest 参见:https://github.com/uncleAndyChen/mybatis-plugin-shard/blob/master/common/common-shard/src/main/java/common/shard/ShardRequest.java
-->
<!-- key 将作为 shardKeyTableNumber 的除数(取余), 余数作为分表后缀-->
<!-- shardKeyTableNumber 通过 ShardRequest 传递,在请求 api 时传递 -->
<property name="shardTableDivideList">
<map>
<entry key="10">
<list>
<value>biz_trade</value>
<value>biz_trade_order</value>
</list>
</entry>
<entry key="5">
<list>
<value>biz_item</value>
<value>biz_item_sku</value>
</list>
</entry>
</map>
</property>
<!-- 打印分表的 sql 语句,默认为 false 即不打印。-->
<property name="printShardSqlInfo" value="true" />
<!-- 不需要分表的 sql 语句列表,以下这句为 MyBatis 操作数据库新增记录时,查询新增的主键值的语句 -->
<property name="notNeedShardSqlList">
<list>
<value>SELECT LAST_INSERT_ID()</value>
</list>
</property>
</bean>
<!-- 用于切面,实现拦截数据库操作,实现分库分表的类 -->
<bean id="dataSourceAspect" class="common.aspect.DataSourceAspect">
<property name="shardTableConfigView" ref="shardConfig" />
</bean>
<!-- 定义切面,用于拦截数据库操作,实现分库分表 -->
<aop:config proxy-target-class="true">
<aop:aspect id="dataSourceAspect" ref="dataSourceAspect" order="1">
<aop:pointcut id="point" expression="(execution(* biz.service.impl.*.*(..)))"/>
<aop:before pointcut-ref="point" method="before"/>
<aop:after pointcut-ref="point" method="afterHandler"/>
</aop:aspect>
</aop:config>
</beans>