Mycat,实践篇 - 基于 MySQL

写在前面

  Mycat作为独立的数据库中间件,我们只需要进行相关的配置,就可以非常方便的帮我们实现水平切分、垂直切分、读写分离等功能,但Mysql的主从复制需要我们通过其它方式实现。这里假设我们已经搭建好相关的环境,下面就开始我们的实践吧!

准备环境

  • Mysql(Version : 5.7)主从环境搭建
  • 对应数据库建立(以下例子中要建的数据库是:master1mycat 和 master2mycat)

配置server.xml

        <user name="mysqlmycat">
                <property name="password">mysqlmycat</property>
                <property name="schemas">mysqlmycats</property>
        </user>

配置schema.xml

        <schema name="mysqlmycats" checkSQLschema="false" sqlMaxLimit="100">
                <table name="tb_user" dataNode="mydn1,mydn2" rule="user-mod-long" />
                <table name="tb_student" dataNode="mydn1,mydn2" rule="student-mod-long" />
        </schema>
        
        <dataNode name="mydn1" dataHost="myhost1" database="master1mycat" />
        <dataNode name="mydn2" dataHost="myhost2" database="master2mycat" />

        <dataHost name="myhost1" maxCon="100" minCon="10" balance="3" writeType="0" dbType="mysql" dbDriver="native">
                <heartbeat>select user()</heartbeat>
                <writeHost host="hostM1" url="localhost:3306" user="root" password="xxx">
                        <readHost host="hostS1" url="localhost:3307" user="root" password="xxx"/>
                </writeHost>
        </dataHost>

        <dataHost name="myhost2" maxCon="100" minCon="10" balance="3" writeType="0" dbType="mysql" dbDriver="native">
                <heartbeat>select user()</heartbeat>
                <writeHost host="hostM2" url="localhost:3308" user="root" password="xxx" >
                        <readHost host="hostS2" url="localhost:3309" user="root" password="xxx"/>
                </writeHost>
        </dataHost>

配置rule.xml

       <tableRule name="user-mod-long">
                <rule>
                        <columns>id</columns>
                        <algorithm>mod-long</algorithm>
                </rule>
        </tableRule>
       <tableRule name="student-mod-long">
                <rule>
                        <columns>user_id</columns>
                        <algorithm>mod-long</algorithm>
                </rule>
        </tableRule>

        <function name="mod-long" class="io.mycat.route.function.PartitionByMod">
            <property name="count">2</property>
        </function>

  修改了配置文件后,别忘了重启Mycat,如果有异常出现,请通过查看logs目录下的日志文件进行排查。

项目搭建(SpringBoot + JPA)

  • 准备:首次建表,设置application.yml中的spring.jpa.hibernate.ddl-auto属性为:create(JPA自动建表解决方案,使用update的话在连接mycat的时候会报找不到表的错误)。为保证数据不被丢失,在建表之后可以更改为:update

  • 添加application.yml:

spring:
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: update
      naming:
        strategy: org.hibernate.cfg.ImprovedNamingStrategy
    properties:
      hibernate:
        dialect: org.hibernate.dialect.MySQL5Dialect
  datasource:
    url: jdbc:mysql://localhost:8066/mysqlmycats?characterEncoding=UTF-8&useSSL=false&autoReconnect=true&rewriteBatchedStatements=true
    username: mysqlmycat
    password: mysqlmycat
  • 添加User Entity
@Entity
@Table(name = "tb_user")
@Data
public class User {

    @Id
    private Long id;

    private String name;

    private Integer gender;

}
  • 添加Student Entity
@Entity
@Table(name = "tb_student")
@Data
public class Student {

    @Id
    private Long id;

    private String name;

    @Column(unique = true)
    private Long userId;

}
  • 添加UserDao
public interface UserDao extends JpaRepository<User, Long> {

    Page<User> findByNameLike(String name, Pageable pageable);

}
  • 添加StudentDao
public interface StudentDao extends JpaRepository<Student, Long> {

    Page<User> findByNameLike(String name, Pageable pageable);

}

项目测试

  1. 测试添加
@Test
public void testAdd() {
        for (long i = 0; i < 30; i++) {
            User user = new User();
            user.setId(i);
            user.setName("张三" + i);
            user.setGender(i % 2 == 0 ? 0 : 1);
            userDao.save(user);

            Student student = new Student();
            student.setId(System.currentTimeMillis() + i);
            student.setName("张三学生" + i);
            student.setUserId(i);
            studentDao.save(student);
        }
}

测试结果:数据按id取模的方式划分到了两个数据库中,同时从库同步了主库的数据

  1. 测试模糊查询+分页
@Test
public void testFind() {
        Pageable pageable = new PageRequest(0, 10, Sort.Direction.DESC, "id");
        List<User> userList = userDao.findByNameLike("%张三2%", pageable).getContent();
        userList.forEach(System.out::println);

        Pageable pageable2 = new PageRequest(0, 10, Sort.Direction.DESC, "userId");
        List<Student> studentList = studentDao.findByNameLike("%张三学生1%", pageable2).getContent();
        studentList.forEach(System.out::println);
}

测试结果:按照模糊匹配及id降序的方式输出结果

测试结果:读操作都走了从库

  1. 删除及修改请自行测试

文章已授权获得转载,原文地址:https://blog.mariojd.cn/mycat-practice-based-mysql.html

转载于:https://www.jianshu.com/p/28d08cee2f5f

猜你喜欢

转载自blog.csdn.net/weixin_34347651/article/details/91076253