admin
2022-04-25 bfb5dfa709ec55ab0cc57981b7d1504088d1bfac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package com.yeshi.buwan.job.domain;
 
import com.tencentcloudapi.clb.v20180317.ClbClient;
import com.tencentcloudapi.clb.v20180317.models.*;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.yeshi.buwan.util.TimeUtil;
 
import java.util.Arrays;
import java.util.List;
 
/**
 * @author hxh
 * @title: TencentCloudSLBUtil
 * @description: 腾讯云负载均衡
 * api:https://console.cloud.tencent.com/api/explorer?Product=clb&Version=2018-03-17&Action=DescribeLoadBalancers&SignVersion=
 * @date 2022/4/16 13:33
 */
public class TencentCloudCLBUtil {
 
    private final static String SECRET_ID = "AKIDTlpgJhLjOozvd6QI2XnpfGbgV4NQJk25";
    private final static String SECRET_KEY = "xhCSUHo55oHUQ6XicFcmfIgspX0EEzWo";
 
 
    /**
     * @return com.tencentcloudapi.clb.v20180317.models.LoadBalancer[]
     * @author hxh
     * @description 获取负载均衡的列表,IsBlock表示是否封堵
     * @date 13:44 2022/4/16
     * @param: name
     **/
    public static LoadBalancer[] getCLBList(String name) throws TencentCloudSDKException {
        // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
        // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
        Credential cred = new Credential(SECRET_ID, SECRET_KEY);
        // 实例化一个http选项,可选的,没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("clb.tencentcloudapi.com");
        // 实例化一个client选项,可选的,没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        ClbClient client = new ClbClient(cred, "ap-guangzhou", clientProfile);
        // 实例化一个请求对象,每个接口都会对应一个request对象
        DescribeLoadBalancersRequest req = new DescribeLoadBalancersRequest();
        req.setLoadBalancerName(name);
        // 返回的resp是一个DescribeLoadBalancersResponse的实例,与请求对象对应
        DescribeLoadBalancersResponse resp = client.DescribeLoadBalancers(req);
 
        return resp.getLoadBalancerSet();
    }
 
 
    /**
     * @return java.lang.String[]
     * @author hxh
     * @description 添加负载均衡
     * @date 17:24 2022/4/25
     * @param: name CLB名称
     **/
    public static String[] addCLB(String name) throws TencentCloudSDKException {
        Credential cred = new Credential(SECRET_ID, SECRET_KEY);
        // 实例化一个http选项,可选的,没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("clb.tencentcloudapi.com");
        // 实例化一个client选项,可选的,没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        ClbClient client = new ClbClient(cred, "ap-guangzhou", clientProfile);
        // 实例化一个请求对象,每个接口都会对应一个request对象
        CreateLoadBalancerRequest req = new CreateLoadBalancerRequest();
        req.setLoadBalancerType("OPEN");
        req.setLoadBalancerName(name);
        // 返回的resp是一个CreateLoadBalancerResponse的实例,与请求对象对应
        CreateLoadBalancerResponse resp = client.CreateLoadBalancer(req);
        return resp.getLoadBalancerIds();
        // 输出json格式的字符串回包
    }
 
    public static LoadBalancer getCLB(String balanceId) throws TencentCloudSDKException {
        Credential cred = new Credential(SECRET_ID, SECRET_KEY);
        // 实例化一个http选项,可选的,没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("clb.tencentcloudapi.com");
        // 实例化一个client选项,可选的,没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        ClbClient client = new ClbClient(cred, "ap-guangzhou", clientProfile);
        // 实例化一个请求对象,每个接口都会对应一个request对象
        DescribeLoadBalancersRequest req = new DescribeLoadBalancersRequest();
        String[] loadBalancerIds1 = {balanceId};
        req.setLoadBalancerIds(loadBalancerIds1);
 
        // 返回的resp是一个DescribeLoadBalancersResponse的实例,与请求对象对应
        DescribeLoadBalancersResponse resp = client.DescribeLoadBalancers(req);
        return resp.getLoadBalancerSet()[0];
    }
 
 
    public static void deleteCLB(String balanceId) throws TencentCloudSDKException {
        Credential cred = new Credential(SECRET_ID, SECRET_KEY);
        // 实例化一个http选项,可选的,没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("clb.tencentcloudapi.com");
        // 实例化一个client选项,可选的,没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        ClbClient client = new ClbClient(cred, "ap-guangzhou", clientProfile);
        // 实例化一个请求对象,每个接口都会对应一个request对象
        DeleteLoadBalancerRequest req = new DeleteLoadBalancerRequest();
        String[] loadBalancerIds1 = {balanceId};
        req.setLoadBalancerIds(loadBalancerIds1);
 
        // 返回的resp是一个DeleteLoadBalancerResponse的实例,与请求对象对应
        DeleteLoadBalancerResponse resp = client.DeleteLoadBalancer(req);
        // 输出json格式的字符串回包
    }
 
