From 49f0ccdeaf2ddf9d5fb335085c91a1c090d95551 Mon Sep 17 00:00:00 2001 From: yichen Date: Fri, 23 Sep 2022 10:54:02 +0800 Subject: [PATCH] ChaosTest succeed --- README.md | 28 +++----- agent/pom.xml | 53 ++++++++++++-- .../src/main/java/com/fanruan/ChaosTest.java | 35 ++++++++++ agent/src/main/resources/socket.properties | 4 +- service/pom.xml | 57 ++++++++++++++- .../src/main/java/com/fanruan/ChaosTest.java | 70 +++++++++++++++++++ .../com/fanruan/pojo/message/RpcRequest.java | 5 -- .../src/main/resources/socketIO.properties | 2 +- 8 files changed, 220 insertions(+), 34 deletions(-) create mode 100644 agent/src/main/java/com/fanruan/ChaosTest.java create mode 100644 service/src/main/java/com/fanruan/ChaosTest.java diff --git a/README.md b/README.md index 6bec31c..1ea8341 100644 --- a/README.md +++ b/README.md @@ -42,19 +42,17 @@ ### 动态代理 -动态代理是该项目中的核心,如在 `Driver`类的 `connect`方法中:返回的`Connection`就被替换为了动态代理增强过的`MyConnection`,实现对`Service`中调用的`JDBC`方法的完全代理。代理类会依靠`info`从缓存中找到命名空间(本项目中以`/dataSoure Name`来区别命名空间)对应的`socket`,将方法调用信息以`RPCReqquest`的方式序列化后发送出去。 +动态代理是该项目中的核心,如在 `Driver`类的 `connect`方法中:返回的`Connection`就被替换为了动态代理增强过的`ServiceConnection`,实现对`Service`中调用的`JDBC`方法的完全代理。代理类会依靠`info`从缓存中找到命名空间(本项目中以`/dataSoureName`来区别命名空间)对应的`socket`,将方法调用信息以`RPCReqquest`的方式序列化后发送出去。 ```java - // In Service Source Code @Override - public Connection connect(String url, Properties info) throws SQLException { - String agentID = info.getProperty("agentID"); + public Connection connect(String url, Properties info){ String dbName = info.getProperty("agentDBName"); if(dbName == null){ dbName = url.split(":")[1]; info.setProperty("agentDBName", dbName); } - MyConnection myConn = (MyConnection) ProxyFactory.getProxy(MyConnection.class, info); + ServiceConnection myConn = (ServiceConnection) ProxyFactory.getProxy(ServiceConnection.class, info); myConn.setInfo(info); return myConn; } @@ -66,24 +64,16 @@ RPC实体类包含如下信息: @Data @Accessors(chain = true) public class RpcRequest { - // Marks whether the method delivered need loopback data - private boolean reply; - // Marks whether the method will create an instance requeired to be cached. - private boolean binding; private String ID; - private String IDtoInvoke; - private Class ServiceClass; - private String MethodName; + private String IDToInvoke; + private String serviceClassName; + private String methodName; private Object[] args; private Class[] argTypes; } ``` -在`Agent`收到`Request`的时候,会按照报文要求对方法进行调用,某些创建的实例会被缓存,以便之后调用。在本项目中,这些实例的类是: - -``` -Drive( MyDriver ), Connection( MyConnection ), Statement( MyStatement ), PreparedStatement( MyPreparedStatement ), ResultSet( MyResult ) -``` +在`Agent`收到`Request`的时候,会按照报文要求对方法进行调用,某些创建的实例会被缓存,以便之后调用。在本项目中,这些类将被注解标识: ```java public Object invokeAsRequest(RpcRequest rpcRequest, BeanCache beanCache) { @@ -292,9 +282,9 @@ public Object intercept(Object o, Method method, Object[] objects, MethodProxy m Object returnObj = methodProxy.invokeSuper(o, objects); - // If the return instance is corresponding with another instance in agent, set the binding ID. + // If the return instance is corresponding with another instance in agent, set the binding ID. if (InterceptorUtils.isInBindList(returnObj)){ - InterceptorUtils.setInvokeHelper(returnObj, "setID", rpcRequest.getID()); + InterceptorUtils.setBindID(returnObj, rpcRequest.getID()); } ``` diff --git a/agent/pom.xml b/agent/pom.xml index a25dd04..3bc2f4b 100644 --- a/agent/pom.xml +++ b/agent/pom.xml @@ -71,9 +71,7 @@ org.hsqldb hsqldb - debug 2.5.2 - test @@ -93,9 +91,54 @@ 1.9.0 test - - - + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + package + + copy-dependencies + + + ${project.build.directory}/lib + false + false + true + + + + + + maven-assembly-plugin + + + + com.fanruan.ChaosTest + + + . + + + + jar-with-dependencies + + + + + make-assembly + package + + single + + + + + + \ No newline at end of file diff --git a/agent/src/main/java/com/fanruan/ChaosTest.java b/agent/src/main/java/com/fanruan/ChaosTest.java new file mode 100644 index 0000000..18f60f7 --- /dev/null +++ b/agent/src/main/java/com/fanruan/ChaosTest.java @@ -0,0 +1,35 @@ +package com.fanruan; + +import com.fanruan.handler.Dispatcher; +import com.fanruan.utils.DBProperties; +import io.socket.client.Socket; + +/** + * @author Yichen Dai + * @date 2022/9/20 9:34 + */ +public class ChaosTest { + public static void main(String[] args){ + try{ + testStart(); + }catch(Exception e){ + e.printStackTrace(); + } + + } + + static void testStart() throws ClassNotFoundException { + Class.forName(DBProperties.HSQL[1]); + String[][] DBs = new String[][]{ + DBProperties.HSQL, + }; + + new AgentStarter(DBs); + + Socket mainSocket = Dispatcher.CACHE.getSocket("/"); + mainSocket.connect(); + + Socket socket = Dispatcher.CACHE.getSocket(DBProperties.HSQL[0]); + socket.connect(); + } +} diff --git a/agent/src/main/resources/socket.properties b/agent/src/main/resources/socket.properties index 79337ee..4bdb89f 100644 --- a/agent/src/main/resources/socket.properties +++ b/agent/src/main/resources/socket.properties @@ -1,11 +1,11 @@ # 服务器地址 -uri = http://127.0.0.1:10246 +uri = http://192.168.2.130:10246 # 代理编号 agentID = 1001 # 重连尝试次数 -reconnectionAttempts = 1 +reconnectionAttempts = 5 # 重连间隔事件 reconnectionDelay = 1000 diff --git a/service/pom.xml b/service/pom.xml index c963489..ed2b634 100644 --- a/service/pom.xml +++ b/service/pom.xml @@ -40,7 +40,6 @@ org.apache.logging.log4j log4j-slf4j-impl 2.17.2 - test @@ -86,6 +85,60 @@ test - + + + org.hsqldb + hsqldb + 2.5.2 + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + package + + copy-dependencies + + + ${project.build.directory}/lib + false + false + true + + + + + + maven-assembly-plugin + + + + com.fanruan.ChaosTest + + + . + + + + jar-with-dependencies + + + + + make-assembly + package + + single + + + + + + \ No newline at end of file diff --git a/service/src/main/java/com/fanruan/ChaosTest.java b/service/src/main/java/com/fanruan/ChaosTest.java new file mode 100644 index 0000000..97bfc53 --- /dev/null +++ b/service/src/main/java/com/fanruan/ChaosTest.java @@ -0,0 +1,70 @@ +package com.fanruan; + + +import com.fanruan.cache.ClientCache; +import com.fanruan.proxy.ProxyFactory; +import com.fanruan.service.jdbc.driver.ServiceDriver; +import com.fanruan.utils.DBProperties; +import org.hsqldb.jdbc.JDBCBlob; + +import java.io.IOException; +import java.sql.*; +import java.util.Properties; + +/** + * @author Yichen Dai + * @date 2022/9/20 9:36 + */ +public class ChaosTest { + static final Properties info = new Properties(); + + static final String[][] dbNameAndDriver = new String[][]{ + DBProperties.HSQL + }; + + static final ServerStater server = new ServerStater(dbNameAndDriver); + + static { + info.setProperty("user", "sa"); + info.setProperty("password", ""); + info.setProperty("agentID", "1001"); + info.setProperty("agentDBName", DBProperties.HSQL[0]); + } + + static void openSocket(){ + while(ClientCache.getClient( + info.getProperty("agentID"), + info.getProperty("agentDBName")) + == null){ + } + } + + public static void main(String[] args) throws SQLException, IOException, InterruptedException { + openSocket(); + Connection connection = getConnection(); + Statement statement = connection.createStatement(); + + statement.execute("create table TestLargeBlob" + + " (testBlob BLOB(10240));" + ); + + Thread.sleep(60000); + + PreparedStatement prepareStatement = connection.prepareStatement("INSERT INTO TestLargeBlob VALUES(?);"); + byte[] chuck = new byte[10240]; + + Blob blob1 = new JDBCBlob(chuck); + prepareStatement.setBlob(1, blob1); + prepareStatement.executeUpdate(); + + ResultSet resultSet = statement.executeQuery("SELECT * FROM TestLargeBlob"); + resultSet.next(); + } + + public static Connection getConnection() throws SQLException { + Driver driver = (ServiceDriver) ProxyFactory.getProxy(ServiceDriver.class, null); + Connection conn = driver.connect("jdbc:hsqldb:mem:test;sql.syntax_mys=true", info); + return conn; + } + +} diff --git a/service/src/main/java/com/fanruan/pojo/message/RpcRequest.java b/service/src/main/java/com/fanruan/pojo/message/RpcRequest.java index ab38997..67502d9 100644 --- a/service/src/main/java/com/fanruan/pojo/message/RpcRequest.java +++ b/service/src/main/java/com/fanruan/pojo/message/RpcRequest.java @@ -9,11 +9,6 @@ import lombok.experimental.Accessors; @Data @Accessors(chain = true) public class RpcRequest { - /** - * Marks whether the method will create an instance requeired to be cached. - * In the project, they are Drive( MyDriver ), Connection( MyConnection ), Statement( MyStatement ), - * PreparedStatement( MyPreparedStatement ), ResultSet( MyResult ). - */ private String ID; private String IDToInvoke; private String serviceClassName; diff --git a/service/src/main/resources/socketIO.properties b/service/src/main/resources/socketIO.properties index 9cc9ca8..df5ac6a 100644 --- a/service/src/main/resources/socketIO.properties +++ b/service/src/main/resources/socketIO.properties @@ -3,7 +3,7 @@ #============================================================================ # host在本地测试可以设置为localhost或者本机IP,在Linux服务器跑可换成服务器IP #监听的ip -host = 127.0.0.1 +host = 0.0.0.0 #监听端口 port = 10246 # 设置最大每帧处理数据的长度,防止他人利用大数据来攻击服务器