mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-20 05:27:48 +00:00
test[gateway]: 简化网关转发测试用例
This commit is contained in:
+13
-11
@@ -1,6 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2020 The zfoo Authors
|
||||
*
|
||||
* 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
|
||||
*
|
||||
@@ -11,15 +10,18 @@
|
||||
* 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.dispatcher.model.anno.PacketReceiver;
|
||||
import com.zfoo.net.packet.gateway.CM_GatewayProvider;
|
||||
import com.zfoo.net.packet.gateway.SM_GatewayProvider;
|
||||
import com.zfoo.net.packet.gateway.GatewayToProviderRequest;
|
||||
import com.zfoo.net.packet.gateway.GatewayToProviderResponse;
|
||||
import com.zfoo.net.packet.model.GatewayPacketAttachment;
|
||||
import com.zfoo.net.session.model.Session;
|
||||
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;
|
||||
|
||||
/**
|
||||
@@ -29,15 +31,15 @@ import org.springframework.stereotype.Component;
|
||||
@Component
|
||||
public class GatewayProviderController {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(GatewayProviderController.class);
|
||||
|
||||
@PacketReceiver
|
||||
public void atCM_GatewayProvider(Session session, CM_GatewayProvider cm, GatewayPacketAttachment gatewayAttachment) {
|
||||
System.out.println("服务器收到消息:" + JsonUtils.object2String(cm));
|
||||
public void atGatewayToProviderRequest(Session session, GatewayToProviderRequest request, GatewayPacketAttachment gatewayAttachment) {
|
||||
logger.info("provider receive [packet:{}] from client", JsonUtils.object2String(request));
|
||||
|
||||
var sm = new SM_GatewayProvider();
|
||||
sm.setA(NetContext.getConfigManager().getLocalConfig().toLocalRegisterVO().toString());
|
||||
sm.setB(cm.getB());
|
||||
var response = new GatewayToProviderResponse();
|
||||
response.setMessage(StringUtils.format("Hello, this is the [provider:{}] response!", NetContext.getConfigManager().getLocalConfig().toLocalRegisterVO().toString()));
|
||||
|
||||
System.out.println("服务器返回消息:" + JsonUtils.object2String(sm));
|
||||
NetContext.getDispatcher().send(session, sm, gatewayAttachment);
|
||||
NetContext.getDispatcher().send(session, response, gatewayAttachment);
|
||||
}
|
||||
}
|
||||
@@ -15,17 +15,20 @@ package com.zfoo.net.core.gateway;
|
||||
|
||||
import com.zfoo.net.NetContext;
|
||||
import com.zfoo.net.core.tcp.TcpClient;
|
||||
import com.zfoo.net.packet.gateway.CM_GatewayProvider;
|
||||
import com.zfoo.net.packet.gateway.SM_GatewayProvider;
|
||||
import com.zfoo.net.packet.gateway.GatewayToProviderRequest;
|
||||
import com.zfoo.net.packet.gateway.GatewayToProviderResponse;
|
||||
import com.zfoo.net.session.SessionUtils;
|
||||
import com.zfoo.protocol.util.JsonUtils;
|
||||
import com.zfoo.util.ThreadUtils;
|
||||
import com.zfoo.util.net.HostAndPort;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
@@ -34,6 +37,8 @@ import java.util.concurrent.Executors;
|
||||
@Ignore
|
||||
public class GatewayTest {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(GatewayTest.class);
|
||||
|
||||
/**
|
||||
* 启动zookeeper,依次运行下面的测试方法启动服务提供者,网关,然后运行clientTest,消息会通过网关转发到服务提供者
|
||||
*/
|
||||
@@ -70,27 +75,32 @@ public class GatewayTest {
|
||||
@Test
|
||||
public void clientTest() {
|
||||
var context = new ClassPathXmlApplicationContext("gateway/gateway_client_config.xml");
|
||||
var executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
|
||||
SessionUtils.printSessionInfo();
|
||||
|
||||
var client = new TcpClient(HostAndPort.valueOf(NetContext.getConfigManager().getLocalConfig().getHostConfig().getAddressMap().get("server0")));
|
||||
var session = client.start();
|
||||
|
||||
for (int i = 0; i <= 100; i++) {
|
||||
int finalI = i;
|
||||
var executorSize = Runtime.getRuntime().availableProcessors() * 2;
|
||||
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 cm = new CM_GatewayProvider();
|
||||
cm.setA(NetContext.getConfigManager().getLocalConfig().toLocalRegisterVO().toString());
|
||||
cm.setB(finalI);
|
||||
try {
|
||||
System.out.println("客户端发送消息:" + JsonUtils.object2String(cm));
|
||||
var sm = NetContext.getDispatcher().syncAsk(session, cm, SM_GatewayProvider.class, null).packet();
|
||||
System.out.println("客户端收到消息:" + JsonUtils.object2String(sm));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
for (int j = 0; j < 10000; j++) {
|
||||
try {
|
||||
var response = NetContext.getDispatcher().syncAsk(session, request, GatewayToProviderResponse.class, null).packet();
|
||||
logger.info("客户端请求[{}]收到消息[{}]", atomicInteger.incrementAndGet(), JsonUtils.object2String(response));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
executor.execute(thread);
|
||||
}
|
||||
|
||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
|
||||
@@ -97,6 +97,7 @@ public class TcpClientTest {
|
||||
|
||||
var executorSize = Runtime.getRuntime().availableProcessors() * 2;
|
||||
var executor = Executors.newFixedThreadPool(executorSize);
|
||||
var atomicInteger = new AtomicInteger(0);
|
||||
|
||||
for (int i = 0; i < executorSize; i++) {
|
||||
var thread = new Thread(() -> {
|
||||
@@ -106,7 +107,7 @@ public class TcpClientTest {
|
||||
|
||||
var answer = NetContext.getDispatcher().asyncAsk(session1, ask, AsyncMess0Answer.class, null);
|
||||
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.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
/**
|
||||
@@ -33,9 +32,6 @@ import java.util.concurrent.Executors;
|
||||
@Ignore
|
||||
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
|
||||
* <p>
|
||||
@@ -43,6 +39,7 @@ public class TcpServerTest {
|
||||
*/
|
||||
@Test
|
||||
public void startServer0() {
|
||||
var context = new ClassPathXmlApplicationContext("config.xml");
|
||||
SessionUtils.printSessionInfo();
|
||||
|
||||
var server0 = new TcpServer(HostAndPort.valueOf(NetContext.getConfigManager().getLocalConfig().getHostConfig().getAddressMap().get("server0")));
|
||||
@@ -52,16 +49,15 @@ public class TcpServerTest {
|
||||
|
||||
@Test
|
||||
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")));
|
||||
server1.start();
|
||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
private void connectServer0() {
|
||||
// 连接server0
|
||||
var executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
|
||||
executor.execute(() -> {
|
||||
while (true) {
|
||||
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
|
||||
* @version 3.0
|
||||
*/
|
||||
public class CM_GatewayProvider implements IPacket {
|
||||
public class GatewayToProviderRequest implements IPacket {
|
||||
|
||||
public static final transient short PROTOCOL_ID = 4102;
|
||||
|
||||
private String a;
|
||||
|
||||
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;
|
||||
}
|
||||
private String message;
|
||||
|
||||
@Override
|
||||
public short protocolId() {
|
||||
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
|
||||
* @version 3.0
|
||||
*/
|
||||
public class SM_GatewayProvider implements IPacket {
|
||||
public class GatewayToProviderResponse implements IPacket {
|
||||
|
||||
public static final transient short PROTOCOL_ID = 4103;
|
||||
|
||||
private String a;
|
||||
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;
|
||||
}
|
||||
private String message;
|
||||
|
||||
@Override
|
||||
public short protocolId() {
|
||||
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">
|
||||
<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="4102" location="com.zfoo.net.packet.gateway.CM_GatewayProvider" enhance="false"/>
|
||||
<protocol id="4103" location="com.zfoo.net.packet.gateway.SM_GatewayProvider" enhance="false"/>
|
||||
<protocol id="4102" location="com.zfoo.net.packet.gateway.GatewayToProviderRequest" enhance="false"/>
|
||||
<protocol id="4103" location="com.zfoo.net.packet.gateway.GatewayToProviderResponse" enhance="false"/>
|
||||
</module>
|
||||
|
||||
</protocols>
|
||||
|
||||
Reference in New Issue
Block a user