Commit a8028b50 authored by wutu's avatar wutu

修复完成多点版本的bug

parent 3b84df6c
...@@ -12,4 +12,6 @@ public interface ClusterService { ...@@ -12,4 +12,6 @@ public interface ClusterService {
List<SeparatedClusterConfig> sendClusterConfigToEdgeNode(List<SeparatedClusterConfig> configs); List<SeparatedClusterConfig> sendClusterConfigToEdgeNode(List<SeparatedClusterConfig> configs);
String removeClusterFromEdgeNode(Long clusterId); String removeClusterFromEdgeNode(Long clusterId);
List<String> getAllEdgeNodeIds();
} }
...@@ -19,6 +19,8 @@ public class ClusterServiceImpl implements ClusterService { ...@@ -19,6 +19,8 @@ public class ClusterServiceImpl implements ClusterService {
= "/cluster/sendClusterConfigToEdgeNode"; = "/cluster/sendClusterConfigToEdgeNode";
private final static String REMOVE_CLUSTER_FROM_EDGE_NODE private final static String REMOVE_CLUSTER_FROM_EDGE_NODE
= "/cluster/removeClusterFromEdgeNode?clusterId="; = "/cluster/removeClusterFromEdgeNode?clusterId=";
private final static String GET_ALL_EDGE_NODE_IDS
= "/cluster/getEdgeNodeIds";
@Resource @Resource
private RestTemplate restTemplate; private RestTemplate restTemplate;
...@@ -42,4 +44,10 @@ public class ClusterServiceImpl implements ClusterService { ...@@ -42,4 +44,10 @@ public class ClusterServiceImpl implements ClusterService {
return restTemplate.getForObject("http://" + clientProperties.getCloudUrl() + return restTemplate.getForObject("http://" + clientProperties.getCloudUrl() +
REMOVE_CLUSTER_FROM_EDGE_NODE + clusterId, String.class); REMOVE_CLUSTER_FROM_EDGE_NODE + clusterId, String.class);
} }
@Override
public List<String> getAllEdgeNodeIds() {
return restTemplate.getForObject("http://" + clientProperties.getCloudUrl() +
GET_ALL_EDGE_NODE_IDS, List.class);
}
} }
...@@ -28,4 +28,9 @@ public class ClusterController { ...@@ -28,4 +28,9 @@ public class ClusterController {
} }
return "fail"; return "fail";
} }
@RequestMapping(value = "/getEdgeNodeIds")
public List<String> getEdgeNodeIds() {
return cloudService.getEdgeNodeIds();
}
} }
...@@ -116,7 +116,13 @@ public class CloudRegisterCenter implements Register { ...@@ -116,7 +116,13 @@ public class CloudRegisterCenter implements Register {
} }
} }
else if(event.getType() == PathChildrenCacheEvent.Type.CHILD_REMOVED) { else if(event.getType() == PathChildrenCacheEvent.Type.CHILD_REMOVED) {
String removeNodeId = new String(event.getData().getData());
LOG.info("节点{}已离线", event.getData().getPath()); LOG.info("节点{}已离线", event.getData().getPath());
// 从列表中移除节点
if(availableEdgeNodes.containsKey(removeNodeId)) {
LOG.info("从云中心节点删除注册的边缘节点{}", removeNodeId);
availableEdgeNodes.remove(removeNodeId);
}
} }
}); });
try { try {
......
...@@ -29,4 +29,7 @@ public interface CloudService { ...@@ -29,4 +29,7 @@ public interface CloudService {
List<ContainerMonitorInfo> getContainerMonitorInfoByAppName(Long clusterId, String appName); List<ContainerMonitorInfo> getContainerMonitorInfoByAppName(Long clusterId, String appName);
String cancelNetworkMonitor(Long clusterId, String appName); String cancelNetworkMonitor(Long clusterId, String appName);
List<String> getEdgeNodeIds();
} }
...@@ -539,4 +539,13 @@ public class CloudServiceImpl implements CloudService { ...@@ -539,4 +539,13 @@ public class CloudServiceImpl implements CloudService {
}); });
return containerMonitorInfos; return containerMonitorInfos;
} }
/**
* 获取全部的边缘节点地址
* @return
*/
@Override
public List<String> getEdgeNodeIds() {
return cloudRegisterCenter.getAvailableEdgeNodes();
}
} }
...@@ -86,6 +86,12 @@ ...@@ -86,6 +86,12 @@
<version>2.12.0</version> <version>2.12.0</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
package top.ninwoo.edgecenter.controller; package top.ninwoo.edgecenter.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import top.ninwoo.common.EdgeNodeEntity; import top.ninwoo.common.EdgeNodeEntity;
import top.ninwoo.edgecenter.service.EdgeService;
import top.ninwoo.utils.util.impl.IpUtils;
import javax.annotation.Resource;
@RestController @RestController
@RequestMapping("/edgeNode") @RequestMapping("/edgeNode")
public class EdgeController { public class EdgeController {
@Resource
private EdgeService edgeService;
/** /**
* 获取边缘节点的信息 * 获取边缘节点的信息
* @return * @return
*/ */
@RequestMapping("/info") @RequestMapping("/info")
public EdgeNodeEntity info() { public EdgeNodeEntity info() {
// TODO: 需要添加真实的功能 return edgeService.info();
EdgeNodeEntity edgeNodeEntity = new EdgeNodeEntity();
edgeNodeEntity.setId(11111);
edgeNodeEntity.setName("test");
edgeNodeEntity.setUrl("http://www.baidu.com");
return edgeNodeEntity;
} }
} }
...@@ -85,7 +85,7 @@ public class IndexController { ...@@ -85,7 +85,7 @@ public class IndexController {
return false; return false;
} }
cids.forEach(cid -> { cids.forEach(cid -> {
// 为每一个cid容器创建一个定时任务 TODO 应该加一个判断,防止重复提交 // 为每一个cid容器创建一个定时任务
if (!scheduledFutureMap.containsKey(cid)) { if (!scheduledFutureMap.containsKey(cid)) {
containerInfoMap.put(cid, new ContainerInfo()); containerInfoMap.put(cid, new ContainerInfo());
NetworkMonitorThread networkMonitorThread = new NetworkMonitorThread(cid, containerInfoMap, clusterService); NetworkMonitorThread networkMonitorThread = new NetworkMonitorThread(cid, containerInfoMap, clusterService);
......
...@@ -61,4 +61,9 @@ public class EdgeNodeRegister implements Register { ...@@ -61,4 +61,9 @@ public class EdgeNodeRegister implements Register {
e.printStackTrace(); e.printStackTrace();
} }
} }
@Override
public String getEdgeNodeName() {
return edgeNodeId;
}
} }
...@@ -4,4 +4,6 @@ public interface Register { ...@@ -4,4 +4,6 @@ public interface Register {
void registerNode(); void registerNode();
void registerNode(String nodeId); void registerNode(String nodeId);
String getEdgeNodeName();
} }
package top.ninwoo.edgecenter.service;
import top.ninwoo.common.EdgeNodeEntity;
public interface EdgeService {
EdgeNodeEntity info();
}
package top.ninwoo.edgecenter.service.impl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import top.ninwoo.common.EdgeNodeEntity;
import top.ninwoo.edgecenter.register.Register;
import top.ninwoo.edgecenter.service.EdgeService;
import top.ninwoo.utils.util.impl.IpUtils;
@Service
public class EdgeServiceImpl implements EdgeService {
@Value("${bs.edgenode.name}")
private String edgeNodeName;
@Value("${server.port}")
private int port;
@Value("${bs.edgenode.ip-prefix}")
private String ipPrefix;
@Override
public EdgeNodeEntity info() {
// TODO: 需要添加真实的功能
EdgeNodeEntity edgeNodeEntity = new EdgeNodeEntity();
// TODO: 这里的id考虑是否
edgeNodeEntity.setId(11111);
edgeNodeEntity.setName(edgeNodeName);
edgeNodeEntity.setUrl("http://" + IpUtils.getHostIp(ipPrefix) + ":" + port);
return edgeNodeEntity;
}
}
...@@ -2,9 +2,9 @@ spring: ...@@ -2,9 +2,9 @@ spring:
application: application:
name: bishe-edge-center name: bishe-edge-center
datasource: datasource:
url: jdbc:mysql://127.0.0.1:3306/bishe url: jdbc:mysql://opengn.org:32306/bishe
username: debian-sys-maint username: root
password: rJu7Hmq3eIozdGwd password: Vudo3423ljo
mybatis: mybatis:
configuration: configuration:
......
package top.ninwoo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import top.ninwoo.bishe.starter.entity.ContainerMonitor;
import top.ninwoo.bishe.starter.service.ClusterService;
import top.ninwoo.bishe.starter.service.NetworkService;
import top.ninwoo.common.entity.*;
import top.ninwoo.test.BisheTestMain;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = BisheTestMain.class)
public class BisheMultiNodeTests {
@Autowired
private ClusterService clusterService;
@Resource
private NetworkService networkService;
@Test
public void testSendSeparateConfig() {
ArrayList<SeparatedClusterConfig> clusterConfigs = new ArrayList<>();
SeparatedClusterConfig separatedClusterConfig = new SeparatedClusterConfig();
// TODO: 这个ID应该是从借口获取的
separatedClusterConfig.setEdgeNodeId("192.168.31.154:8081");
ClusterConfig clusterConfig = new ClusterConfig();
clusterConfig.setId(11111l);
clusterConfig.setOwner("joliu");
ContainerDescription containerDescription = new ContainerDescription();
containerDescription.setMode("normal");
containerDescription.setReplicas(1);
DockerContainer container = new DockerContainer();
container.setName("APP1");
container.setCommand("sh");
container.setImage("joliu/networktest");
containerDescription.setDockerContainer(container);
List<ContainerDescription> cds = new ArrayList<>();
cds.add(containerDescription);
ContainerDescription containerDescription1 = new ContainerDescription();
containerDescription1.setMode("normal");
containerDescription1.setReplicas(1);
DockerContainer container1 = new DockerContainer();
container1.setName("APP2");
container1.setCommand("sh");
container1.setImage("joliu/networktest");
containerDescription1.setDockerContainer(container1);
cds.add(containerDescription1);
clusterConfig.setDockers(cds);
NetworkTopology topo = new NetworkTopology();
topo.setAppNames(new String[]{"APP1", "APP2", "br:ovs1", "br:remote:ovs2:192.168.31.16"});
// 这个参数好像没啥用
topo.setTopologyId(11);
topo.setTopology(new int[][]{{0,0,0,0},{0,0,0,0},{1,1,0,0},{0,0,1,0}});
clusterConfig.setTopology(topo);
separatedClusterConfig.setClusterConfig(clusterConfig);
clusterConfigs.add(separatedClusterConfig);
// 构建第二个节点
SeparatedClusterConfig separatedClusterConfig1 = new SeparatedClusterConfig();
// TODO: 这个ID应该是从借口获取的
separatedClusterConfig1.setEdgeNodeId("192.168.31.16:8081");
ClusterConfig clusterConfig1 = new ClusterConfig();
clusterConfig1.setId(11111l);
clusterConfig1.setOwner("joliu");
ContainerDescription containerDescription11 = new ContainerDescription();
containerDescription11.setMode("normal");
containerDescription11.setReplicas(1);
DockerContainer container11 = new DockerContainer();
container11.setName("APP3");
container11.setCommand("sh");
container11.setImage("joliu/networktest");
containerDescription11.setDockerContainer(container11);
List<ContainerDescription> cds1 = new ArrayList<>();
cds1.add(containerDescription11);
ContainerDescription containerDescription12 = new ContainerDescription();
containerDescription12.setMode("normal");
containerDescription12.setReplicas(1);
DockerContainer container12 = new DockerContainer();
container12.setName("APP4");
container12.setCommand("sh");
container12.setImage("joliu/networktest");
containerDescription12.setDockerContainer(container12);
cds1.add(containerDescription12);
clusterConfig1.setDockers(cds1);
NetworkTopology topo1 = new NetworkTopology();
topo1.setAppNames(new String[]{"APP3", "APP4", "br:ovs2", "br:remote:ovs1:192.168.31.154"});
// 这个参数好像没啥用
topo1.setTopologyId(11);
topo1.setTopology(new int[][]{{0,0,0,0},{0,0,0,0},{1,1,0,0},{0,0,1,0}});
clusterConfig1.setTopology(topo1);
separatedClusterConfig1.setClusterConfig(clusterConfig1);
clusterConfigs.add(separatedClusterConfig1);
clusterService.sendClusterConfigToEdgeNode(clusterConfigs);
}
@Test
public void removeClusterFromEdgeNodeTest() {
clusterService.removeClusterFromEdgeNode(11111L);
}
@Test
public void getLogicalNetworkTopologyTest() {
NetworkTopology logicalNetworkTopology = networkService.getLogicalNetworkTopology(11111L);
System.out.println(logicalNetworkTopology);
}
@Test
public void getIpListByAppNameTest() {
List<String> ipList = networkService.getIpListByAppName(11111L, "APP3");
System.out.println(ipList);
}
@Test
public void enableNetworkMonitorTest() throws InterruptedException {
String res = networkService.enableNetworkMonitor(11111L, "Run");
System.out.println(res);
}
@Test
public void cancelNetworkMonitorTest() {
String run = networkService.cancelNetworkMonitor(11111L, "Run");
System.out.println(run);
}
@Test
public void getNetworkMonitorTest() throws InterruptedException {
List<ContainerMonitor> run = networkService.getContainerMonitors(11111L, "Run");
int i = 0;
while(i < 10) {
run.forEach(c -> {
System.out.println(c.getContainerInfo());
});
Thread.sleep(500);
i++;
}
}
@Test
public void getAllEdgeNodeIdsTest() {
List<String> allEdgeNodeIds = clusterService.getAllEdgeNodeIds();
System.out.println(allEdgeNodeIds);
}
}
...@@ -155,9 +155,10 @@ public class OvsUtilsImpl implements OvsUtils { ...@@ -155,9 +155,10 @@ public class OvsUtilsImpl implements OvsUtils {
@Override @Override
public void delBridge(String name) { public void delBridge(String name) {
String cmd = "echo 'Vudo3423' | sudo -S ovs-vsctl del-br " + name; String cmd = "echo 'Vudo3423' | sudo -S ovs-vsctl del-br " + name;
LOG.info("删除网桥[{}]", name);
String res = linuxCtlUtils.runCmd(cmd); String res = linuxCtlUtils.runCmd(cmd);
if(res.contains("Error")) { if(res.contains("Error")) {
throw new RuntimeException("bridge not found!"); LOG.warn("bridge[{}] not found!", name);
} }
} }
......
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