1、有時候常常會遇到配置數據庫主從主從分離是存儲和訪問數據,就須要配置和修改代碼了,mysql安裝我就不說了mysql
2、 主服務器master的配置spring
1. vim /etc/my.cnfsql
2. 重啓服務 service mysqld restart數據庫
3. 建立一個用戶vim
4. 賦予用戶的權限(mysql之用戶密碼)安全
2. 另外一種重啓MySQL服務:服務器
3. 鏈接到master上session
4. 從庫上查看同步狀態mybatis
發現有錯誤併發
解決:
stop slave 中止主從同步
vim /etc/my.cnf
搜索server-id發現配置文件已經默認配置了一個,須要刪除文件本身默認配置的
重啓服務
打開主從同步start slave
查看狀態:發現已經沒有錯誤了
就是說讀的時候到從數據庫讀,修改的時候到主數據庫修改
1. 建立一個包
2. 配置抽象路由數據源DynamicDataSource
package com.itwang.o2o.dao.split; import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; //繼承抽象路由數據源 public class DynamicDataSource extends AbstractRoutingDataSource { @Override protected Object determineCurrentLookupKey() { return DynamicDataSourceHolder.getDBType(); } }
3. 配置DynamicDataSourceHolder
package com.itwang.o2o.dao.split; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class DynamicDataSourceHolder { private static Logger logger = LoggerFactory.getLogger(DynamicDataSourceHolder.class); //ThreadLocal保證線程安全 private static ThreadLocal<String> contextHolder = new ThreadLocal<String>(); public static final String DB_MASTER = "master"; public static final String DB_SLAVE = "slave"; /** * 獲取線程DBType * @return */ public static String getDBType(){ String db = contextHolder.get(); if (db == null){ db = DB_MASTER; } return db; } /** * 設置線程DBType * @param str */ public static void setDBType(String str){ logger.debug("所使用的數據源爲:" + str); contextHolder.set(str); } /** * 清空ThreadLocal對象 */ public static void clearDBType(){ contextHolder.remove(); } }
4. 配置mybatis的攔截器,DynamicDataSourceInterceptor
package com.itwang.o2o.dao.split; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class DynamicDataSourceHolder { private static Logger logger = LoggerFactory.getLogger(DynamicDataSourceHolder.class); //ThreadLocal保證線程安全 private static ThreadLocal<String> contextHolder = new ThreadLocal<String>(); public static final String DB_MASTER = "master"; public static final String DB_SLAVE = "slave"; /** * 獲取線程DBType * @return */ public static String getDBType(){ String db = contextHolder.get(); if (db == null){ db = DB_MASTER; } return db; } /** * 設置線程DBType * @param str */ public static void setDBType(String str){ logger.debug("所使用的數據源爲:" + str); contextHolder.set(str); } /** * 清空ThreadLocal對象 */ public static void clearDBType(){ contextHolder.remove(); } }
5.配置SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--配置全局屬性-->
<settings>
<!--使用jdbc的getGeneratedKeys獲取數據庫自增主鍵值-->
<setting name="useGeneratedKeys" value="true"/>
<!--開啓駝峯命名轉化-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<plugins>
<plugin interceptor="com.itwang.o2o.dao.split.DynamicDataSourceInterceptor"></plugin>
</plugins>
</configuration>
6. 配置dao層數據源配置文件
6.1 原始配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 數據庫鏈接池 --> <!-- 加載配置文件 --> <context:property-placeholder location="classpath:resource/*.properties"/> <!-- 數據庫鏈接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="driverClassName" value="${jdbc.driver}"/> <property name="maxActive" value="10"/> <property name="minIdle" value="5"/> </bean> <!-- 配置sqlsessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property> <property name="dataSource" ref="dataSource"></property> <!--掃描pojo包使用別名--> <property name="typeAliasesPackage" value="com.itwang.o2o.pojo"></property> <!--掃描sql配置文件:mapper須要的xml文件--> <property name="mapperLocations" value="classpath:mapper/*.xml"></property> </bean> <!-- 配置掃描包,加載mapper代理對象 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.itwang.o2o.dao"></property> <!--注入sqlSessionFactory--> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>
6.2 修改後的配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 數據庫鏈接池 --> <!-- 加載配置文件 --> <context:property-placeholder location="classpath:resource/*.properties"/> <!-- 數據庫鏈接池 --> <bean id="abstractDataSource" abstract="true" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <!-- 最大併發鏈接數 --> <property name="maxActive" value="10"/> <!-- 初始化鏈接數量 --> <property name = "initialSize" value = "1" /> <!-- 配置獲取鏈接等待超時的時間 --> <property name = "maxWait" value = "60000" /> <!-- 最小空閒鏈接數 --> <property name="minIdle" value="5"/> </bean> <bean id="master" parent="abstractDataSource"> <property name="url" value="${jdbc.master.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="driverClassName" value="${jdbc.driver}"/> </bean> <bean id="slave" parent="abstractDataSource"> <property name="url" value="${jdbc.slave.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="driverClassName" value="${jdbc.driver}"/> </bean> <!--配置動態數據源,這裏的targetDataSources就是路由數據源所對應的名稱--> <bean id="dynamicDataSource" class="com.itwang.o2o.dao.split.DynamicDataSource"> <property name="targetDataSources"> <map> <entry value-ref="master" key="master"></entry> <entry value-ref="slave" key="slave"></entry> </map> </property> </bean> <!--配置懶加載--> <bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy"> <property name="targetDataSource" ref="dynamicDataSource"></property> </bean> <!-- 配置sqlsessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property> <property name="dataSource" ref="dataSource"></property> <!--掃描pojo包使用別名--> <property name="typeAliasesPackage" value="com.itwang.o2o.pojo"></property> <!--掃描sql配置文件:mapper須要的xml文件--> <property name="mapperLocations" value="classpath:mapper/*.xml"></property> </bean> <!-- 配置掃描包,加載mapper代理對象 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.itwang.o2o.dao"></property> <!--注入sqlSessionFactory--> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>
7. 配置數據庫參數文件配置db.properties(這裏我主從庫配置的同樣方便測試)
jdbc.driver=com.mysql.jdbc.Driver jdbc.master.url=jdbc:mysql://localhost:3306/o2o?characterEncoding=UTF-8 jdbc.slave.url=jdbc:mysql://localhost:3306/o2o?characterEncoding=UTF-8 jdbc.username=root jdbc.password=root