----初始化项目

This commit is contained in:
2025-09-19 20:49:14 +08:00
parent b345d2828d
commit df7765c400
2867 changed files with 359313 additions and 89 deletions

96
cmdb/pom.xml Normal file
View File

@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 1999-2018 Alibaba Group Holding Ltd.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>nacos-all</artifactId>
<groupId>com.alibaba.nacos</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nacos-cmdb</artifactId>
<packaging>jar</packaging>
<name>nacos-cmdb ${project.version}</name>
<url>https://nacos.io</url>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>nacos-core</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>nacos-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
<argLine>-Dnacos.standalone=true</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.alibaba.nacos.cmdb.CmdbApp</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>application.properties</exclude>
</excludes>
</resource>
</resources>
</build>
</project>

View File

@ -0,0 +1,34 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.cmdb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* CMDB starter.
*
* @author nkorange
* @since 0.7.0
*/
@SpringBootApplication
public class CmdbApp {
public static void main(String[] args) {
SpringApplication.run(CmdbApp.class, args);
}
}

View File

@ -0,0 +1,55 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.cmdb.controllers;
import com.alibaba.nacos.cmdb.memory.CmdbProvider;
import com.alibaba.nacos.cmdb.utils.UtilsAndCommons;
import com.alibaba.nacos.core.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
/**
* Operation controller.
*
* @author nkorange
* @since 0.7.0
*/
@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);
}
}

View File

@ -0,0 +1,58 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.cmdb.core;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* Switch and options.
*
* @author nkorange
* @since 0.7.0
*/
@Component
public class SwitchAndOptions {
@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;
public int getDumpTaskInterval() {
return dumpTaskInterval;
}
public int getEventTaskInterval() {
return eventTaskInterval;
}
public int getLabelTaskInterval() {
return labelTaskInterval;
}
public boolean isLoadDataAtStart() {
return loadDataAtStart;
}
}

View File

@ -0,0 +1,277 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.cmdb.memory;
import com.alibaba.nacos.api.cmdb.pojo.Entity;
import com.alibaba.nacos.api.cmdb.pojo.EntityEvent;
import com.alibaba.nacos.api.cmdb.pojo.Label;
import com.alibaba.nacos.api.cmdb.spi.CmdbService;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.cmdb.core.SwitchAndOptions;
import com.alibaba.nacos.cmdb.service.CmdbReader;
import com.alibaba.nacos.cmdb.service.CmdbWriter;
import com.alibaba.nacos.cmdb.utils.CmdbExecutor;
import com.alibaba.nacos.cmdb.utils.Loggers;
import com.alibaba.nacos.common.spi.NacosServiceLoader;
import com.alibaba.nacos.common.utils.JacksonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
/**
* CMDB provider.
*
* @author nkorange
* @since 0.7.0
*/
@Component
public class CmdbProvider implements CmdbReader, CmdbWriter {
@Autowired
private SwitchAndOptions switches;
private CmdbService cmdbService;
private final Collection<CmdbService> services = NacosServiceLoader.load(CmdbService.class);
private Map<String, Map<String, Entity>> entityMap = new ConcurrentHashMap<>();
private Map<String, Label> labelMap = new ConcurrentHashMap<>();
private Set<String> entityTypeSet = new HashSet<>();
private long eventTimestamp = System.currentTimeMillis();
public CmdbProvider() throws NacosException {
}
private void initCmdbService() throws NacosException {
Iterator<CmdbService> iterator = services.iterator();
if (iterator.hasNext()) {
cmdbService = iterator.next();
}
if (cmdbService == null && switches.isLoadDataAtStart()) {
throw new NacosException(NacosException.SERVER_ERROR, "Cannot initialize CmdbService!");
}
}
/**
* load data.
*/
public void load() {
if (!switches.isLoadDataAtStart()) {
return;
}
// init label map:
Set<String> labelNames = cmdbService.getLabelNames();
if (labelNames == null || labelNames.isEmpty()) {
Loggers.MAIN.warn("[LOAD] init label names failed!");
} else {
for (String labelName : labelNames) {
// If get null label, it's still ok. We will try it later when we meet this label:
labelMap.put(labelName, cmdbService.getLabel(labelName));
}
}
// init entity type set:
entityTypeSet = cmdbService.getEntityTypes();
// init entity map:
entityMap = cmdbService.getAllEntities();
}
/**
* Init, called by spring.
*
* @throws NacosException nacos exception
*/
@PostConstruct
public void init() throws NacosException {
initCmdbService();
load();
CmdbExecutor.scheduleCmdbTask(new CmdbDumpTask(), switches.getDumpTaskInterval(), TimeUnit.SECONDS);
CmdbExecutor.scheduleCmdbTask(new CmdbLabelTask(), switches.getLabelTaskInterval(), TimeUnit.SECONDS);
CmdbExecutor.scheduleCmdbTask(new CmdbEventTask(), switches.getEventTaskInterval(), TimeUnit.SECONDS);
}
@Override
public Entity queryEntity(String entityName, String entityType) {
if (!entityMap.containsKey(entityType)) {
return null;
}
return entityMap.get(entityType).get(entityName);
}
@Override
public String queryLabel(String entityName, String entityType, String labelName) {
Entity entity = queryEntity(entityName, entityType);
if (entity == null) {
return null;
}
return entity.getLabels().get(labelName);
}
@Override
public List<Entity> queryEntitiesByLabel(String labelName, String labelValue) {
throw new UnsupportedOperationException("Not available now!");
}
/**
* Remove CMDB entity.
*
* @param entityName entity name
* @param entityType entity type
*/
public void removeEntity(String entityName, String entityType) {
if (!entityMap.containsKey(entityType)) {
return;
}
entityMap.get(entityType).remove(entityName);
}
/**
* Update entity.
*
* @param entity entity
*/
public void updateEntity(Entity entity) {
if (!entityTypeSet.contains(entity.getType())) {
return;
}
entityMap.get(entity.getType()).put(entity.getName(), entity);
}
public class CmdbLabelTask implements Runnable {
@Override
public void run() {
Loggers.MAIN.debug("LABEL-TASK {}", "start dump.");
if (cmdbService == null) {
return;
}
try {
Map<String, Label> tmpLabelMap = new HashMap<>(16);
Set<String> labelNames = cmdbService.getLabelNames();
if (labelNames == null || labelNames.isEmpty()) {
Loggers.MAIN.warn("CMDB-LABEL-TASK {}", "load label names failed!");
} else {
for (String labelName : labelNames) {
// If get null label, it's still ok. We will try it later when we meet this label:
tmpLabelMap.put(labelName, cmdbService.getLabel(labelName));
}
if (Loggers.MAIN.isDebugEnabled()) {
Loggers.MAIN.debug("LABEL-TASK {}", "got label map:" + JacksonUtils.toJson(tmpLabelMap));
}
labelMap = tmpLabelMap;
}
} catch (Exception e) {
Loggers.MAIN.error("CMDB-LABEL-TASK {}", "dump failed!", e);
} finally {
CmdbExecutor.scheduleCmdbTask(this, switches.getLabelTaskInterval(), TimeUnit.SECONDS);
}
}
}
public class CmdbDumpTask implements Runnable {
@Override
public void run() {
try {
Loggers.MAIN.debug("DUMP-TASK {}", "start dump.");
if (cmdbService == null) {
return;
}
// refresh entity map:
entityMap = cmdbService.getAllEntities();
} catch (Exception e) {
Loggers.MAIN.error("DUMP-TASK {}", "dump failed!", e);
} finally {
CmdbExecutor.scheduleCmdbTask(this, switches.getDumpTaskInterval(), TimeUnit.SECONDS);
}
}
}
public class CmdbEventTask implements Runnable {
@Override
public void run() {
try {
Loggers.MAIN.debug("EVENT-TASK {}", "start dump.");
if (cmdbService == null) {
return;
}
long current = System.currentTimeMillis();
List<EntityEvent> events = cmdbService.getEntityEvents(eventTimestamp);
eventTimestamp = current;
if (Loggers.MAIN.isDebugEnabled()) {
Loggers.MAIN.debug("EVENT-TASK {}", "got events size:" + ", events:" + JacksonUtils.toJson(events));
}
if (events != null && !events.isEmpty()) {
for (EntityEvent event : events) {
switch (event.getType()) {
case ENTITY_REMOVE:
removeEntity(event.getEntityName(), event.getEntityType());
break;
case ENTITY_ADD_OR_UPDATE:
updateEntity(cmdbService.getEntity(event.getEntityName(), event.getEntityType()));
break;
default:
break;
}
}
}
} catch (Exception e) {
Loggers.MAIN.error("CMDB-EVENT {}", "event task failed!", e);
} finally {
CmdbExecutor.scheduleCmdbTask(this, switches.getEventTaskInterval(), TimeUnit.SECONDS);
}
}
}
}

