Skip to content

Commit

Permalink
Merge pull request MyCATApache#25 from MyCATApache/master
Browse files Browse the repository at this point in the history
从主干合并
  • Loading branch information
yanjunli authored Sep 25, 2017
2 parents 45d5595 + 970ee1d commit 6eb3f2a
Show file tree
Hide file tree
Showing 66 changed files with 1,418 additions and 1,289 deletions.
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ Genel TCP Proxy using Java NIO ,simple and fast
### 集群配置说明

- 非集群模式
- 修改`mycat.yml`中的`clusterEnable: false`
- 指定`mycat.yml`中的ip和port
- 修改`cluster.yml`中的`enable: false`,设置cluster的port


- 集群模式
- 修改`mycat.yml`中的`clusterEnable: true`
- `myNodeId`需要在集群中唯一
- 开启集群:`cluster.yml`中的`enable: true`,设置cluster的port,`myNodeId`需要在集群中唯一
- 负载均衡:`balancer.yml`中的`enable: true`,设置balancer的port及strategy
- 集群模式下,只有在集群状态下才提供代理服务,在脱离集群状态下将暂时无法提供代理服务


Expand Down Expand Up @@ -57,12 +58,15 @@ Genel TCP Proxy using Java NIO ,simple and fast
#### 四 启动第二个mycat,并自动加入集群。

4.1 集群相关配置文件
`conf` 目录下, mycat1.yml,mycat2.yml,mycat3.yml 三个配置文件,分别为集群中三个节点的配置文件。<br>
配置文件名称格式为: `mycat[unique id]. conf`<br>
`conf` 目录下, 修改mycat.yml,cluster.yml,balancer.yml 三个配置文件。<br>
配置文件中 `cluster.allnodes` 属性 需要将集群中,所有节点的信息配置上。<br>

`conf` 目录下, 设置 `wrapper.conf` 配置文件中,<br>
`wrapper.app.parameter.2` 参数的值 为 mycat[`unique id`]. yml 中的 `unique id` 。<br>
确定从哪个mycat[unique id]. conf 配置文件中 读取配置。<br>
4.2 需要注意的是,每个节点一套mycat程序。<br>
4.3 配置完成后 按照第三步 启动mycat. 新启动的mycat 将自动加入集群中。<br>

### 五 在IDEA中调试集群

