Commit 81831dd5 authored by wutu's avatar wutu

分离分配IP的服务接口

parent 6450eb38
<?xml version="1.0" encoding="UTF-8"?>
<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>bishe</artifactId>
<groupId>top.ninwoo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>bishe-cloud-ipservice</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package top.ninwoo.cloud.ipservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class IpServiceMainApp {
public static void main(String[] args) {
SpringApplication.run(IpServiceMainApp.class, args);
}
}
package top.ninwoo.cloud.ipservice.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.ninwoo.cloud.ipservice.service.IpService;
import javax.annotation.Resource;
import java.util.Set;
@RestController
public class IpController {
@Resource
private IpService ipService;
@RequestMapping("/assignIp")
public String assignIp(long clusterId, String appName, String cid, String ipRange) {
return ipService.assignIp(clusterId, appName, cid, ipRange);
}
@RequestMapping("/getIpByContainerId")
public String getIpByContainerId(String cid) {
return ipService.getContainerIp(cid);
}
public Set<String> getIpListByAppName(long clusterId, String appName) {
return ipService.getIpListByAppName(clusterId, appName);
}
}
\ No newline at end of file
package top.ninwoo.cloud.ipservice.service;
import java.util.Set;
public interface IpService {
String assignIp(long clusterId, String appName, String containerId, String ipRange);
String getContainerIp(String containerId);
Set<String> getIpListByAppName(long clusterId, String appName);
}
package top.ninwoo.cloud.ipservice.service.impl;
import org.springframework.stereotype.Service;
import top.ninwoo.cloud.ipservice.service.IpService;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
@Service
public class IpServiceImpl implements IpService {
HashMap<String, String> ipMap = new HashMap<>();
HashMap<Long, HashMap<String, AtomicInteger>> countMap = new HashMap<>();
// 存储app对应的全部的ip地址
HashMap<Long, Map<String, Set<String>>> appIp = new HashMap<>();
/**
* 分配IP地址
* @param clusterId
* @param appName
* @param containerId
* @param ipRange
* @return
*/
@Override
public String assignIp(long clusterId, String appName, String containerId, String ipRange) {
if(!countMap.containsKey(clusterId)) {
countMap.put(clusterId, new HashMap<>());
}
HashMap<String, AtomicInteger> clusterCountMap = countMap.get(clusterId);
AtomicInteger atomicInteger;
if(ipMap.containsKey(containerId)) {
return ipMap.get(containerId);
}
if(!clusterCountMap.containsKey(ipRange)) {
clusterCountMap.put(ipRange, new AtomicInteger(2));
}
atomicInteger = clusterCountMap.get(ipRange);
String ip = ipRange.substring(0, ipRange.length() - 4) + atomicInteger.getAndIncrement()
+ ipRange.substring(ipRange.length()-3);
ipMap.put(containerId, ip);
// 放到appIp中
if (!appIp.containsKey(clusterId)) {
appIp.put(clusterId, new HashMap<>());
}
Map<String, Set<String>> clusterAppId = appIp.get(clusterId);
if (!clusterAppId.containsKey(appName)) {
clusterAppId.put(appName, new HashSet<>());
}
Set<String> ipSet = clusterAppId.get(appName);
ipSet.add(ip);
return ip;
}
/**
* 通过容器id获取ip地址
* @param containerId
* @return
*/
@Override
public String getContainerIp(String containerId) {
if(!ipMap.containsKey(containerId)) {
throw new RuntimeException("docker容器并没有设置ip地址");
}
return ipMap.get(containerId);
}
@Override
public Set<String> getIpListByAppName(long clusterId, String appName) {
if (!appIp.containsKey(clusterId)) {
return new HashSet<>();
}
Map<String, Set<String>> clusterAppIp = appIp.get(clusterId);
if (!clusterAppIp.containsKey(appName)) {
return new HashSet<>();
}
return clusterAppIp.get(appName);
}
// TODO: 差一个把ip还回去的功能模块
}
\ No newline at end of file
server.port=23333
spring.application.name=bishe-cloud-ipService
\ No newline at end of file
package top.ninwoo.edgecenter.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfiguration {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
...@@ -13,7 +13,8 @@ public interface IpService { ...@@ -13,7 +13,8 @@ public interface IpService {
* @param containerId 容器ID * @param containerId 容器ID
* @return Ipv4的地址 * @return Ipv4的地址
*/ */
String assignIpString(String networkSegment, String containerId); // TODO: 这个应该结合数据库,由云端提供服务,暂时提供一个测试版本
String assignIpString(long clusterId, String appName, String containerId, String networkSegment);
String getContainerIp(String containerId); String getContainerIp(String containerId);
} }
package top.ninwoo.edgecenter.service.impl; package top.ninwoo.edgecenter.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import top.ninwoo.edgecenter.service.IpService; import top.ninwoo.edgecenter.service.IpService;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
...@@ -13,26 +19,34 @@ import java.util.concurrent.atomic.AtomicInteger; ...@@ -13,26 +19,34 @@ import java.util.concurrent.atomic.AtomicInteger;
*/ */
@Service @Service
public class IpServiceImpl implements IpService { public class IpServiceImpl implements IpService {
private static final String ASSIGN_IP = "/assignIp";
private static final String GET_IP_BY_CONTAINERID = "/getIpByContainerId";
@Value("${bs.cloud.ip}")
private String ipServiceHost;
@Autowired
RestTemplate restTemplate;
ConcurrentHashMap<String, AtomicInteger> countMap = new ConcurrentHashMap<>(); ConcurrentHashMap<String, AtomicInteger> countMap = new ConcurrentHashMap<>();
ConcurrentHashMap<String, String> ipMap = new ConcurrentHashMap<>(); ConcurrentHashMap<String, String> ipMap = new ConcurrentHashMap<>();
// TODO: 这个应该结合数据库,由云端提供服务,暂时提供一个测试版本 // TODO: 这个应该结合数据库,由云端提供服务,暂时提供一个测试版本
@Override @Override
public String assignIpString(String networkSegment, String containerId) { public String assignIpString(long clusterId, String appName, String containerId, String networkSegment) {
// networkSegment 10.100.1.0/24 Map<String, Object> map = new HashMap<>();
// 在分配IP前还需要校验下这个网段是否是空闲的网段 map.put("clusterId", clusterId);
AtomicInteger atomicInteger; map.put("appName", appName);
if(ipMap.containsKey(containerId)) { map.put("cid", containerId);
return ipMap.get(containerId); map.put("ipRange", networkSegment);
} ResponseEntity<String> forEntity = restTemplate.getForEntity("http://" + ipServiceHost + ASSIGN_IP, String.class, map);
if(!countMap.containsKey(networkSegment)) { if (!forEntity.getStatusCode().is2xxSuccessful()) {
countMap.put(networkSegment, new AtomicInteger(2)); throw new RuntimeException("获取ip失败" + forEntity.getBody());
} }
atomicInteger = countMap.get(networkSegment); return forEntity.getBody();
String ip = networkSegment.substring(0, networkSegment.length() - 4) + atomicInteger.getAndIncrement()
+ networkSegment.substring(networkSegment.length()-3);
ipMap.put(containerId, ip);
return ip;
} }
@Override @Override
......
...@@ -152,7 +152,7 @@ public class TopologyServiceImpl implements TopologyService { ...@@ -152,7 +152,7 @@ public class TopologyServiceImpl implements TopologyService {
cids.forEach( cid -> { cids.forEach( cid -> {
// ip分配服务 // ip分配服务
// TODO: 网段应该是和网络拓扑绑定到一起,需要重新进行设计 // TODO: 网段应该是和网络拓扑绑定到一起,需要重新进行设计
String ip = ipService.assignIpString("10.10.1.0/24", cid); String ip = ipService.assignIpString(clusterId, containerName,cid,"10.10.1.0/24");
// ovsDockerService.addPort(ovsName, "eth1", containerName, "ip"); // ovsDockerService.addPort(ovsName, "eth1", containerName, "ip");
ovsDockerService.addPort(ovsName, "eth1", cid, ip); ovsDockerService.addPort(ovsName, "eth1", cid, ip);
}); });
...@@ -240,7 +240,7 @@ public class TopologyServiceImpl implements TopologyService { ...@@ -240,7 +240,7 @@ public class TopologyServiceImpl implements TopologyService {
if(appNames[i].startsWith("br:")) { if(appNames[i].startsWith("br:")) {
String ovsName = appNames[i].substring(3); String ovsName = appNames[i].substring(3);
// 添加和交换机的网络链接 // 添加和交换机的网络链接
String ip = ipService.assignIpString("10.10.1.0/24", containerId); String ip = ipService.assignIpString(clusterId, appName,containerId,"10.10.1.0/24");
// ovsDockerService.addPort(ovsName, "eth1", containerName, "ip"); // ovsDockerService.addPort(ovsName, "eth1", containerName, "ip");
ovsDockerService.addPort(ovsName, "eth1", containerId, ip); ovsDockerService.addPort(ovsName, "eth1", containerId, ip);
} }
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
<module>bishe-utils</module> <module>bishe-utils</module>
<module>bishe-cloud-center</module> <module>bishe-cloud-center</module>
<module>bishe-common-api</module> <module>bishe-common-api</module>
<module>bishe-cloud-ipservice</module>
</modules> </modules>
......
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