Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.alibaba.csp.ahas.sentinel.acm.ApsaraSdkInitService Maven / Gradle / Ivy
package com.alibaba.csp.ahas.sentinel.acm;
import com.alibaba.csp.ahas.sentinel.datasource.parser.ParamFlowRuleParser;
import com.alibaba.csp.ahas.sentinel.datasource.parser.gateway.GatewayApiDefinitionParser;
import com.alibaba.csp.ahas.sentinel.datasource.parser.gateway.GatewayFlowRuleParser;
import com.alibaba.csp.ahas.sentinel.datasource.parser.setting.SentinelAdapterSettingParser;
import com.alibaba.csp.ahas.sentinel.datasource.parser.setting.SentinelGeneralSettingParser;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
import com.alibaba.csp.sentinel.datasource.acm.DiamondDataSource;
import com.alibaba.csp.sentinel.datasource.acm.SentinelAcmConstants;
import com.alibaba.csp.sentinel.datasource.acm.parser.DegradeRuleConfigParser;
import com.alibaba.csp.sentinel.datasource.acm.parser.FlowRuleConfigParser;
import com.alibaba.csp.sentinel.datasource.acm.parser.SystemRuleConfigParser;
import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.setting.adapter.AdapterSettingManager;
import com.alibaba.csp.sentinel.setting.adapter.SentinelAdapterSettingEntity;
import com.alibaba.csp.sentinel.setting.general.GeneralSettingManager;
import com.alibaba.csp.sentinel.setting.general.SentinelGeneralSettingEntity;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager;
import com.alibaba.csp.sentinel.slots.system.SystemRuleManager;
import com.taobao.csp.ahas.service.api.client.ClientInfoService;
import com.taobao.diamond.exception.DiamondException;
import java.util.Set;
import static com.alibaba.csp.sentinel.datasource.acm.SentinelAcmConstants.SENTINEL_GROUP_ID;
/**
* @Author: yunyin
* @Date: 2020/8/24
*/
public class ApsaraSdkInitService extends AbstractSentinelSdkInitService {
public ApsaraSdkInitService(ClientInfoService clientInfoService) {
super(clientInfoService);
}
/**
* Apsara sdk init method
* @param sdkVersion
*/
@Override
public void init0(String sdkVersion) throws Exception{
initAcmDataSource();
}
/**
* Init acm datasource
* @throws DiamondException
*/
private void initAcmDataSource() throws DiamondException {
String namespace = clientInfoService.getNamespace();
String userId = clientInfoService.getUserId();
String consoleTenantId = clientInfoService.getTid();
String appName = clientInfoService.getAppName();
initSettingDs(userId, namespace, appName, consoleTenantId);
initClientRuleDs(userId, namespace, appName, consoleTenantId);
// Register data source related to gateway adapter.
if (isGatewayAppType()) {
initApiGatewayDs(userId, namespace, appName, consoleTenantId);
}
}
private void initClientRuleDs(final String userId, final String namespace, String appName, String consoleTenantId) throws DiamondException {
String flowDataId = SentinelAcmConstants.formFlowRuleDataId(userId, namespace, appName);
FlowRuleManager.register2Property(new DiamondDataSource<>(consoleTenantId,
flowDataId, SENTINEL_GROUP_ID, new FlowRuleConfigParser()).getProperty());
String degradeDataId = SentinelAcmConstants.formDegradeRuleDataId(userId, namespace, appName);
DegradeRuleManager.register2Property(new DiamondDataSource<>(consoleTenantId,
degradeDataId, SENTINEL_GROUP_ID, new DegradeRuleConfigParser()).getProperty());
String systemDataId = SentinelAcmConstants.formSystemRuleDataId(userId, namespace, appName);
SystemRuleManager.register2Property(new DiamondDataSource<>(consoleTenantId,
systemDataId, SENTINEL_GROUP_ID, new SystemRuleConfigParser()).getProperty());
// Since AHAS Sentinel client 1.5.0.
final String paramFlowDataId = SentinelAcmConstants.formParamFlowRuleDataId(userId, namespace, appName);
ParamFlowRuleManager.register2Property(new DiamondDataSource<>(consoleTenantId,
paramFlowDataId, SENTINEL_GROUP_ID, new ParamFlowRuleParser()).getProperty());
String log = String.format("Diamond, flowDataId: %s, degradeDataId: %s, groupId: %s, namespace: %s",
flowDataId, degradeDataId, SENTINEL_GROUP_ID, namespace);
RecordLog.info(log);
}
private void initApiGatewayDs(String userId, String acmNamespace, String appName, String consoleTenantId)
throws DiamondException {
RecordLog.info("[SentinelDiamondDsService] Initializing data source for API gateway integration");
String gatewayFlowRuleDataId = SentinelAcmConstants.formGatewayFlowRuleDataId(userId, acmNamespace, appName);
// Init gateway flow rule data source.
DiamondDataSource> gatewayFlowRuleDs = new DiamondDataSource<>(
consoleTenantId, gatewayFlowRuleDataId, SENTINEL_GROUP_ID, new GatewayFlowRuleParser());
GatewayRuleManager.register2Property(gatewayFlowRuleDs.getProperty());
String gatewayApiDefDataId = SentinelAcmConstants.formGatewayApiDefinitionDataId(userId, acmNamespace, appName);
// Init gateway API definition data source.
DiamondDataSource> gatewayApiDefinitionDs = new DiamondDataSource<>(
consoleTenantId, gatewayApiDefDataId, SENTINEL_GROUP_ID, new GatewayApiDefinitionParser());
GatewayApiDefinitionManager.register2Property(gatewayApiDefinitionDs.getProperty());
}
private void initSettingDs(String userId, String acmNamespace, String appName, String consoleTenantId)
throws DiamondException {
// We could register the observer for adapter settings here.
// For example, we could leverage the consumer to update the configuration of Web URL prefix cleaner.
String gsDataId = SentinelAcmConstants.formGeneralSettingDataId(userId, acmNamespace, appName);
DiamondDataSource generalSettingDs = new DiamondDataSource<>(
consoleTenantId, gsDataId, SENTINEL_GROUP_ID, new SentinelGeneralSettingParser());
GeneralSettingManager.register2Property(generalSettingDs.getProperty());
String asDataId = SentinelAcmConstants.formAdapterSettingDataId(userId, acmNamespace, appName);
DiamondDataSource adapterSettingDs = new DiamondDataSource<>(
consoleTenantId, asDataId, SENTINEL_GROUP_ID, new SentinelAdapterSettingParser());
AdapterSettingManager.register2Property(adapterSettingDs.getProperty());
}
}