mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-16 08:15:41 +00:00
test[gateway]: 简化网关转发测试用例
This commit is contained in:
1 parent
b9d6f825f0
commit
de87bd7a21
7 files changed
+71
-76
No files matched your search
+13
-11
@@ -1,6 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2020 The zfoo Authors
|
* Copyright (C) 2020 The zfoo Authors
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
|
||||||
* in compliance with the License. You may obtain a copy of the License at
|
* in compliance with the License. You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
@@ -11,15 +10,18 @@
|
|||||||
* See the License for the specific language governing permissions and limitations under the License.
|
* See the License for the specific language governing permissions and limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.zfoo.net.core.gateway.controller;
|
package com.zfoo.net.core.gateway;
|
||||||
|
|
||||||
import com.zfoo.net.NetContext;
|
import com.zfoo.net.NetContext;
|
||||||
import com.zfoo.net.dispatcher.model.anno.PacketReceiver;
|
import com.zfoo.net.dispatcher.model.anno.PacketReceiver;
|
||||||
import com.zfoo.net.packet.gateway.CM_GatewayProvider;
|
import com.zfoo.net.packet.gateway.GatewayToProviderRequest;
|
||||||
import com.zfoo.net.packet.gateway.SM_GatewayProvider;
|
import com.zfoo.net.packet.gateway.GatewayToProviderResponse;
|
||||||
import com.zfoo.net.packet.model.GatewayPacketAttachment;
|
import com.zfoo.net.packet.model.GatewayPacketAttachment;
|
||||||
import com.zfoo.net.session.model.Session;
|
import com.zfoo.net.session.model.Session;
|
||||||
import com.zfoo.protocol.util.JsonUtils;
|
import com.zfoo.protocol.util.JsonUtils;
|
||||||
|
import com.zfoo.protocol.util.StringUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -29,15 +31,15 @@ import org.springframework.stereotype.Component;
|
|||||||
@Component
|
@Component
|
||||||
public class GatewayProviderController {
|
public class GatewayProviderController {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(GatewayProviderController.class);
|
||||||
|
|
||||||
@PacketReceiver
|
@PacketReceiver
|
||||||
public void atCM_GatewayProvider(Session session, CM_GatewayProvider cm, GatewayPacketAttachment gatewayAttachment) {
|
public void atGatewayToProviderRequest(Session session, GatewayToProviderRequest request, GatewayPacketAttachment gatewayAttachment) {
|
||||||
System.out.println("服务器收到消息:" + JsonUtils.object2String(cm));
|
logger.info("provider receive [packet:{}] from client", JsonUtils.object2String(request));
|
||||||
|
|
||||||
var sm = new SM_GatewayProvider();
|
var response = new GatewayToProviderResponse();
|
||||||
sm.setA(NetContext.getConfigManager().getLocalConfig().toLocalRegisterVO().toString());
|
response.setMessage(StringUtils.format("Hello, this is the [provider:{}] response!", NetContext.getConfigManager().getLocalConfig().toLocalRegisterVO().toString()));
|
||||||
sm.setB(cm.getB());
|
|
||||||
|
|
||||||
System.out.println("服务器返回消息:" + JsonUtils.object2String(sm));
|
NetContext.getDispatcher().send(session, response, gatewayAttachment);
|
||||||
NetContext.getDispatcher().send(session, sm, gatewayAttachment);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -15,17 +15,20 @@ package com.zfoo.net.core.gateway;
|
|||||||
|
|
||||||
import com.zfoo.net.NetContext;
|
import com.zfoo.net.NetContext;
|
||||||
import com.zfoo.net.core.tcp.TcpClient;
|
import com.zfoo.net.core.tcp.TcpClient;
|
||||||
import com.zfoo.net.packet.gateway.CM_GatewayProvider;
|
import com.zfoo.net.packet.gateway.GatewayToProviderRequest;
|
||||||
import com.zfoo.net.packet.gateway.SM_GatewayProvider;
|
import com.zfoo.net.packet.gateway.GatewayToProviderResponse;
|
||||||
import com.zfoo.net.session.SessionUtils;
|
import com.zfoo.net.session.SessionUtils;
|
||||||
import com.zfoo.protocol.util.JsonUtils;
|
import com.zfoo.protocol.util.JsonUtils;
|
||||||
import com.zfoo.util.ThreadUtils;
|
import com.zfoo.util.ThreadUtils;
|
||||||
import com.zfoo.util.net.HostAndPort;
|
import com.zfoo.util.net.HostAndPort;
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author jaysunxiao
|
* @author jaysunxiao
|
||||||
@@ -34,6 +37,8 @@ import java.util.concurrent.Executors;
|
|||||||
@Ignore
|
@Ignore
|
||||||
public class GatewayTest {
|
public class GatewayTest {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(GatewayTest.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 启动zookeeper,依次运行下面的测试方法启动服务提供者,网关,然后运行clientTest,消息会通过网关转发到服务提供者
|
* 启动zookeeper,依次运行下面的测试方法启动服务提供者,网关,然后运行clientTest,消息会通过网关转发到服务提供者
|
||||||
*/
|
*/
|
||||||
@@ -70,27 +75,32 @@ public class GatewayTest {
|
|||||||
@Test
|
@Test
|
||||||
public void clientTest() {
|
public void clientTest() {
|
||||||
var context = new ClassPathXmlApplicationContext("gateway/gateway_client_config.xml");
|
var context = new ClassPathXmlApplicationContext("gateway/gateway_client_config.xml");
|
||||||
var executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
|
|
||||||
SessionUtils.printSessionInfo();
|
SessionUtils.printSessionInfo();
|
||||||
|
|
||||||
var client = new TcpClient(HostAndPort.valueOf(NetContext.getConfigManager().getLocalConfig().getHostConfig().getAddressMap().get("server0")));
|
var client = new TcpClient(HostAndPort.valueOf(NetContext.getConfigManager().getLocalConfig().getHostConfig().getAddressMap().get("server0")));
|
||||||
var session = client.start();
|
var session = client.start();
|
||||||
|
|
||||||
for (int i = 0; i <= 100; i++) {
|
var executorSize = Runtime.getRuntime().availableProcessors() * 2;
|
||||||
int finalI = i;
|
var executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
|
||||||
|
|
||||||
|
var request = new GatewayToProviderRequest();
|
||||||
|
request.setMessage(NetContext.getConfigManager().getLocalConfig().toLocalRegisterVO().toString());
|
||||||
|
var atomicInteger = new AtomicInteger(0);
|
||||||
|
|
||||||
|
for (int i = 0; i < executorSize; i++) {
|
||||||
var thread = new Thread(() -> {
|
var thread = new Thread(() -> {
|
||||||
var cm = new CM_GatewayProvider();
|
for (int j = 0; j < 10000; j++) {
|
||||||
cm.setA(NetContext.getConfigManager().getLocalConfig().toLocalRegisterVO().toString());
|
try {
|
||||||
cm.setB(finalI);
|
var response = NetContext.getDispatcher().syncAsk(session, request, GatewayToProviderResponse.class, null).packet();
|
||||||
try {
|
logger.info("客户端请求[{}]收到消息[{}]", atomicInteger.incrementAndGet(), JsonUtils.object2String(response));
|
||||||
System.out.println("客户端发送消息:" + JsonUtils.object2String(cm));
|
} catch (Exception e) {
|
||||||
var sm = NetContext.getDispatcher().syncAsk(session, cm, SM_GatewayProvider.class, null).packet();
|
e.printStackTrace();
|
||||||
System.out.println("客户端收到消息:" + JsonUtils.object2String(sm));
|
}
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
executor.execute(thread);
|
executor.execute(thread);
|
||||||
}
|
}
|
||||||
|
|
||||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -97,6 +97,7 @@ public class TcpClientTest {
|
|||||||
|
|
||||||
var executorSize = Runtime.getRuntime().availableProcessors() * 2;
|
var executorSize = Runtime.getRuntime().availableProcessors() * 2;
|
||||||
var executor = Executors.newFixedThreadPool(executorSize);
|
var executor = Executors.newFixedThreadPool(executorSize);
|
||||||
|
var atomicInteger = new AtomicInteger(0);
|
||||||
|
|
||||||
for (int i = 0; i < executorSize; i++) {
|
for (int i = 0; i < executorSize; i++) {
|
||||||
var thread = new Thread(() -> {
|
var thread = new Thread(() -> {
|
||||||
@@ -106,7 +107,7 @@ public class TcpClientTest {
|
|||||||
|
|
||||||
var answer = NetContext.getDispatcher().asyncAsk(session1, ask, AsyncMess0Answer.class, null);
|
var answer = NetContext.getDispatcher().asyncAsk(session1, ask, AsyncMess0Answer.class, null);
|
||||||
answer.whenComplete(sm -> {
|
answer.whenComplete(sm -> {
|
||||||
logger.info("异步请求收到结果[{}]", JsonUtils.object2String(answer));
|
logger.info("异步请求[{}]收到结果[{}]", atomicInteger.incrementAndGet(), JsonUtils.object2String(answer));
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ import org.junit.Ignore;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||||
|
|
||||||
import java.util.concurrent.ExecutorService;
|
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -33,9 +32,6 @@ import java.util.concurrent.Executors;
|
|||||||
@Ignore
|
@Ignore
|
||||||
public class TcpServerTest {
|
public class TcpServerTest {
|
||||||
|
|
||||||
private static final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
|
|
||||||
private static final ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 单机服务器教程,启动成功过后在com.zfoo.net.core.tcp.client.TcpClientTest中运行startClientTest
|
* 单机服务器教程,启动成功过后在com.zfoo.net.core.tcp.client.TcpClientTest中运行startClientTest
|
||||||
* <p>
|
* <p>
|
||||||
@@ -43,6 +39,7 @@ public class TcpServerTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void startServer0() {
|
public void startServer0() {
|
||||||
|
var context = new ClassPathXmlApplicationContext("config.xml");
|
||||||
SessionUtils.printSessionInfo();
|
SessionUtils.printSessionInfo();
|
||||||
|
|
||||||
var server0 = new TcpServer(HostAndPort.valueOf(NetContext.getConfigManager().getLocalConfig().getHostConfig().getAddressMap().get("server0")));
|
var server0 = new TcpServer(HostAndPort.valueOf(NetContext.getConfigManager().getLocalConfig().getHostConfig().getAddressMap().get("server0")));
|
||||||
@@ -52,16 +49,15 @@ public class TcpServerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void startServer1() {
|
public void startServer1() {
|
||||||
SessionUtils.printSessionInfo();
|
var context = new ClassPathXmlApplicationContext("config.xml");
|
||||||
|
|
||||||
connectServer0();
|
SessionUtils.printSessionInfo();
|
||||||
|
|
||||||
var server1 = new TcpServer(HostAndPort.valueOf(NetContext.getConfigManager().getLocalConfig().getHostConfig().getAddressMap().get("server1")));
|
var server1 = new TcpServer(HostAndPort.valueOf(NetContext.getConfigManager().getLocalConfig().getHostConfig().getAddressMap().get("server1")));
|
||||||
server1.start();
|
server1.start();
|
||||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void connectServer0() {
|
// 连接server0
|
||||||
|
var executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
|
||||||
executor.execute(() -> {
|
executor.execute(() -> {
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
@@ -75,6 +71,9 @@ public class TcpServerTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
+11
-20
@@ -19,32 +19,23 @@ import com.zfoo.protocol.IPacket;
|
|||||||
* @author jaysunxiao
|
* @author jaysunxiao
|
||||||
* @version 3.0
|
* @version 3.0
|
||||||
*/
|
*/
|
||||||
public class CM_GatewayProvider implements IPacket {
|
public class GatewayToProviderRequest implements IPacket {
|
||||||
|
|
||||||
public static final transient short PROTOCOL_ID = 4102;
|
public static final transient short PROTOCOL_ID = 4102;
|
||||||
|
|
||||||
private String a;
|
private String message;
|
||||||
|
|
||||||
private int b;
|
|
||||||
|
|
||||||
public String getA() {
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setA(String a) {
|
|
||||||
this.a = a;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getB() {
|
|
||||||
return b;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setB(int b) {
|
|
||||||
this.b = b;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public short protocolId() {
|
public short protocolId() {
|
||||||
return PROTOCOL_ID;
|
return PROTOCOL_ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getMessage() {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMessage(String message) {
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
+11
-19
@@ -19,31 +19,23 @@ import com.zfoo.protocol.IPacket;
|
|||||||
* @author jaysunxiao
|
* @author jaysunxiao
|
||||||
* @version 3.0
|
* @version 3.0
|
||||||
*/
|
*/
|
||||||
public class SM_GatewayProvider implements IPacket {
|
public class GatewayToProviderResponse implements IPacket {
|
||||||
|
|
||||||
public static final transient short PROTOCOL_ID = 4103;
|
public static final transient short PROTOCOL_ID = 4103;
|
||||||
|
|
||||||
private String a;
|
private String message;
|
||||||
private int b;
|
|
||||||
|
|
||||||
public String getA() {
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setA(String a) {
|
|
||||||
this.a = a;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getB() {
|
|
||||||
return b;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setB(int b) {
|
|
||||||
this.b = b;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public short protocolId() {
|
public short protocolId() {
|
||||||
return PROTOCOL_ID;
|
return PROTOCOL_ID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getMessage() {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMessage(String message) {
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -75,8 +75,8 @@
|
|||||||
<module id="5" name="providerTest" minId="3000" maxId="8000" version="1.0.0">
|
<module id="5" name="providerTest" minId="3000" maxId="8000" version="1.0.0">
|
||||||
<protocol id="4100" location="com.zfoo.net.packet.provider.CM_Provider" enhance="false"/>
|
<protocol id="4100" location="com.zfoo.net.packet.provider.CM_Provider" enhance="false"/>
|
||||||
<protocol id="4101" location="com.zfoo.net.packet.provider.SM_Provider" enhance="false"/>
|
<protocol id="4101" location="com.zfoo.net.packet.provider.SM_Provider" enhance="false"/>
|
||||||
<protocol id="4102" location="com.zfoo.net.packet.gateway.CM_GatewayProvider" enhance="false"/>
|
<protocol id="4102" location="com.zfoo.net.packet.gateway.GatewayToProviderRequest" enhance="false"/>
|
||||||
<protocol id="4103" location="com.zfoo.net.packet.gateway.SM_GatewayProvider" enhance="false"/>
|
<protocol id="4103" location="com.zfoo.net.packet.gateway.GatewayToProviderResponse" enhance="false"/>
|
||||||
</module>
|
</module>
|
||||||
|
|
||||||
</protocols>
|
</protocols>
|
||||||
Reference in new issue
Block a user