- 浏览: 34864 次
- 性别:
- 来自: 广州
最新评论
-
appache:
我问一下,MapGuide里面使用的账户验证机制如何能添加自己 ...
一个GIS项目开发(采用MapGuide)的简单总结(转载)
一、配置定义数据库连接属性
二、定义bean
<!-- 主数据库连接池 -->
<bean id="masterDataSource" parent="abstractDataSource">
<property name="url" value="${master.jdbc.url}"/>
<property name="username" value="${master.jdbc.username}"/>
<property name="password" value="${master.jdbc.password}"/>
</bean>
<!-- 从数据库连接池1 -->
<bean id="slave1DataSource" parent="abstractDataSource">
<property name="url" value="${slave1.jdbc.url}"/>
<property name="username" value="${slave1.jdbc.username}"/>
<property name="password" value="${slave1.jdbc.password}"/>
</bean>
<!-- 从数据库连接池2 -->
<bean id="slave2DataSource" parent="abstractDataSource">
<property name="url" value="${slave2.jdbc.url}"/>
<property name="username" value="${slave2.jdbc.username}"/>
<property name="password" value="${slave2.jdbc.password}"/>
</bean>
<!-- 从数据库连接池3 -->
<bean id="slave3DataSource" parent="abstractDataSource">
<property name="url" value="${slave3.jdbc.url}"/>
<property name="username" value="${slave3.jdbc.username}"/>
<property name="password" value="${slave3.jdbc.password}"/>
</bean>
<!-- 动态切换数据源 -->
<bean id="dynamicDataSource" class="com.common.datasource.DynamicDataSource">
<property name="writeDataSource" ref="masterDataSource"></property>
<property name="readDataSources">
<list>
<ref bean="slave1DataSource" />
<ref bean="slave2DataSource" />
<ref bean="slave3DataSource" />
</list>
</property>
<!--轮询方式-->
<property name="readDataSourcePollPattern" value="1" />
<property name="onlyWrite" value="true" />
</bean>
三、事务定义
<!-- 事务管理器 -->
<bean id="dynamicTransactionManager" class="com.common.datasource.DynamicDataSourceTransactionManager">
<property name="dataSource" ref="dynamicDataSource"/>
</bean>
<tx:annotation-driven transaction-manager="dynamicTransactionManager"/>
四、配置插件
<!-- 配置分页插件 -->
<plugins>
<plugin interceptor="com.common.datasource.DynamicPlugin" />
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库 -->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
五、编写动态数据源类
public class DynamicDataSource extends AbstractRoutingDataSource {
private static final Long MAX_POOL = Long.MAX_VALUE;
private final Lock lock = new ReentrantLock();
private Object writeDataSource; //写数据源
private List<Object> readDataSources; //多个读数据源
private int readDataSourceSize; //读数据源个数
private int readDataSourcePollPattern = 0; //获取读数据源方式,0:随机,1:轮询
private AtomicLong counter = new AtomicLong(0);
private boolean onlyWrite=false;
@Override
public void afterPropertiesSet() {
if (this.writeDataSource == null) {
throw new IllegalArgumentException("Property 'writeDataSource' is required");
}
setDefaultTargetDataSource(writeDataSource);
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DynamicDataSourceGlobal.WRITE.name(), writeDataSource);
if (readDataSources != null) {
for (int i = 0; i < readDataSources.size(); i++) {
targetDataSources.put(DynamicDataSourceGlobal.READ.name() + i, readDataSources.get(i));
}
readDataSourceSize = readDataSources.size();
} else {
readDataSourceSize = 0;
}
setTargetDataSources(targetDataSources);
super.afterPropertiesSet();
}
@Override
protected Object determineCurrentLookupKey() {
if (onlyWrite){
return DynamicDataSourceGlobal.WRITE.name();
}
DynamicDataSourceGlobal dynamicDataSourceGlobal = DynamicDataSourceHolder.getDataSource();
if (dynamicDataSourceGlobal == null || dynamicDataSourceGlobal == DynamicDataSourceGlobal.WRITE || readDataSourceSize <= 0) {
return DynamicDataSourceGlobal.WRITE.name();
}
//index表示选择第几个从数据库
int index;
if(readDataSourcePollPattern == 1) {
//轮询方式
long currValue = counter.incrementAndGet();
if((currValue + 1) >= MAX_POOL) {
try {
lock.lock();
if((currValue + 1) >= MAX_POOL) {
counter.set(0);
}
} finally {
lock.unlock();
}
}
index = (int) (currValue % readDataSourceSize);
} else {
//随机方式
index = ThreadLocalRandom.current().nextInt(0, readDataSourceSize);
}
return dynamicDataSourceGlobal.name() + index;
}
public void setWriteDataSource(Object writeDataSource) {
this.writeDataSource = writeDataSource;
}
public List<Object> getReadDataSources() {
return readDataSources;
}
public void setReadDataSources(List<Object> readDataSources) {
this.readDataSources = readDataSources;
}
public Object getWriteDataSource() {
return writeDataSource;
}
public int getReadDataSourceSize() {
return readDataSourceSize;
}
public void setReadDataSourceSize(int readDataSourceSize) {
this.readDataSourceSize = readDataSourceSize;
}
public int getReadDataSourcePollPattern() {
return readDataSourcePollPattern;
}
public void setReadDataSourcePollPattern(int readDataSourcePollPattern) {
this.readDataSourcePollPattern = readDataSourcePollPattern;
}
public boolean isOnlyWrite() {
return onlyWrite;
}
public void setOnlyWrite(boolean onlyWrite) {
this.onlyWrite = onlyWrite;
}
}
public class DynamicDataSourceHolder {
private static final ThreadLocal<DynamicDataSourceGlobal> holder = new ThreadLocal<DynamicDataSourceGlobal>();
private DynamicDataSourceHolder() {
//
}
public static void putDataSource(DynamicDataSourceGlobal dataSource) {
holder.set(dataSource);
}
public static DynamicDataSourceGlobal getDataSource() {
return holder.get();
}
public static void clearDataSource() {
holder.remove();
}
}
public class DynamicDataSourceTransactionManager extends DataSourceTransactionManager {
/**
* 只读事务到读库,读写事务到写库
*
* @param transaction
* @param definition
*/
@Override
protected void doBegin(Object transaction, TransactionDefinition definition) {
//设置数据源
boolean readOnly = definition.isReadOnly();
if (readOnly) {
DynamicDataSourceHolder.putDataSource(DynamicDataSourceGlobal.READ);
} else {
DynamicDataSourceHolder.putDataSource(DynamicDataSourceGlobal.WRITE);
}
super.doBegin(transaction, definition);
}
/**
* 清理本地线程的数据源
*
* @param transaction
*/
@Override
protected void doCleanupAfterCompletion(Object transaction) {
super.doCleanupAfterCompletion(transaction);
DynamicDataSourceHolder.clearDataSource();
}
}
/**
* Desc: Mybatis动态数据库切换插件
*/
@Intercepts({
@Signature(type = Executor.class, method = "update", args = {
MappedStatement.class, Object.class}),
@Signature(type = Executor.class, method = "query", args = {
MappedStatement.class, Object.class, RowBounds.class,
ResultHandler.class})})
public class DynamicPlugin implements Interceptor {
protected static final Logger logger = LoggerFactory.getLogger(DynamicPlugin.class);
private static final String REGEX = ".*insert\\u0020.*|.*delete\\u0020.*|.*update\\u0020.*";
private static final Map<String, DynamicDataSourceGlobal> cacheMap = new ConcurrentHashMap<>();
@Override
public Object intercept(Invocation invocation) throws Throwable {
boolean synchronizationActive = TransactionSynchronizationManager.isSynchronizationActive();
if (!synchronizationActive) {
Object[] objects = invocation.getArgs();
MappedStatement ms = (MappedStatement) objects[0];
DynamicDataSourceGlobal dynamicDataSourceGlobal;
if ((dynamicDataSourceGlobal = cacheMap.get(ms.getId())) == null) {
//读方法
if (ms.getSqlCommandType().equals(SqlCommandType.SELECT)) {
//!selectKey 为自增id查询主键(SELECT LAST_INSERT_ID() )方法,使用主库
if (ms.getId().contains(SelectKeyGenerator.SELECT_KEY_SUFFIX)) {
dynamicDataSourceGlobal = DynamicDataSourceGlobal.WRITE;
} else {
BoundSql boundSql = ms.getSqlSource().getBoundSql(objects[1]);
String sql = boundSql.getSql().toLowerCase(Locale.CHINA).replaceAll("[\\t\\n\\r]", " ");
if (sql.matches(REGEX)) {
dynamicDataSourceGlobal = DynamicDataSourceGlobal.WRITE;
} else {
dynamicDataSourceGlobal = DynamicDataSourceGlobal.READ;
}
}
} else {
dynamicDataSourceGlobal = DynamicDataSourceGlobal.WRITE;
}
logger.warn("设置方法[{}] use [{}] Strategy, SqlCommandType [{}]..", ms.getId(), dynamicDataSourceGlobal.name(), ms.getSqlCommandType().name());
cacheMap.put(ms.getId(), dynamicDataSourceGlobal);
}
DynamicDataSourceHolder.putDataSource(dynamicDataSourceGlobal);
}
return invocation.proceed();
}
@Override
public Object plugin(Object target) {
if (target instanceof Executor) {
return Plugin.wrap(target, this);
} else {
return target;
}
}
@Override
public void setProperties(Properties properties) {
//
}
二、定义bean
<!-- 主数据库连接池 -->
<bean id="masterDataSource" parent="abstractDataSource">
<property name="url" value="${master.jdbc.url}"/>
<property name="username" value="${master.jdbc.username}"/>
<property name="password" value="${master.jdbc.password}"/>
</bean>
<!-- 从数据库连接池1 -->
<bean id="slave1DataSource" parent="abstractDataSource">
<property name="url" value="${slave1.jdbc.url}"/>
<property name="username" value="${slave1.jdbc.username}"/>
<property name="password" value="${slave1.jdbc.password}"/>
</bean>
<!-- 从数据库连接池2 -->
<bean id="slave2DataSource" parent="abstractDataSource">
<property name="url" value="${slave2.jdbc.url}"/>
<property name="username" value="${slave2.jdbc.username}"/>
<property name="password" value="${slave2.jdbc.password}"/>
</bean>
<!-- 从数据库连接池3 -->
<bean id="slave3DataSource" parent="abstractDataSource">
<property name="url" value="${slave3.jdbc.url}"/>
<property name="username" value="${slave3.jdbc.username}"/>
<property name="password" value="${slave3.jdbc.password}"/>
</bean>
<!-- 动态切换数据源 -->
<bean id="dynamicDataSource" class="com.common.datasource.DynamicDataSource">
<property name="writeDataSource" ref="masterDataSource"></property>
<property name="readDataSources">
<list>
<ref bean="slave1DataSource" />
<ref bean="slave2DataSource" />
<ref bean="slave3DataSource" />
</list>
</property>
<!--轮询方式-->
<property name="readDataSourcePollPattern" value="1" />
<property name="onlyWrite" value="true" />
</bean>
三、事务定义
<!-- 事务管理器 -->
<bean id="dynamicTransactionManager" class="com.common.datasource.DynamicDataSourceTransactionManager">
<property name="dataSource" ref="dynamicDataSource"/>
</bean>
<tx:annotation-driven transaction-manager="dynamicTransactionManager"/>
四、配置插件
<!-- 配置分页插件 -->
<plugins>
<plugin interceptor="com.common.datasource.DynamicPlugin" />
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库 -->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
五、编写动态数据源类
public class DynamicDataSource extends AbstractRoutingDataSource {
private static final Long MAX_POOL = Long.MAX_VALUE;
private final Lock lock = new ReentrantLock();
private Object writeDataSource; //写数据源
private List<Object> readDataSources; //多个读数据源
private int readDataSourceSize; //读数据源个数
private int readDataSourcePollPattern = 0; //获取读数据源方式,0:随机,1:轮询
private AtomicLong counter = new AtomicLong(0);
private boolean onlyWrite=false;
@Override
public void afterPropertiesSet() {
if (this.writeDataSource == null) {
throw new IllegalArgumentException("Property 'writeDataSource' is required");
}
setDefaultTargetDataSource(writeDataSource);
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DynamicDataSourceGlobal.WRITE.name(), writeDataSource);
if (readDataSources != null) {
for (int i = 0; i < readDataSources.size(); i++) {
targetDataSources.put(DynamicDataSourceGlobal.READ.name() + i, readDataSources.get(i));
}
readDataSourceSize = readDataSources.size();
} else {
readDataSourceSize = 0;
}
setTargetDataSources(targetDataSources);
super.afterPropertiesSet();
}
@Override
protected Object determineCurrentLookupKey() {
if (onlyWrite){
return DynamicDataSourceGlobal.WRITE.name();
}
DynamicDataSourceGlobal dynamicDataSourceGlobal = DynamicDataSourceHolder.getDataSource();
if (dynamicDataSourceGlobal == null || dynamicDataSourceGlobal == DynamicDataSourceGlobal.WRITE || readDataSourceSize <= 0) {
return DynamicDataSourceGlobal.WRITE.name();
}
//index表示选择第几个从数据库
int index;
if(readDataSourcePollPattern == 1) {
//轮询方式
long currValue = counter.incrementAndGet();
if((currValue + 1) >= MAX_POOL) {
try {
lock.lock();
if((currValue + 1) >= MAX_POOL) {
counter.set(0);
}
} finally {
lock.unlock();
}
}
index = (int) (currValue % readDataSourceSize);
} else {
//随机方式
index = ThreadLocalRandom.current().nextInt(0, readDataSourceSize);
}
return dynamicDataSourceGlobal.name() + index;
}
public void setWriteDataSource(Object writeDataSource) {
this.writeDataSource = writeDataSource;
}
public List<Object> getReadDataSources() {
return readDataSources;
}
public void setReadDataSources(List<Object> readDataSources) {
this.readDataSources = readDataSources;
}
public Object getWriteDataSource() {
return writeDataSource;
}
public int getReadDataSourceSize() {
return readDataSourceSize;
}
public void setReadDataSourceSize(int readDataSourceSize) {
this.readDataSourceSize = readDataSourceSize;
}
public int getReadDataSourcePollPattern() {
return readDataSourcePollPattern;
}
public void setReadDataSourcePollPattern(int readDataSourcePollPattern) {
this.readDataSourcePollPattern = readDataSourcePollPattern;
}
public boolean isOnlyWrite() {
return onlyWrite;
}
public void setOnlyWrite(boolean onlyWrite) {
this.onlyWrite = onlyWrite;
}
}
public class DynamicDataSourceHolder {
private static final ThreadLocal<DynamicDataSourceGlobal> holder = new ThreadLocal<DynamicDataSourceGlobal>();
private DynamicDataSourceHolder() {
//
}
public static void putDataSource(DynamicDataSourceGlobal dataSource) {
holder.set(dataSource);
}
public static DynamicDataSourceGlobal getDataSource() {
return holder.get();
}
public static void clearDataSource() {
holder.remove();
}
}
public class DynamicDataSourceTransactionManager extends DataSourceTransactionManager {
/**
* 只读事务到读库,读写事务到写库
*
* @param transaction
* @param definition
*/
@Override
protected void doBegin(Object transaction, TransactionDefinition definition) {
//设置数据源
boolean readOnly = definition.isReadOnly();
if (readOnly) {
DynamicDataSourceHolder.putDataSource(DynamicDataSourceGlobal.READ);
} else {
DynamicDataSourceHolder.putDataSource(DynamicDataSourceGlobal.WRITE);
}
super.doBegin(transaction, definition);
}
/**
* 清理本地线程的数据源
*
* @param transaction
*/
@Override
protected void doCleanupAfterCompletion(Object transaction) {
super.doCleanupAfterCompletion(transaction);
DynamicDataSourceHolder.clearDataSource();
}
}
/**
* Desc: Mybatis动态数据库切换插件
*/
@Intercepts({
@Signature(type = Executor.class, method = "update", args = {
MappedStatement.class, Object.class}),
@Signature(type = Executor.class, method = "query", args = {
MappedStatement.class, Object.class, RowBounds.class,
ResultHandler.class})})
public class DynamicPlugin implements Interceptor {
protected static final Logger logger = LoggerFactory.getLogger(DynamicPlugin.class);
private static final String REGEX = ".*insert\\u0020.*|.*delete\\u0020.*|.*update\\u0020.*";
private static final Map<String, DynamicDataSourceGlobal> cacheMap = new ConcurrentHashMap<>();
@Override
public Object intercept(Invocation invocation) throws Throwable {
boolean synchronizationActive = TransactionSynchronizationManager.isSynchronizationActive();
if (!synchronizationActive) {
Object[] objects = invocation.getArgs();
MappedStatement ms = (MappedStatement) objects[0];
DynamicDataSourceGlobal dynamicDataSourceGlobal;
if ((dynamicDataSourceGlobal = cacheMap.get(ms.getId())) == null) {
//读方法
if (ms.getSqlCommandType().equals(SqlCommandType.SELECT)) {
//!selectKey 为自增id查询主键(SELECT LAST_INSERT_ID() )方法,使用主库
if (ms.getId().contains(SelectKeyGenerator.SELECT_KEY_SUFFIX)) {
dynamicDataSourceGlobal = DynamicDataSourceGlobal.WRITE;
} else {
BoundSql boundSql = ms.getSqlSource().getBoundSql(objects[1]);
String sql = boundSql.getSql().toLowerCase(Locale.CHINA).replaceAll("[\\t\\n\\r]", " ");
if (sql.matches(REGEX)) {
dynamicDataSourceGlobal = DynamicDataSourceGlobal.WRITE;
} else {
dynamicDataSourceGlobal = DynamicDataSourceGlobal.READ;
}
}
} else {
dynamicDataSourceGlobal = DynamicDataSourceGlobal.WRITE;
}
logger.warn("设置方法[{}] use [{}] Strategy, SqlCommandType [{}]..", ms.getId(), dynamicDataSourceGlobal.name(), ms.getSqlCommandType().name());
cacheMap.put(ms.getId(), dynamicDataSourceGlobal);
}
DynamicDataSourceHolder.putDataSource(dynamicDataSourceGlobal);
}
return invocation.proceed();
}
@Override
public Object plugin(Object target) {
if (target instanceof Executor) {
return Plugin.wrap(target, this);
} else {
return target;
}
}
@Override
public void setProperties(Properties properties) {
//
}
相关推荐
spring +springboot+mybatis+maven 读写分离,数据库采用mysql, 采用springboot 采用项目框架搭建,继承spring 中的AbstractRoutingDataSource,实现 determineCurrentLookupKey 进行数据源的动态切换,采用Spring ...
通过以上步骤,我们成功地在Spring Boot项目中实现了基于AOP的读写分离。这种方式不需要额外的插件,代码简洁,易于理解和维护。在高并发场景下,这种方案能够有效地减轻数据库压力,提高系统响应速度,是优化数据库...
springboot+mybatis+mysql实现读写分离 先在建好mysql主从数据库的配置,然后在代码中根据读写分离或强制读取master数据库中的数据 mysql数据库设置主从,参考: ...
在本资源中,我们主要探讨如何使用Spring MVC、Spring和Mybatis这三大流行框架来构建一个支持分库分表的应用。这些技术都是Java Web开发中的关键组件,它们各自承担着不同的职责并协同工作,以实现高效、可扩展的...
Mybatis允许开发者编写SQL语句,将数据库操作与业务逻辑分离,提高了代码的可读性和可维护性。 3. **Druid**:Druid是一个高性能的数据库连接池组件,它提供了监控、扩展性和性能优化功能。Druid可以集成到Spring中...
在数据库读写分离的场景下,MyBatis可以灵活地配置数据源,分别处理读操作和写操作。 3. **Druid**:Druid是阿里巴巴开源的数据库连接池实现,它具有监控、扩展性好、性能优秀等特点。在读写分离的架构中,Druid...
本项目结合了Spring、SpringMVC、MyBatis和Maven等技术,与MySQL数据库一起实现读写分离,以提升应用的处理能力。下面将详细解释这些技术及其在读写分离中的作用。 首先,Spring是一个开源的Java平台,它提供了全面...
在SpringMVC中,可以通过Spring的依赖注入(DI)和AOP(面向切面编程)特性,将MyBatis的SqlSessionFactory和RedisTemplate作为bean注入到需要的地方。在MyBatis中,可以配置数据源、事务管理器以及Mapper接口,以便...
本项目以"springboot+mybatis-plus+shardingsphere 实现读写分离"为主题,结合了Spring Boot的快速开发能力、MyBatis-Plus的简化数据操作以及ShardingSphere的分布式数据库解决方案,旨在提高系统的读取性能和可扩展...
在IT行业中,数据库读写分离...在这个压缩包`mybatis_dxfl`中,可能包含了实现上述步骤的代码示例,包括SpringBoot的启动类、配置文件、Mapper接口和XML配置等,读者可以参考这些代码来理解和实践读写分离的配置过程。
本项目将这些框架进行了整合,并进行了数据库读写分离的配置,以提高系统的稳定性和性能。 首先,Spring框架作为基础,提供了强大的依赖注入(Dependency Injection,DI)和面向切面编程(Aspect-Oriented ...
SSM(Spring、SpringMVC、MyBatis)与Redis结合的简洁Demo是一个常见的Web开发实践,用于构建高效、可扩展的应用程序。这个Demo展示了如何整合这三个流行的技术框架以及使用Redis作为缓存来提高性能。 **Spring框架...
SpringMVC、德鲁伊(Druid)、MyBatis 和 MySQL 是四个在Java Web开发中常用的组件,它们各自承担着不同的职责,而将它们结合在一起则可以构建出一个高效、可扩展的数据库读写分离解决方案。 SpringMVC是Spring框架...
本示例"Spring+MySQL+MyBatis+SpringMVC 读写分离"是针对这种策略的一个实现,旨在帮助开发者理解如何在实际项目中应用这一技术。下面将详细介绍这个Demo中的关键知识点。 首先,我们要理解什么是读写分离。读写...
《构建基于Spring、SpringMVC和MyBatis的湖北电信渠道营销支撑系统》 在信息技术高速发展的今天,电信行业的业务支撑系统已经成为企业运营的核心部分。本文将深入探讨如何利用Spring、SpringMVC和MyBatis这三大开源...
Spring MVC是Spring框架的一个子模块,专门用于构建Web应用程序,它通过模型-视图-控制器(Model-View-Controller,MVC)模式实现了业务逻辑、数据处理和用户界面的分离。 MyBatis是一个持久层框架,它将SQL语句与...
Spring AOP + SpringMVC +Mybatis做动态数据源,实现读写分离Spring AOP + SpringMVC +Mybatis做动态数据源,实现读写分离Spring AOP + SpringMVC +Mybatis做动态数据源,实现读写分离
5. **性能优化**:通过Sharding-JDBC,不仅可以实现分库分表,还可以进行读写分离、分布式事务等高级功能,进一步提升系统的性能和可用性。同时,开发者需要注意SQL优化,避免全表扫描和笛卡尔积等问题,确保在分库...
Struts2+Spring+Mybatis 是一种常见的Java Web开发框架组合,被广泛应用于企业级应用中。这个框架组合的优点在于它能有效地管理控制层、业务层和服务层,从而提高开发效率和代码的可维护性。 Struts2是Action导向的...
在IT行业中,数据库分片和数据路由是一种常见的优化策略,特别是在大型系统中,为了提高数据库的性能和可扩展性,通常会采用读写分离、分布式数据库等技术。本项目是基于Spring和Mybatis实现的数据路由功能,它允许...