mycat2 注释配置

  • Post author:
  • Post category:其他

mycat2大部分 配置都可以使用 注释语句 进行创建

所有的语句都要在mycat平台输入

重置配置

/*+ mycat:resetConfig{} */
重置所有配置

创建用户

/*+ mycat:createUser{
	"username":"user",
	"password":"",
	"ip":"127.0.0.1",
	"transactionType":"xa"
} */
创建一个登录到mycat的用户 
username: 用户名
password:密码
ip: 允许登录的ip
transactionType:事务类型

删除用户

/*+ mycat:dropUser{
	"username":"user"} */

显示用户

/*+ mycat:showUsers */

创建schmea

/*+ mycat:createSchema{
	"customTables":{},
	"globalTables":{},
	"normalTables":{},
	"schemaName":"testSchemaDB",
	"shardingTables":{},
	"targetName":"prototype"
} */;

schemaName:物理库的库名
targetName: 物理库所在的集群,默认应该将prototype集群内的MySQL连接 设置为物理库连接

创建成功后,mycat 会映射物理库的所有单表,可以检测是否成功

创建数据源

/*+ mycat:createDataSource{
"name":"r0",
"url":"jdbc:mysql://127.0.0.1:3306/mysql",
"user":"root",
"password":"xxxx"
} */;

/*+ mycat:createDataSource{
"name":"w0",
"url":"jdbc:mysql://127.0.0.1:3306/mysql",
"user":"root",
"password":"xxxx"
} */;


/*+ mycat:createDataSource{
"name":"r1",
"url":"jdbc:mysql://127.0.0.1:3307/mysql",
"user":"root",
"password":"xxxx"
} */;

/*+ mycat:createDataSource{
"name":"w1",
"url":"jdbc:mysql://127.0.0.1:3307/mysql",
"user":"root",
"password":"xxxx"
} */;
配置2组读写数据库连接

执行完成后 在/conf/datasources 文件夹下会出现对应名称的 配置文件

删除数据源

/*+ mycat:dropDataSource{
	"name":"newDs"
} */;

显示数据源

/*+ mycat:showDataSources{} */

添加集群配置

/*! mycat:createCluster{"name":"c0","masters":["w0"],"replicas":["r0"]} */;
/*! mycat:createCluster{"name":"c1","masters":["w1"],"replicas":["r1"]} */;
执行完成后  在/conf/clusters  文件夹下出现对应名称的 配置文件

c0集群 主节点为w0的数据连接,从节点为r0的数据连接 
c1集群 主节点为w1的数据连接,从节点为r1的数据连接 

在创建数据库连接配置的时候  也可以使用不一样的连接地址做读写分离,但是需要mysql物理数据库先做主从同步
这里不过多介绍

删除集群

/*! mycat:dropCluster{
	"name":"testAddCluster"
} */;

显示集群

/*+ mycat:showClusters{} */

映射单表

/*+ mycat:createTable{
	"normalTable":{
		"createTableSQL":"create table normal(id int)",
		"locality":{
			"schemaName":"testSchema", //物理库
			"tableName":"normal", //物理表
			"targetName":"prototype" //目标
		}
	},
	"schemaName":"testSchema",//逻辑库
	"tableName":"normal" //逻辑表
} */;
将物理库的表,映射到mycat
当目标是集群的时候,自动进行读写分离
如果目标是数据源,就直接发送sql到这个数据源
应用场景: 读写分离,映射不需要分库分表的 物理表

创建分库分表结构


/*+ mycat:createTable{
	"schemaName":"testSchema",
	"shardingTable":{
		"createTableSQL":"create table sharding  xxxxxxxxxxxxx  (建表语句)",
		"function":{
        	"properties":{
	          "dbNum":1,
	          "mappingFormat":"c${targetIndex}/数据库名_${dbIndex}/表名_${index}",
	          "tableNum":10,
	          "tableMethod":"mod_hash(id)",
	          "storeNum":1,
	          "dbMethod":"mod_hash(id)"
      		}
    	},
		"partition":{
			
		}
	},
	"tableName":"表名"
} */;
schemaName: 创建的schmea 名字, 作用:指定创建逻辑表的地方
createTableSQL: mysql建表语句
dbNum: 分库数量
mappingFormat: 映射配置  c${targetIndex}  映射集群 
tableNum:分表数量
tableMethod:分表算法
dbMethod:分库算法
storeNum:存储节点数量

创建自定义算法分库分表(http方案)


/*+ mycat:createTable{
	"schemaName":"testSchema",
	"shardingTable":{
		"createTableSQL":"create table sharding  xxxxxxxxxxxxx  (建表语句)",
		"function":{
			"clazz":"io.mycat.router.custom.HttpCustomRuleFunction",
			"properties":{
				"name":"test",
				"shardingDbKeys":"",
				"shardingTableKeys":"id",
				"shardingTargetKeys":"",
				"allScanPartitionTimeout":5,
				"fetchTimeout":60000,
				"routerServiceAddress":"http://127.0.0.1:9066/router_service_address"
			}
		},
		"partition":{
			
		}
	},
	"tableName":"表名"
} */;
schemaName: 创建的schmea 名字, 作用:指定创建逻辑表的地方
createTableSQL: mysql建表语句
name: 自定义算法名字
shardingDbKeys: 分库的字段
shardingTableKeys:分表字段
shardingTargetKeys:分集群字段
fetchTimeout:http超时时间
routerServiceAddress:http 的请求地址

参考 c${targetIndex}/数据库名_${dbIndex}/表名_${index}", 规则

mycat请求接口会传递3个参数
schemaName:逻辑库名
tableName:分表的名称
condition:条件

java获取示例
在这里插入图片描述
condition 在等于{} 或者 没有条件时 要返回当前表的所有分区

自定义分库分表的接口始终需要返回数组

[
	{
		"targetName":"prototype",
		"dbIndex":0,
		"tableIndex":1,
		"index":0,
		"targetIndex":1
		"table":"sharding",
		"schema":"testschema"
	}
]

targetName: 数据库位置   例:c0 mycat就会去c0数据源中查找数据
dbIndex:数据库下标  
tableIndex:表下标 
table:分表的名称,注意如果一个库分10张表  这10个返回对象应该每个对象的表名都不一样 例 sharding_0 sharding_1
schema: 数据库名,如果一个mysql 服务分了多个库,库名不能重复
targetIndex:集群下标
index:总分表下标  
 例 :分了2张表  index就应该为  
 [
	{
		"targetName":"prototype",
		"dbIndex":0,
		"tableIndex":1,
		"index":0,
		"targetIndex":1
		"table":"sharding",
		"schema":"testschema"
	},
		{
		"targetName":"prototype",
		"dbIndex":0,
		"tableIndex":1,
		"index":1,
		"targetIndex":2
		"table":"sharding",
		"schema":"testschema"
	}
]
参考:c${targetIndex}/数据库名_${dbIndex}/表名_${index}", 映射规范

示例

   public Map<String, Object> getSealDataRouter(String areaCode, Integer index,Integer count, String tableName) {
        HashMap<String, Object> map = new HashMap<>(7);
        map.put("targetName", "c"+ index);
        map.put("dbIndex", Integer.parseInt(areaCode));
        map.put("tableIndex", 0);
        map.put("index", count);
        map.put("table", "xxxx");
        map.put("targetIndex", index);
        map.put("schema", "xxxx_xxxx_" + areaCode);
        MAP.put(tableName + "_" + areaCode, map);
        return map;
    }
这边我用的多库单表  多库多表的话 table名称也要设置称动态的

版权声明:本文为weixin_44624815原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。