Commit 9fde8bff authored by SN150021's avatar SN150021

初始化阿里云ESC api掉用Client

parent 819c7c2a
......@@ -140,7 +140,7 @@
<artifactId>joda-time</artifactId>
<version>2.10.3</version>
</dependency>
<dependency>
<!-- <dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.4.0</version>
......@@ -151,7 +151,7 @@
<artifactId>elasticsearch</artifactId>
<version>7.4.0</version>
<scope>provided</scope>
</dependency>
</dependency>-->
<dependency>
<groupId>net.javacrumbs.shedlock</groupId>
<artifactId>shedlock-spring</artifactId>
......@@ -188,6 +188,12 @@
<artifactId> aspectjweaver</artifactId >
<version> 1.8.7</version >
</dependency>
<!--阿里云云服务器ECS api -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>ecs20140526</artifactId>
<version>5.1.9</version>
</dependency>
</dependencies>
......
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);
}
}
}
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