Mycat, practice papers - MySQL based

EDITORIAL

  Mycat as a standalone database middleware, we only need to perform configuration, it can be very convenient for us to achieve the level of segmentation and vertical segmentation, separate read and write functions, but we realize the main Mysql required for replication by other means . It is assumed that we have built a good associated environmental, following began our practice of it!

Preparing the environment

  • Mysql (Version: 5.7) from the main building environment
  • Establish a corresponding database (the following example is to build the database: master1mycat and master2mycat)

Configuring server.xml

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

Configuration 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>

Configuration 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>

  After modifying the configuration file, do not forget to restart Mycat, if an exception occurs, troubleshoot by viewing the log file in the logs directory.

Project to build (SpringBoot + JPA)

  • Preparation: for the first time to build tables, spring.jpa.hibernate.ddl-auto property is set in application.yml to: create (JPA automatically create a table solutions, using update, then when the connection mycat will report the error can not find a table) . To ensure that data is not lost, it can change after the construction of the table: update

  • Add 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
  • Add User Entity
@Entity
@Table(name = "tb_user")
@Data
public class User {

    @Id
    private Long id;

    private String name;

    private Integer gender;

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

    @Id
    private Long id;

    private String name;

    @Column(unique = true)
    private Long userId;

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

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

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

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

}

Project Test

  1. Add test
@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);
        }
}

Test Results: The data division manner id modulo two to the database, while simultaneously the data from the master database library

  1. Test fuzzy query paging +
@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);
}

Test Results: fuzzy matching manner and the descending output id

Test Results: Read all gone from the library

  1. Delete, and modify your own test

Articles have been authorized to obtain reproduced the original address: https://blog.mariojd.cn/mycat-practice-based-mysql.html

Reproduced in: https: //www.jianshu.com/p/28d08cee2f5f

Guess you like

Origin blog.csdn.net/weixin_34347651/article/details/91076253