参考: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账户是只读123456 study
user study true
# 修改物理节点相关配置
vim schema.xml
# 先修改schema的name为我们的数据库study
# 修改table标签,表为user,使用dn1节点
# 根据集群修改dataHost
select user()
mycat start | stop | restart | status
服务器开放端口8066。
使用可视化工具链接。账户密码:root\123456。
插入或查询数据。
查看当前server_id: SELECT @@server_id;
跟Springboot整合只需修改mysql的数据源配置。
spring:datasource:username: rootpassword: 123456type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.jdbc.Driverurl: 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
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.6.RELEASE cn.lsj read-write-separation 0.0.1-SNAPSHOT read-write-separation Demo project for Spring Boot 8 org.springframework.boot spring-boot-starter-web com.baomidou mybatis-plus-boot-starter 3.5.1 org.springframework.boot spring-boot-starter-jdbc com.alibaba druid 1.1.22 mysql mysql-connector-java 5.1.6 org.projectlombok lombok true org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok
@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 {
}public interface UserService extends IService {
}@Service
public class UserServiceImpl extends ServiceImpl implements UserService {
}