    /**
     * @return java.lang.String[]
     * @author hxh
     * @description 添加监听器
     * @date 17:26 2022/4/25
     * @param: balanceId
     * @param: port
     **/
    public static String[] addListener(String balanceId, Long port) throws TencentCloudSDKException {
        // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
        // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
        Credential cred = new Credential(SECRET_ID, SECRET_KEY);
        // 实例化一个http选项,可选的,没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("clb.tencentcloudapi.com");
        // 实例化一个client选项,可选的,没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        ClbClient client = new ClbClient(cred, "ap-guangzhou", clientProfile);
        // 实例化一个请求对象,每个接口都会对应一个request对象
        CreateListenerRequest req = new CreateListenerRequest();
        req.setLoadBalancerId(balanceId);
 
        Long[] ports1 = {port};
        req.setPorts(ports1);
        req.setProtocol("TCP");
        // 返回的resp是一个CreateListenerResponse的实例,与请求对象对应
        CreateListenerResponse resp = client.CreateListener(req);
        return resp.getListenerIds();
    }
 
 
    /**
     * @return com.tencentcloudapi.clb.v20180317.models.Listener[]
     * @author hxh
     * @description 获取负载均衡的监听器接口
     * @date 17:10 2022/4/25
     * @param: balanceId
     **/
    public static Listener[] listListner(String balanceId) throws TencentCloudSDKException {
 
        // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
        // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
        Credential cred = new Credential(SECRET_ID, SECRET_KEY);
        // 实例化一个http选项,可选的,没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("clb.tencentcloudapi.com");
        // 实例化一个client选项,可选的,没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        ClbClient client = new ClbClient(cred, "ap-guangzhou", clientProfile);
        // 实例化一个请求对象,每个接口都会对应一个request对象
        DescribeListenersRequest req = new DescribeListenersRequest();
        req.setLoadBalancerId(balanceId);
        // 返回的resp是一个DescribeListenersResponse的实例,与请求对象对应
        DescribeListenersResponse resp = client.DescribeListeners(req);
 
        return resp.getListeners();
 
    }
 
 
    /**
     * @return void
     * @author hxh
     * @description 绑定负载均衡
     * @date 17:11 2022/4/25
     * @param: balancerId
     * @param: listenerId
     * @param: servers
     **/
    public static void bindServer(String balancerId, String listenerId, List<BindServerInfo> servers) throws TencentCloudSDKException {
        Credential cred = new Credential(SECRET_ID, SECRET_KEY);
        // 实例化一个http选项,可选的,没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("clb.tencentcloudapi.com");
        // 实例化一个client选项,可选的,没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        ClbClient client = new ClbClient(cred, "ap-guangzhou", clientProfile);
        // 实例化一个请求对象,每个接口都会对应一个request对象
        RegisterTargetsRequest req = new RegisterTargetsRequest();
        req.setLoadBalancerId(balancerId);
        req.setListenerId(listenerId);
 
        Target[] targets1 = new Target[servers.size()];
        for (int i = 0; i < servers.size(); i++) {
            Target target = new Target();
            target.setType("CVM");
            target.setInstanceId(servers.get(i).getCvmInstanceId());
            target.setPort(servers.get(i).getPort());
            target.setWeight(servers.get(i).getWeight());
            targets1[i] = target;
        }
 
        req.setTargets(targets1);
 
        // 返回的resp是一个RegisterTargetsResponse的实例,与请求对象对应
        RegisterTargetsResponse resp = client.RegisterTargets(req);
        // 输出json格式的字符串回包
        System.out.println(RegisterTargetsResponse.toJsonString(resp));
    }
 
    public static void createBuWanCLB(int count) throws TencentCloudSDKException {
        String[] clbs = addCLB("布丸-备用" + count + "-" + TimeUtil.getGernalTime(System.currentTimeMillis(), "yyyyMMddHHmmss"));
        if (clbs.length > 0) {
            String balancerId = clbs[0];
            //查询是否正常
            for (int i = 0; i < 5; i++) {
                LoadBalancer balancer = getCLB(balancerId);
                if (balancer.getStatus() == 1) {
                    break;
                }
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            try {
                String[] listeners = addListener(balancerId, 8089L);
                if (listeners.length > 0) {
                    for (int j = 0; j < 5; j++) {
                        try {
                            bindServer(balancerId, listeners[0], Arrays.asList(new BindServerInfo[]{
                                    new BindServerInfo("ins-j7d29j8o", 8089L, 10L),
                                    new BindServerInfo("ins-j7d29j8o", 8086L, 1L),
                            }));
                            break;
                        } catch (Exception e) {
                            Thread.sleep(1000);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                for (int i = 0; i < 5; i++) {
                    try {
                        deleteCLB(balancerId);
                        break;
                    } catch (Exception e1) {
                        e1.printStackTrace();
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e2) {
                            e2.printStackTrace();
                        }
                    }
                }
            }
 
        }
    }
 
 
    public static void main(String[] args) throws TencentCloudSDKException {
        deleteCLB("lb-rmp1hyzm");
    }
 
    static class BindServerInfo {
        //ins-j7d29j8o
        private String cvmInstanceId;
        private long port;
        private long weight;
 
        public BindServerInfo(String cvmInstanceId, long port, long weight) {
            this.cvmInstanceId = cvmInstanceId;
            this.port = port;
            this.weight = weight;
        }
 
        public String getCvmInstanceId() {
            return cvmInstanceId;
        }
 
        public void setCvmInstanceId(String cvmInstanceId) {
            this.cvmInstanceId = cvmInstanceId;
        }
 
        public long getPort() {
            return port;
        }
 
        public void setPort(long port) {
            this.port = port;
        }
 
        public long getWeight() {
            return weight;
        }
 
        public void setWeight(long weight) {
            this.weight = weight;
        }
    }
 
 
}