|
@@ -8,7 +8,6 @@ import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.crypto.Mac;
|
|
import javax.crypto.Mac;
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
-import java.beans.ConstructorProperties;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.net.URLEncoder;
|
|
import java.net.URLEncoder;
|
|
import java.text.SimpleDateFormat;
|
|
import java.text.SimpleDateFormat;
|
|
@@ -23,19 +22,27 @@ import java.util.*;
|
|
public class AliYunScalingService implements ScalingService{
|
|
public class AliYunScalingService implements ScalingService{
|
|
|
|
|
|
@Value("${scaling.group.id}")
|
|
@Value("${scaling.group.id}")
|
|
- private String ScalingGroupId;
|
|
|
|
|
|
+ private String scalingGroupId;
|
|
|
|
|
|
@Value("${scaling.rule.ari}")
|
|
@Value("${scaling.rule.ari}")
|
|
- private String ScalingRuleAri;
|
|
|
|
|
|
+ private String scalingRuleAri;
|
|
|
|
|
|
- private static String AccessKeyId = "LTAI4GKZQBM1zZZZBJK7nGjR";
|
|
|
|
|
|
+ @Value("${scaling.accessKeyId:#{null}}")
|
|
|
|
+ private String accessKeyId;
|
|
|
|
+
|
|
|
|
+ @Value("${scaling.accessKeySecret:#{null}}")
|
|
|
|
+ private String accessKeySecret;
|
|
|
|
+
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
+ Float maxRate = new Float(41.42);
|
|
|
|
+ System.out.println(maxRate > 42);
|
|
|
|
+ }
|
|
|
|
|
|
private String sign(String action, Map<String, String> parameters) throws Exception{
|
|
private String sign(String action, Map<String, String> parameters) throws Exception{
|
|
final String HTTP_METHOD = "GET";
|
|
final String HTTP_METHOD = "GET";
|
|
|
|
|
|
final String ALGORITHM = "HmacSHA1";
|
|
final String ALGORITHM = "HmacSHA1";
|
|
final String ENCODING = "UTF-8";
|
|
final String ENCODING = "UTF-8";
|
|
- String keySecret = "bo1ura8KODXASVyZ5fofy0fWFILumz&";
|
|
|
|
|
|
|
|
// 对参数进行排序
|
|
// 对参数进行排序
|
|
String[] sortedKeys = parameters.keySet().toArray(new String[]{});
|
|
String[] sortedKeys = parameters.keySet().toArray(new String[]{});
|
|
@@ -58,7 +65,7 @@ public class AliYunScalingService implements ScalingService{
|
|
stringToSign.append(percentEncode(canonicalizedQueryString.toString().substring(1)));
|
|
stringToSign.append(percentEncode(canonicalizedQueryString.toString().substring(1)));
|
|
|
|
|
|
Mac mac = Mac.getInstance(ALGORITHM);
|
|
Mac mac = Mac.getInstance(ALGORITHM);
|
|
- mac.init(new SecretKeySpec(keySecret.getBytes(ENCODING), ALGORITHM));
|
|
|
|
|
|
+ mac.init(new SecretKeySpec(accessKeySecret.getBytes(ENCODING), ALGORITHM));
|
|
byte[] signData = mac.doFinal(stringToSign.toString().getBytes(ENCODING));
|
|
byte[] signData = mac.doFinal(stringToSign.toString().getBytes(ENCODING));
|
|
|
|
|
|
String signature = new String(org.apache.commons.codec.binary.Base64.encodeBase64(signData));
|
|
String signature = new String(org.apache.commons.codec.binary.Base64.encodeBase64(signData));
|
|
@@ -80,25 +87,33 @@ public class AliYunScalingService implements ScalingService{
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public String createEcs(){
|
|
public String createEcs(){
|
|
- Map<String,String> configs = new HashMap<>(2);
|
|
|
|
- configs.put("ruleAri",ScalingRuleAri);
|
|
|
|
- return createEcsByConfig(configs);
|
|
|
|
|
|
+ return this.createEcs(scalingRuleAri);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public String createEcs(String ruleAri) {
|
|
|
|
+ return this.createEcsByConfig(ruleAri);
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public String createEcsByConfig(Map<String, String> configs) {
|
|
public String createEcsByConfig(Map<String, String> configs) {
|
|
String ruleAri = configs.get("ruleAri");
|
|
String ruleAri = configs.get("ruleAri");
|
|
- try {
|
|
|
|
|
|
+ return this.createEcsByConfig(ruleAri);
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
+ public String createEcsByConfig(String ruleAri) {
|
|
|
|
+ StringBuffer parameterBuffer = new StringBuffer();
|
|
|
|
+ try {
|
|
boolean tag = true;
|
|
boolean tag = true;
|
|
Map<String, String> parameters = null;
|
|
Map<String, String> parameters = null;
|
|
while (tag){
|
|
while (tag){
|
|
- parameters = new HashMap<String, String>();
|
|
|
|
|
|
+ parameters = new HashMap();
|
|
// 加入请求参数
|
|
// 加入请求参数
|
|
parameters.put("Action", "ExecuteScalingRule");
|
|
parameters.put("Action", "ExecuteScalingRule");
|
|
parameters.put("ScalingRuleAri", ruleAri);
|
|
parameters.put("ScalingRuleAri", ruleAri);
|
|
parameters.put("Version", "2014-08-28");
|
|
parameters.put("Version", "2014-08-28");
|
|
- parameters.put("AccessKeyId", AccessKeyId);
|
|
|
|
|
|
+ parameters.put("AccessKeyId", accessKeyId);
|
|
parameters.put("Timestamp", formatIso8601Date(new Date()));
|
|
parameters.put("Timestamp", formatIso8601Date(new Date()));
|
|
parameters.put("SignatureMethod", "HMAC-SHA1");
|
|
parameters.put("SignatureMethod", "HMAC-SHA1");
|
|
parameters.put("SignatureVersion", "1.0");
|
|
parameters.put("SignatureVersion", "1.0");
|
|
@@ -106,15 +121,12 @@ public class AliYunScalingService implements ScalingService{
|
|
parameters.put("Format", "JSON");
|
|
parameters.put("Format", "JSON");
|
|
|
|
|
|
String signature = sign("AttachInstances", parameters);
|
|
String signature = sign("AttachInstances", parameters);
|
|
- System.out.println(signature);
|
|
|
|
if(!signature.contains("+") && !signature.contains("/")){
|
|
if(!signature.contains("+") && !signature.contains("/")){
|
|
tag = false;
|
|
tag = false;
|
|
}
|
|
}
|
|
parameters.put("Signature", signature);
|
|
parameters.put("Signature", signature);
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
- StringBuffer parameterBuffer = new StringBuffer();
|
|
|
|
if (parameters != null) {
|
|
if (parameters != null) {
|
|
Iterator iterator = parameters.keySet().iterator();
|
|
Iterator iterator = parameters.keySet().iterator();
|
|
String key = null;
|
|
String key = null;
|
|
@@ -122,7 +134,7 @@ public class AliYunScalingService implements ScalingService{
|
|
while (iterator.hasNext()) {
|
|
while (iterator.hasNext()) {
|
|
key = (String) iterator.next();
|
|
key = (String) iterator.next();
|
|
if (parameters.get(key) != null) {
|
|
if (parameters.get(key) != null) {
|
|
- value = (String) parameters.get(key);
|
|
|
|
|
|
+ value = parameters.get(key);
|
|
} else {
|
|
} else {
|
|
value = "";
|
|
value = "";
|
|
}
|
|
}
|
|
@@ -133,9 +145,9 @@ public class AliYunScalingService implements ScalingService{
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- return OkHttpUtils.httpGet("http://ess.aliyuncs.com?" + parameterBuffer.toString());
|
|
|
|
|
|
+ return OkHttpUtils.httpGet("http://ess.aliyuncs.com?" + parameterBuffer);
|
|
}catch (Exception e){
|
|
}catch (Exception e){
|
|
- e.printStackTrace();
|
|
|
|
|
|
+ log.error("启动弹性伸缩异常,url:{}", "http://ess.aliyuncs.com?" + parameterBuffer);
|
|
}
|
|
}
|
|
|
|
|
|
return null;
|
|
return null;
|
|
@@ -143,17 +155,23 @@ public class AliYunScalingService implements ScalingService{
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public String deleteEcs(String id){
|
|
public String deleteEcs(String id){
|
|
|
|
+ return this.deleteEcs(scalingGroupId, id);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public String deleteEcs(String groupId, String instanceId){
|
|
|
|
+ StringBuffer parameterBuffer = new StringBuffer();
|
|
try {
|
|
try {
|
|
boolean tag = true;
|
|
boolean tag = true;
|
|
Map<String, String> parameters = null;
|
|
Map<String, String> parameters = null;
|
|
while (tag){
|
|
while (tag){
|
|
- parameters = new HashMap<String, String>();
|
|
|
|
|
|
+ parameters = new HashMap();
|
|
// 加入请求参数
|
|
// 加入请求参数
|
|
parameters.put("Action", "RemoveInstances");
|
|
parameters.put("Action", "RemoveInstances");
|
|
- parameters.put("InstanceId.1", id);
|
|
|
|
- parameters.put("ScalingGroupId", ScalingGroupId);
|
|
|
|
|
|
+ parameters.put("InstanceId.1", instanceId);
|
|
|
|
+ parameters.put("ScalingGroupId", groupId);
|
|
parameters.put("Version", "2014-08-28");
|
|
parameters.put("Version", "2014-08-28");
|
|
- parameters.put("AccessKeyId", AccessKeyId);
|
|
|
|
|
|
+ parameters.put("AccessKeyId", accessKeyId);
|
|
parameters.put("Timestamp", formatIso8601Date(new Date()));
|
|
parameters.put("Timestamp", formatIso8601Date(new Date()));
|
|
parameters.put("SignatureMethod", "HMAC-SHA1");
|
|
parameters.put("SignatureMethod", "HMAC-SHA1");
|
|
parameters.put("SignatureVersion", "1.0");
|
|
parameters.put("SignatureVersion", "1.0");
|
|
@@ -161,14 +179,12 @@ public class AliYunScalingService implements ScalingService{
|
|
parameters.put("Format", "JSON");
|
|
parameters.put("Format", "JSON");
|
|
|
|
|
|
String signature = sign("AttachInstances", parameters);
|
|
String signature = sign("AttachInstances", parameters);
|
|
-// System.out.println(signature);
|
|
|
|
if(!signature.contains("+") && !signature.contains("/")){
|
|
if(!signature.contains("+") && !signature.contains("/")){
|
|
tag = false;
|
|
tag = false;
|
|
}
|
|
}
|
|
parameters.put("Signature", signature);
|
|
parameters.put("Signature", signature);
|
|
}
|
|
}
|
|
|
|
|
|
- StringBuffer parameterBuffer = new StringBuffer();
|
|
|
|
if (parameters != null) {
|
|
if (parameters != null) {
|
|
Iterator iterator = parameters.keySet().iterator();
|
|
Iterator iterator = parameters.keySet().iterator();
|
|
String key = null;
|
|
String key = null;
|
|
@@ -176,7 +192,7 @@ public class AliYunScalingService implements ScalingService{
|
|
while (iterator.hasNext()) {
|
|
while (iterator.hasNext()) {
|
|
key = (String) iterator.next();
|
|
key = (String) iterator.next();
|
|
if (parameters.get(key) != null) {
|
|
if (parameters.get(key) != null) {
|
|
- value = (String) parameters.get(key);
|
|
|
|
|
|
+ value = parameters.get(key);
|
|
} else {
|
|
} else {
|
|
value = "";
|
|
value = "";
|
|
}
|
|
}
|
|
@@ -187,10 +203,9 @@ public class AliYunScalingService implements ScalingService{
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-// System.out.println("POST parameter : " + parameterBuffer.toString());
|
|
|
|
- return OkHttpUtils.httpGet("http://ess.aliyuncs.com?" + parameterBuffer.toString());
|
|
|
|
|
|
+ return OkHttpUtils.httpGet("http://ess.aliyuncs.com?" + parameterBuffer);
|
|
}catch (Exception e){
|
|
}catch (Exception e){
|
|
- e.printStackTrace();
|
|
|
|
|
|
+ log.error("启动弹性伸缩异常,url:{}", "http://ess.aliyuncs.com?" + parameterBuffer);
|
|
}
|
|
}
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|