NutzCN Logo
问答 关于Nutz Dao的读写分离
发布于 2639天前 作者 文涛(wentao) 1852 次浏览 复制 上一个帖子 下一个帖子
标签:

设置了读写分离,但发现日志里显示只连接了主库,如何判断是否已经开启了读写分离

日志如下:

[DEBUG] 14:11:36.226 - Jdbcs init complete
[INFO ] 14:11:36.226 - Get Connection from DataSource for JdbcExpert, if you lock at here, check your database server and configure
[DEBUG] 14:11:36.731 - select expert : org.nutz.dao.impl.jdbc.mysql.MysqlJdbcExpert
[DEBUG] 14:11:36.742 - JDBC Driver --> mysql-connector-java-5.1.40 ( Revision: 402933ef52cad9aa82624e80acbea46e3a701ce6 )
[DEBUG] 14:11:36.742 - JDBC Name   --> MySQL Connector Java
[DEBUG] 14:11:36.742 - JDBC URL    --> jdbc:mysql://10.2.223.42:3306/boss
[DEBUG] 14:11:36.941 - Mysql : character_set_client=utf8
[DEBUG] 14:11:36.941 - Mysql : character_set_connection=utf8
[DEBUG] 14:11:36.942 - Mysql : character_set_database=utf8
[DEBUG] 14:11:36.942 - Mysql : character_set_filesystem=binary
[DEBUG] 14:11:36.942 - Mysql : character_set_results=
[DEBUG] 14:11:36.943 - Mysql : character_set_server=utf8
[DEBUG] 14:11:36.943 - Mysql : character_set_system=utf8
[DEBUG] 14:11:36.943 - Mysql : character_sets_dir=/usr/local/mysql/share/charsets/
[DEBUG] 14:11:36.949 - Mysql : binlog_format=ROW
[DEBUG] 14:11:36.953 - Mysql : database=test

配置如下:

dataSource : {
        // master数据源
        factory : "$conf#make",
        args : ["com.alibaba.druid.pool.DruidDataSource", "db_master."],
        type : "com.alibaba.druid.pool.DruidDataSource",
        events : {
            create : "init",
            depose : 'close'
        }
    },
    slaveDataSource : {
        // slave数据源,负责读
        factory : "$conf#make",
        args : ["com.alibaba.druid.pool.DruidDataSource", "db_slave."],
        type : "com.alibaba.druid.pool.DruidDataSource",
        events : {
            create : "init",
            depose : 'close'
        }
    },
    dao : {
        type : "org.nutz.dao.impl.NutDao",
        args : [{refer:"dataSource"}], //引用主数据源
        fields : {
            runner : {refer: "daoRunner"}
        }
    },
    daoRunner: {
        type : "org.nutz.dao.impl.sql.run.NutDaoRunner",
        fields : {
            slaveDataSource : {refer:"slaveDataSource"} //引用slave数据源
        }
    },

db 配置

# db_master
db_master.url=jdbc:mysql://10.2.223.42:3306/test
db_master.username=test
db_master.password=test
db_master.validationQuery=select 1
db_master.maxActive=50
db_master.testWhileIdle=true
db_master.filters=mergeStat
db_master.connectionProperties=druid.stat.slowSqlMillis=2000
db_master.defaultAutoCommit=true

# db_slave
db_slave.url=jdbc:mysql://10.2.223.45:3306/test
db_slave.username=test
db_slave.password=test
db_slave.validationQuery=select 1
db_slave.maxActive=50
db_slave.testWhileIdle=true
db_slave.filters=mergeStat
db_slave.connectionProperties=druid.stat.slowSqlMillis=2000
db_slave.defaultAutoCommit=true
2 回复

从库是45,但是日志连接的是42

从库仅提供数据库连接, 并不读取它的参数

执行一下select语句, debug一下org.nutz.dao.impl.sql.run.NutDaoRunner.selectDataSource(Transaction, DataSource, ConnCallback) 方法就知道了

添加回复
请先登陆
回到顶部