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
e80aee55
Commit
e80aee55
authored
Jul 28, 2024
by
chenchao.deng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
对接阿里云Ecs Api
parent
4355b99c
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
266 additions
and
74 deletions
+266
-74
pom.xml
pom.xml
+2
-3
CloudPlatformOrder.java
...gec/browserbackend/browser/domain/CloudPlatformOrder.java
+30
-0
AliEcsClient.java
...in/java/com/edgec/browserbackend/client/AliEcsClient.java
+0
-70
AliEcsClient.java
.../com/edgec/browserbackend/common/client/AliEcsClient.java
+162
-0
RegionMappingEnum.java
.../edgec/browserbackend/common/enums/RegionMappingEnum.java
+50
-0
application.yml
src/main/resources/application.yml
+1
-1
BrowserBackendApplicationTests.java
.../edgec/browserbackend/BrowserBackendApplicationTests.java
+21
-0
No files found.
pom.xml
View file @
e80aee55
...
...
@@ -191,11 +191,10 @@
<!--阿里云云服务器ECS api -->
<dependency>
<groupId>
com.aliyun
</groupId>
<artifactId>
ecs20140526
</artifactId>
<version>
5.
1.9
</version>
<artifactId>
alibabacloud-
ecs20140526
</artifactId>
<version>
5.
0.13
</version>
</dependency>
</dependencies>
<build>
...
...
src/main/java/com/edgec/browserbackend/browser/domain/CloudPlatformOrder.java
0 → 100644
View file @
e80aee55
package
com
.
edgec
.
browserbackend
.
browser
.
domain
;
import
com.fasterxml.jackson.annotation.JsonIgnoreProperties
;
import
lombok.Getter
;
import
lombok.NoArgsConstructor
;
import
lombok.Setter
;
import
org.springframework.data.annotation.Id
;
import
org.springframework.data.mongodb.core.mapping.Document
;
/**
* 云平台订单表
*
* @Author: Chen
* @Date: 2024/07/28
*/
@Getter
@Setter
@NoArgsConstructor
@Document
(
collection
=
"cloudPlatformOrder"
)
@JsonIgnoreProperties
(
ignoreUnknown
=
true
)
public
class
CloudPlatformOrder
{
@Id
private
String
id
;
private
String
ipResourceId
;
private
String
platformOrderId
;
}
src/main/java/com/edgec/browserbackend/client/AliEcsClient.java
deleted
100644 → 0
View file @
4355b99c
package
com
.
edgec
.
browserbackend
.
client
;
import
com.aliyun.ecs20140526.Client
;
import
com.aliyun.ecs20140526.models.DescribeInstancesRequest
;
import
com.aliyun.tea.TeaException
;
import
com.aliyun.teaopenapi.models.Config
;
import
com.aliyun.teautil.models.RuntimeOptions
;
import
lombok.RequiredArgsConstructor
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.stereotype.Service
;
/**
* @Description //阿里云ECS相关api
* url:https://next.api.aliyun.com/api-tools/sdk/Ecs?version=2014-05-26&language=java-tea&tab=primer-doc
**/
@Service
@RequiredArgsConstructor
@Slf4j
public
class
AliEcsClient
{
/*
* @Description //创建链接
* @Date 2024/7/25 10:32
**/
private
Client
createClient
()
throws
Exception
{
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
// 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html。
Config
config
=
new
Config
()
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
.
setAccessKeyId
(
System
.
getenv
(
"ALIBABA_CLOUD_ACCESS_KEY_ID"
))
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
.
setAccessKeySecret
(
System
.
getenv
(
"ALIBABA_CLOUD_ACCESS_KEY_SECRET"
));
// Endpoint 请参考 https://api.aliyun.com/product/Ecs
config
.
endpoint
=
"ecs.cn-shenzhen.aliyuncs.com"
;
return
new
Client
(
config
);
}
/**
* @Description //查询实列
* @Date 2024/7/25 10:37
**/
public
void
getDescribeInstances
()
throws
Exception
{
java
.
util
.
List
<
String
>
args
=
java
.
util
.
Arrays
.
asList
(
"test"
);
Client
client
=
this
.
createClient
();
DescribeInstancesRequest
describeInstancesRequest
=
new
DescribeInstancesRequest
()
.
setRegionId
(
"cn-shenzhen"
);
RuntimeOptions
runtime
=
new
RuntimeOptions
();
try
{
// 复制代码运行请自行打印 API 的返回值
client
.
describeInstancesWithOptions
(
describeInstancesRequest
,
runtime
);
}
catch
(
TeaException
error
)
{
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
// 错误 message
System
.
out
.
println
(
error
.
getMessage
());
// 诊断地址
System
.
out
.
println
(
error
.
getData
().
get
(
"Recommend"
));
com
.
aliyun
.
teautil
.
Common
.
assertAsString
(
error
.
message
);
}
catch
(
Exception
_error
)
{
TeaException
error
=
new
TeaException
(
_error
.
getMessage
(),
_error
);
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
// 错误 message
System
.
out
.
println
(
error
.
getMessage
());
// 诊断地址
System
.
out
.
println
(
error
.
getData
().
get
(
"Recommend"
));
com
.
aliyun
.
teautil
.
Common
.
assertAsString
(
error
.
message
);
}
}
}
src/main/java/com/edgec/browserbackend/common/client/AliEcsClient.java
0 → 100644
View file @
e80aee55
package
com
.
edgec
.
browserbackend
.
common
.
client
;
import
com.alibaba.fastjson.JSON
;
import
com.aliyun.auth.credentials.Credential
;
import
com.aliyun.auth.credentials.provider.StaticCredentialProvider
;
import
com.aliyun.sdk.service.ecs20140526.AsyncClient
;
import
com.aliyun.sdk.service.ecs20140526.models.DeleteInstanceRequest
;
import
com.aliyun.sdk.service.ecs20140526.models.DeleteInstanceResponse
;
import
com.aliyun.sdk.service.ecs20140526.models.DescribeInstancesRequest
;
import
com.aliyun.sdk.service.ecs20140526.models.DescribeInstancesResponse
;
import
com.aliyun.sdk.service.ecs20140526.models.RenewInstanceRequest
;
import
com.aliyun.sdk.service.ecs20140526.models.RenewInstanceResponse
;
import
com.aliyun.sdk.service.ecs20140526.models.RunInstancesRequest
;
import
com.aliyun.sdk.service.ecs20140526.models.RunInstancesResponse
;
import
darabonba.core.client.ClientOverrideConfiguration
;
import
java.util.concurrent.CompletableFuture
;
import
lombok.RequiredArgsConstructor
;
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
* @Data 2024年7月28日
**/
@Service
@RequiredArgsConstructor
@Slf4j
public
class
AliEcsClient
{
//access_key_id
private
static
String
getAccessKeyId
()
{
return
"LTAI5tEdz1DY4A4oYUawUuNH"
;
}
//access_key_secret
private
static
String
getAccessKeySecret
()
{
return
"8KWmYgO0QIicZpyulhEwUhZqStrTwe"
;
}
//launch_template_id
private
static
String
getLaunchTemplateId
()
{
return
"lt-wz9g4apcsdali7t0lec8"
;
}
/*
* @Description //创建链接
* @Date 2024/7/25 10:32
**/
private
static
AsyncClient
createClient
()
{
StaticCredentialProvider
provider
=
StaticCredentialProvider
.
create
(
Credential
.
builder
()
.
accessKeyId
(
getAccessKeyId
())
.
accessKeySecret
(
getAccessKeySecret
())
.
build
());
return
AsyncClient
.
builder
()
.
region
(
"cn-shenzhen"
)
.
credentialsProvider
(
provider
)
.
overrideConfiguration
(
ClientOverrideConfiguration
.
create
()
.
setEndpointOverride
(
"ecs.cn-shenzhen.aliyuncs.com"
)
)
.
build
();
}
/**
* 创建并运行实例
*/
public
static
RunInstancesResponse
runInstances
(
String
username
,
String
regionId
)
{
AsyncClient
client
=
createClient
();
RunInstancesResponse
runInstancesResponse
=
null
;
try
{
RunInstancesRequest
runInstancesRequest
=
RunInstancesRequest
.
builder
()
.
regionId
(
regionId
)
.
launchTemplateId
(
"lt-wz9g4apcsdali7t0lec8"
)
//.userData("NULL")
.
build
();
log
.
info
(
"调用aliEcs创建并运行实例,入参:userName:{},regionId:{}"
,
username
,
regionId
);
CompletableFuture
<
RunInstancesResponse
>
response
=
client
.
runInstances
(
runInstancesRequest
);
runInstancesResponse
=
response
.
get
();
log
.
info
(
"调用aliEcs创建并运行实例,响应参数:userName:{},response:{}"
,
username
,
JSON
.
toJSON
(
runInstancesResponse
));
}
catch
(
Exception
e
)
{
log
.
error
(
"fail to aliEcs runInstances {}"
,
e
.
getMessage
());
}
finally
{
client
.
close
();
}
return
runInstancesResponse
;
}
/**
* 续费实例
*/
public
static
RenewInstanceResponse
renewInstance
(
String
regionId
)
throws
Exception
{
RenewInstanceRequest
renewInstanceRequest
=
RenewInstanceRequest
.
builder
()
//TODO待确定传参
.
build
();
AsyncClient
client
=
createClient
();
CompletableFuture
<
RenewInstanceResponse
>
response
=
client
.
renewInstance
(
renewInstanceRequest
);
client
.
close
();
return
response
.
get
();
}
/**
* 查询单个实例
*/
public
static
DescribeInstancesResponse
getDescribeInstances
(
String
username
,
String
instanceId
,
String
regionId
)
{
AsyncClient
client
=
createClient
();
DescribeInstancesResponse
resp
=
null
;
try
{
String
instanceIds
=
String
.
format
(
"[\"%s\"]"
,
instanceId
);
DescribeInstancesRequest
describeInstancesRequest
=
DescribeInstancesRequest
.
builder
()
.
regionId
(
regionId
)
.
instanceIds
(
instanceIds
)
.
build
();
log
.
info
(
"调用aliEcs查询单个实例,入参:userName:{},regionId:{},instanceId:{}"
,
username
,
regionId
,
instanceId
);
CompletableFuture
<
DescribeInstancesResponse
>
response
=
client
.
describeInstances
(
describeInstancesRequest
);
resp
=
response
.
get
();
log
.
info
(
"调用aliEcs查询单个实例,响应参数:userName:{},response:{}"
,
username
,
JSON
.
toJSON
(
resp
));
}
catch
(
Exception
e
)
{
log
.
error
(
"fail to aliEcs getDescribeInstances {}"
,
e
.
getMessage
());
}
finally
{
client
.
close
();
}
return
resp
;
}
/**
* 删除ECS
*/
public
static
DeleteInstanceResponse
deleteInstance
(
String
username
,
String
instanceId
)
{
AsyncClient
client
=
createClient
();
DeleteInstanceResponse
resp
=
null
;
try
{
DeleteInstanceRequest
deleteInstanceRequest
=
DeleteInstanceRequest
.
builder
()
.
instanceId
(
instanceId
)
.
force
(
true
)
.
build
();
log
.
info
(
"调用aliEcs删除ECS实例,入参:userName:{},instanceId:{}"
,
username
,
instanceId
);
CompletableFuture
<
DeleteInstanceResponse
>
response
=
client
.
deleteInstance
(
deleteInstanceRequest
);
resp
=
response
.
get
();
log
.
info
(
"调用aliEcs删除ECS实例,响应参数:userName:{},response:{}"
,
username
,
JSON
.
toJSON
(
resp
));
}
catch
(
Exception
e
)
{
log
.
error
(
"fail to aliEcs deleteInstance {}"
,
e
.
getMessage
());
}
finally
{
client
.
close
();
}
return
resp
;
}
}
src/main/java/com/edgec/browserbackend/common/enums/RegionMappingEnum.java
0 → 100644
View file @
e80aee55
package
com
.
edgec
.
browserbackend
.
common
.
enums
;
import
lombok.AllArgsConstructor
;
import
lombok.Getter
;
/**
* @Description //地区映射枚举
* @Date 2023/11/21 10:49
* @Author Chen
**/
@AllArgsConstructor
@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"
),
;
private
final
String
region
;
//地区
private
final
String
regionCn
;
//地区中文
private
final
String
aliRegionApi
;
//阿里云地区api
}
src/main/resources/application.yml
View file @
e80aee55
...
...
@@ -14,7 +14,7 @@ spring:
host
:
smtp.qiye.aliyun.com
port
:
465
username
:
notify@bkunyun.com
password
:
iTbtfIbVmQk9iV&R
password
:
Browser123456
properties
:
mail
:
smtp
:
...
...
src/test/java/com/edgec/browserbackend/BrowserBackendApplicationTests.java
View file @
e80aee55
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.common.client.AliEcsClient
;
import
javax.annotation.Resource
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.boot.test.context.SpringBootTest
;
@SpringBootTest
class
BrowserBackendApplicationTests
{
@Resource
private
UserBalanceRepository
userBalanceRepository
;
@Test
void
contextLoads
()
{
UserBalance
userBalance
=
new
UserBalance
();
userBalance
.
setUsername
(
"13323269174"
);
userBalance
.
setBalanced
(
100
l
);
userBalance
.
setUsed
(
0
l
);
userBalanceRepository
.
save
(
userBalance
);
}
@Test
void
buyEcsTest
()
throws
Exception
{
//AliEcsClient.runInstances("cn-shenzhen");
//AliEcsClient.getDescribeInstances("测试","i-wz9hf0i13xj6h904k14p","cn-shenzhen");
DeleteInstanceResponse
response
=
AliEcsClient
.
deleteInstance
(
"测试"
,
"i-wz9hf0i13xj6h904k14pz"
);
}
}
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