mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-06-04 04:12:34 +00:00
test[provider]: 简化服务提供者测试用例
This commit is contained in:
@@ -84,7 +84,7 @@ public class GatewayTest {
|
||||
var executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
|
||||
|
||||
var request = new GatewayToProviderRequest();
|
||||
request.setMessage(NetContext.getConfigManager().getLocalConfig().toLocalRegisterVO().toString());
|
||||
request.setMessage("Hello, this is the client!");
|
||||
var atomicInteger = new AtomicInteger(0);
|
||||
|
||||
for (int i = 0; i < executorSize; i++) {
|
||||
|
||||
+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,14 +10,17 @@
|
||||
* See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package com.zfoo.net.core.provider.controller;
|
||||
package com.zfoo.net.core.provider;
|
||||
|
||||
import com.zfoo.net.NetContext;
|
||||
import com.zfoo.net.dispatcher.model.anno.PacketReceiver;
|
||||
import com.zfoo.net.packet.provider.CM_Provider;
|
||||
import com.zfoo.net.packet.provider.SM_Provider;
|
||||
import com.zfoo.net.packet.provider.ProviderMessAnswer;
|
||||
import com.zfoo.net.packet.provider.ProviderMessAsk;
|
||||
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;
|
||||
|
||||
/**
|
||||
@@ -28,15 +30,15 @@ import org.springframework.stereotype.Component;
|
||||
@Component
|
||||
public class ProviderController {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ProviderController.class);
|
||||
|
||||
@PacketReceiver
|
||||
public void atCM_Provider(Session session, CM_Provider cm) {
|
||||
System.out.println("服务器收到消息:" + JsonUtils.object2String(cm));
|
||||
public void atProviderMessAsk(Session session, ProviderMessAsk ask) {
|
||||
logger.info("provider receive [packet:{}] from consumer", JsonUtils.object2String(ask));
|
||||
|
||||
var sm = new SM_Provider();
|
||||
sm.setA(NetContext.getConfigManager().getLocalConfig().toLocalRegisterVO().toString());
|
||||
sm.setB(cm.getB());
|
||||
var response = new ProviderMessAnswer();
|
||||
response.setMessage(StringUtils.format("Hello, this is the [provider:{}] answer!", NetContext.getConfigManager().getLocalConfig().toLocalRegisterVO().toString()));
|
||||
|
||||
System.out.println("服务器返回消息:" + JsonUtils.object2String(sm));
|
||||
NetContext.getDispatcher().send(session, sm);
|
||||
NetContext.getDispatcher().send(session, response);
|
||||
}
|
||||
}
|
||||
@@ -14,15 +14,19 @@
|
||||
package com.zfoo.net.core.provider;
|
||||
|
||||
import com.zfoo.net.NetContext;
|
||||
import com.zfoo.net.packet.provider.CM_Provider;
|
||||
import com.zfoo.net.packet.provider.SM_Provider;
|
||||
import com.zfoo.net.packet.provider.ProviderMessAnswer;
|
||||
import com.zfoo.net.packet.provider.ProviderMessAsk;
|
||||
import com.zfoo.net.session.SessionUtils;
|
||||
import com.zfoo.protocol.util.JsonUtils;
|
||||
import com.zfoo.util.ThreadUtils;
|
||||
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.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
@@ -30,6 +34,8 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
@Ignore
|
||||
public class ProviderTest {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ProviderTest.class);
|
||||
|
||||
/**
|
||||
* RPC教程:
|
||||
* 1.首先必须保证启动zookeeper
|
||||
@@ -62,21 +68,16 @@ public class ProviderTest {
|
||||
* 随机消费,同步请求的方式
|
||||
*/
|
||||
@Test
|
||||
public void startSyncRandomConsumer() {
|
||||
public void startSyncRandomConsumer() throws Exception {
|
||||
var context = new ClassPathXmlApplicationContext("provider/consumer_random_config.xml");
|
||||
SessionUtils.printSessionInfo();
|
||||
|
||||
var ask = new ProviderMessAsk();
|
||||
ask.setMessage("Hello, this is the consumer!");
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
ThreadUtils.sleep(3000);
|
||||
var cm = new CM_Provider();
|
||||
cm.setA(NetContext.getConfigManager().getLocalConfig().toLocalRegisterVO().toString());
|
||||
try {
|
||||
System.out.println("客户端发送消息:" + JsonUtils.object2String(cm));
|
||||
var sm = NetContext.getConsumer().syncAsk(cm, SM_Provider.class, null).packet();
|
||||
System.out.println("客户端收到消息:" + JsonUtils.object2String(sm));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
var response = NetContext.getConsumer().syncAsk(ask, ProviderMessAnswer.class, null).packet();
|
||||
logger.info("消费者请求[{}]收到消息[{}]", i, JsonUtils.object2String(response));
|
||||
}
|
||||
|
||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||
@@ -90,13 +91,14 @@ public class ProviderTest {
|
||||
var context = new ClassPathXmlApplicationContext("provider/consumer_random_config.xml");
|
||||
SessionUtils.printSessionInfo();
|
||||
|
||||
var ask = new ProviderMessAsk();
|
||||
ask.setMessage("Hello, this is the consumer!");
|
||||
var atomicInteger = new AtomicInteger(0);
|
||||
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
ThreadUtils.sleep(3000);
|
||||
var cm = new CM_Provider();
|
||||
cm.setA(NetContext.getConfigManager().getLocalConfig().toLocalRegisterVO().toString());
|
||||
System.out.println("客户端发送消息:" + JsonUtils.object2String(cm));
|
||||
NetContext.getConsumer().asyncAsk(cm, SM_Provider.class, null).whenComplete(sm -> {
|
||||
System.out.println("客户端收到消息:" + JsonUtils.object2String(sm));
|
||||
NetContext.getConsumer().asyncAsk(ask, ProviderMessAnswer.class, null).whenComplete(answer -> {
|
||||
logger.info("消费者请求[{}]收到消息[{}]", atomicInteger.incrementAndGet(), JsonUtils.object2String(answer));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -110,14 +112,15 @@ public class ProviderTest {
|
||||
public void startConsistentSessionConsumer() {
|
||||
var context = new ClassPathXmlApplicationContext("provider/consumer_consistent_session_config.xml");
|
||||
SessionUtils.printSessionInfo();
|
||||
ThreadUtils.sleep(3000);
|
||||
|
||||
var ask = new ProviderMessAsk();
|
||||
ask.setMessage("Hello, this is the consumer!");
|
||||
var atomicInteger = new AtomicInteger(0);
|
||||
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
ThreadUtils.sleep(3000);
|
||||
var cm = new CM_Provider();
|
||||
cm.setA(NetContext.getConfigManager().getLocalConfig().toLocalRegisterVO().toString());
|
||||
System.out.println("客户端发送消息:" + JsonUtils.object2String(cm));
|
||||
NetContext.getConsumer().asyncAsk(cm, SM_Provider.class, 100).whenComplete(sm -> {
|
||||
System.out.println("客户端收到消息:" + JsonUtils.object2String(sm));
|
||||
NetContext.getConsumer().asyncAsk(ask, ProviderMessAnswer.class, 100).whenComplete(answer -> {
|
||||
logger.info("消费者请求[{}]收到消息[{}]", atomicInteger.incrementAndGet(), JsonUtils.object2String(answer));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -131,13 +134,15 @@ public class ProviderTest {
|
||||
public void startShortestTimeConsumer() {
|
||||
var context = new ClassPathXmlApplicationContext("provider/consumer_shortest_time_config.xml");
|
||||
SessionUtils.printSessionInfo();
|
||||
|
||||
var ask = new ProviderMessAsk();
|
||||
ask.setMessage("Hello, this is the consumer!");
|
||||
var atomicInteger = new AtomicInteger(0);
|
||||
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
ThreadUtils.sleep(3000);
|
||||
var cm = new CM_Provider();
|
||||
cm.setA(NetContext.getConfigManager().getLocalConfig().toLocalRegisterVO().toString());
|
||||
System.out.println("客户端发送消息:" + JsonUtils.object2String(cm));
|
||||
NetContext.getConsumer().asyncAsk(cm, SM_Provider.class, null).whenComplete(sm -> {
|
||||
System.out.println("客户端收到消息:" + JsonUtils.object2String(sm));
|
||||
NetContext.getConsumer().asyncAsk(ask, ProviderMessAnswer.class, null).whenComplete(answer -> {
|
||||
logger.info("消费者请求[{}]收到消息[{}]", atomicInteger.incrementAndGet(), JsonUtils.object2String(answer));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ import com.zfoo.protocol.IPacket;
|
||||
*/
|
||||
public class GatewayToProviderRequest implements IPacket {
|
||||
|
||||
public static final transient short PROTOCOL_ID = 4102;
|
||||
public static final transient short PROTOCOL_ID = 5000;
|
||||
|
||||
private String message;
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ import com.zfoo.protocol.IPacket;
|
||||
*/
|
||||
public class GatewayToProviderResponse implements IPacket {
|
||||
|
||||
public static final transient short PROTOCOL_ID = 4103;
|
||||
public static final transient short PROTOCOL_ID = 5001;
|
||||
|
||||
private String message;
|
||||
|
||||
|
||||
+12
-20
@@ -19,31 +19,23 @@ import com.zfoo.protocol.IPacket;
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
public class SM_Provider implements IPacket {
|
||||
public class ProviderMessAnswer implements IPacket {
|
||||
|
||||
public static final transient short PROTOCOL_ID = 4101;
|
||||
public static final transient short PROTOCOL_ID = 4001;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
+12
-21
@@ -19,32 +19,23 @@ import com.zfoo.protocol.IPacket;
|
||||
* @author jaysunxiao
|
||||
* @version 3.0
|
||||
*/
|
||||
public class CM_Provider implements IPacket {
|
||||
public class ProviderMessAsk implements IPacket {
|
||||
|
||||
public static final transient short PROTOCOL_ID = 4100;
|
||||
public static final transient short PROTOCOL_ID = 4000;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,8 +18,7 @@
|
||||
|
||||
<context:component-scan base-package="com.zfoo"/>
|
||||
|
||||
<net:config id="applicationNameTest" protocol-location="protocol.xml"
|
||||
generate-js-protocol="false" generate-cs-protocol="false">
|
||||
<net:config id="applicationNameTest" protocol-location="protocol.xml">
|
||||
|
||||
<net:host center="direct connect" user="jaysunxiao" password="123456">
|
||||
<net:address name="server0" url="127.0.0.1:9000"/>
|
||||
|
||||
@@ -18,8 +18,7 @@
|
||||
|
||||
<context:component-scan base-package="com.zfoo"/>
|
||||
|
||||
<net:config id="applicationNameTest" protocol-location="protocol.xml"
|
||||
generate-js-protocol="false" generate-cs-protocol="false">
|
||||
<net:config id="applicationNameTest" protocol-location="protocol.xml">
|
||||
|
||||
<net:registry center="zookeeper" user="" password="">
|
||||
<net:address name="firstZookeeper" url="127.0.0.1:2181"/>
|
||||
|
||||
@@ -73,10 +73,11 @@
|
||||
</module>
|
||||
|
||||
<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.GatewayToProviderRequest" enhance="false"/>
|
||||
<protocol id="4103" location="com.zfoo.net.packet.gateway.GatewayToProviderResponse" enhance="false"/>
|
||||
<protocol id="4000" location="com.zfoo.net.packet.provider.ProviderMessAsk" enhance="false"/>
|
||||
<protocol id="4001" location="com.zfoo.net.packet.provider.ProviderMessAnswer" enhance="false"/>
|
||||
|
||||
<protocol id="5000" location="com.zfoo.net.packet.gateway.GatewayToProviderRequest" enhance="false"/>
|
||||
<protocol id="5001" location="com.zfoo.net.packet.gateway.GatewayToProviderResponse" enhance="false"/>
|
||||
</module>
|
||||
|
||||
</protocols>
|
||||
|
||||
@@ -19,14 +19,12 @@
|
||||
|
||||
<context:component-scan base-package="com.zfoo"/>
|
||||
|
||||
<net:config id="applicationNameTest" protocol-location="protocol.xml"
|
||||
generate-js-protocol="false" generate-cs-protocol="false">
|
||||
<net:config id="applicationNameTest" protocol-location="protocol.xml">
|
||||
|
||||
<net:registry center="${registry.center}" user="${registry.user}" password="${registry.password}">
|
||||
<net:address name="${registry.address.name}" url="${registry.address.url}"/>
|
||||
</net:registry>
|
||||
|
||||
|
||||
<net:consumer load-balancer="consistent-hash">
|
||||
<net:module name="providerTest"/>
|
||||
</net:consumer>
|
||||
|
||||
@@ -18,14 +18,12 @@
|
||||
<context:property-placeholder location="classpath:deploy-dev.properties"/>
|
||||
<context:component-scan base-package="com.zfoo"/>
|
||||
|
||||
<net:config id="applicationNameTest" protocol-location="protocol.xml"
|
||||
generate-js-protocol="false" generate-cs-protocol="false">
|
||||
<net:config id="applicationNameTest" protocol-location="protocol.xml">
|
||||
|
||||
<net:registry center="${registry.center}" user="${registry.user}" password="${registry.password}">
|
||||
<net:address name="${registry.address.name}" url="${registry.address.url}"/>
|
||||
</net:registry>
|
||||
|
||||
|
||||
<net:consumer load-balancer="random">
|
||||
<net:module name="providerTest"/>
|
||||
</net:consumer>
|
||||
|
||||
@@ -18,8 +18,7 @@
|
||||
<context:property-placeholder location="classpath:deploy-dev.properties"/>
|
||||
<context:component-scan base-package="com.zfoo"/>
|
||||
|
||||
<net:config id="applicationNameTest" protocol-location="protocol.xml"
|
||||
generate-js-protocol="false" generate-cs-protocol="false">
|
||||
<net:config id="applicationNameTest" protocol-location="protocol.xml">
|
||||
|
||||
<net:registry center="${registry.center}" user="${registry.user}" password="${registry.password}">
|
||||
<net:address name="${registry.address.name}" url="${registry.address.url}"/>
|
||||
|
||||
@@ -19,8 +19,7 @@
|
||||
|
||||
<context:component-scan base-package="com.zfoo"/>
|
||||
|
||||
<net:config id="applicationNameTest" protocol-location="protocol.xml"
|
||||
generate-js-protocol="false" generate-cs-protocol="false">
|
||||
<net:config id="applicationNameTest" protocol-location="protocol.xml">
|
||||
|
||||
<net:registry center="${registry.center}" user="${registry.user}" password="${registry.password}">
|
||||
<net:address name="${registry.address.name}" url="${registry.address.url}"/>
|
||||
|
||||
Reference in New Issue
Block a user