LD is tigger forever,CG are not brothers forever, throw the pot and shine forever.
Modesty is not false, solid is not naive, treacherous but not deceitful, stay with good people, and stay away from poor people.
talk is cheap, show others the code,Keep progress,make a better result.
Survive during the day and develop at night。
目录
概 述
nacos -cmdb 服务
操作控制器:
@RestController
@RequestMapping(UtilsAndCommons.NACOS_CMDB_CONTEXT + "/ops")
public class OperationController {
@Autowired
private CmdbProvider cmdbProvider;
/**
* query label.
*
* @param request http request
* @return query result
* @throws Exception exception
*/
@RequestMapping(value = "/label", method = RequestMethod.GET)
public String queryLabel(HttpServletRequest request) throws Exception {
String entry = WebUtils.required(request, "entry");
String label = WebUtils.required(request, "label");
return cmdbProvider.queryLabel(entry, "ip", label);
}
}
这个注入一个CmdbProvider Service:
那么CmdbProvider 是如何的?
CmdbProvider 提供了cmdb标签任务,dump 任务cmdb事件任务:
包含了加载初始化,查询实体,查询标签,根据标签查询实体,移除实体,更新实体等。
其中最重要的是:注入了:
@Value("${nacos.cmdb.dumpTaskInterval:3600}")
private int dumpTaskInterval;
@Value("${nacos.cmdb.eventTaskInterval:10}")
private int eventTaskInterval;
@Value("${nacos.cmdb.labelTaskInterval:300}")
private int labelTaskInterval;
@Value("${nacos.cmdb.loadDataAtStart:false}")
private boolean loadDataAtStart;
都是一些属性数值。
取出对应的配置文件的数值:
server.port=8848
server.servlet.context-path=/nacos
nacos.cmdb.dumpTaskInterval=3600
nacos.cmdb.eventTaskInterval=10
nacos.cmdb.loadDataAtStart=true
目前提供了Reader 和Writer方法:
*/
public interface CmdbReader {
/**
* Get entity.
*
* @param entityName name of entity
* @param entityType type of entity
* @return entity
*/
Entity queryEntity(String entityName, String entityType);
/**
* Get label of entity.
*
* @param entityName name of entity
* @param entityType type of entity
* @param labelName label name
* @return label value
*/
String queryLabel(String entityName, String entityType, String labelName);
/**
* Get entities of selected label.
*
* @param labelName name of label
* @param labelValue value of label
* @return list of entity
*/
List<Entity> queryEntitiesByLabel(String labelName, String labelValue);
}
最后交由CmdbExecutor 来执行。
执行者执行:
private static final ScheduledExecutorService GLOBAL_EXECUTOR = ExecutorFactory.Managed
.newScheduledExecutorService(ClassUtils.getCanonicalName(CmdbApp.class), EnvUtil.getAvailableProcessors(),
new NameThreadFactory(“com.alibaba.nacos.cmdb.global.executor”));
public static void scheduleCmdbTask(Runnable runnable, long delay, TimeUnit unit) {
GLOBAL_EXECUTOR.schedule(runnable, delay, unit);
}
主要流程步骤如下:
1.initCmdsService:
2.加载数据,首先判断switches 是否启动,初始化label 标签,使用cmdbService获取Label
3.初始化实体集合,和实体映射数据。
4.调用Spring post方法,开始线程初始化,首先判断cmdb服务是否为空,
不为空,获取实体事件队列。
依次判断事件类型:如果是实体移除的方法:
如果是实体移除的方法调用移除实体的方法,如果是实体的更新方法:
调用实体的更新的方法:
总结:
相关工具如下:
分析:
小结:
主要讲述了nacos -cmdb 服务相关的分析,请大家指正~
参考资料和推荐阅读
1.链接: link