In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
What is the use of notifyConfigInfo in nacos? I believe many inexperienced people don't know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
CommunicationController
Nacos-1.1.3/config/src/main/java/com/alibaba/nacos/config/server/controller/CommunicationController.java
@ Controller@RequestMapping (Constants.COMMUNICATION_CONTROLLER_PATH) public class CommunicationController {private final DumpService dumpService; private final LongPollingService longPollingService; private String trueStr = "true"; @ Autowired public CommunicationController (DumpService dumpService, LongPollingService longPollingService) {this.dumpService = dumpService; this.longPollingService = longPollingService } / * Notification configuration information changes * / @ RequestMapping (value = "/ dataChange", method = RequestMethod.GET) @ ResponseBody public Boolean notifyConfigInfo (HttpServletRequest request, HttpServletResponse response, @ RequestParam ("dataId") String dataId, @ RequestParam ("group") String group, @ RequestParam (value = "tenant", required = false) DefaultValue = StringUtils.EMPTY) String tenant, @ RequestParam (value = "tag" required = false) String tag) {dataId = dataId.trim () Group = group.trim (); String lastModified = request.getHeader (NotifyService.NOTIFY_HEADER_LAST_MODIFIED); long lastModifiedTs = StringUtils.isEmpty (lastModified)?-1: Long.parseLong (lastModified); String handleIp = request.getHeader (NotifyService.NOTIFY_HEADER_OP_HANDLE_IP); String isBetaStr = request.getHeader ("isBeta") If (StringUtils.isNotBlank (isBetaStr) & & trueStr.equals (isBetaStr)) {dumpService.dump (dataId, group, tenant, lastModifiedTs, handleIp, true);} else {dumpService.dump (dataId, group, tenant, tag, lastModifiedTs, handleIp);} return true;} / /.}
The notifyConfigInfo method mainly executes the dumpService.dump method, but whether the dump method called by beta is different
DumpService
Nacos-1.1.3/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpService.java
@ Servicepublic class DumpService {@ Autowired private Environment env; @ Autowired PersistService persistService; @ PostConstruct public void init () {LogUtil.defaultLog.warn ("DumpService start"); DumpProcessor processor = new DumpProcessor (this); DumpAllProcessor dumpAllProcessor = new DumpAllProcessor (this); DumpAllBetaProcessor dumpAllBetaProcessor = new DumpAllBetaProcessor (this); DumpAllTagProcessor dumpAllTagProcessor = new DumpAllTagProcessor (this); dumpTaskMgr = new TaskManager ("com.alibaba.nacos.server.DumpTaskManager") DumpTaskMgr.setDefaultTaskProcessor (processor); dumpAllTaskMgr = new TaskManager ("com.alibaba.nacos.server.DumpAllTaskManager"); dumpAllTaskMgr.setDefaultTaskProcessor (dumpAllProcessor); / /. } / * Total dump interval * / static final int DUMP_ALL_INTERVAL_IN_MINUTE = 6 * 60; / * Total dump interval * / static final int INITIAL_DELAY_IN_MINUTE = 6 * 60; private TaskManager dumpTaskMgr; private TaskManager dumpAllTaskMgr; private static final Logger log = LoggerFactory.getLogger (DumpService.class); static final AtomicInteger FINISHED = new AtomicInteger () Static final int INIT_THREAD_COUNT = 10; int total = 0; private final static String TRUE_STR = "true"; private final static String BETA_TABLE_NAME = "config_info_beta"; private final static String TAG_TABLE_NAME = "config_info_tag"; Boolean isQuickStart = false; private int retentionDays = 30; / /. Public void dump (String dataId, String group, String tenant, long lastModified, String handleIp, boolean isBeta) {String groupKey = GroupKey2.getKey (dataId, group, tenant); dumpTaskMgr.addTask (groupKey, new DumpTask (groupKey, lastModified, handleIp, isBeta));} public void dump (String dataId, String group, String tenant, String tag, long lastModified, String handleIp) {dump (dataId, group, tenant, tag, lastModified, handleIp, false) } public void dump (String dataId, String group, String tenant, String tag, long lastModified, String handleIp, boolean isBeta) {String groupKey = GroupKey2.getKey (dataId, group, tenant); dumpTaskMgr.addTask (groupKey, new DumpTask (groupKey, tag, lastModified, handleIp, isBeta);} /.}
The last step of the dump method is to add the defaultTaskProcessor of DumpTask;dumpTaskMgr to dumpTaskMgr as dumpProcessor
TaskManager
Nacos-1.1.3/config/src/main/java/com/alibaba/nacos/config/server/manager/TaskManager.java
Public final class TaskManager implements TaskManagerMBean {private static final Logger log = LogUtil.defaultLog; private final ConcurrentHashMap tasks = new ConcurrentHashMap (); private final ConcurrentHashMap taskProcessors = new ConcurrentHashMap (); private TaskProcessor defaultTaskProcessor; Thread processingThread; private final AtomicBoolean closed = new AtomicBoolean (true); private String name Class ProcessRunnable implements Runnable {@ Override public void run () {while (! TaskManager.this.closed.get ()) {try {Thread.sleep (100); TaskManager.this.process () } catch (Throwable e) {}} ReentrantLock lock = new ReentrantLock (); Condition notEmpty = this.lock.newCondition (); public TaskManager () {this (null);} public AbstractTask getTask (String type) {return this.tasks.get (type) } public TaskProcessor getTaskProcessor (String type) {return this.taskProcessors.get (type);} @ SuppressWarnings ("PMD.AvoidManuallyCreateThreadRule") public TaskManager (String name) {this.name = name; if (null! = name & & name.length () > 0) {this.processingThread = new Thread (new ProcessRunnable (), name);} else {this.processingThread = new Thread (new ProcessRunnable ()) } this.processingThread.setDaemon (true); this.closed.set (false); this.processingThread.start ();} / /. / * add task to task Map * * @ param type * @ param task * / public void addTask (String type, AbstractTask task) {this.lock.lock (); try {AbstractTask oldTask = tasks.put (type, task); MetricsMonitor.getDumpTaskMonitor () .set (tasks.size ()) If (null! = oldTask) {task.merge (oldTask);}} finally {this.lock.unlock ();}} protected void process () {for (Map.Entry entry: this.tasks.entrySet ()) {AbstractTask task = null; this.lock.lock () Try {/ / get task task = entry.getValue (); if (null! = task) {if (! task.shouldProcess ()) {/ / the task currently does not need to be executed, just skip continue } / / first delete the task from the task Map this.tasks.remove (entry.getKey ()); MetricsMonitor.getDumpTaskMonitor () .set (tasks.size ());}} finally {this.lock.unlock () } if (null! = task) {/ / get task processor TaskProcessor processor = this.taskProcessors.get (entry.getKey ()) If (null = = processor) {/ / if there is no processor set according to the task type, use the default processor processor = this.getDefaultTaskProcessor ();} if (null! = processor) {boolean result = false Try {/ / processing task result = processor.process (entry.getKey (), task);} catch (Throwable t) {log.error ("task_fail", "failed to process task", t) } if (! result) {/ / task processing failed, set the last processing time task.setLastProcessTime (System.currentTimeMillis ()) / / rejoin the task to the task Map this.addTask (entry.getKey (), task);} if (tasks.isEmpty ()) {this.lock.lock () Try {this.notEmpty.signalAll ();} finally {this.lock.unlock ();}} / /.}
TaskManager's addTask method adds AbstractTask; to tasks its constructor starts ProcessRunnable, and its run method mainly executes the TaskManager.this.process () method; this method traverses the tasks, fetches the task, and then executes the task through the TaskProcessor's process method
DumpProcessor
Nacos-1.1.3/config/src/main/java/com/alibaba/nacos/config/server/service/dump/DumpTask.java
Class DumpProcessor implements TaskProcessor {DumpProcessor (DumpService dumpService) {this.dumpService = dumpService;} @ Override public boolean process (String taskType, AbstractTask task) {DumpTask dumpTask = (DumpTask) task; String [] pair = GroupKey2.parseKey (dumpTask.groupKey); String dataId = pair [0]; String group = pair [1]; String tenant = pair [2]; long lastModified = dumpTask.lastModified; String handleIp = dumpTask.handleIp Boolean isBeta = dumpTask.isBeta; String tag = dumpTask.tag; if (isBeta) {/ / beta publish, then dump data, update beta cache ConfigInfo4Beta cf = dumpService.persistService.findConfigInfo4Beta (dataId, group, tenant); boolean result If (null! = cf) {result = ConfigService.dumpBeta (dataId, group, tenant, cf.getContent (), lastModified, cf.getBetaIps ()) If (result) {ConfigTraceService.logDumpEvent (dataId, group, tenant, null, lastModified, handleIp, ConfigTraceService.DUMP_EVENT_OK, System.currentTimeMillis ()-lastModified, cf.getContent (). Length ());} else {result = ConfigService.removeBeta (dataId, group, tenant) If (result) {ConfigTraceService.logDumpEvent (dataId, group, tenant, null, lastModified, handleIp, ConfigTraceService.DUMP_EVENT_REMOVE_OK, System.currentTimeMillis ()-lastModified, 0);}} return result } else {if (StringUtils.isBlank (tag)) {ConfigInfo cf = dumpService.persistService.findConfigInfo (dataId, group, tenant); if (dataId.equals (AggrWhitelist.AGGRIDS_METADATA)) {if (null! = cf) {AggrWhitelist.load (cf.getContent ()) } else {AggrWhitelist.load (null);}} if (dataId.equals (ClientIpWhiteList.CLIENT_IP_WHITELIST_METADATA)) {if (null! = cf) {ClientIpWhiteList.load (cf.getContent ()) } else {ClientIpWhiteList.load (null);}} if (dataId.equals (SwitchService.SWITCH_META_DATAID)) {if (null! = cf) {SwitchService.load (cf.getContent ()) } else {SwitchService.load (null);}} boolean result; if (null! = cf) {result = ConfigService.dump (dataId, group, tenant, cf.getContent (), lastModified) If (result) {ConfigTraceService.logDumpEvent (dataId, group, tenant, null, lastModified, handleIp, ConfigTraceService.DUMP_EVENT_OK, System.currentTimeMillis ()-lastModified, cf.getContent (). Length ()) }} else {result = ConfigService.remove (dataId, group, tenant); if (result) {ConfigTraceService.logDumpEvent (dataId, group, tenant, null, lastModified, handleIp, ConfigTraceService.DUMP_EVENT_REMOVE_OK, System.currentTimeMillis ()-lastModified, 0) }} return result;} else {ConfigInfo4Tag cf = dumpService.persistService.findConfigInfo4Tag (dataId, group, tenant, tag); / / boolean result If (null! = cf) {result = ConfigService.dumpTag (dataId, group, tenant, tag, cf.getContent (), lastModified) If (result) {ConfigTraceService.logDumpEvent (dataId, group, tenant, null, lastModified, handleIp, ConfigTraceService.DUMP_EVENT_OK, System.currentTimeMillis ()-lastModified, cf.getContent (). Length ()) }} else {result = ConfigService.removeTag (dataId, group, tenant, tag); if (result) {ConfigTraceService.logDumpEvent (dataId, group, tenant, null, lastModified, handleIp, ConfigTraceService.DUMP_EVENT_REMOVE_OK, System.currentTimeMillis ()-lastModified, 0) }} return result;} final DumpService dumpService;}
DumpProcessor implements the TaskProcessor interface, and its process method is mainly to execute ConfigService.dump or remove methods according to different conditions.
After reading the above, have you mastered the useful methods of notifyConfigInfo in nacos? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.