View File

@ -0,0 +1,58 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.cmdb.service;
import com.alibaba.nacos.api.cmdb.pojo.Entity;
import java.util.List;
/**
* CMDB Reader.
*
* @author nkorange
* @since 0.7.0
*/
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);
}

View File

@ -0,0 +1,27 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.cmdb.service;
/**
* CMDB writer.
*
* @author nkorange
* @since 0.7.0
*/
public interface CmdbWriter {
}

View File

@ -0,0 +1,43 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.cmdb.utils;
import com.alibaba.nacos.cmdb.CmdbApp;
import com.alibaba.nacos.common.executor.ExecutorFactory;
import com.alibaba.nacos.common.executor.NameThreadFactory;
import com.alibaba.nacos.core.utils.ClassUtils;
import com.alibaba.nacos.sys.env.EnvUtil;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
/**
* Cmdb executor.
*
* @author wangweizZZ
* @date 2020/7/13 1:54 PM
*/
public class 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);
}
}

View File

@ -0,0 +1,31 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.cmdb.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Loggers holder.
*
* @author nacos
* @since 0.7.0
*/
public class Loggers {
public static final Logger MAIN = LoggerFactory.getLogger("com.alibaba.nacos.cmdb.main");
}

View File

@ -0,0 +1,31 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.cmdb.utils;
/**
* Utils and constants.
*
* @author nkorange
* @since 0.7.0
*/
public class UtilsAndCommons {
private static final String NACOS_SERVER_VERSION = "/v1";
public static final String NACOS_CMDB_CONTEXT = NACOS_SERVER_VERSION + "/cmdb";
}

View File

@ -0,0 +1,20 @@
#
# Copyright 1999-2018 Alibaba Group Holding Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
server.port=8848
server.servlet.context-path=/nacos
nacos.cmdb.dumpTaskInterval=3600
nacos.cmdb.eventTaskInterval=10
nacos.cmdb.loadDataAtStart=true