diff --git a/source/src/main/java/io/mycat/mycat2/ConfigLoader.java b/source/src/main/java/io/mycat/mycat2/ConfigLoader.java index ce61f1b..e756edb 100644 --- a/source/src/main/java/io/mycat/mycat2/ConfigLoader.java +++ b/source/src/main/java/io/mycat/mycat2/ConfigLoader.java @@ -135,13 +135,24 @@ public static List loadMySQLRepBean(String datasourceuri){ repBean.setSwitchType(switchType); List mysqlNodes=getChildNodes(curRepNode,"mysql"); List 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); } @@ -161,7 +172,7 @@ 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); } @@ -169,7 +180,7 @@ 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()); } diff --git a/source/src/main/java/io/mycat/mycat2/beans/MySQLBean.java b/source/src/main/java/io/mycat/mycat2/beans/MySQLBean.java index 711ed5c..68425e9 100644 --- a/source/src/main/java/io/mycat/mycat2/beans/MySQLBean.java +++ b/source/src/main/java/io/mycat/mycat2/beans/MySQLBean.java @@ -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; @@ -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; } @@ -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 + "]"; } } diff --git a/source/src/main/java/io/mycat/mycat2/net/DefaultMycatSessionHandler.java b/source/src/main/java/io/mycat/mycat2/net/DefaultMycatSessionHandler.java index 4d7fbdc..f3fb2fa 100644 --- a/source/src/main/java/io/mycat/mycat2/net/DefaultMycatSessionHandler.java +++ b/source/src/main/java/io/mycat/mycat2/net/DefaultMycatSessionHandler.java @@ -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); @@ -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) { // 认证成功后开始同步会话状态至后端 diff --git a/source/src/main/java/io/mycat/mycat2/tasks/BackendConCreateTask.java b/source/src/main/java/io/mycat/mycat2/tasks/BackendConCreateTask.java index ff58686..8f103d7 100644 --- a/source/src/main/java/io/mycat/mycat2/tasks/BackendConCreateTask.java +++ b/source/src/main/java/io/mycat/mycat2/tasks/BackendConCreateTask.java @@ -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; @@ -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 @@ -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(); diff --git a/source/src/main/resources/datasource.xml b/source/src/main/resources/datasource.xml index 6198726..c1d1079 100644 --- a/source/src/main/resources/datasource.xml +++ b/source/src/main/resources/datasource.xml @@ -2,7 +2,7 @@ - +