mar*_*tin 0 java mysql spring hibernate database-replication
我需要使用单独的读取器/写入器 MySQL 服务器。一个写入器和一个(或多个)只读副本。
做这个的最好方式是什么?
我找到了很多例子:
http://www.dragishak.com/?p=307
使用特殊的 JDBC 驱动程序: com.mysql.jdbc.ReplicationDriver
用法是:
@Transactional(readOnly=true)
@ReadOnlyConnection
public Result serviceMethod(…) {
…
}
Run Code Online (Sandbox Code Playgroud)
使用弹簧AbstractRoutingDatasource:
用法:
@Transactional(readOnly = true)
public Page<BookDTO> getBooks(Pageable p) {
try{
DbContextHolder.setDbType(DbType.REPLICA1); // <----- set ThreadLocal DataSource lookup key
Run Code Online (Sandbox Code Playgroud)
在每种方法中,我都需要设置 DbType。
是否可以自动将“读取查询”发送到副本服务器并将“写入查询”(插入/更新)发送到主服务器?
第二个问题:
我想每个用户有一个 mysql 数据库(非常大)。我预计大约有 2000 名用户。所以我不能为每个用户*读者+作者定义数据源。
例子:
spring.ds_items.driverClassName=com.mysql.jdbc.Driver
spring.ds_items.url=jdbc:mysql://mysql-master/user1
spring.ds_items.username=root
spring.ds_items.password=12345
spring.ds_items.driverClassName=com.mysql.jdbc.Driver
spring.ds_items.url=jdbc:mysql://mysql-replica1/user1
spring.ds_items.username=root
spring.ds_items.password=12345
spring.ds_items.driverClassName=com.mysql.jdbc.Driver
spring.ds_items.url=jdbc:mysql://mysql-master/user2
spring.ds_items.username=root
spring.ds_items.password=12345
spring.ds_items.driverClassName=com.mysql.jdbc.Driver
spring.ds_items.url=jdbc:mysql://mysql-replica1/user2
spring.ds_items.username=root
spring.ds_items.password=12345
Run Code Online (Sandbox Code Playgroud)
我想要一个“主 mysql 数据库”,其中包含如下表:
user db_name
--------------
test1 db_test1
test2 db_test2
Run Code Online (Sandbox Code Playgroud)
如果我需要用户的一些数据,test2我会查看“主数据库”并获取db_test2要使用的数据库信息。之后将查询发送到数据库db_test2。
最好的方法是使用 MySQL Connector/J 复制感知驱动程序配置。您的连接字符串如下所示
jdbc:mysql:replication://[master host][:port],[slave host 1][:port][,[slave host 2][:port]]...[/[database]]
Run Code Online (Sandbox Code Playgroud)
一旦您将事务标记为只读,它将自动路由到只读从属。
https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-master-slave-replication-connection.html
| 归档时间: |
|
| 查看次数: |
3607 次 |
| 最近记录: |