Skip to content

Commit

Permalink
Merge pull request MyCATApache#21 from Gao-Zhiwen/test
Browse files Browse the repository at this point in the history
修改datasource解析,去除MySQLBean中的defaultSchema属性
  • Loading branch information
apachemycat authored Aug 17, 2017
2 parents 73aba6c + 386fb88 commit 142cc44
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 32 deletions.
29 changes: 20 additions & 9 deletions source/src/main/java/io/mycat/mycat2/ConfigLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,24 @@ public static List<MySQLRepBean> loadMySQLRepBean(String datasourceuri){
repBean.setSwitchType(switchType);
List<Node> mysqlNodes=getChildNodes(curRepNode,"mysql");
List<MySQLBean> allMysqls=mysqlNodes.stream().map(mysqlNode->{
NamedNodeMap attrs = mysqlNode.getAttributes();
String ip=getAttribute(attrs,"ip",null);
String user=getAttribute(attrs,"user",null);
String password=getAttribute(attrs,"password",null);
int port=getIntAttribute(attrs,"port",3306);
MySQLBean mysql=new MySQLBean(ip,port,user,password);
return mysql;}).collect(Collectors.toList()) ;
NamedNodeMap attrs = mysqlNode.getAttributes();
String hostName = getAttribute(attrs, "hostname", null);
String ip=getAttribute(attrs,"ip",null);
String user=getAttribute(attrs,"user",null);
String password=getAttribute(attrs,"password",null);
int port=getIntAttribute(attrs,"port",3306);
Integer minCon = getIntAttribute(attrs, "min-con", null);
Integer maxCon = getIntAttribute(attrs, "max-con", null);

MySQLBean mysql=new MySQLBean(ip,port,user,password);
if (hostName != null)
mysql.setHostName(hostName);
if (minCon != null)
mysql.setMinCon(minCon);
if (maxCon != null)
mysql.setMaxCon(maxCon);
return mysql;
}).collect(Collectors.toList()) ;
repBean.setMysqls(allMysqls);
list.add(repBean);
}
Expand All @@ -161,15 +172,15 @@ private static String getAttribute(NamedNodeMap map,String attr,String defaultVa
{
return getValue(map.getNamedItem(attr),defaultVal);
}
private static int getIntAttribute(NamedNodeMap map,String attr,int defaultVal)
private static Integer getIntAttribute(NamedNodeMap map,String attr,Integer defaultVal)
{
return getIntValue(map.getNamedItem(attr),defaultVal);
}
private static String getValue(Node node,String defaultVal)
{
return node==null?defaultVal:node.getNodeValue();
}
private static int getIntValue(Node node,int defaultVal)
private static Integer getIntValue(Node node,Integer defaultVal)
{
return node==null?defaultVal:Integer.valueOf(node.getNodeValue());
}
Expand Down
11 changes: 1 addition & 10 deletions source/src/main/java/io/mycat/mycat2/beans/MySQLBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class MySQLBean {
private int port;
private String user;
private String password;
private String defaultSchema = "mysql";
private int maxCon = 1000;
private int minCon = 1;

Expand Down Expand Up @@ -89,14 +88,6 @@ public void setPassword(String password) {
this.password = password;
}

public String getDefaultSchema() {
return defaultSchema;
}

public void setDefaultSchema(String defaultSchema) {
this.defaultSchema = defaultSchema;
}

public int getMaxCon() {
return maxCon;
}
Expand All @@ -116,7 +107,7 @@ public void setMinCon(int minCon) {
@Override
public String toString() {
return "MySQLBean [hostName=" + hostName + ", ip=" + ip + ", port=" + port + ", user=" + user + ", password="
+ password + ", defaultSchema=" + defaultSchema + ", maxCon=" + maxCon + ", minCon=" + minCon + "]";
+ password + ", maxCon=" + maxCon + ", minCon=" + minCon + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ public void onFrontRead(final MySQLSession session) throws IOException {
}
if (session.backendChannel == null) {
// todo ,从连接池中获取连接,获取不到后创建新连接,
final MySQLDataSource datas = session.getDatasource();
final MySQLDataSource ds = session.getDatasource();

logger.info("hang cur sql for backend connection ready ");
String serverIP = datas.getConfig().getIp();
int serverPort = datas.getConfig().getPort();
String serverIP = ds.getConfig().getIp();
int serverPort = ds.getConfig().getPort();
InetSocketAddress serverAddress = new InetSocketAddress(serverIP, serverPort);
session.backendChannel = SocketChannel.open();
session.backendChannel.configureBlocking(false);
Expand All @@ -82,7 +82,7 @@ public void onFrontRead(final MySQLSession session) throws IOException {
session.backendKey = selectKey;
logger.info("Connecting to server " + serverIP + ":" + serverPort);

BackendConCreateTask authProcessor = new BackendConCreateTask(session, null);
BackendConCreateTask authProcessor = new BackendConCreateTask(session, ds);
authProcessor.setCallback((optSession, Sender, exeSucces, retVal) -> {
if (exeSucces) {
// 认证成功后开始同步会话状态至后端
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.security.NoSuchAlgorithmException;

import io.mycat.mycat2.beans.MySQLBean;
import io.mycat.mycat2.beans.SchemaBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -29,10 +30,11 @@ public class BackendConCreateTask extends AbstractBackendIOTask {
private static Logger logger = LoggerFactory.getLogger(BackendConCreateTask.class);
private HandshakePacket handshake;
private boolean welcomePkgReceived = false;
private MySQLDataSource ds;

public BackendConCreateTask(MySQLSession session, MySQLDataSource ds) {

super(session);
this.ds = ds;
}

@Override
Expand All @@ -59,22 +61,20 @@ public void onBackendRead(MySQLSession session) throws IOException {
return;
}
// 发送应答报文给后端
final MySQLBean mySQLBean = session.getDatasource().getConfig();
String user = mySQLBean.getUser();
String password = mySQLBean.getPassword();
String schema = mySQLBean.getDefaultSchema();
final MySQLBean mySQLBean = ds.getConfig();
AuthPacket packet = new AuthPacket();
packet.packetId = 1;
packet.clientFlags = initClientFlags();
packet.maxPacketSize = 1024 * 1000;
packet.charsetIndex = charsetIndex;
packet.user = user;
packet.user = mySQLBean.getUser();
try {
packet.password = passwd(password, handshake);
packet.password = passwd(mySQLBean.getPassword(), handshake);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e.getMessage());
}
packet.database = schema;
SchemaBean schema = session.schema;
packet.database = (schema == null) ? null : schema.getName();

// 不透传的状态下,需要自己控制Buffer的状态,这里每次写数据都切回初始Write状态
session.frontBuffer.reset();
Expand Down
2 changes: 1 addition & 1 deletion source/src/main/resources/datasource.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<mysql-ds>
<!-- type=0表示普通主从,当前哪个节点为读节点,在另外的文件中保存 -->
<mysql-replica name="test" type="0" switch-type="0">
<!--<mysql ip="10.211.55.5" port="3306" user="root" password="123456" />-->
<!--<mysql hostname="localhost" ip="10.211.55.5" port="3306" user="root" password="123456" min-con="1" max-con="5"/>-->
<mysql ip="127.0.0.1" port="3306" user="root" password="123456" />
</mysql-replica>
</mysql-ds>
Expand Down

0 comments on commit 142cc44

Please sign in to comment.