All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.sentinel.nacos.datasource.NacosDataSourceAdaptor Maven / Gradle / Ivy

The newest version!
package org.zodiac.sentinel.nacos.datasource;

import javax.validation.constraints.NotNull;

import org.springframework.core.env.ConfigurableEnvironment;
import org.zodiac.sentinel.base.constants.SentinelDataSourceConstants;
import org.zodiac.sentinel.base.datasource.AbstractDataSourceAdaptor;
import org.zodiac.sentinel.nacos.cluster.client.NacosSingletonClusterDataSourceOption;

public class NacosDataSourceAdaptor extends AbstractDataSourceAdaptor {

    private final NacosSingletonClusterDataSourceOption dataSourceOption = NacosSingletonClusterDataSourceOption.getInstance();

    public NacosDataSourceAdaptor() {
    }

    public NacosDataSourceAdaptor(String applicationName) {
        super(applicationName);
    }

    public NacosDataSourceAdaptor(String applicationName, String baseConfPrefx) {
        super(applicationName, baseConfPrefx);
    }

    @Override
    protected void doAdapt(ConfigurableEnvironment environment, String sentinelAppName,
        String sentinelConfigPropertyFormat) {
        NacosSingletonClusterDataSourceOption dataSourceOption = obtainDataSourceOption();
        System.setProperty(
            String.format(sentinelConfigPropertyFormat, SentinelDataSourceConstants.FLOW, SentinelDataSourceConstants.FLOW_RULES_KEY),
            dataSourceOption.getFlowDataId());
        System.setProperty(
            String.format(sentinelConfigPropertyFormat, SentinelDataSourceConstants.FLOW, SentinelDataSourceConstants.NAMESPACE),
            dataSourceOption.getSentinelNamespaceName() + sentinelAppName);
        System.setProperty(String.format(sentinelConfigPropertyFormat, SentinelDataSourceConstants.FLOW, SentinelDataSourceConstants.RULE_TYPE),
            SentinelDataSourceConstants.FLOW);

        System.setProperty(
            String.format(sentinelConfigPropertyFormat, SentinelDataSourceConstants.DEGRADE, SentinelDataSourceConstants.FLOW_RULES_KEY),
            dataSourceOption.getDegradeDataId());
        System.setProperty(
            String.format(sentinelConfigPropertyFormat, SentinelDataSourceConstants.DEGRADE, SentinelDataSourceConstants.NAMESPACE),
            dataSourceOption.getSentinelNamespaceName() + sentinelAppName);
        System.setProperty(
            String.format(sentinelConfigPropertyFormat, SentinelDataSourceConstants.DEGRADE, SentinelDataSourceConstants.RULE_TYPE),
            SentinelDataSourceConstants.DEGRADE);

        System.setProperty(
            String.format(sentinelConfigPropertyFormat, SentinelDataSourceConstants.AUTHORITY, SentinelDataSourceConstants.FLOW_RULES_KEY),
            dataSourceOption.getAuthorityDataId());
        System.setProperty(
            String.format(sentinelConfigPropertyFormat, SentinelDataSourceConstants.AUTHORITY, SentinelDataSourceConstants.NAMESPACE),
            dataSourceOption.getSentinelNamespaceName() + sentinelAppName);
        System.setProperty(
            String.format(sentinelConfigPropertyFormat, SentinelDataSourceConstants.AUTHORITY, SentinelDataSourceConstants.RULE_TYPE),
            SentinelDataSourceConstants.AUTHORITY);

        System.setProperty(
            String.format(sentinelConfigPropertyFormat, SentinelDataSourceConstants.SYSTEM, SentinelDataSourceConstants.FLOW_RULES_KEY),
            dataSourceOption.getSystemDataId());
        System.setProperty(
            String.format(sentinelConfigPropertyFormat, SentinelDataSourceConstants.SYSTEM, SentinelDataSourceConstants.NAMESPACE),
            dataSourceOption.getSentinelNamespaceName() + sentinelAppName);
        System.setProperty(
            String.format(sentinelConfigPropertyFormat, SentinelDataSourceConstants.SYSTEM, SentinelDataSourceConstants.RULE_TYPE),
            SentinelDataSourceConstants.SYSTEM);

        System.setProperty(
            String.format(sentinelConfigPropertyFormat, SentinelDataSourceConstants.PARAM_FLOW, SentinelDataSourceConstants.FLOW_RULES_KEY),
            dataSourceOption.getParamFlowDataId());
        System.setProperty(
            String.format(sentinelConfigPropertyFormat, SentinelDataSourceConstants.PARAM_FLOW, SentinelDataSourceConstants.NAMESPACE),
            dataSourceOption.getSentinelNamespaceName() + sentinelAppName);
        System.setProperty(
            String.format(sentinelConfigPropertyFormat, SentinelDataSourceConstants.PARAM_FLOW, SentinelDataSourceConstants.RULE_TYPE),
            SentinelDataSourceConstants.PARAM_FLOW);
    }

    @Override
    protected @NotNull NacosSingletonClusterDataSourceOption obtainDataSourceOption() {
        return dataSourceOption;
    }

    public static NacosDataSourceAdaptor getDefault() {
        return NacosDataSourceAdaptorHolder.DEFAULT;
    }

    private static class NacosDataSourceAdaptorHolder {
        private static final NacosDataSourceAdaptor DEFAULT = new NacosDataSourceAdaptor();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy