运维小知识

Mycat(配置篇)

2018-03-07  本文已影响0人  happyJared

Mycat目录说明

配置文件目录说明图:

Mycat3大配置文件Mycat3大配置文件

3大配置文件说明

server.xml

  包含了Mycat需要的系统配置信息,用户配置信息以及逻辑库配置信息,源代码中的映射类为:SystemConfig.class

    <user name="mycat">
        <property name="password">mycat</property>
        <property name="schemas">mycats</property><!--schemas:逻辑库名称,具体配置在scheme.xml中-->
    </user>

schema.xml

  可以说是最重要的配置文件,管理着 MyCat 的逻辑库、表、分片规则、DataNode 以及 DataSource

    <schema name="mycats" checkSQLschema="false" sqlMaxLimit="100">
        <!-- 逻辑表配置 -->
        <table name="tb_user" dataNode="dn1,dn2" rule="mod-long" /><!--name:实际物理库的数据表名;dataNode:表对应的分片;rule:分片规则名称,具体配置在rule.xml中-->
    </schema>

    <dataNode name="dn1" dataHost="host1" database="mycat1" /><!--name:分片名称;database:实际物理库的数据库名-->
    <dataNode name="dn2" dataHost="host1" database="mycat2" />

    <dataHost name="host1" maxCon="100" minCon="10" balance="0" writeType="0" dbType="mysql" dbDriver="native">
       <heartbeat>select user()</heartbeat><!--mysql心跳检测命令-->
       <writeHost host="hostM1" url="localhost:3306" user="root" password="xxx" /><!--实际物理库的配置信息-->
    </dataHost>

rule.xml

  定义了表拆分所涉及到的规则定义。根据业务可以灵活的对表使用不同的分片算法(目前已实现十余种不同的分片规则,对应所在源码包为:io.mycat.route.function),或者对表使用相同的算法但具体的参数不同。

    <tableRule name="mod-long"> <!-- 对应表的分片规则 -->
        <rule>
            <columns>id</columns><!-- 对应数据表要取模的字段名称 -->
            <algorithm>mod-long</algorithm><!-- 对应function的名称 -->
        </rule>
    </tableRule>
    
    <function name="mod-long" class="io.mycat.route.function.PartitionByMod"><!-- name:对应tableRule的名称;class:切分规则对应的切分类 -->
        <!--  scheme.xml中有多少个dataNode就改成多少个 -->
        <property name="count">2</property>
    </function>

代码测试(SpringBoot + JPA)

DROP TABLE IF EXISTS `tb_user`;
CREATE TABLE `tb_user` (
  `id` bigint(20) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

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/mycats?characterEncoding=UTF-8&useSSL=false&autoReconnect=true&rewriteBatchedStatements=true
    username: mycat
    password: mycat
@Entity
@Table(name = "tb_user")
@Data
public class User {

    @Id
    private Long id;

    private String name;

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

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

}

  1. 测试添加
@Test
public void testAdd() {
    for (long i = 0; i < 50; i++) {
        User user = new User();
        user.setId(i);
        user.setName("ls" + i);
        userDao.save(user);
    }
}

测试结果:数据按id取模的方式划分到了两个数据库中

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

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

测试模糊查询+分页测试模糊查询+分页
  1. 删除及修改请自行测试

参考链接

Mycat官网
Mycat从零开始
Mycat权威指南
GitHub:Mycat-Server
Wiki:Mycat-Server
Issues:Mycat-Server
mysql中间件研究(Atlas,Cobar,TDDL)
mysql中间件研究(Atlas,Cobar,TDDL,Mycat,Heisenberg,Oceanus,Vitess,OneProxy)

上一篇下一篇

猜你喜欢

热点阅读