Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
O
openXG-WIC-Cnf
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
alex037yang
openXG-WIC-Cnf
Commits
81831dd5
Commit
81831dd5
authored
Nov 20, 2019
by
wutu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
分离分配IP的服务接口
parent
6450eb38
Changes
11
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
213 additions
and
17 deletions
+213
-17
bishe-cloud-ipservice/pom.xml
bishe-cloud-ipservice/pom.xml
+22
-0
bishe-cloud-ipservice/src/main/java/top/ninwoo/cloud/ipservice/IpServiceMainApp.java
...ain/java/top/ninwoo/cloud/ipservice/IpServiceMainApp.java
+11
-0
bishe-cloud-ipservice/src/main/java/top/ninwoo/cloud/ipservice/controller/IpController.java
...a/top/ninwoo/cloud/ipservice/controller/IpController.java
+29
-0
bishe-cloud-ipservice/src/main/java/top/ninwoo/cloud/ipservice/service/IpService.java
...in/java/top/ninwoo/cloud/ipservice/service/IpService.java
+12
-0
bishe-cloud-ipservice/src/main/java/top/ninwoo/cloud/ipservice/service/impl/IpServiceImpl.java
...op/ninwoo/cloud/ipservice/service/impl/IpServiceImpl.java
+90
-0
bishe-cloud-ipservice/src/main/resources/application.properties
...cloud-ipservice/src/main/resources/application.properties
+3
-0
bishe-edge-center/src/main/java/top/ninwoo/edgecenter/config/RestTemplateConfiguration.java
...p/ninwoo/edgecenter/config/RestTemplateConfiguration.java
+13
-0
bishe-edge-center/src/main/java/top/ninwoo/edgecenter/service/IpService.java
...rc/main/java/top/ninwoo/edgecenter/service/IpService.java
+2
-1
bishe-edge-center/src/main/java/top/ninwoo/edgecenter/service/impl/IpServiceImpl.java
...ava/top/ninwoo/edgecenter/service/impl/IpServiceImpl.java
+28
-14
bishe-edge-center/src/main/java/top/ninwoo/edgecenter/service/impl/TopologyServiceImpl.java
...p/ninwoo/edgecenter/service/impl/TopologyServiceImpl.java
+2
-2
pom.xml
pom.xml
+1
-0
No files found.
bishe-cloud-ipservice/pom.xml
0 → 100644
View file @
81831dd5
<?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
bishe-cloud-ipservice/src/main/java/top/ninwoo/cloud/ipservice/IpServiceMainApp.java
0 → 100644
View file @
81831dd5
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
);
}
}
bishe-cloud-ipservice/src/main/java/top/ninwoo/cloud/ipservice/controller/IpController.java
0 → 100644
View file @
81831dd5
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
bishe-cloud-ipservice/src/main/java/top/ninwoo/cloud/ipservice/service/IpService.java
0 → 100644
View file @
81831dd5
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
);
}
bishe-cloud-ipservice/src/main/java/top/ninwoo/cloud/ipservice/service/impl/IpServiceImpl.java
0 → 100644
View file @
81831dd5
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
bishe-cloud-ipservice/src/main/resources/application.properties
0 → 100644
View file @
81831dd5
server.port
=
23333
spring.application.name
=
bishe-cloud-ipService
\ No newline at end of file
bishe-edge-center/src/main/java/top/ninwoo/edgecenter/config/RestTemplateConfiguration.java
0 → 100644
View file @
81831dd5
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
();
}
}
bishe-edge-center/src/main/java/top/ninwoo/edgecenter/service/IpService.java
View file @
81831dd5
...
...
@@ -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
);
}
bishe-edge-center/src/main/java/top/ninwoo/edgecenter/service/impl/IpServiceImpl.java
View file @
81831dd5
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
...
...
bishe-edge-center/src/main/java/top/ninwoo/edgecenter/service/impl/TopologyServiceImpl.java
View file @
81831dd5
...
...
@@ -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
);
}
...
...
pom.xml
View file @
81831dd5
...
...
@@ -13,6 +13,7 @@
<module>
bishe-utils
</module>
<module>
bishe-cloud-center
</module>
<module>
bishe-common-api
</module>
<module>
bishe-cloud-ipservice
</module>
</modules>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment