EDITORIAL
スタンドアロン・データベース・ミドルウェアとしてMycatは、我々は唯一の独立した読み取りおよび書き込み機能、私たちはセグメンテーションと垂直分割のレベルを達成するために、それは非常に便利なことができ、設定を実行する必要がありますが、私たちは、メインMySQLは、他の手段による複製に必要な実現します。以下のことは、それを私たちの練習を始めた、私たちは良い伴う環境構築していることが想定されます!
環境を準備します
- 本館環境から:MySQLの(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>
設定ファイルを変更した後、例外が発生した場合、logsディレクトリにログファイルを閲覧することにより、トラブルシューティング、Mycatを再起動することを忘れないでください。
構築するためのプロジェクト(SpringBoot + JPA)
準備:テーブルを構築するために初めて、spring.jpa.hibernate.ddl-autoプロパティがにapplication.ymlに設定されています(その後、接続mycatがエラーを報告しますときに、テーブルを見つけることができない、JPAは自動的にアップデートを使用して、テーブルのソリューションを作成)を作成。そのデータが失われないようにするために、それはテーブルの構築後に変更することができます更新を
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
- ユーザエンティティを追加します。
@Entity
@Table(name = "tb_user")
@Data
public class User {
@Id
private Long id;
private String name;
private Integer gender;
}
- 学生エンティティを追加
@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);
}
プロジェクトのテスト
- テストを追加します。
@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をモジュロ2つのデータベースに、一方同時にマスタ・データベース・ライブラリからデータ
- テストファジークエリページング+
@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
試験結果:すべてのライブラリから消えて読みます
- 削除し、独自のテストを修正
記事は、元のアドレスを再現し得ることを許可されています:https://blog.mariojd.cn/mycat-practice-based-mysql.html
ます。https://www.jianshu.com/p/28d08cee2f5fで再現