IDEA中调试可以设置启动参数,支持的启动参数:
-mycat.proxy.port 8067
-mycat.cluster.enable true
-mycat.cluster.port 9067
-mycat.cluster.myNodeId leader-2
6 changes: 3 additions & 3 deletions source/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@
<daemon>
<id>mycat</id>
<mainClass>io.mycat.mycat2.MycatCore</mainClass>
<commandLineArguments>
<commandLineArgument>1</commandLineArgument>
</commandLineArguments>
<!--<commandLineArguments>-->
<!--<commandLineArgument>1</commandLineArgument>-->
<!--</commandLineArguments>-->
<platforms>
<platform>jsw</platform>
</platforms>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @author wuzhihui
*
*/
public abstract class AbstractMySQLSession extends AbstractSession {
public abstract class AbstractMySQLSession extends AbstractSession {

// 当前接收到的包类型
public enum CurrPacketType {
Expand Down
109 changes: 33 additions & 76 deletions source/src/main/java/io/mycat/mycat2/ConfigLoader.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package io.mycat.mycat2;

import io.mycat.mycat2.beans.ReplicaConfBean;
import io.mycat.mycat2.beans.ReplicaIndexBean;
import io.mycat.mycat2.beans.SchemaConfBean;
import io.mycat.mycat2.sqlannotations.AnnotationProcessor;
import io.mycat.proxy.ConfigEnum;
import io.mycat.proxy.Configurable;
import io.mycat.proxy.ProxyRuntime;
import io.mycat.util.YamlUtil;
import org.slf4j.Logger;
Expand All @@ -27,108 +25,67 @@ public class ConfigLoader {
public static final String DIR_PREPARE = "prepare" + File.separator;
public static final String DIR_ARCHIVE = "archive" + File.separator;

private ConfigLoader() {}
public void loadCore() throws IOException {
ConfigLoader.INSTANCE.load(ConfigEnum.PROXY, null);
ConfigLoader.INSTANCE.load(ConfigEnum.HEARTBEAT, null);
ConfigLoader.INSTANCE.load(ConfigEnum.CLUSTER, null);
ConfigLoader.INSTANCE.load(ConfigEnum.BALANCER, null);
}

public void loadAll() throws IOException {
MycatConfig conf = (MycatConfig) ProxyRuntime.INSTANCE.getProxyConfig();
// 保证文件夹存在
YamlUtil.createDirectoryIfNotExists(DIR_PREPARE);
YamlUtil.createDirectoryIfNotExists(DIR_ARCHIVE);

loadReplicaIndex(false, conf, ConfigEnum.REPLICA_INDEX, null);
loadDatasource(false, conf, ConfigEnum.DATASOURCE, null);
loadSchema(false, conf, ConfigEnum.SCHEMA, null);
loadConfig(false, ConfigEnum.REPLICA_INDEX, null);
loadConfig(false, ConfigEnum.DATASOURCE, null);
loadConfig(false, ConfigEnum.SCHEMA, null);

ProxyRuntime.INSTANCE.getConfig().initRepMap();
ProxyRuntime.INSTANCE.getConfig().initSchemaMap();

// 清空prepare文件夹
YamlUtil.clearDirectory(DIR_PREPARE, null);
AnnotationProcessor.getInstance();//强制初始化动态注解
}

public void load(ConfigEnum configEnum, Integer targetVersion) throws IOException {
MycatConfig conf = (MycatConfig) ProxyRuntime.INSTANCE.getProxyConfig();
switch (configEnum) {
case REPLICA_INDEX:
loadReplicaIndex(true, conf, ConfigEnum.REPLICA_INDEX, targetVersion);
YamlUtil.clearDirectory(DIR_PREPARE, ConfigEnum.REPLICA_INDEX.getFileName());
break;
case DATASOURCE:
loadDatasource(true, conf, ConfigEnum.DATASOURCE, targetVersion);
YamlUtil.clearDirectory(DIR_PREPARE, ConfigEnum.DATASOURCE.getFileName());
break;
case SCHEMA:
loadSchema(true, conf, ConfigEnum.SCHEMA, targetVersion);
YamlUtil.clearDirectory(DIR_PREPARE, ConfigEnum.SCHEMA.getFileName());
break;
case SHARDING_RULE:
break;
default:
return;
if (targetVersion != null) {
loadConfig(true, configEnum, targetVersion);
YamlUtil.clearDirectory(DIR_PREPARE, configEnum.getFileName());
} else {
loadConfig(false, configEnum, targetVersion);
}

if (configEnum == ConfigEnum.SCHEMA) {
ProxyRuntime.INSTANCE.getConfig().initSchemaMap();
} else if (configEnum == ConfigEnum.DATASOURCE) {
ProxyRuntime.INSTANCE.getConfig().initRepMap();
}
}

/**
* replica-index.yml
* 加载指定配置文件
* @param needAchive 是否需要归档
* @param configEnum 配置文件的枚举
* @param targetVersion 指定的版本号
* @throws IOException
*/
private void loadReplicaIndex(boolean needAchive, MycatConfig conf, ConfigEnum configEnum, Integer targetVersion) throws IOException {
private void loadConfig(boolean needAchive, ConfigEnum configEnum, Integer targetVersion) throws IOException {
// 加载replica-index
String fileName = configEnum.getFileName();
byte configType = configEnum.getType();
MycatConfig conf = ProxyRuntime.INSTANCE.getConfig();

Integer curVersion = conf.getConfigVersion(configType);
Integer curVersion = conf.getConfigVersion(configEnum);
if (needAchive) {
Integer repVersion = YamlUtil.archive(fileName, curVersion, targetVersion);
if (repVersion == null) {
return;
}
curVersion = repVersion;
}
LOGGER.debug("load config for {}", configEnum);
ReplicaIndexBean replicaIndexBean = YamlUtil.load(fileName, ReplicaIndexBean.class);
conf.addRepIndex(replicaIndexBean);
conf.putConfig(configType, replicaIndexBean, curVersion);
}

/**
* datasource.yml
*/
private void loadDatasource(boolean needAchive, MycatConfig conf, ConfigEnum configEnum, Integer targetVersion) throws IOException {
// 加载datasource
String fileName = configEnum.getFileName();
byte configType = configEnum.getType();

Integer curVersion = conf.getConfigVersion(configType);
if (needAchive) {
Integer dsVersion = YamlUtil.archive(fileName, curVersion, targetVersion);
if (dsVersion == null) {
return;
}
curVersion = dsVersion;
}
LOGGER.debug("load config for {}", configEnum);
ReplicaConfBean replicaConfBean = YamlUtil.load(fileName, ReplicaConfBean.class);
replicaConfBean.getMysqlReplicas().forEach(replicaBean -> conf.addMySQLRepBean(replicaBean));
conf.putConfig(configType, replicaConfBean, curVersion);
}

/**
* schema.yml
*/
private void loadSchema(boolean needAchive, MycatConfig conf, ConfigEnum configEnum, Integer targetVersion) throws IOException {
// 加载schema
String fileName = configEnum.getFileName();
byte configType = configEnum.getType();

Integer curVersion = conf.getConfigVersion(configType);
if (needAchive) {
Integer schemaVersion = YamlUtil.archive(fileName, curVersion, targetVersion);
if (schemaVersion == null) {
return;
}
curVersion = schemaVersion;
}
LOGGER.debug("load config for {}", configEnum);
SchemaConfBean schemaConfBean = YamlUtil.load(fileName, SchemaConfBean.class);
schemaConfBean.getSchemas().forEach(schemaBean -> conf.addSchemaBean(schemaBean));
conf.putConfig(configType, schemaConfBean, curVersion);
conf.putConfig(configEnum, (Configurable) YamlUtil.load(fileName, configEnum.getClazz()), curVersion);
}
}
2 changes: 1 addition & 1 deletion source/src/main/java/io/mycat/mycat2/MySQLSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void setMySQLMetaBean(MySQLMetaBean metaBean) {

@Override
public String toString() {
return "MySQLSession [database=" + database + ", ip=" + mysqlMetaBean.getIp() + ",port="+mysqlMetaBean.getPort()+"]";
return "MySQLSession [database=" + database + ", ip=" + mysqlMetaBean.getDsMetaBean().getIp() + ",port=" + mysqlMetaBean.getDsMetaBean().getPort() + "]";
}

}
Loading

0 comments on commit 6eb3f2a

Please sign in to comment.