修复WebSocketNodeService无sncp服务情况下不注入CacheSource的bug

This commit is contained in:
Redkale
2022-12-08 17:18:55 +08:00
parent b41f34cade
commit 5093663cfd
3 changed files with 42 additions and 50 deletions

View File

@@ -241,11 +241,12 @@ public final class Application {
//Server根ClassLoader //Server根ClassLoader
private final RedkaleClassLoader serverClassLoader; private final RedkaleClassLoader serverClassLoader;
//config: 不带redkale的配置项
Application(final AnyValue config) { Application(final AnyValue config) {
this(false, false, config); this(false, false, config);
} }
@SuppressWarnings("UseSpecificCatch") @SuppressWarnings("UseSpecificCatch") //config: 不带redkale的配置项
Application(final boolean singletonMode, boolean compileMode, final AnyValue config) { Application(final boolean singletonMode, boolean compileMode, final AnyValue config) {
this.singletonMode = singletonMode; this.singletonMode = singletonMode;
this.compileMode = compileMode; this.compileMode = compileMode;
@@ -269,7 +270,7 @@ public final class Application {
this.homePath = this.home.getPath(); this.homePath = this.home.getPath();
String confDir = System.getProperty(RESNAME_APP_CONF_DIR, "conf"); String confDir = System.getProperty(RESNAME_APP_CONF_DIR, "conf");
if (confDir.contains("://") || confDir.startsWith("file:") || confDir.startsWith("resource:") || confDir.contains("!")) { //graalvm native-image startwith resource:META-INF if (confDir.contains("://") || confDir.startsWith("file:") || confDir.startsWith("resource:") || confDir.contains("!")) { //graalvm native-image startwith resource:META-INF
this.confPath = new URI(confDir); this.confPath = URI.create(confDir);
if (confDir.startsWith("file:")) { if (confDir.startsWith("file:")) {
confFile = new File(this.confPath.getPath()).getCanonicalFile(); confFile = new File(this.confPath.getPath()).getCanonicalFile();
} }
@@ -280,7 +281,7 @@ public final class Application {
confFile = new File(this.home, confDir).getCanonicalFile(); confFile = new File(this.home, confDir).getCanonicalFile();
this.confPath = confFile.toURI(); this.confPath = confFile.toURI();
} }
} catch (Exception e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
String localaddr = config.getValue("address", "").trim(); String localaddr = config.getValue("address", "").trim();
@@ -297,18 +298,17 @@ public final class Application {
this.resourceFactory.register(RESNAME_APP_CONF_DIR, Path.class, confFile.toPath()); this.resourceFactory.register(RESNAME_APP_CONF_DIR, Path.class, confFile.toPath());
} }
this.resourceFactory.register(Environment.class, environment); this.resourceFactory.register(Environment.class, environment);
{ { //设置系统变量
int nid = config.getIntValue("nodeid", 0); int nid = config.getIntValue("nodeid", 0);
this.nodeid = nid; this.nodeid = nid;
this.resourceFactory.register(RESNAME_APP_NODEID, nid); this.resourceFactory.register(RESNAME_APP_NODEID, nid);
System.setProperty(RESNAME_APP_NODEID, "" + nid); System.setProperty(RESNAME_APP_NODEID, "" + nid);
}
{
this.name = checkName(config.getValue("name", "")); this.name = checkName(config.getValue("name", ""));
this.resourceFactory.register(RESNAME_APP_NAME, name); this.resourceFactory.register(RESNAME_APP_NAME, name);
System.setProperty(RESNAME_APP_NAME, name); System.setProperty(RESNAME_APP_NAME, name);
} }
{ { //初始化ClassLoader
ClassLoader currClassLoader = Thread.currentThread().getContextClassLoader(); ClassLoader currClassLoader = Thread.currentThread().getContextClassLoader();
if (currClassLoader instanceof RedkaleClassLoader) { if (currClassLoader instanceof RedkaleClassLoader) {
this.classLoader = (RedkaleClassLoader) currClassLoader; this.classLoader = (RedkaleClassLoader) currClassLoader;
@@ -336,8 +336,7 @@ public final class Application {
Thread.currentThread().setContextClassLoader(this.classLoader); Thread.currentThread().setContextClassLoader(this.classLoader);
} }
} }
//以下是初始化日志配置 { //以下是初始化日志配置
{
URI logConfURI; URI logConfURI;
File logConfFile = null; File logConfFile = null;
if (configFromCache) { if (configFromCache) {
@@ -376,46 +375,39 @@ public final class Application {
logger.log(Level.INFO, colorMessage(logger, 36, 1, "-------------------------------- Redkale " + Redkale.getDotedVersion() + " --------------------------------")); logger.log(Level.INFO, colorMessage(logger, 36, 1, "-------------------------------- Redkale " + Redkale.getDotedVersion() + " --------------------------------"));
//------------------------------------ 基本设置 ------------------------------------ //------------------------------------ 基本设置 ------------------------------------
System.setProperty("redkale.net.transport.poolmaxconns", "100"); {
System.setProperty("redkale.net.transport.pinginterval", "30"); final String confDir = this.confPath.toString();
System.setProperty("redkale.net.transport.checkinterval", "30"); logger.log(Level.INFO, "APP_OS = " + System.getProperty("os.name") + " " + System.getProperty("os.version") + " " + System.getProperty("os.arch") + "\r\n"
System.setProperty("redkale.convert.tiny", "true"); + "APP_JAVA = " + System.getProperty("java.runtime.name", System.getProperty("org.graalvm.nativeimage.kind") != null ? "Nativeimage" : "")
System.setProperty("redkale.convert.pool.size", "128"); + " " + System.getProperty("java.runtime.version", System.getProperty("java.vendor.version", System.getProperty("java.vm.version"))) + "\r\n" //graalvm.nativeimage 模式下无 java.runtime.xxx 属性
System.setProperty("redkale.convert.writer.buffer.defsize", "4096"); + "APP_PID = " + ProcessHandle.current().pid() + "\r\n"
System.setProperty("redkale.trace.enable", "false"); + RESNAME_APP_NODEID + " = " + this.nodeid + "\r\n"
+ "APP_LOADER = " + this.classLoader.getClass().getSimpleName() + "\r\n"
+ RESNAME_APP_ADDR + " = " + this.localAddress.getHostString() + ":" + this.localAddress.getPort() + "\r\n"
+ RESNAME_APP_HOME + " = " + homePath + "\r\n"
+ RESNAME_APP_CONF_DIR + " = " + confDir.substring(confDir.indexOf('!') + 1));
final String confDir = this.confPath.toString(); if (!compileMode && !(classLoader instanceof RedkaleClassLoader.RedkaleCacheClassLoader)) {
// String pidstr = ""; String lib = replaceValue(config.getValue("lib", "${APP_HOME}/libs/*").trim());
// try { //JDK 9+ lib = lib.isEmpty() ? confDir : (lib + ";" + confDir);
// Class phclass = Thread.currentThread().getContextClassLoader().loadClass("java.lang.ProcessHandle"); Server.loadLib(classLoader, logger, lib);
// Object phobj = phclass.getMethod("current").invoke(null); }
// Object pid = phclass.getMethod("pid").invoke(phobj);
// pidstr = "APP_PID = " + pid + "\r\n";
// } catch (Throwable t) {
// }
logger.log(Level.INFO, "APP_OS = " + System.getProperty("os.name") + " " + System.getProperty("os.version") + " " + System.getProperty("os.arch") + "\r\n" System.setProperty("redkale.net.transport.poolmaxconns", "100");
+ "APP_JAVA = " + System.getProperty("java.runtime.name", System.getProperty("org.graalvm.nativeimage.kind") != null ? "Nativeimage" : "") System.setProperty("redkale.net.transport.pinginterval", "30");
+ " " + System.getProperty("java.runtime.version", System.getProperty("java.vendor.version", System.getProperty("java.vm.version"))) + "\r\n" //graalvm.nativeimage 模式下无 java.runtime.xxx 属性 System.setProperty("redkale.net.transport.checkinterval", "30");
+ "APP_PID = " + ProcessHandle.current().pid() + "\r\n" System.setProperty("redkale.convert.tiny", "true");
+ RESNAME_APP_NODEID + " = " + this.nodeid + "\r\n" System.setProperty("redkale.convert.pool.size", "128");
+ "APP_LOADER = " + this.classLoader.getClass().getSimpleName() + "\r\n" System.setProperty("redkale.convert.writer.buffer.defsize", "4096");
+ RESNAME_APP_ADDR + " = " + this.localAddress.getHostString() + ":" + this.localAddress.getPort() + "\r\n" System.setProperty("redkale.trace.enable", "false");
+ RESNAME_APP_HOME + " = " + homePath + "\r\n"
+ RESNAME_APP_CONF_DIR + " = " + confDir.substring(confDir.indexOf('!') + 1));
if (!compileMode && !(classLoader instanceof RedkaleClassLoader.RedkaleCacheClassLoader)) { this.resourceFactory.register(BsonFactory.root());
String lib = replaceValue(config.getValue("lib", "${APP_HOME}/libs/*").trim()); this.resourceFactory.register(JsonFactory.root());
lib = lib.isEmpty() ? confDir : (lib + ";" + confDir); this.resourceFactory.register(BsonFactory.root().getConvert());
Server.loadLib(classLoader, logger, lib); this.resourceFactory.register(JsonFactory.root().getConvert());
this.resourceFactory.register("bsonconvert", Convert.class, BsonFactory.root().getConvert());
this.resourceFactory.register("jsonconvert", Convert.class, JsonFactory.root().getConvert());
} }
this.resourceFactory.register(BsonFactory.root());
this.resourceFactory.register(JsonFactory.root());
this.resourceFactory.register(BsonFactory.root().getConvert());
this.resourceFactory.register(JsonFactory.root().getConvert());
this.resourceFactory.register("bsonconvert", Convert.class, BsonFactory.root().getConvert());
this.resourceFactory.register("jsonconvert", Convert.class, JsonFactory.root().getConvert());
//------------------------------------ 读取配置 ------------------------------------ //------------------------------------ 读取配置 ------------------------------------
try { try {
loadResourceProperties(); loadResourceProperties();

View File

@@ -287,16 +287,16 @@ public abstract class NodeServer {
final Service srcService = (Service) srcObj; final Service srcService = (Service) srcObj;
SncpClient client = Sncp.getSncpClient(srcService); SncpClient client = Sncp.getSncpClient(srcService);
final InetSocketAddress sncpAddr = client == null ? null : client.getClientAddress(); final InetSocketAddress sncpAddr = client == null ? null : client.getClientAddress();
final boolean ws = (srcObj instanceof org.redkale.net.http.WebSocketNodeService) && sncpAddr != null; //final boolean ws = (srcObj instanceof org.redkale.net.http.WebSocketNodeService) && sncpAddr != null; //不配置SNCP服务会导致ws=false时没有注入CacheMemorySource
final boolean ws = (srcObj instanceof org.redkale.net.http.WebSocketNodeService);
CacheSource source = application.loadCacheSource(resourceName, ws); CacheSource source = application.loadCacheSource(resourceName, ws);
field.set(srcObj, source); field.set(srcObj, source);
if (ws) { //只有WebSocketNodeService的服务才需要给SNCP服务注入CacheMemorySource if (ws && sncpAddr != null) { //只有WebSocketNodeService的服务才需要给SNCP服务注入CacheMemorySource
NodeSncpServer sncpServer = application.findNodeSncpServer(sncpAddr); NodeSncpServer sncpServer = application.findNodeSncpServer(sncpAddr);
if (source != null && source.getClass().getAnnotation(Local.class) == null) { //本地模式的Service不生成SncpServlet if (sncpServer != null && source != null && source.getClass().getAnnotation(Local.class) == null) { //本地模式的Service不生成SncpServlet
sncpServer.getSncpServer().addSncpServlet((Service) source); sncpServer.getSncpServer().addSncpServlet((Service) source);
} }
//logger.info("[" + Thread.currentThread().getName() + "] Load Service " + source);
} }
logger.info("[" + Thread.currentThread().getName() + "] Load CacheSource (type = " + (source == null ? null : source.getClass().getSimpleName()) + ", resourceName = '" + resourceName + "')"); logger.info("[" + Thread.currentThread().getName() + "] Load CacheSource (type = " + (source == null ? null : source.getClass().getSimpleName()) + ", resourceName = '" + resourceName + "')");
} catch (Exception e) { } catch (Exception e) {

View File

@@ -24,7 +24,7 @@ import org.redkale.service.*;
import org.redkale.util.*; import org.redkale.util.*;
/** /**
* 第三方服务发现管理接口cluster * 服务注册中心管理类cluster
* *
* *
* 详情见: https://redkale.org * 详情见: https://redkale.org