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 {
* @param containerId 容器ID
* @return Ipv4的地址
*/
String assignIpString(String networkSegment, String containerId);
// TODO: 这个应该结合数据库,由云端提供服务,暂时提供一个测试版本
String assignIpString(long clusterId, String appName, String containerId, String networkSegment);
String getContainerIp(String containerId);
}
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.web.client.RestTemplate;
import top.ninwoo.edgecenter.service.IpService;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
......@@ -13,26 +19,34 @@ import java.util.concurrent.atomic.AtomicInteger;
*/
@Service
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, String> ipMap = new ConcurrentHashMap<>();
// TODO: 这个应该结合数据库,由云端提供服务,暂时提供一个测试版本
@Override
public String assignIpString(String networkSegment, String containerId) {
// networkSegment 10.100.1.0/24
// 在分配IP前还需要校验下这个网段是否是空闲的网段
AtomicInteger atomicInteger;
if(ipMap.containsKey(containerId)) {
return ipMap.get(containerId);
}
if(!countMap.containsKey(networkSegment)) {
countMap.put(networkSegment, new AtomicInteger(2));
public String assignIpString(long clusterId, String appName, String containerId, String networkSegment) {
Map<String, Object> map = new HashMap<>();
map.put("clusterId", clusterId);
map.put("appName", appName);
map.put("cid", containerId);
map.put("ipRange", networkSegment);
ResponseEntity<String> forEntity = restTemplate.getForEntity("http://" + ipServiceHost + ASSIGN_IP, String.class, map);
if (!forEntity.getStatusCode().is2xxSuccessful()) {
throw new RuntimeException("获取ip失败" + forEntity.getBody());
}
atomicInteger = countMap.get(networkSegment);
String ip = networkSegment.substring(0, networkSegment.length() - 4) + atomicInteger.getAndIncrement()
+ networkSegment.substring(networkSegment.length()-3);
ipMap.put(containerId, ip);
return ip;
return forEntity.getBody();
}
@Override
......
......@@ -152,7 +152,7 @@ public class TopologyServiceImpl implements TopologyService {
cids.forEach( cid -> {
// ip分配服务
// 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", cid, ip);
});
......@@ -240,7 +240,7 @@ public class TopologyServiceImpl implements TopologyService {
if(appNames[i].startsWith("br:")) {
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", containerId, ip);
}
......
......@@ -13,6 +13,7 @@
<module>bishe-utils</module>
<module>bishe-cloud-center</module>
<module>bishe-common-api</module>
<module>bishe-cloud-ipservice</module>
</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