Skip to content

Commit

Permalink
[change] notify helper use new api
Browse files Browse the repository at this point in the history
[fix bug] fix springcloud read tarsserver config error
  • Loading branch information
TimmyYu committed Nov 28, 2023
1 parent f334c4b commit 90000a5
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import com.qq.tars.common.util.Loader;
import com.qq.tars.common.util.StringUtils;
import com.qq.tars.support.log.LoggerFactory;
import org.slf4j.Logger;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
Expand All @@ -38,6 +36,7 @@
import java.util.Properties;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock;
import org.slf4j.Logger;

public final class ServantCacheManager {
private static final Logger LOGGER = LoggerFactory.getClientLogger();
Expand Down Expand Up @@ -91,7 +90,12 @@ private File getCacheFile(String dataPath) throws Exception {
}
File f = new File(path, Constants.SERVER_NODE_CACHE_FILENAME);
if (!f.exists()) {
f.createNewFile();
try {
f.createNewFile();
}catch(IOException e){
LOGGER.error("Create TarsClient cachefile error,please check path is correct, path is :{}{}", path , Constants.SERVER_NODE_CACHE_FILENAME );
throw new IOException(e);
}
}
return f;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import com.qq.tars.support.trace.TraceCallbackFilter;
import com.qq.tars.support.trace.TraceClientFilter;
import com.qq.tars.support.trace.TraceServerFilter;

import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
Expand Down Expand Up @@ -67,6 +66,7 @@ public abstract class BaseAppContext implements AppContext {
@Override
public void init() {
try {

loadServants();
//inject om admin servant
injectAdminServant();
Expand All @@ -79,7 +79,6 @@ public void init() {
System.out.println("[SERVER] failed to start the application.");
}
}

protected abstract void loadServants() throws Exception;

@Override
Expand Down
10 changes: 9 additions & 1 deletion core/src/main/java/com/qq/tars/server/config/ServerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import com.qq.tars.common.support.Endpoint;
import com.qq.tars.common.util.Config;
import com.qq.tars.support.om.OmConstants;

import java.io.File;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;

Expand Down Expand Up @@ -55,6 +55,14 @@ public class ServerConfig {
private LinkedHashMap<String, ServantAdapterConfig> servantAdapterConfMap;
private CommunicatorConfig communicatorConfig;


public void loadSystemConfig(){
try {
this.load(Config.parseFile(System.getProperty("config")));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public ServerConfig load(Config conf) {
application = conf.get("/tars/application/server<app>", "UNKNOWN");
serverName = conf.get("/tars/application/server<server>", null);
Expand Down
13 changes: 6 additions & 7 deletions core/src/main/java/com/qq/tars/support/notify/NotifyHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ private void notify(NOTIFYLEVEL level, String message) {
if (communicator == null) {
return;
}

NotifyPrx notifyPrx = communicator.stringToProxy(NotifyPrx.class, ConfigurationManager.getInstance().getServerConfig().getNotify());
notifyPrx.async_notifyServer(null, app + "." + server, level.value(), message);
report( message, level,false);
} catch (Exception e) {
omLogger.error("RemoteNotify|notify error", e);
}
Expand All @@ -69,23 +67,24 @@ public void notifyError(String info) {
}

public void syncReport(String result) {
report(result, true);
report(result,NOTIFYLEVEL.NOTIFYNORMAL, true);
}

public void asyncReport(String result) {
report(result, false);
report(result, NOTIFYLEVEL.NOTIFYNORMAL,false);
}

private void report(String result, boolean sync) {
private void report(String result,final NOTIFYLEVEL notifylevel, final Boolean sync) {
try {
if (communicator == null) {
return;
}
NotifyPrx notifyPrx = communicator.stringToProxy(NotifyPrx.class, ConfigurationManager.getInstance().getServerConfig().getNotify());
final NotifyPrx notifyPrx = communicator.stringToProxy(NotifyPrx.class, ConfigurationManager.getInstance().getServerConfig().getNotify());
final ReportInfo reportInfo = new ReportInfo();
reportInfo.setSThreadId(String.valueOf(Thread.currentThread().getId()));
reportInfo.setSServer(app + "." + server );
reportInfo.setSMessage( result);
reportInfo.setELevel(notifylevel.value());
if(sync){
notifyPrx.reportNotifyInfo(reportInfo);
}else{
Expand Down
84 changes: 0 additions & 84 deletions core/src/main/java/com/qq/tars/support/notify/prx/NotifyPrx.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,90 +28,6 @@

@Servant
public interface NotifyPrx {
/**
* The information reported by the framework is stored in the database
* @param sServerName, server name
* @param sThreadId, server current thread id
* @param sMessage, message
**/
public void reportServer(String sServerName, String sThreadId, String sMessage);
/**
* The information reported by the framework is stored in the database
* @param sServerName, server name
* @param sThreadId, server current thread id
* @param sMessage, message
**/
public void reportServer(String sServerName, String sThreadId, String sMessage, @TarsContext java.util.Map<String, String> ctx);
/**
* The information reported by the framework is stored in the database
* @param sServerName, server name
* @param sThreadId, server current thread id
* @param sMessage, message
**/
public void async_reportServer(@TarsCallback NotifyPrxCallback callback, String sServerName, String sThreadId, String sMessage);
/**
* The information reported by the framework is stored in the database
* @param sServerName, server name
* @param sThreadId, server current thread id
* @param sMessage, message
**/
public void async_reportServer(@TarsCallback NotifyPrxCallback callback, String sServerName, String sThreadId, String sMessage, @TarsContext java.util.Map<String, String> ctx);
/**
* Business reported information, used for alarm
* @param sServerName, server name
* @param level, notify level
* @param sMessage, message
**/
public void notifyServer(String sServerName, int level, String sMessage);
/**
* Business reported information, used for alarm
* @param sServerName, server name
* @param level, notify level
* @param sMessage, message
**/
public void notifyServer(String sServerName, int level, String sMessage, @TarsContext java.util.Map<String, String> ctx);
/**
* Business reported information, used for alarm
* @param sServerName, server name
* @param level, notify level
* @param sMessage, message
**/
public void async_notifyServer(@TarsCallback NotifyPrxCallback callback, String sServerName, int level, String sMessage);
/**
* Business reported information, used for alarm
* @param sServerName, server name
* @param level, notify level
* @param sMessage, message
**/
public void async_notifyServer(@TarsCallback NotifyPrxCallback callback, String sServerName, int level, String sMessage, @TarsContext java.util.Map<String, String> ctx);
/**
* Get report information
* @param sServerName, server name
* @param out , notify info detail
* @return int 0=success, others=failed
**/
public int getNotifyInfo(NotifyKey stKey, @TarsHolder Holder<NotifyInfo> stInfo);
/**
* Get report information
* @param sServerName, server name
* @param out , notify info detail
* @return int 0=success, others=failed
**/
public int getNotifyInfo(NotifyKey stKey, @TarsHolder Holder<NotifyInfo> stInfo, @TarsContext java.util.Map<String, String> ctx);
/**
* Get report information
* @param sServerName, server name
* @param out , notify info detail
* @return int 0=success, others=failed
**/
public void async_getNotifyInfo(@TarsCallback NotifyPrxCallback callback, NotifyKey stKey);
/**
* Get report information
* @param sServerName, server name
* @param out , notify info detail
* @return int 0=success, others=failed
**/
public void async_getNotifyInfo(@TarsCallback NotifyPrxCallback callback, NotifyKey stKey, @TarsContext java.util.Map<String, String> ctx);

public void reportNotifyInfo(ReportInfo info);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static OmServiceMngr getInstance() {
return Instance;
}

public void initAndStartOmService() {
public synchronized void initAndStartOmService() {
Communicator communicator = CommunicatorFactory.getInstance().getCommunicator();
String app = ConfigurationManager.getInstance().getServerConfig().getApplication();
String serverName = ConfigurationManager.getInstance().getServerConfig().getServerName();
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>com.tencent.tars</groupId>
<artifactId>tars-parent</artifactId>
<version>${revision}</version>
<version>${revision}</version>
<packaging>pom</packaging>
<name>Tars</name>
<description>Super POM For Projects</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

package com.qq.tars.server.apps;

import com.qq.tars.common.support.Endpoint;
import com.qq.tars.common.util.StringUtils;
import com.qq.tars.protocol.annotation.Servant;
import com.qq.tars.protocol.util.TarsHelper;
import com.qq.tars.server.config.ConfigurationException;
import com.qq.tars.server.config.ConfigurationManager;
import com.qq.tars.server.config.ServantAdapterConfig;
import com.qq.tars.server.config.ServerConfig;
Expand All @@ -28,7 +28,11 @@
import com.qq.tars.spring.annotation.TarsServant;
import com.qq.tars.spring.config.TarsClientProperties;
import com.qq.tars.spring.config.TarsServerProperties;
import com.qq.tars.support.om.OmServiceMngr;
import com.qq.tars.support.trace.TarsTraceZipkinConfiguration;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
Expand All @@ -38,10 +42,6 @@
import org.springframework.context.SmartLifecycle;
import org.springframework.core.annotation.AnnotatedElementUtils;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

public class TarsStartLifecycle extends BaseAppContext implements SmartLifecycle, ApplicationContextAware {

private static final Log log = LogFactory.getLog(TarsStartLifecycle.class);
Expand Down Expand Up @@ -82,12 +82,12 @@ public void start() {

initConfig();
loadAppServants();
initServants();
injectAdminServant();
initServants();
appContextStarted();
startServantAdapter();
setAppContext();

OmServiceMngr.getInstance().initAndStartOmService();
this.isRunning = true;
} catch (IOException e) {
e.printStackTrace();
Expand Down Expand Up @@ -119,14 +119,13 @@ private void initConfig() {
serverProperties.setServantAdapterConfMap(new LinkedHashMap<String, ServantAdapterConfig>());
ConfigurationManager.getInstance().setServerConfig(this.serverProperties);
ConfigurationManager.getInstance().setCommunicatorConfig(this.clientProperties);
try {
ConfigurationManager.getInstance().init();
} catch (ConfigurationException e) {
log.error("spring cloud init server config error!" ,e);
throw new RuntimeException(e);
}

servantAdapterConfig = new ServantAdapterConfig();
servantAdapterConfig.setEndpoint(new Endpoint(serverProperties.isUdp() ? "udp" : "tcp", StringUtils.isEmpty(serverProperties.getLocalIP()) ? "0.0.0.0" : serverProperties.getLocalIP(), serverProperties.getPort(), 0, 0, 0, null));
servantAdapterConfig.setHandleGroup("default");
servantAdapterConfig.setMaxConns(serverProperties.getMaxConns());
servantAdapterConfig.setThreads(serverProperties.getThreads());
servantAdapterConfig.setQueueCap(serverProperties.getQueueCap());
servantAdapterConfig.setQueueTimeout(serverProperties.getQueueTimeout());

TarsTraceZipkinConfiguration.getInstance().init();
}
Expand Down Expand Up @@ -178,7 +177,7 @@ private ServantHomeSkeleton loadServant(Object bean) throws Exception {

skeleton = new ServantHomeSkeleton(homeName, homeClassImpl, homeApiClazz, null, null, maxLoadLimit);
skeleton.setAppContext(this);

servantAdapterConfig = ConfigurationManager.getInstance().getServerConfig().getServantAdapterConfMap().get(homeName);
ConfigurationManager.getInstance().getServerConfig().getServantAdapterConfMap().put(homeName, servantAdapterConfig);

return skeleton;
Expand Down

0 comments on commit 90000a5

Please sign in to comment.