如何用java5分钟实现一个最简单的mysql代理服务器

展开全部首先,准备开发工具套件,我们并不会引入过多工具包,仅仅需要:java8vert.x 3如果你是用maven做为项目管理工具,请将vert.x 3引入:12345<dependency><groupId>io.vertx</groupId><artifactId>vertx-core</artifactId><version>3.3.2</version></dependency>代码实现:12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485packagecom.maxleap.mysqlproxy;importio.vertx.core.AbstractVerticle;importio.vertx.core.Vertx;importio.vertx.core.logging.Logger;importio.vertx.core.logging.LoggerFactory;importio.vertx.core.net.NetClient;importio.vertx.core.net.NetServer;importio.vertx.core.net.NetSocket;/***@author sneaky*@since 1.0.0*/publicclassMysqlProxyServer{privatestaticfinalLoggerlogger=LoggerFactory.getLogger(MysqlProxyServer.class);publicstaticvoidmain(String[]args){Vertx.vertx().deployVerticle(newMysqlProxyServerVerticle());}publicstaticclassMysqlProxyServerVerticleextendsAbstractVerticle{privatefinalintport=3306;privatefinalStringmysqlHost="10.10.0.6";@Overridepublicvoidstart()throwsException{NetServernetServer=vertx.createNetServer();//创建代理服务器NetClientnetClient=vertx.createNetClient();//创建连接mysql客户端netServer.connectHandler(socket->netClient.connect(port,mysqlHost,result->{//响应来自客户端的连接请求,成功之后,在建立一个与目标mysql服务器的连接if(result.succeeded()){//与目标mysql服务器成功连接连接之后,创造一个MysqlProxyConnection对象,并执行代理方法newMysqlProxyConnection(socket,result.result()).proxy();