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
0546beb9
Commit
0546beb9
authored
Sep 03, 2024
by
SN150021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
对接腾讯云api
parent
2bb0c460
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
202 additions
and
31 deletions
+202
-31
BrowserTask.java
...va/com/edgec/browserbackend/browser/task/BrowserTask.java
+36
-1
TenCentEcsClient.java
.../edgec/browserbackend/common/client/TenCentEcsClient.java
+120
-6
RegionMappingEnum.java
.../edgec/browserbackend/common/enums/RegionMappingEnum.java
+46
-24
No files found.
src/main/java/com/edgec/browserbackend/browser/task/BrowserTask.java
View file @
0546beb9
...
...
@@ -31,6 +31,7 @@ import com.edgec.browserbackend.browser.repository.IpHistoryRepository;
import
com.edgec.browserbackend.browser.repository.IpResourceRepository
;
import
com.edgec.browserbackend.browser.service.IpAndShopService
;
import
com.edgec.browserbackend.common.client.AliEcsClient
;
import
com.edgec.browserbackend.common.client.TenCentEcsClient
;
import
com.edgec.browserbackend.common.commons.utils.NotifyUtils
;
import
com.edgec.browserbackend.common.enums.RegionMappingEnum
;
import
com.edgec.browserbackend.common.utils.ThreadPoolUtils
;
...
...
@@ -170,7 +171,6 @@ public class BrowserTask {
}
CloudPlatformConfig
config
=
cloudPlatformConfigRepository
.
findByPlatformAndRegionId
(
"ali"
,
aliRegion
);
if
(
Objects
.
isNull
(
config
)){
log
.
error
(
"buyIpNewTasks-购买ip error:查询不到阿里云模板配置-region:{}"
,
aliRegion
);
return
;
...
...
@@ -190,6 +190,33 @@ public class BrowserTask {
cloudPlatformOrderRepository
.
save
(
cloudPlatformOrder
);
result
=
true
;
}
}
else
if
(
ipResource
.
getVendorCn
().
equals
(
"腾讯云"
)){
//获取腾讯云服务器region映射
String
tenCentRegion
=
RegionMappingEnum
.
getTenCentRegion
(
ipResource
.
getRegion
());
if
(
StringUtils
.
isEmpty
(
tenCentRegion
)){
log
.
error
(
"buyIpNewTasks-购买ip error:查询不到腾讯云映射api-region:{}"
,
ipResource
.
getRegion
());
return
;
}
CloudPlatformConfig
config
=
cloudPlatformConfigRepository
.
findByPlatformAndRegionId
(
"tenCent"
,
tenCentRegion
);
if
(
Objects
.
isNull
(
config
)){
log
.
error
(
"buyIpNewTasks-购买ip error:查询不到腾讯云模板配置-region:{}"
,
tenCentRegion
);
return
;
}
com
.
tencentcloudapi
.
cvm
.
v20170312
.
models
.
RunInstancesResponse
response
=
TenCentEcsClient
.
runInstances
(
ipResource
.
getOwner
(),
ipResource
.
getUsername
(),
ipResource
.
getPassword
(),
config
);
if
(
Objects
.
nonNull
(
response
.
getInstanceIdSet
())){
String
instanceId
=
response
.
getInstanceIdSet
()[
0
];
cloudPlatformOrder
.
setPlatformOrderId
(
instanceId
);
cloudPlatformOrder
.
setPlatformType
(
"tenCent"
);
cloudPlatformOrder
.
setRegionId
(
tenCentRegion
);
cloudPlatformOrder
.
setRetryCount
(
0
);
ipResource
.
setStatus
(
3
);
ipResourceRepository
.
save
(
ipResource
);
cloudPlatformOrderRepository
.
save
(
cloudPlatformOrder
);
result
=
true
;
}
}
/**
...
...
@@ -349,6 +376,14 @@ public class BrowserTask {
}
}
}
else
if
(
"tenCent"
.
equals
(
cloudPlatformOrder
.
getPlatformType
())){
com
.
tencentcloudapi
.
cvm
.
v20170312
.
models
.
DescribeInstancesResponse
response
=
TenCentEcsClient
.
getDescribeInstances
(
ipResource
.
getOwner
(),
cloudPlatformOrder
.
getPlatformOrderId
(),
cloudPlatformOrder
.
getRegionId
());
/* if (!response.getStatusCode().equals(200)) {
log.error(", fail to query ip : {}",
JSON.toJSON(response.getBody()));
}*/
}
}
catch
(
Exception
e
)
{
log
.
error
(
e
.
getMessage
(),
e
);
...
...
src/main/java/com/edgec/browserbackend/common/client/TenCentEcsClient.java
View file @
0546beb9
...
...
@@ -4,12 +4,23 @@ import com.alibaba.fastjson.JSON;
import
com.edgec.browserbackend.browser.domain.CloudPlatformConfig
;
import
com.edgec.browserbackend.common.utils.Aes
;
import
com.tencentcloudapi.common.Credential
;
import
com.tencentcloudapi.common.profile.HttpProfile
;
import
com.tencentcloudapi.common.profile.ClientProfile
;
import
com.tencentcloudapi.common.profile.HttpProfile
;
import
com.tencentcloudapi.cvm.v20170312.CvmClient
;
import
com.tencentcloudapi.cvm.v20170312.models.*
;
import
darabonba.core.client.ClientOverrideConfiguration
;
import
java.util.concurrent.CompletableFuture
;
import
com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesRequest
;
import
com.tencentcloudapi.cvm.v20170312.models.DescribeInstancesResponse
;
import
com.tencentcloudapi.cvm.v20170312.models.InstanceChargePrepaid
;
import
com.tencentcloudapi.cvm.v20170312.models.LaunchTemplate
;
import
com.tencentcloudapi.cvm.v20170312.models.LoginSettings
;
import
com.tencentcloudapi.cvm.v20170312.models.ModifyInstancesChargeTypeRequest
;
import
com.tencentcloudapi.cvm.v20170312.models.ModifyInstancesChargeTypeResponse
;
import
com.tencentcloudapi.cvm.v20170312.models.RenewInstancesRequest
;
import
com.tencentcloudapi.cvm.v20170312.models.RenewInstancesResponse
;
import
com.tencentcloudapi.cvm.v20170312.models.RunInstancesRequest
;
import
com.tencentcloudapi.cvm.v20170312.models.RunInstancesResponse
;
import
com.tencentcloudapi.cvm.v20170312.models.TerminateInstancesRequest
;
import
com.tencentcloudapi.cvm.v20170312.models.TerminateInstancesResponse
;
import
java.util.Collections
;
import
lombok.RequiredArgsConstructor
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.stereotype.Service
;
...
...
@@ -69,9 +80,16 @@ public class TenCentEcsClient {
String
userData
=
String
.
format
(
getUserData
(),
username
,
password
,
username
);
try
{
RunInstancesRequest
runInstancesRequest
=
new
RunInstancesRequest
();
runInstancesRequest
.
setUserData
(
Aes
.
base64Encode
(
userData
.
getBytes
()));
LaunchTemplate
launchTemplate
=
new
LaunchTemplate
();
launchTemplate
.
setLaunchTemplateId
(
config
.
getTemplateId
());
launchTemplate
.
setLaunchTemplateVersion
(
config
.
getRegionId
().
equals
(
"ap-beijing"
)
?
2L
:
1L
);
runInstancesRequest
.
setLaunchTemplate
(
launchTemplate
);
LoginSettings
loginSettings
=
new
LoginSettings
();
loginSettings
.
setKeyIds
(
Collections
.
singletonList
(
"skey-j9n2x0pl"
).
toArray
(
new
String
[
0
]));
runInstancesRequest
.
setLoginSettings
(
loginSettings
);
log
.
info
(
"调用TenCentEcs创建并运行实例,入参:owner:{},regionId:{}"
,
owner
,
config
.
getRegionId
());
runInstancesResponse
=
cvmClient
.
RunInstances
(
runInstancesRequest
);
runInstancesResponse
=
cvmClient
.
RunInstances
(
runInstancesRequest
);
log
.
info
(
"调用TenCentEcs创建并运行实例,响应参数:owner:{},response:{}"
,
owner
,
JSON
.
toJSON
(
runInstancesResponse
));
}
catch
(
Exception
e
)
{
...
...
@@ -80,4 +98,100 @@ public class TenCentEcsClient {
return
runInstancesResponse
;
}
/**
* 查询单个实例
*/
public
static
DescribeInstancesResponse
getDescribeInstances
(
String
owner
,
String
instanceId
,
String
regionId
)
{
CvmClient
cvmClient
=
createClient
(
regionId
);
DescribeInstancesResponse
describeInstancesResponse
=
null
;
try
{
DescribeInstancesRequest
req
=
new
DescribeInstancesRequest
();
String
[]
instanceIds
=
{
"['"
+
instanceId
+
"']"
};
req
.
setInstanceIds
(
instanceIds
);
log
.
info
(
"调用TenCentEcs查询单个实例,入参:owner:{},regionId:{},instanceId:{}"
,
owner
,
regionId
,
instanceId
);
describeInstancesResponse
=
cvmClient
.
DescribeInstances
(
req
);
log
.
info
(
"调用TenCentEcs查询单个实例,响应参数:owner:{},response:{}"
,
owner
,
JSON
.
toJSON
(
describeInstancesResponse
));
}
catch
(
Exception
e
)
{
log
.
error
(
"fail to TenCentEcs getDescribeInstances {}"
,
e
.
getMessage
());
}
return
describeInstancesResponse
;
}
/**
* 删除ECS(退还实列)
*/
public
static
TerminateInstancesResponse
deleteInstance
(
String
owner
,
String
instanceId
,
String
regionId
)
{
CvmClient
cvmClient
=
createClient
(
regionId
);
TerminateInstancesResponse
response
=
null
;
try
{
TerminateInstancesRequest
req
=
new
TerminateInstancesRequest
();
String
[]
instanceIds
=
{
"['"
+
instanceId
+
"']"
};
req
.
setInstanceIds
(
instanceIds
);
log
.
info
(
"调用TenCentEcs删除ECS实例,入参:owner:{},instanceId:{},regionId{}"
,
owner
,
instanceId
,
regionId
);
response
=
cvmClient
.
TerminateInstances
(
req
);
log
.
info
(
"调用TenCentEcs删除ECS实例,响应参数:owner:{},response:{}"
,
owner
,
JSON
.
toJSON
(
response
));
}
catch
(
Exception
e
)
{
log
.
error
(
"fail to TenCentEcs deleteInstance {}"
,
e
.
getMessage
());
}
return
response
;
}
/**
* 将按量改为包年包月
*/
public
static
ModifyInstancesChargeTypeResponse
modifyInstanceChargeType
(
String
owner
,
int
period
,
String
regionId
,
String
instanceId
)
{
CvmClient
cvmClient
=
createClient
(
regionId
);
ModifyInstancesChargeTypeResponse
response
=
null
;
try
{
String
[]
instanceIds
=
{
"['"
+
instanceId
+
"']"
};
ModifyInstancesChargeTypeRequest
req
=
new
ModifyInstancesChargeTypeRequest
();
req
.
setInstanceIds
(
instanceIds
);
req
.
setInstanceChargeType
(
"PREPAID"
);
InstanceChargePrepaid
instanceChargePrepaid1
=
new
InstanceChargePrepaid
();
instanceChargePrepaid1
.
setPeriod
((
long
)
period
);
req
.
setInstanceChargePrepaid
(
instanceChargePrepaid1
);
log
.
info
(
"调用TenCentEcss将实例由量改为包年包月,入参:owner:{},regionId:{},period:{}, instanceId{}"
,
owner
,
regionId
,
period
,
instanceId
);
response
=
cvmClient
.
ModifyInstancesChargeType
(
req
);
log
.
info
(
"调用TenCentEcss将实例由量改为包年包月,响应参数:owner:{},response:{}"
,
owner
,
JSON
.
toJSON
(
response
));
}
catch
(
Exception
e
)
{
log
.
error
(
"fail to TenCentEcss modifyInstanceChargeType {}"
,
e
.
getMessage
());
}
return
response
;
}
/**
* 续费实例
*/
public
static
RenewInstancesResponse
renewInstance
(
String
owner
,
String
regionId
,
String
instanceId
,
Integer
period
){
CvmClient
cvmClient
=
createClient
(
regionId
);
RenewInstancesResponse
response
=
null
;
try
{
RenewInstancesRequest
req
=
new
RenewInstancesRequest
();
String
[]
instanceIds1
=
{
""
};
req
.
setInstanceIds
(
instanceIds1
);
InstanceChargePrepaid
instanceChargePrepaid1
=
new
InstanceChargePrepaid
();
instanceChargePrepaid1
.
setPeriod
((
long
)
period
);
req
.
setInstanceChargePrepaid
(
instanceChargePrepaid1
);
log
.
info
(
"调用TenCentEcs续费实例,入参:owner:{},regionId:{},instanceId:{},period:{}"
,
owner
,
regionId
,
instanceId
,
period
);
response
=
cvmClient
.
RenewInstances
(
req
);
log
.
info
(
"调用TenCentEcs续费实例,响应参数:owner:{},response:{}"
,
owner
,
JSON
.
toJSON
(
response
));
}
catch
(
Exception
e
)
{
log
.
error
(
"fail to TenCentEcs runInstances {}"
,
e
.
getMessage
());
}
return
response
;
}
}
src/main/java/com/edgec/browserbackend/common/enums/RegionMappingEnum.java
View file @
0546beb9
...
...
@@ -15,30 +15,30 @@ import lombok.Getter;
@Getter
public
enum
RegionMappingEnum
{
MAIN_LAND
(
"mainland"
,
"中国随机"
,
""
),
CN_HZ
(
"hangzhou"
,
"杭州"
,
"cn-hangzhou"
),
CN_SH
(
"shanghai"
,
"上海"
,
"cn-shanghai"
),
CN_SZ
(
"shenzhen"
,
"深圳"
,
"cn-shenzhen"
),
CN_GZ
(
"guangzhou"
,
"广州"
,
"cn-guangzhou"
),
CN_BJ
(
"beijing"
,
"北京"
,
"cn-beijing"
),
CN_QD
(
"qingdao"
,
"青岛"
,
"cn-qingdao"
),
CN_ZJK
(
"zhangjiakou"
,
"张家口"
,
"cn-zhangjiakou"
),
CN_HHHT
(
"huhehaote"
,
"呼和浩特"
,
"cn-huhehaote"
),
CN_CD
(
"chengdu"
,
"成都"
,
"cn-chengdu"
),
CN_HY
(
"heyuan"
,
"河源"
,
"cn-heyuan"
),
CN_HK
(
"hongkong"
,
"中国香港"
,
"cn-hongkong"
),
US
(
"us"
,
"美国随机"
,
""
),
US_CALIFORNIA
(
"california"
,
"加利福尼亚"
,
""
),
//待确认
US_VIRGINIA
(
"virginia"
,
"弗吉尼亚"
,
"us-east-1"
),
EU_UK
(
"uk"
,
"英国-伦敦"
,
"eu-west-1"
),
EU_CENTRAL
(
"germany"
,
"德国-法兰克福"
,
"eu-central-1"
),
JAPAN
(
"japan"
,
"日本-东京"
,
"ap-northeast-1"
),
SINGAPORE
(
"singapore"
,
"新加坡"
,
"ap-southeast-1"
),
AUSTRALIA
(
"australia"
,
"澳大利亚-悉尼"
,
""
),
//待确认
MALAYSIA
(
"malaysia"
,
"马来西亚-吉隆坡"
,
"ap-southeast-3"
),
YAJIADA
(
"yajiada"
,
"印度尼西亚-雅加达"
,
"ap-southeast-5"
),
INDIA
(
"india"
,
"孟买"
,
""
),
//待确认
MEAST
(
"meast"
,
"迪拜"
,
"me-east-1"
),
MAIN_LAND
(
"mainland"
,
"中国随机"
,
""
,
""
),
CN_HZ
(
"hangzhou"
,
"杭州"
,
"cn-hangzhou"
,
""
),
CN_SH
(
"shanghai"
,
"上海"
,
"cn-shanghai"
,
"ap-shanghai"
),
CN_SZ
(
"shenzhen"
,
"深圳"
,
"cn-shenzhen"
,
""
),
CN_GZ
(
"guangzhou"
,
"广州"
,
"cn-guangzhou"
,
"ap-guangzhou"
),
CN_BJ
(
"beijing"
,
"北京"
,
"cn-beijing"
,
"ap-beijing"
),
CN_QD
(
"qingdao"
,
"青岛"
,
"cn-qingdao"
,
""
),
CN_ZJK
(
"zhangjiakou"
,
"张家口"
,
"cn-zhangjiakou"
,
""
),
CN_HHHT
(
"huhehaote"
,
"呼和浩特"
,
"cn-huhehaote"
,
""
),
CN_CD
(
"chengdu"
,
"成都"
,
"cn-chengdu"
,
""
),
CN_HY
(
"heyuan"
,
"河源"
,
"cn-heyuan"
,
""
),
CN_HK
(
"hongkong"
,
"中国香港"
,
"cn-hongkong"
,
""
),
US
(
"us"
,
"美国随机"
,
""
,
""
),
US_CALIFORNIA
(
"california"
,
"加利福尼亚"
,
""
,
""
),
//待确认
US_VIRGINIA
(
"virginia"
,
"弗吉尼亚"
,
"us-east-1"
,
""
),
EU_UK
(
"uk"
,
"英国-伦敦"
,
"eu-west-1"
,
""
),
EU_CENTRAL
(
"germany"
,
"德国-法兰克福"
,
"eu-central-1"
,
""
),
JAPAN
(
"japan"
,
"日本-东京"
,
"ap-northeast-1"
,
""
),
SINGAPORE
(
"singapore"
,
"新加坡"
,
"ap-southeast-1"
,
""
),
AUSTRALIA
(
"australia"
,
"澳大利亚-悉尼"
,
""
,
""
),
//待确认
MALAYSIA
(
"malaysia"
,
"马来西亚-吉隆坡"
,
"ap-southeast-3"
,
""
),
YAJIADA
(
"yajiada"
,
"印度尼西亚-雅加达"
,
"ap-southeast-5"
,
""
),
INDIA
(
"india"
,
"孟买"
,
""
,
""
),
//待确认
MEAST
(
"meast"
,
"迪拜"
,
"me-east-1"
,
""
),
;
...
...
@@ -49,6 +49,8 @@ public enum RegionMappingEnum {
private
final
String
aliRegionApi
;
//阿里云地区api
private
final
String
tenCentRegionApi
;
//腾讯云地区api
public
static
RegionMappingEnum
getByRegion
(
String
type
)
{
return
Arrays
.
stream
(
RegionMappingEnum
.
values
())
...
...
@@ -100,4 +102,24 @@ public enum RegionMappingEnum {
}
return
aliResion
;
}
/**
* @author Chen
* @description 获取腾讯云映射 region
* @date 2024/7/30 21:40
*/
public
static
String
getTenCentRegion
(
String
region
)
{
String
aliResion
=
""
;
RegionMappingEnum
byRegion
=
getByRegion
(
region
);
if
(
Objects
.
isNull
(
byRegion
)){
return
aliResion
;
}
if
(
region
.
equals
(
"mainland"
)){
RegionMappingEnum
regionMappingEnum
=
RegionMappingEnum
.
getCnRandom
();
aliResion
=
regionMappingEnum
.
getTenCentRegionApi
();
}
else
{
aliResion
=
byRegion
.
getTenCentRegionApi
();
}
return
aliResion
;
}
}
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