sncp优化

This commit is contained in:
redkale
2023-02-08 23:07:50 +08:00
parent 2e40c98b81
commit c2b5733ceb
3 changed files with 58 additions and 33 deletions

View File

@@ -347,12 +347,15 @@ public abstract class MessageAgent implements Resourcable {
//格式: sncp.req.user
public final String generateSncpReqTopic(Service service) {
if (service instanceof WebSocketNode) {
String resname = Sncp.getResourceName(service);
return "sncp.req.ws" + (resname.isEmpty() ? "" : ("-" + resname)) + ".node" + nodeid;
return generateSncpReqTopic(Sncp.getResourceName(service), Sncp.getResourceType(service));
}
//格式: sncp.req.user
public final String generateSncpReqTopic(String resourceName, Class resourceType) {
if (WebSocketNode.class.isAssignableFrom(resourceType)) {
return "sncp.req.ws" + (resourceName.isEmpty() ? "" : ("-" + resourceName)) + ".node" + nodeid;
}
String resname = Sncp.getResourceName(service);
return "sncp.req." + Sncp.getResourceType(service).getSimpleName().replaceAll("Service.*$", "").toLowerCase() + (resname.isEmpty() ? "" : ("-" + resname));
return "sncp.req." + resourceType.getSimpleName().replaceAll("Service.*$", "").toLowerCase() + (resourceName.isEmpty() ? "" : ("-" + resourceName));
}
//格式: consumer-sncp.req.user 不提供外部使用

View File

@@ -151,8 +151,8 @@ public abstract class Sncp {
}
public static <T extends Service> SncpServiceInfo createSncpServiceInfo(String resourceName,
Class<T> resourceServiceType, T service, Convert convert, SncpClient sncpClient, MessageAgent messageAgent, SncpMessageClient messageClient) {
return new SncpServiceInfo(resourceName, resourceServiceType, service, convert, sncpClient, messageAgent, messageClient);
Class<T> resourceServiceType, Class<T> serviceImplClass, Convert convert, SncpClient sncpClient, MessageAgent messageAgent, SncpMessageClient messageClient) {
return new SncpServiceInfo(resourceName, resourceServiceType, serviceImplClass, convert, sncpClient, messageAgent, messageClient);
}
public static Uint128 actionid(final RpcAction action) {
@@ -268,11 +268,11 @@ public abstract class Sncp {
return null;
}
try {
Field ts = service.getClass().getDeclaredField(FIELDPREFIX + "_messageagent");
Field ts = service.getClass().getDeclaredField(FIELDPREFIX + "_messageAgent");
ts.setAccessible(true);
return (MessageAgent) ts.get(service);
} catch (Exception e) {
throw new SncpException(service + " not found " + FIELDPREFIX + "_messageagent");
throw new SncpException(service + " not found " + FIELDPREFIX + "_messageAgent");
}
}
@@ -281,7 +281,7 @@ public abstract class Sncp {
return;
}
try {
Field ts = service.getClass().getDeclaredField(FIELDPREFIX + "_messageagent");
Field ts = service.getClass().getDeclaredField(FIELDPREFIX + "_messageAgent");
ts.setAccessible(true);
ts.set(service, messageAgent);
if (service instanceof WebSocketNode) {
@@ -290,7 +290,7 @@ public abstract class Sncp {
c.set(service, messageAgent);
}
} catch (Exception e) {
throw new SncpException(service + " not found " + FIELDPREFIX + "_messageagent");
throw new SncpException(service + " not found " + FIELDPREFIX + "_messageAgent");
}
}
@@ -478,7 +478,9 @@ public abstract class Sncp {
}
final String supDynName = serviceImplClass.getName().replace('.', '/');
final String clientName = OldSncpClient.class.getName().replace('.', '/');
final String sncpInfoName = SncpServiceInfo.class.getName().replace('.', '/');
final String resDesc = Type.getDescriptor(Resource.class);
final String sncpInfoDesc = Type.getDescriptor(SncpServiceInfo.class);
final String clientDesc = Type.getDescriptor(OldSncpClient.class);
final String anyValueDesc = Type.getDescriptor(AnyValue.class);
final String sncpDynDesc = Type.getDescriptor(SncpDyn.class);
@@ -546,7 +548,11 @@ public abstract class Sncp {
fv.visitEnd();
}
{
fv = cw.visitField(ACC_PRIVATE, FIELDPREFIX + "_messageagent", Type.getDescriptor(MessageAgent.class), null, null);
fv = cw.visitField(ACC_PRIVATE, FIELDPREFIX + "_sncpInfo", sncpInfoDesc, null, null);
fv.visitEnd();
}
{
fv = cw.visitField(ACC_PRIVATE, FIELDPREFIX + "_messageAgent", Type.getDescriptor(MessageAgent.class), null, null);
fv.visitEnd();
}
{ //构造函数
@@ -561,7 +567,7 @@ public abstract class Sncp {
{ // toString()
mv = new MethodDebugVisitor(cw.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null));
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, newDynName, FIELDPREFIX + "_client", clientDesc);
mv.visitFieldInsn(GETFIELD, newDynName, FIELDPREFIX + "_sncpInfo", sncpInfoDesc);
Label l1 = new Label();
mv.visitJumpInsn(IFNONNULL, l1);
mv.visitVarInsn(ALOAD, 0);
@@ -571,8 +577,8 @@ public abstract class Sncp {
mv.visitJumpInsn(GOTO, l2);
mv.visitLabel(l1);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, newDynName, FIELDPREFIX + "_client", clientDesc);
mv.visitMethodInsn(INVOKEVIRTUAL, clientName, "toSimpleString", "()Ljava/lang/String;", false);
mv.visitFieldInsn(GETFIELD, newDynName, FIELDPREFIX + "_sncpInfo", sncpInfoDesc);
mv.visitMethodInsn(INVOKEVIRTUAL, sncpInfoName, "toSimpleString", "()Ljava/lang/String;", false);
mv.visitLabel(l2);
mv.visitInsn(ARETURN);
mv.visitMaxs(1, 1);
@@ -593,7 +599,9 @@ public abstract class Sncp {
RedkaleClassLoader.putReflectionField(newDynName.replace('/', '.'), c);
c = newClazz.getDeclaredField(FIELDPREFIX + "_client");
RedkaleClassLoader.putReflectionField(newDynName.replace('/', '.'), c);
c = newClazz.getDeclaredField(FIELDPREFIX + "_messageagent");
c = newClazz.getDeclaredField(FIELDPREFIX + "_sncpInfo");
RedkaleClassLoader.putReflectionField(newDynName.replace('/', '.'), c);
c = newClazz.getDeclaredField(FIELDPREFIX + "_messageAgent");
RedkaleClassLoader.putReflectionField(newDynName.replace('/', '.'), c);
} catch (Exception e) {
}
@@ -679,7 +687,7 @@ public abstract class Sncp {
}
}
if (messageAgent != null) {
Field c = newClazz.getDeclaredField(FIELDPREFIX + "_messageagent");
Field c = newClazz.getDeclaredField(FIELDPREFIX + "_messageAgent");
c.setAccessible(true);
c.set(service, messageAgent);
}
@@ -777,7 +785,9 @@ public abstract class Sncp {
}
final String supDynName = serviceTypeOrImplClass.getName().replace('.', '/');
final String clientName = OldSncpClient.class.getName().replace('.', '/');
final String sncpInfoName = SncpServiceInfo.class.getName().replace('.', '/');
final String resDesc = Type.getDescriptor(Resource.class);
final String sncpInfoDesc = Type.getDescriptor(SncpServiceInfo.class);
final String clientDesc = Type.getDescriptor(OldSncpClient.class);
final String sncpDynDesc = Type.getDescriptor(SncpDyn.class);
final String anyValueDesc = Type.getDescriptor(AnyValue.class);
@@ -799,7 +809,7 @@ public abstract class Sncp {
c.set(service, client);
}
if (messageAgent != null) {
Field c = newClazz.getDeclaredField(FIELDPREFIX + "_messageagent");
Field c = newClazz.getDeclaredField(FIELDPREFIX + "_messageAgent");
c.setAccessible(true);
c.set(service, messageAgent);
if (service instanceof WebSocketNode) {
@@ -861,7 +871,11 @@ public abstract class Sncp {
fv.visitEnd();
}
{
fv = cw.visitField(ACC_PRIVATE, FIELDPREFIX + "_messageagent", Type.getDescriptor(MessageAgent.class), null, null);
fv = cw.visitField(ACC_PRIVATE, FIELDPREFIX + "_sncpInfo", sncpInfoDesc, null, null);
fv.visitEnd();
}
{
fv = cw.visitField(ACC_PRIVATE, FIELDPREFIX + "_messageAgent", Type.getDescriptor(MessageAgent.class), null, null);
fv.visitEnd();
}
{ //构造函数
@@ -894,7 +908,7 @@ public abstract class Sncp {
{ // toString()
mv = new MethodDebugVisitor(cw.visitMethod(ACC_PUBLIC, "toString", "()Ljava/lang/String;", null, null));
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, newDynName, FIELDPREFIX + "_client", clientDesc);
mv.visitFieldInsn(GETFIELD, newDynName, FIELDPREFIX + "_sncpInfo", sncpInfoDesc);
Label l1 = new Label();
mv.visitJumpInsn(IFNONNULL, l1);
mv.visitVarInsn(ALOAD, 0);
@@ -904,8 +918,8 @@ public abstract class Sncp {
mv.visitJumpInsn(GOTO, l2);
mv.visitLabel(l1);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, newDynName, FIELDPREFIX + "_client", clientDesc);
mv.visitMethodInsn(INVOKEVIRTUAL, clientName, "toSimpleString", "()Ljava/lang/String;", false);
mv.visitFieldInsn(GETFIELD, newDynName, FIELDPREFIX + "_sncpInfo", sncpInfoDesc);
mv.visitMethodInsn(INVOKEVIRTUAL, sncpInfoName, "toSimpleString", "()Ljava/lang/String;", false);
mv.visitLabel(l2);
mv.visitInsn(ARETURN);
mv.visitMaxs(1, 1);
@@ -1025,7 +1039,7 @@ public abstract class Sncp {
RedkaleClassLoader.putReflectionField(newDynName.replace('/', '.'), c);
}
if (messageAgent != null) {
Field c = newClazz.getDeclaredField(FIELDPREFIX + "_messageagent");
Field c = newClazz.getDeclaredField(FIELDPREFIX + "_messageAgent");
c.setAccessible(true);
c.set(service, messageAgent);
RedkaleClassLoader.putReflectionField(newDynName.replace('/', '.'), c);

View File

@@ -32,8 +32,6 @@ public final class SncpServiceInfo<T extends Service> {
protected final Class<T> serviceType;
protected final T service;
protected final Uint128 serviceid;
protected final int serviceVersion;
@@ -59,21 +57,19 @@ public final class SncpServiceInfo<T extends Service> {
//远程模式, 可能为null
protected Set<InetSocketAddress> remoteAddresses;
SncpServiceInfo(String resourceName, Class<T> resourceServiceType, final T service, Convert convert,
SncpServiceInfo(String resourceName, Class<T> resourceServiceType, final Class<T> serviceImplClass, Convert convert,
SncpClient sncpClient, MessageAgent messageAgent, SncpMessageClient messageClient) {
this.sncpClient = sncpClient;
this.name = resourceName;
this.serviceType = resourceServiceType;
this.serviceid = Sncp.serviceid(resourceName, resourceServiceType);
this.service = service;
this.convert = convert;
this.serviceVersion = 0;
this.messageAgent = messageAgent;
this.messageClient = messageAgent == null ? null : messageAgent.getSncpMessageClient();
this.topic = messageAgent == null ? null : messageAgent.generateSncpReqTopic(service);
this.topic = messageAgent == null ? null : messageAgent.generateSncpReqTopic(resourceName, resourceServiceType);
final List<SncpServiceAction> serviceActions = new ArrayList<>();
final Class serviceImplClass = service.getClass();
for (Map.Entry<Uint128, Method> en : loadMethodActions(resourceServiceType).entrySet()) {
serviceActions.add(new SncpServiceAction(serviceImplClass, en.getValue(), serviceid, en.getKey()));
}
@@ -85,6 +81,22 @@ public final class SncpServiceInfo<T extends Service> {
return sncpClient.remote(this, index, params);
}
@Override
public String toString() {
InetSocketAddress clientSncpAddress = sncpClient == null ? null : sncpClient.getClientSncpAddress();
return this.getClass().getSimpleName() + "(service = " + serviceType.getSimpleName() + ", serviceid = " + serviceid + ", serviceVersion = " + serviceVersion + ", name = '" + name
+ "', address = " + (clientSncpAddress == null ? "" : (clientSncpAddress.getHostString() + ":" + clientSncpAddress.getPort()))
+ ", actions.size = " + actions.length + ")";
}
public String toSimpleString() { //给Sncp产生的Service用
InetSocketAddress clientSncpAddress = sncpClient == null ? null : sncpClient.getClientSncpAddress();
return serviceType.getSimpleName() + "(name = '" + name + "', serviceid = " + serviceid + ", serviceVersion = " + serviceVersion
+ ", clientaddr = " + (clientSncpAddress == null ? "" : (clientSncpAddress.getHostString() + ":" + clientSncpAddress.getPort()))
+ ((remoteGroups == null || remoteGroups.isEmpty()) ? "" : ", remoteGroups = " + remoteGroups)
+ ", actions.size = " + actions.length + ")";
}
public void updateRemoteAddress(Set<String> remoteGroups, Set<InetSocketAddress> remoteAddresses) {
this.remoteGroups = remoteGroups;
this.remoteAddresses = remoteAddresses;
@@ -98,10 +110,6 @@ public final class SncpServiceInfo<T extends Service> {
return serviceType;
}
public T getService() {
return service;
}
public Uint128 getServiceid() {
return serviceid;
}