修改:rpc 调用端订阅 topic 逻辑修改
This commit is contained in:
parent
1f23dbf3cd
commit
d7058907d6
@ -15,6 +15,7 @@ import java.io.OutputStream;
|
|||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@ -58,8 +59,9 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
private static boolean isFirst = true;
|
/*private static boolean isFirst = true;
|
||||||
private boolean isMain = false;
|
private boolean isMain = false;*/
|
||||||
|
private static Map<String, ZHubClient> mainHub = new HashMap<>(); // 127.0.0.1:1216 - ZHubClient
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(AnyValue config) {
|
public void init(AnyValue config) {
|
||||||
@ -75,9 +77,12 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 设置第一个启动的 实例为主实例
|
// 设置第一个启动的 实例为主实例
|
||||||
if (isFirst) {
|
/*if (isFirst) {
|
||||||
isMain = true;
|
isMain = true;
|
||||||
isFirst = false;
|
isFirst = false;
|
||||||
|
}*/
|
||||||
|
if (!mainHub.containsKey(host + ":" + port)) { // 确保同步执行此 init 逻辑
|
||||||
|
mainHub.put(host + ":" + port, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!initSocket(0)) {
|
if (!initSocket(0)) {
|
||||||
@ -312,7 +317,9 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
send("groupid " + groupid);
|
send("groupid " + groupid);
|
||||||
|
|
||||||
StringBuffer buf = new StringBuffer("subscribe lock");
|
StringBuffer buf = new StringBuffer("subscribe lock");
|
||||||
if (isMain) { // TODO:
|
/*if (isMain) {
|
||||||
|
}*/
|
||||||
|
if (mainHub.containsValue(this)) {
|
||||||
buf.append(" " + APP_NAME);
|
buf.append(" " + APP_NAME);
|
||||||
}
|
}
|
||||||
for (String topic : getTopics()) {
|
for (String topic : getTopics()) {
|
||||||
|
@ -26,11 +26,11 @@ public class AppTest {
|
|||||||
//启动并开启消费监听
|
//启动并开启消费监听
|
||||||
MyConsumer consumer = Application.singleton(MyConsumer.class);
|
MyConsumer consumer = Application.singleton(MyConsumer.class);
|
||||||
|
|
||||||
/*consumer.subscribe("a", str -> {
|
consumer.subscribe("a", str -> {
|
||||||
logger.info("我收到了消息 a 事件:" + str);
|
logger.info("我收到了消息 a 事件:" + str);
|
||||||
});
|
});
|
||||||
|
|
||||||
consumer.timer("a", () -> {
|
/*consumer.timer("a", () -> {
|
||||||
System.out.println(Utility.now() + " timer a 执行了");
|
System.out.println(Utility.now() + " timer a 执行了");
|
||||||
try {
|
try {
|
||||||
Thread.sleep(3000);
|
Thread.sleep(3000);
|
||||||
|
37
test/com/zdemo/test/HelloService.java
Normal file
37
test/com/zdemo/test/HelloService.java
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package com.zdemo.test;
|
||||||
|
|
||||||
|
import com.zdemo.IConsumer;
|
||||||
|
import com.zdemo.zhub.RpcResult;
|
||||||
|
import com.zdemo.zhub.ZHubClient;
|
||||||
|
import org.redkale.net.http.RestService;
|
||||||
|
import org.redkale.service.Service;
|
||||||
|
import org.redkale.util.AnyValue;
|
||||||
|
import org.redkale.util.TypeToken;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
|
@RestService(automapping = true)
|
||||||
|
public class HelloService implements Service {
|
||||||
|
|
||||||
|
@Resource(name = "zhub")
|
||||||
|
private ZHubClient zhub;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(AnyValue config) {
|
||||||
|
// Function<Rpc<T>, RpcResult<R>> fun
|
||||||
|
zhub.rpcSubscribe("x", new TypeToken<String>() {
|
||||||
|
}, r -> {
|
||||||
|
|
||||||
|
return r.buildResp(r.getValue().toUpperCase() + ": Ok");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public RpcResult<String> x(String v) {
|
||||||
|
if (v == null) {
|
||||||
|
v = "";
|
||||||
|
}
|
||||||
|
RpcResult<String> x = zhub.rpc("x", v, IConsumer.TYPE_TOKEN_STRING).join();
|
||||||
|
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user