This commit is contained in:
zhangchenbiao
2023-05-31 16:47:04 +08:00
parent b00a91d060
commit 5070f52076
19 changed files with 319 additions and 46 deletions

View File

@@ -71,5 +71,24 @@
<orderEntry type="library" name="Maven: com.github.pagehelper:pagehelper:5.3.2" level="project" />
<orderEntry type="library" name="Maven: com.github.jsqlparser:jsqlparser:4.5" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:guava:20.0" level="project" />
<orderEntry type="library" name="Maven: com.aliyun.openservices:ons-client:1.8.8.3.Final" level="project" />
<orderEntry type="library" name="Maven: com.aliyun:ons20190214:1.0.0" level="project" />
<orderEntry type="library" name="Maven: com.aliyun:tea-util:0.2.9" level="project" />
<orderEntry type="library" name="Maven: com.aliyun:endpoint-util:0.0.6" level="project" />
<orderEntry type="library" name="Maven: com.aliyun:tea:1.2.7" level="project" />
<orderEntry type="library" name="Maven: com.squareup.okhttp3:okhttp:3.14.7" level="project" />
<orderEntry type="library" name="Maven: com.squareup.okio:okio:1.17.2" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.6" level="project" />
<orderEntry type="library" name="Maven: org.jacoco:org.jacoco.agent:runtime:0.8.4" level="project" />
<orderEntry type="library" name="Maven: com.aliyun:tea-openapi:0.0.19" level="project" />
<orderEntry type="library" name="Maven: com.aliyun:credentials-java:0.2.4" level="project" />
<orderEntry type="library" name="Maven: org.ini4j:ini4j:0.5.4" level="project" />
<orderEntry type="library" name="Maven: javax.xml.bind:jaxb-api:2.3.1" level="project" />
<orderEntry type="library" name="Maven: javax.activation:javax.activation-api:1.2.0" level="project" />
<orderEntry type="library" name="Maven: com.sun.xml.bind:jaxb-core:2.3.0" level="project" />
<orderEntry type="library" name="Maven: com.sun.xml.bind:jaxb-impl:2.3.0" level="project" />
<orderEntry type="library" name="Maven: com.aliyun:openapiutil:0.1.9" level="project" />
<orderEntry type="library" name="Maven: org.bouncycastle:bcpkix-jdk15on:1.65" level="project" />
<orderEntry type="library" name="Maven: org.bouncycastle:bcprov-jdk15on:1.65" level="project" />
</component>
</module>

View File

