Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
browser-backend
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
browser-backend
Commits
acd05077
Commit
acd05077
authored
Jul 30, 2024
by
chenchao.deng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
对接阿里云Ecs Api
parent
9eca9001
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
54 additions
and
35 deletions
+54
-35
BrowserTask.java
...va/com/edgec/browserbackend/browser/task/BrowserTask.java
+8
-20
AliEcsClient.java
.../com/edgec/browserbackend/common/client/AliEcsClient.java
+14
-12
RegionMappingEnum.java
.../edgec/browserbackend/common/enums/RegionMappingEnum.java
+25
-2
BrowserBackendApplicationTests.java
.../edgec/browserbackend/BrowserBackendApplicationTests.java
+7
-1
No files found.
src/main/java/com/edgec/browserbackend/browser/task/BrowserTask.java
View file @
acd05077
...
...
@@ -29,18 +29,13 @@ import com.edgec.browserbackend.common.commons.utils.NotifyUtils;
import
com.edgec.browserbackend.common.enums.RegionMappingEnum
;
import
com.edgec.browserbackend.common.utils.ThreadPoolUtils
;
import
com.edgec.browserbackend.common.utils.Trans
;
import
javax.management.ObjectName
;
import
okhttp3.*
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.poi.ss.formula.DataValidationEvaluator.ValidationEnum
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.cloud.CloudPlatform
;
import
org.springframework.http.HttpMethod
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.scheduling.annotation.Scheduled
;
import
org.springframework.stereotype.Component
;
...
...
@@ -137,7 +132,7 @@ public class BrowserTask {
log
.
info
(
"{}, ip resource purchase : {}"
,
logs
,
ipResource
.
getId
());
try
{
RegionMappingEnum
byRegion
=
RegionMappingEnum
.
getByRegion
(
ipResource
.
getRegion
Cn
());
RegionMappingEnum
byRegion
=
RegionMappingEnum
.
getByRegion
(
ipResource
.
getRegion
());
if
(
Objects
.
isNull
(
byRegion
)){
log
.
error
(
"buyIpNewTasks-购买ip error:查询不到地区映射-region:{}"
,
ipResource
.
getRegion
());
return
;
...
...
@@ -150,29 +145,21 @@ public class BrowserTask {
boolean
result
=
false
;
if
(
ipResource
.
getVendorCn
().
equals
(
"阿里云"
)){
//获取阿里云服务器region映射
String
aliResion
=
null
;
if
(
ipResource
.
getRegion
().
equals
(
"us"
)){
RegionMappingEnum
regionMappingEnum
=
RegionMappingEnum
.
getUsRandom
();
aliResion
=
regionMappingEnum
.
getAliRegionApi
();
}
else
if
(
ipResource
.
getRegion
().
equals
(
"mainland"
)){
RegionMappingEnum
regionMappingEnum
=
RegionMappingEnum
.
getCnRandom
();
aliResion
=
regionMappingEnum
.
getAliRegionApi
();
}
else
{
aliResion
=
byRegion
.
getAliRegionApi
();
}
if
(
StringUtils
.
isEmpty
(
aliResion
)){
String
aliRegion
=
RegionMappingEnum
.
getAliRegion
(
ipResource
.
getRegion
());
if
(
StringUtils
.
isEmpty
(
aliRegion
)){
log
.
error
(
"buyIpNewTasks-购买ip error:查询不到阿里云映射api-region:{}"
,
ipResource
.
getRegion
());
return
;
}
RunInstancesResponse
response
=
AliEcsClient
.
runInstances
(
ipResource
.
getOwner
(),
ipResource
.
getUsername
(),
ipResource
.
getPassword
(),
aliRe
s
ion
);
ipResource
.
getOwner
(),
ipResource
.
getUsername
(),
ipResource
.
getPassword
(),
aliRe
g
ion
);
if
(
null
==
response
||
!
response
.
getStatusCode
().
equals
(
200
))
{
log
.
error
(
"{}, fail to buy ip : {}"
,
logs
,
JSON
.
toJSON
(
response
.
getBody
()));
}
else
if
(
Objects
.
nonNull
(
response
.
getBody
())){
String
instanceId
=
response
.
getBody
().
getInstanceIdSets
().
getInstanceIdSet
().
get
(
0
);
cloudPlatformOrder
.
setPlatformOrderId
(
instanceId
);
cloudPlatformOrder
.
setPlatformType
(
"aliyun"
);
cloudPlatformOrder
.
setRegionId
(
aliRe
s
ion
);
cloudPlatformOrder
.
setRegionId
(
aliRe
g
ion
);
ipResource
.
setStatus
(
3
);
ipResourceRepository
.
save
(
ipResource
);
cloudPlatformOrderRepository
.
save
(
cloudPlatformOrder
);
...
...
@@ -544,8 +531,9 @@ public class BrowserTask {
CloudPlatformOrder
cloudPlatformOrder
=
list
.
get
(
0
);
Boolean
isDelet
=
false
;
if
(
"aliyun"
.
equals
(
cloudPlatformOrder
.
getPlatformType
())){
String
aliRegion
=
RegionMappingEnum
.
getAliRegion
(
ipResource
.
getRegion
());
DeleteInstanceResponse
response
=
AliEcsClient
.
deleteInstance
(
ipResource
.
getOwner
(),
cloudPlatformOrder
.
getPlatformOrderId
());
ipResource
.
getOwner
(),
cloudPlatformOrder
.
getPlatformOrderId
()
,
aliRegion
);
if
(
null
!=
response
&&
response
.
getStatusCode
().
equals
(
200
))
{
isDelet
=
true
;
}
...
...
src/main/java/com/edgec/browserbackend/common/client/AliEcsClient.java
View file @
acd05077
...
...
@@ -20,7 +20,8 @@ import lombok.extern.slf4j.Slf4j;
import
org.springframework.stereotype.Service
;
/**
* @Description 阿里云ECS相关api url:https://next.api.aliyun.com/document/Ecs/2014-05-26/overview
* @Description 阿里云ECS相关api \
* url:https://next.api.aliyun.com/document/Ecs/2014-05-26/overview
* @Data 2024年7月28日
**/
...
...
@@ -61,17 +62,18 @@ public class AliEcsClient {
* @Description //创建链接
* @Date 2024/7/25 10:32
**/
private
static
AsyncClient
createClient
()
{
private
static
AsyncClient
createClient
(
String
regionId
)
{
StaticCredentialProvider
provider
=
StaticCredentialProvider
.
create
(
Credential
.
builder
()
.
accessKeyId
(
getAccessKeyId
())
.
accessKeySecret
(
getAccessKeySecret
())
.
build
());
String
endpoint
=
String
.
format
(
"ecs.%s.aliyuncs.com"
,
regionId
);
return
AsyncClient
.
builder
()
.
region
(
"cn-shenzhen"
)
.
region
(
regionId
)
.
credentialsProvider
(
provider
)
.
overrideConfiguration
(
ClientOverrideConfiguration
.
create
()
.
setEndpointOverride
(
"ecs.cn-shenzhen.aliyuncs.com"
)
.
setEndpointOverride
(
endpoint
)
)
.
build
();
}
...
...
@@ -80,7 +82,7 @@ public class AliEcsClient {
* 创建并运行实例
*/
public
static
RunInstancesResponse
runInstances
(
String
owner
,
String
username
,
String
password
,
String
regionId
)
{
AsyncClient
client
=
createClient
();
AsyncClient
client
=
createClient
(
regionId
);
RunInstancesResponse
runInstancesResponse
=
null
;
String
userData
=
String
.
format
(
getUserData
(),
username
,
password
,
username
);
...
...
@@ -89,7 +91,7 @@ public class AliEcsClient {
.
regionId
(
regionId
)
.
launchTemplateId
(
"lt-wz9g4apcsdali7t0lec8"
)
.
userData
(
Aes
.
base64Encode
(
userData
.
getBytes
()))
.
keyPairName
(
"proxy-ip"
)
//
.keyPairName("proxy-ip")
.
build
();
log
.
info
(
"调用aliEcs创建并运行实例,入参:owner:{},regionId:{}"
,
owner
,
regionId
);
CompletableFuture
<
RunInstancesResponse
>
response
=
client
.
runInstances
(
...
...
@@ -112,7 +114,7 @@ public class AliEcsClient {
RenewInstanceRequest
renewInstanceRequest
=
RenewInstanceRequest
.
builder
()
//TODO待确定传参
.
build
();
AsyncClient
client
=
createClient
();
AsyncClient
client
=
createClient
(
regionId
);
CompletableFuture
<
RenewInstanceResponse
>
response
=
client
.
renewInstance
(
renewInstanceRequest
);
client
.
close
();
...
...
@@ -124,7 +126,7 @@ public class AliEcsClient {
*/
public
static
DescribeInstancesResponse
getDescribeInstances
(
String
owner
,
String
instanceId
,
String
regionId
)
{
AsyncClient
client
=
createClient
();
AsyncClient
client
=
createClient
(
regionId
);
DescribeInstancesResponse
resp
=
null
;
try
{
String
instanceIds
=
String
.
format
(
"[\"%s\"]"
,
instanceId
);
...
...
@@ -152,16 +154,16 @@ public class AliEcsClient {
/**
* 删除ECS
*/
public
static
DeleteInstanceResponse
deleteInstance
(
String
owner
,
String
instanceId
)
{
AsyncClient
client
=
createClient
();
public
static
DeleteInstanceResponse
deleteInstance
(
String
owner
,
String
instanceId
,
String
regionId
)
{
AsyncClient
client
=
createClient
(
regionId
);
DeleteInstanceResponse
resp
=
null
;
try
{
DeleteInstanceRequest
deleteInstanceRequest
=
DeleteInstanceRequest
.
builder
()
.
instanceId
(
instanceId
)
.
force
(
true
)
.
build
();
log
.
info
(
"调用aliEcs删除ECS实例,入参:owner:{},instanceId:{}"
,
owner
,
instanceId
);
log
.
info
(
"调用aliEcs删除ECS实例,入参:owner:{},instanceId:{}
,regionId{}
"
,
owner
,
instanceId
,
regionId
);
CompletableFuture
<
DeleteInstanceResponse
>
response
=
client
.
deleteInstance
(
deleteInstanceRequest
);
resp
=
response
.
get
();
...
...
src/main/java/com/edgec/browserbackend/common/enums/RegionMappingEnum.java
View file @
acd05077
...
...
@@ -50,9 +50,9 @@ public enum RegionMappingEnum {
private
final
String
aliRegionApi
;
//阿里云地区api
public
static
RegionMappingEnum
getByRegion
(
String
type
)
{
public
static
RegionMappingEnum
getByRegion
(
String
type
)
{
return
Arrays
.
stream
(
RegionMappingEnum
.
values
())
.
filter
(
item
->
Objects
.
equals
(
item
.
getRegion
Cn
(),
type
))
.
filter
(
item
->
Objects
.
equals
(
item
.
getRegion
(),
type
))
.
findFirst
().
orElse
(
null
);
}
...
...
@@ -75,4 +75,27 @@ public enum RegionMappingEnum {
return
US_VIRGINIA
.
US_VIRGINIA
;
}
}
/**
* @author Chen
* @description 获取阿里云映射 region
* @date 2024/7/30 21:40
*/
public
static
String
getAliRegion
(
String
region
)
{
String
aliResion
=
""
;
RegionMappingEnum
byRegion
=
getByRegion
(
region
);
if
(
Objects
.
isNull
(
byRegion
)){
return
aliResion
;
}
if
(
region
.
equals
(
"us"
)){
RegionMappingEnum
regionMappingEnum
=
getUsRandom
();
aliResion
=
regionMappingEnum
.
getAliRegionApi
();
}
else
if
(
region
.
equals
(
"mainland"
)){
RegionMappingEnum
regionMappingEnum
=
RegionMappingEnum
.
getCnRandom
();
aliResion
=
regionMappingEnum
.
getAliRegionApi
();
}
else
{
aliResion
=
byRegion
.
getAliRegionApi
();
}
return
aliResion
;
}
}
src/test/java/com/edgec/browserbackend/BrowserBackendApplicationTests.java
View file @
acd05077
...
...
@@ -3,6 +3,7 @@ package com.edgec.browserbackend;
import
com.aliyun.sdk.service.ecs20140526.models.DeleteInstanceResponse
;
import
com.edgec.browserbackend.account.domain.UserBalance
;
import
com.edgec.browserbackend.account.repository.UserBalanceRepository
;
import
com.edgec.browserbackend.browser.task.BrowserTask
;
import
com.edgec.browserbackend.common.client.AliEcsClient
;
import
com.edgec.browserbackend.common.utils.Aes
;
import
javax.annotation.Resource
;
...
...
@@ -15,6 +16,9 @@ class BrowserBackendApplicationTests {
@Resource
private
UserBalanceRepository
userBalanceRepository
;
@Resource
private
BrowserTask
browserTask
;
@Test
void
contextLoads
()
{
UserBalance
userBalance
=
new
UserBalance
();
...
...
@@ -26,9 +30,11 @@ class BrowserBackendApplicationTests {
@Test
void
buyEcsTest
()
throws
Exception
{
//browserTask.buyIpNewTasks();
//AliEcsClient.runInstances("11","cn-shenzhen","2222");
//AliEcsClient.getDescribeInstances("测试","i-wz9hf0i13xj6h904k14p","cn-shenzhen");
//DeleteInstanceResponse response = AliEcsClient.deleteInstance("测试", "i-wz9ebrei1dvowzej6nf
n");
DeleteInstanceResponse
response
=
AliEcsClient
.
deleteInstance
(
"测试"
,
"i-wz91k777e49z85nt5xl3"
,
"cn-shenzhe
n"
);
}
@Test
...
...
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