.
This commit is contained in:
parent
9a3314e0e6
commit
094d3fc5a2
19
pom.xml
19
pom.xml
@ -5,7 +5,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<groupId>net.tccn</groupId>
|
<groupId>net.tccn</groupId>
|
||||||
<artifactId>zhub-client</artifactId>
|
<artifactId>zhub-cli</artifactId>
|
||||||
<version>1.0</version>
|
<version>1.0</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
@ -27,4 +27,21 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>maven-nexus</id>
|
||||||
|
<name>maven-nexus</name>
|
||||||
|
<url>http://47.106.237.198:8081/repository/maven-public/</url>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
|
|
||||||
|
<distributionManagement>
|
||||||
|
<repository>
|
||||||
|
<id>mvn-release</id>
|
||||||
|
<name>mvn-release</name>
|
||||||
|
<url>http://47.106.237.198:8081/repository/maven-releases/</url>
|
||||||
|
</repository>
|
||||||
|
</distributionManagement>
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -19,7 +19,7 @@ public abstract class AbstractConsumer implements IConsumer {
|
|||||||
public Gson gson = Rpc.gson;
|
public Gson gson = Rpc.gson;
|
||||||
|
|
||||||
// @Resource(name = "APP_NAME")
|
// @Resource(name = "APP_NAME")
|
||||||
protected String APP_NAME = "";
|
protected String APP_ID = "";
|
||||||
|
|
||||||
private Map<String, EventType> eventMap = new HashMap<>();
|
private Map<String, EventType> eventMap = new HashMap<>();
|
||||||
|
|
||||||
|
@ -62,21 +62,20 @@ public class Rpc<T> {
|
|||||||
return ruk.split("::")[0];
|
return ruk.split("::")[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
public <R> RpcResult<R> buildResp() {
|
public <R> RpcResult<R> render() {
|
||||||
RpcResult<R> response = new RpcResult<>();
|
RpcResult<R> response = new RpcResult<>();
|
||||||
response.setRuk(ruk);
|
response.setRuk(ruk);
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <R> RpcResult<R> buildResp(int retcode, String retinfo) {
|
public <R> RpcResult<R> render(R result) {
|
||||||
RpcResult<R> response = new RpcResult<>();
|
RpcResult<R> response = new RpcResult<>();
|
||||||
response.setRuk(ruk);
|
response.setRuk(ruk);
|
||||||
response.setRetcode(retcode);
|
response.setResult(result);
|
||||||
response.setRetinfo(retinfo);
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <R> RpcResult<R> buildError(String retinfo) {
|
public <R> RpcResult<R> retError(String retinfo) {
|
||||||
RpcResult<R> response = new RpcResult<>();
|
RpcResult<R> response = new RpcResult<>();
|
||||||
response.setRuk(ruk);
|
response.setRuk(ruk);
|
||||||
response.setRetcode(100);
|
response.setRetcode(100);
|
||||||
@ -84,10 +83,11 @@ public class Rpc<T> {
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <R> RpcResult<R> buildResp(R result) {
|
public <R> RpcResult<R> retError(int retcode, String retinfo) {
|
||||||
RpcResult<R> response = new RpcResult<>();
|
RpcResult<R> response = new RpcResult<>();
|
||||||
response.setRuk(ruk);
|
response.setRuk(ruk);
|
||||||
response.setResult(result);
|
response.setRetcode(retcode);
|
||||||
|
response.setRetinfo(retinfo);
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -55,10 +55,10 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
private boolean isMain = false;*/
|
private boolean isMain = false;*/
|
||||||
private static final Map<String, ZHubClient> mainHub = new HashMap<>(); // 127.0.0.1:1216 - ZHubClient
|
private static final Map<String, ZHubClient> mainHub = new HashMap<>(); // 127.0.0.1:1216 - ZHubClient
|
||||||
|
|
||||||
public ZHubClient(String addr, String groupid, String appname, String auth) {
|
public ZHubClient(String addr, String groupid, String appid, String auth) {
|
||||||
this.addr = addr;
|
this.addr = addr;
|
||||||
this.groupid = groupid;
|
this.groupid = groupid;
|
||||||
this.APP_NAME = appname;
|
this.APP_ID = appid;
|
||||||
this.auth = auth;
|
this.auth = auth;
|
||||||
init(null);
|
init(null);
|
||||||
}
|
}
|
||||||
@ -72,7 +72,7 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
if (config != null) {
|
if (config != null) {
|
||||||
addr = config.getOrDefault("addr", addr);
|
addr = config.getOrDefault("addr", addr);
|
||||||
groupid = config.getOrDefault("groupid", groupid);
|
groupid = config.getOrDefault("groupid", groupid);
|
||||||
APP_NAME = config.getOrDefault("appname", APP_NAME);
|
APP_ID = config.getOrDefault("appname", APP_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置第一个启动的 实例为主实例
|
// 设置第一个启动的 实例为主实例
|
||||||
@ -142,7 +142,7 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
// rpc back msg
|
// rpc back msg
|
||||||
if (APP_NAME.equals(topic)) {
|
if (APP_ID.equals(topic)) {
|
||||||
rpcBackQueue.add(Event.of(topic, value));
|
rpcBackQueue.add(Event.of(topic, value));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -359,7 +359,7 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
/*if (isMain) {
|
/*if (isMain) {
|
||||||
}*/
|
}*/
|
||||||
if (mainHub.containsValue(this)) {
|
if (mainHub.containsValue(this)) {
|
||||||
buf.append(" ").append(APP_NAME);
|
buf.append(" ").append(APP_ID);
|
||||||
}
|
}
|
||||||
for (String topic : getTopics()) {
|
for (String topic : getTopics()) {
|
||||||
buf.append(" ").append(topic);
|
buf.append(" ").append(topic);
|
||||||
@ -533,7 +533,7 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
|
|
||||||
// rpc call
|
// rpc call
|
||||||
public <T, R> RpcResult<R> rpc(String topic, T v, TypeToken<R> typeToken, long timeout) {
|
public <T, R> RpcResult<R> rpc(String topic, T v, TypeToken<R> typeToken, long timeout) {
|
||||||
Rpc rpc = new Rpc<>(APP_NAME, UUID.randomUUID().toString().replaceAll("-", ""), topic, v);
|
Rpc rpc = new Rpc<>(APP_ID, UUID.randomUUID().toString().replaceAll("-", ""), topic, v);
|
||||||
String ruk = rpc.getRuk();
|
String ruk = rpc.getRuk();
|
||||||
rpcMap.put(ruk, rpc);
|
rpcMap.put(ruk, rpc);
|
||||||
if (typeToken != null) {
|
if (typeToken != null) {
|
||||||
@ -553,7 +553,7 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
RpcResult rpcResult = rpc.buildResp(505, "请求超时");
|
RpcResult rpcResult = rpc.retError(505, "请求超时");
|
||||||
rpc.setRpcResult(rpcResult);
|
rpc.setRpcResult(rpcResult);
|
||||||
logger.warning("rpc timeout: " + gson.toJson(rpc));
|
logger.warning("rpc timeout: " + gson.toJson(rpc));
|
||||||
rpc.notify();
|
rpc.notify();
|
||||||
@ -566,7 +566,7 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
// call error
|
// call error
|
||||||
RpcResult rpcResult = rpc.buildResp(501, "请求失败");
|
RpcResult rpcResult = rpc.retError(501, "请求失败");
|
||||||
rpc.setRpcResult(rpcResult);
|
rpc.setRpcResult(rpcResult);
|
||||||
}
|
}
|
||||||
return rpc.getRpcResult();
|
return rpc.getRpcResult();
|
||||||
@ -632,7 +632,7 @@ public class ZHubClient extends AbstractConsumer implements IConsumer, IProducer
|
|||||||
publish(rpc.getBackTopic(), result);
|
publish(rpc.getBackTopic(), result);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.log(Level.WARNING, "rpc call consumer error: " + v, e);
|
logger.log(Level.WARNING, "rpc call consumer error: " + v, e);
|
||||||
publish(rpc.getBackTopic(), rpc.buildError("服务调用失败!"));
|
publish(rpc.getBackTopic(), rpc.retError("服务调用失败!"));
|
||||||
}
|
}
|
||||||
// back
|
// back
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user