参考:https://www.cnblogs.com/binghe001/p/13959992.html
Mycat支持水平分片、垂直分片。
在schema.xml文件我们可以清楚的认识这些概念。
根据服务器情况下载对应的版本。
https://github.com/MyCATApache/Mycat-download/tree/master/1.6-RELEASE
# 解压
tar -xvf xxx
# 解压结果
root@ubuntu:/home/liangshijie/mycat# ls
bin catlet conf lib logs version.txt
# 配置环境变量
vim /etc/profile
# 追加:
export PATH=/home/liangshijie/mycat/bin:${PATH}
# 刷新环境变量
source /etc/profile
环境:ubuntu18、使用docker启动;
数据库:study;
数据表:user;
mysql5.7主从集群:
节点 | 地址 |
---|---|
主 | 192.168.204.139 |
从 | 192.168.204.138 |
从 | 192.168.204.140 |
# 进入mycat 的conf目录
cd /home/liangshijie/mycat/conf
# 修改
vim server.xml
# 将schemas改为study。
# 在这里可以mycat客户端登录密码。
# root账户默认读写
# user账户是只读
<user name="root">
<property name="password">123456</property>
<property name="schemas">study</property>
</user>
<user name="user">
<property name="password">user</property>
<property name="schemas">study</property>
<property name="readOnly">true</property>
</user>
# 修改物理节点相关配置
vim schema.xml
# 先修改schema的name为我们的数据库study
# 修改table标签,表为user,使用dn1节点
# 根据集群修改dataHost
DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">
<schema name="study" checkSQLschema="true" sqlMaxLimit="100">
<table name="user" dataNode="dn1" />
schema>
<dataNode name="dn1" dataHost="node1" database="study" />
<dataHost name="node1" maxCon="1000" minCon="10" balance="1"
writeType="0" dbType="mysql" dbDriver="native" switchType="1" slaveThreshold="100">
<heartbeat>select user()heartbeat>
<writeHost host="hostM1" url="192.168.204.139:3306" user="root" password="123456">
<readHost host="hostS2" url="192.168.204.137:3306" user="root" password="123456" />
<readHost host="hostS3" url="192.168.204.140:3306" user="root" password="123456" />
writeHost>
dataHost>
mycat:schema>
mycat start | stop | restart | status
服务器开放端口8066。
使用可视化工具链接。账户密码:root\123456。
插入或查询数据。
查看当前server_id: SELECT @@server_id;
跟Springboot整合只需修改mysql的数据源配置。
spring:
datasource:
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.204.142:8066/study?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true
server:
port: 8062
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.6.RELEASEversion>
<relativePath/>
parent>
<groupId>cn.lsjgroupId>
<artifactId>read-write-separationartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>read-write-separationname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.5.1version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.22version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.6version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
exclude>
excludes>
configuration>
plugin>
plugins>
build>
project>
@SpringBootApplication
@MapperScan("cn.lsj.readwriteseparation.mapper")
public class ReadWriteSeparationApplication {
public static void main(String[] args) {
SpringApplication.run(ReadWriteSeparationApplication.class, args);
}
}
@RequestMapping("test")
@AllArgsConstructor
@RestController
public class TestController {
private final UserService userService;
@GetMapping("read")
public User read() {
return userService.getById(1);
}
@PostMapping("write")
public String write(@RequestBody User user) {
userService.saveOrUpdate(user);
return "成功";
}
}
@Data
public class User {
private int id;
private String username;
}
public interface UserMapper extends BaseMapper<User> {
}
public interface UserService extends IService<User> {
}
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}