一、有时候经常会遇到配置数据库主从主从分离是存储和访问数据,就需要配置和修改代码了,mysql安装我就不说了
二、 主服务器master的配置
1. vim /etc/my.cnf
2. 重启服务 service mysqld restart
3. 创建一个用户
4. 赋予用户的权限(mysql之用户密码)
三、 从服务器slaver的配置
- 进入到my.cnf下面 vim /etc/my.cnf
2. 另一种重启MySQL服务:
3. 连接到master上
4. 从库上查看同步状态
发现有错误
解决:
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