@@ -70,6 +70,18 @@
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun.openservices</groupId>
<artifactId>ons-client</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>ons20190214</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>tea-openapi</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,198 @@
package com.cool.store.utils;
import com.aliyun.ons20190214.Client;
import com.aliyun.ons20190214.models.*;
import com.aliyun.teaopenapi.models.Config;
import com.cool.store.constants.CommonConstants;
import com.cool.store.enums.RocketMqGroupEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author chenyupeng
* @since 2022/1/5
*/
@Slf4j
public class RocketMqUtil {
private static final String INSTANCE_ID = "MQ_INST_1947409023213164_BX3sLZnA";
/**
* 本地
*/
private static final String LOCAL_INSTANCE_ID = "MQ_INST_1255228665351616_BX3wfyPO";
private static final String INSTANCE_ID_ONLINE_HD_PRE = "MQ_INST_1947409023213164_BX5N7rwl";
private static final String ACCESS_KEY = "LTAI5t5ouXZuFgxJMbQea3b2";
private static final String ACCESS_SECRET = "yuomDstRjSdihtN5zo8viDbWu8Z0ig";
/**
* 使用AK&SK初始化账号Client
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
Config config = new Config()
// 您的AccessKey ID
.setAccessKeyId(accessKeyId)
// 您的AccessKey Secret
.setAccessKeySecret(accessKeySecret);
// 访问的域名
config.endpoint = "ons.cn-hangzhou.aliyuncs.com";
return new Client(config);
}
public static Client createLocalClient(String accessKeyId, String accessKeySecret) throws Exception {
Config config = new Config()
// 您的AccessKey ID
.setAccessKeyId(accessKeyId)
// 您的AccessKey Secret
.setAccessKeySecret(accessKeySecret);
// 访问的域名
config.endpoint = "ons.mq-internet-access.aliyuncs.com";
return new Client(config);
}
/**
* 新增测试、开发环境的group
* @author chenyupeng
* @date 2022/1/13
* @param profiles 环境后缀,逗号分隔 例如: dev,dev2
* @return void
*/
public static void initGroup(String profiles) throws Exception {
Client client = RocketMqUtil.createClient(ACCESS_KEY, ACCESS_SECRET);
List<String> profileList = Arrays.asList(profiles.split(CommonConstants.COMMA));
if(CollectionUtils.isEmpty(profileList)){
return;
}
OnsGroupListRequest onsGroupListRequest = new OnsGroupListRequest();
onsGroupListRequest.setInstanceId(INSTANCE_ID);
OnsGroupListResponse onsGroupListResponse = client.onsGroupList(onsGroupListRequest);
List<OnsGroupListResponseBody.OnsGroupListResponseBodyDataSubscribeInfoDo> subscribeInfoDo = onsGroupListResponse.getBody().getData().getSubscribeInfoDo();
Map<String, OnsGroupListResponseBody.OnsGroupListResponseBodyDataSubscribeInfoDo> collect = ListUtils.emptyIfNull(subscribeInfoDo).stream().
collect(Collectors.toMap(OnsGroupListResponseBody.OnsGroupListResponseBodyDataSubscribeInfoDo::getGroupId, data -> data, (a, b) -> a));
for (String profile : profileList) {
for (RocketMqGroupEnum groupEnum : RocketMqGroupEnum.values()) {
String groupId = groupEnum.getDefaultGroup() + "_" + profile;
//已经创建就继续
if(collect.get(groupId) != null){
continue;
}
OnsGroupCreateRequest onsGroupCreateRequest = new OnsGroupCreateRequest();
onsGroupCreateRequest.setGroupId(groupId);
onsGroupCreateRequest.setInstanceId(INSTANCE_ID);
// 复制代码运行请自行打印 API 的返回值
try {
client.onsGroupCreate(onsGroupCreateRequest);
}catch (Exception e){
log.error("initGroupAndTag fail",e);
}
Thread.sleep(1000);
}
}
}
public static void initLocalGroup(String profiles) throws Exception {
Client client = RocketMqUtil.createLocalClient("LTAI5tPWCTeCyngfYLqoSGWk", "jkzIXlvNF17ne5TPPEFP1sQhcfg4Je");
List<String> profileList = Arrays.asList(profiles.split(CommonConstants.COMMA));
if(CollectionUtils.isEmpty(profileList)){
return;
}
OnsGroupListRequest onsGroupListRequest = new OnsGroupListRequest();
onsGroupListRequest.setInstanceId(LOCAL_INSTANCE_ID);
OnsGroupListResponse onsGroupListResponse = client.onsGroupList(onsGroupListRequest);
List<OnsGroupListResponseBody.OnsGroupListResponseBodyDataSubscribeInfoDo> subscribeInfoDo = onsGroupListResponse.getBody().getData().getSubscribeInfoDo();
Map<String, OnsGroupListResponseBody.OnsGroupListResponseBodyDataSubscribeInfoDo> collect = ListUtils.emptyIfNull(subscribeInfoDo).stream().
collect(Collectors.toMap(OnsGroupListResponseBody.OnsGroupListResponseBodyDataSubscribeInfoDo::getGroupId, data -> data, (a, b) -> a));
for (String profile : profileList) {
for (RocketMqGroupEnum groupEnum : RocketMqGroupEnum.values()) {
String groupId = groupEnum.getDefaultGroup() + "_" + profile;
//已经创建就继续
if(collect.get(groupId) != null){
continue;
}
OnsGroupCreateRequest onsGroupCreateRequest = new OnsGroupCreateRequest();
onsGroupCreateRequest.setGroupId(groupId);
onsGroupCreateRequest.setInstanceId(LOCAL_INSTANCE_ID);
// 复制代码运行请自行打印 API 的返回值
try {
client.onsGroupCreate(onsGroupCreateRequest);
}catch (Exception e){
log.error("initGroupAndTag fail",e);
}
Thread.sleep(1000);
}
}
}
/**
* 新增线上、灰度、预发环境的group
* @author chenyupeng
* @date 2022/1/13
* @param profiles 环境后缀,逗号分隔 例如: online,hd,pre
* @return void
*/
public static void initOnlineAndHdAndPreGroup(String profiles) throws Exception {
Client client = RocketMqUtil.createClient(ACCESS_KEY, ACCESS_SECRET);
List<String> profileList = Arrays.asList(profiles.split(CommonConstants.COMMA));
if(CollectionUtils.isEmpty(profileList)){
return;
}
OnsGroupListRequest onsGroupListRequest = new OnsGroupListRequest();
onsGroupListRequest.setInstanceId(INSTANCE_ID_ONLINE_HD_PRE);
OnsGroupListResponse onsGroupListResponse = client.onsGroupList(onsGroupListRequest);
List<OnsGroupListResponseBody.OnsGroupListResponseBodyDataSubscribeInfoDo> subscribeInfoDo = onsGroupListResponse.getBody().getData().getSubscribeInfoDo();
Map<String, OnsGroupListResponseBody.OnsGroupListResponseBodyDataSubscribeInfoDo> collect = ListUtils.emptyIfNull(subscribeInfoDo).stream().
collect(Collectors.toMap(OnsGroupListResponseBody.OnsGroupListResponseBodyDataSubscribeInfoDo::getGroupId, data -> data, (a, b) -> a));
for (String profile : profileList) {
for (RocketMqGroupEnum groupEnum : RocketMqGroupEnum.values()) {
String groupId = groupEnum.getDefaultGroup() + "_" + profile;
//已经创建就继续
if(collect.get(groupId) != null){
continue;
}
OnsGroupCreateRequest onsGroupCreateRequest = new OnsGroupCreateRequest();
onsGroupCreateRequest.setGroupId(groupId);
onsGroupCreateRequest.setInstanceId(INSTANCE_ID_ONLINE_HD_PRE);
// 复制代码运行请自行打印 API 的返回值
try {
client.onsGroupCreate(onsGroupCreateRequest);
}catch (Exception e){
log.error("initGroupAndTag fail",e);
}
Thread.sleep(1000);
}
}
}
/**
* 删除测试、开发环境的group
* @author chenyupeng
* @date 2022/1/13
* @param groupId
* @return void
*/
public static void deleteGroupById(String groupId) throws Exception {
Client client = RocketMqUtil.createClient(ACCESS_KEY, ACCESS_SECRET);
OnsGroupDeleteRequest request = new OnsGroupDeleteRequest();
request.setGroupId(groupId);
request.setInstanceId(INSTANCE_ID);
client.onsGroupDelete(request);
}
}