Browse Source

ChaosTest succeed

chaosTest
yichen 2 years ago
parent
commit
49f0ccdeaf
  1. 28
      README.md
  2. 53
      agent/pom.xml
  3. 35
      agent/src/main/java/com/fanruan/ChaosTest.java
  4. 4
      agent/src/main/resources/socket.properties
  5. 57
      service/pom.xml
  6. 70
      service/src/main/java/com/fanruan/ChaosTest.java
  7. 5
      service/src/main/java/com/fanruan/pojo/message/RpcRequest.java
  8. 2
      service/src/main/resources/socketIO.properties

28
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());
}
```

53
agent/pom.xml

@ -71,9 +71,7 @@
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<classifier>debug</classifier>
<version>2.5.2</version>
<scope>test</scope>
</dependency>
@ -93,9 +91,54 @@
<version>1.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.fanruan.ChaosTest</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

35
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();
}
}

4
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

57
service/pom.xml

@ -40,7 +40,6 @@
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.17.2</version>
<scope>test</scope>
</dependency>
<dependency>
@ -86,6 +85,60 @@
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hsqldb/hsqldb -->
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.5.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.fanruan.ChaosTest</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

70
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;
}
}

5
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;

2
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
# 设置最大每帧处理数据的长度,防止他人利用大数据来攻击服务器

Loading…
Cancel
Save