Commit ffdfc09b authored by wutu's avatar wutu

实现周期性更新OVS状态

parent e7939cd6
......@@ -16,7 +16,21 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.2.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--日志模块-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
......
......@@ -5,6 +5,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.ninwoo.edgecenter.service.ClusterService;
import top.ninwoo.utils.entity.DockerContainer;
import top.ninwoo.utils.entity.OvsBridge;
import java.util.List;
......@@ -20,4 +21,10 @@ public class IndexController {
List<DockerContainer> containerIds = clusterService.getContainerIds(flag == 0);
return containerIds;
}
@RequestMapping("/ovs")
public List<OvsBridge> getOvsBridgeList() {
List<OvsBridge> bridges = clusterService.getOvsBridges();
return bridges;
}
}
......@@ -2,6 +2,7 @@ package top.ninwoo.edgecenter.service;
import top.ninwoo.edgecenter.entity.ClusterConfig;
import top.ninwoo.utils.entity.DockerContainer;
import top.ninwoo.utils.entity.OvsBridge;
import java.util.List;
import java.util.Set;
......@@ -21,4 +22,6 @@ public interface ClusterService {
void removeContainersByClusterId(long clusterId);
void removeContainersByClusterIdAndContainerName(long clusterId, String name);
List<OvsBridge> getOvsBridges();
}
package top.ninwoo.edgecenter.service.impl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.ninwoo.edgecenter.entity.ClusterConfig;
import top.ninwoo.edgecenter.entity.ContainerDescription;
import top.ninwoo.edgecenter.service.ClusterService;
import top.ninwoo.utils.entity.DockerContainer;
import top.ninwoo.utils.entity.OvsBridge;
import top.ninwoo.utils.service.DockerService;
import top.ninwoo.utils.service.OVSService;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
......@@ -18,10 +22,14 @@ import java.util.concurrent.ConcurrentHashMap;
*/
@Service
public class ClusterServiceImpl implements ClusterService {
private final Logger LOG = LoggerFactory.getLogger(ClusterServiceImpl.class);
private ConcurrentHashMap<Long, Map<String, Set<String>>> clustersInfo = new ConcurrentHashMap<>();
@Autowired
DockerService dockerService;
@Autowired
OVSService ovsService;
/**
* 定时更新容器列表
*/
......@@ -149,6 +157,7 @@ public class ClusterServiceImpl implements ClusterService {
*/
@Override
public void removeContainersByClusterIdAndContainerName(long clusterId, String containerName) {
LOG.debug("删除集群 [%l] 的容器 [%s]", clusterId, containerName);
if(!clustersInfo.containsKey(clusterId)) {
return;
}
......@@ -157,4 +166,14 @@ public class ClusterServiceImpl implements ClusterService {
);
clustersInfo.get(clusterId).remove(containerName);
}
/**
* 获取ovs列表
* @return
*/
@Override
public List<OvsBridge> getOvsBridges() {
LOG.debug("查询ovs网桥");
return ovsService.getOvsBridges();
}
}
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!--日志级别以及优先级排序: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<!--Configuration后面的status,这个用于设置log4j2自身内部的信息输出,可以不设置,当设置成trace时,你会看到log4j2内部各种详细输出-->
<!--monitorInterval:Log4j能够自动检测修改配置 文件和重新配置本身,设置间隔秒数-->
<configuration status="WARN" monitorInterval="30">
<Properties>
<Property name="log.path">log</Property>
</Properties>
<!--先定义所有的appender-->
<appenders>
<!--这个输出控制台的配置-->
<console name="Console" target="SYSTEM_OUT">
<!--输出日志的格式-->
<PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
</console>
<File name="log" fileName="${log.path}/test.log" append="false">
<PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
</File>
<RollingFile name="RollingFileInfo" fileName="${log.path}/info.log"
filePattern="${log.path}/logs/${date:yyyy-MM}/info-%d{yyyy-MM-dd}.log.zip">
<!--只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy modulate="true" interval="1"/>
</Policies>
</RollingFile>
</appenders>
<!--然后定义logger,只有定义了logger并引入appender,appender才会生效-->
<loggers>
<!--过滤掉spring和mybatis的一些无用的DEBUG信息-->
<logger name="org.springframework" level="INFO"/>
<logger name="org.mybatis" level="INFO"/>
<logger name="com.baiding" level="INFO"/>
<root level="info">
<appender-ref ref="Console"/>
<appender-ref ref="RollingFileInfo"/>
</root>
</loggers>
</configuration>
\ No newline at end of file
......@@ -27,7 +27,9 @@ public class OvsBridge {
if(obj instanceof OvsBridge) {
OvsBridge bridge = (OvsBridge) obj;
if(bridge.bridgeId.equals(this.bridgeId)) {
if(this.ports.size() == bridge.ports.size()) {
if(this.ports == null && bridge.ports == null) {
return true;
} else if(this.ports.size() == bridge.ports.size()) {
for (int i = 0; i < this.ports.size(); i++) {
if(!this.ports.get(i).equals(bridge.ports.get(i))) {
return false;
......
package top.ninwoo.utils.service;
import top.ninwoo.utils.entity.OvsBridge;
import java.util.List;
public interface OVSService {
List<OvsBridge> getOvsBridges();
}
......@@ -2,6 +2,8 @@ package top.ninwoo.utils.service.impl;
import com.spotify.docker.client.messages.Container;
import com.spotify.docker.client.messages.ContainerInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
......@@ -19,6 +21,8 @@ import java.util.concurrent.locks.ReentrantLock;
@Service
public class DockerServiceImpl implements DockerService, InitializingBean {
private final Logger LOG = LoggerFactory.getLogger(DockerServiceImpl.class);
@Autowired
private DockerUtils dockerUtils;
......@@ -125,6 +129,7 @@ public class DockerServiceImpl implements DockerService, InitializingBean {
@Scheduled(fixedRate = 500)
@Override
public void addNewContainers() {
LOG.debug("更新容器列表");
Map<String, DockerContainer> containers = dockerUtils.getContainers(false);
containers.forEach((cid, container) -> {
if(runingContainers.containsKey(cid)) {
......
......@@ -12,7 +12,9 @@ import top.ninwoo.utils.util.LinuxCtlUtils;
import top.ninwoo.utils.util.OvsUtils;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
......@@ -36,6 +38,13 @@ public class OVSServiceImpl implements OVSService, InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
fullUpdate();
}
/**
* 全量更新
*/
public void fullUpdate() {
isInstall = ovsUtils.isInstall();
if(!isInstall) {
......@@ -51,13 +60,47 @@ public class OVSServiceImpl implements OVSService, InitializingBean {
ovsId = ovs.getId();
}
// TODO:定时更新ovs-vsctl
/**
* 定时更新Ovs状态
*/
@Scheduled(fixedRate = 500)
public void updateOvsStatus() {
Ovs ovs = ovsUtils.showDetails();
// 第一次检查,判断Bridge列表是否减少
if(!ovs.getId().equals(ovsId)) {
// 清空原有的配置,进行一次全量更新
fullUpdate();
}
// 第二次检查,判断是否有新的bridge添加
List<OvsBridge> bridges = ovs.getBridges();
Set<String> updateList = new HashSet<>();
bridges.forEach(b -> {
if(!ovsBridges.containsKey(b.getBridgeId())) {
ovsBridges.put(b.getBridgeId(), b);
updateList.add(b.getBridgeId());
} else if(ovsBridges.containsKey(b.getBridgeId())
&& !ovsBridges.get(b.getBridgeId()).equals(b)) {
ovsBridges.put(b.getBridgeId(), b);
updateList.add(b.getBridgeId());
} else if(ovsBridges.containsKey(b.getBridgeId())
&& ovsBridges.get(b.getBridgeId()).equals(b)) {
updateList.add(b.getBridgeId());
}
});
// 删除丢失的点
ovsBridges.keySet().forEach(k -> {
if(!updateList.contains(k)) {
ovsBridges.remove(k);
}
});
}
/**
* 获取ovs网桥列表
* @return
*/
@Override
public List<OvsBridge> getOvsBridges() {
return new ArrayList<>(ovsBridges.values());
}
}
......@@ -8,6 +8,8 @@ import com.spotify.docker.client.messages.Container;
import com.spotify.docker.client.messages.ContainerInfo;
import com.spotify.docker.client.messages.ExecState;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import top.ninwoo.utils.entity.DockerContainer;
import top.ninwoo.utils.util.DockerUtils;
......@@ -28,6 +30,8 @@ import java.util.Map;
*/
@Utils
public class DockerUtilsImpl implements DockerUtils {
private final Logger LOG = LoggerFactory.getLogger(DockerUtilsImpl.class);
@Autowired
DockerClient dockerClient;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment