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

org.zodiac.sentinel.base.config.configurer.SentinelConfigurer Maven / Gradle / Ivy

package org.zodiac.sentinel.base.config.configurer;

import java.util.Objects;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.core.env.Environment;
import org.zodiac.sdk.toolkit.util.AssertUtil;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;
import org.zodiac.sentinel.base.config.SentinelConfigInfo;
import org.zodiac.sentinel.base.constants.SentinelBaseSystemPropertiesConstants;
import org.zodiac.sentinel.base.datasource.SentinelDataSourceHandler;
import org.zodiac.sentinel.base.datasource.converter.JsonSentinelDataSourceConverter;
import org.zodiac.sentinel.base.datasource.converter.XmlSentinelDataSourceConverter;

import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
import com.alibaba.csp.sentinel.config.SentinelConfig;
import com.alibaba.csp.sentinel.init.InitExecutor;
import com.alibaba.csp.sentinel.log.LogBase;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.alibaba.csp.sentinel.slots.system.SystemRule;
import com.alibaba.csp.sentinel.transport.config.TransportConfig;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

public class SentinelConfigurer implements InitializingBean {

    private final String projectName;
    private final SentinelConfigInfo sentinelConfigInfo;

    public SentinelConfigurer(String projectName, SentinelConfigInfo sentinelConfigInfo) {
        this.projectName = AssertUtil.notBlankOf(projectName, "projectName");
        this.sentinelConfigInfo = Objects.requireNonNull(sentinelConfigInfo);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        init();
    }

    protected void init() {
        if (StrUtil.isEmpty(System.getProperty(LogBase.LOG_DIR))
            && StrUtil.isNotBlank(sentinelConfigInfo.getLog().getDir())) {
            System.setProperty(LogBase.LOG_DIR, sentinelConfigInfo.getLog().getDir());
        }
        if (StrUtil.isEmpty(System.getProperty(LogBase.LOG_NAME_USE_PID)) && sentinelConfigInfo.getLog().isSwitchPid()) {
            System.setProperty(LogBase.LOG_NAME_USE_PID, String.valueOf(sentinelConfigInfo.getLog().isSwitchPid()));
        }
        if (StrUtil.isEmpty(System.getProperty(SentinelConfig.APP_NAME_PROP_KEY))
            && StrUtil.isNotBlank(projectName)) {
            System.setProperty(SentinelConfig.APP_NAME_PROP_KEY, projectName);
        }
        if (StrUtil.isEmpty(System.getProperty(TransportConfig.SERVER_PORT))
            && sentinelConfigInfo.getTransport().getPort() > 0) {
            System.setProperty(TransportConfig.SERVER_PORT, String.valueOf(sentinelConfigInfo.getTransport().getPort()));
        }
        if (StrUtil.isEmpty(System.getProperty(TransportConfig.CONSOLE_SERVER))
            && StrUtil.isNotBlank(sentinelConfigInfo.getTransport().getDashboard())) {
            System.setProperty(TransportConfig.CONSOLE_SERVER, sentinelConfigInfo.getTransport().getDashboard());
        }
        if (StrUtil.isEmpty(System.getProperty(TransportConfig.HEARTBEAT_INTERVAL_MS))
            && StrUtil.isNotBlank(sentinelConfigInfo.getTransport().getHeartbeatMills())) {
            System.setProperty(TransportConfig.HEARTBEAT_INTERVAL_MS,
                sentinelConfigInfo.getTransport().getHeartbeatMills());
        }
        if (StrUtil.isEmpty(System.getProperty(TransportConfig.HEARTBEAT_CLIENT_IP))
            && StrUtil.isNotBlank(sentinelConfigInfo.getTransport().getClientIp())) {
            System.setProperty(TransportConfig.HEARTBEAT_CLIENT_IP, sentinelConfigInfo.getTransport().getClientIp());
        }
        if (StrUtil.isEmpty(System.getProperty(SentinelConfig.CHARSET))
            && StrUtil.isNotBlank(sentinelConfigInfo.getMetric().getCharset())) {
            System.setProperty(SentinelConfig.CHARSET, sentinelConfigInfo.getMetric().getCharset());
        }
        if (StrUtil.isEmpty(System.getProperty(SentinelConfig.SINGLE_METRIC_FILE_SIZE))
            && StrUtil.isNotBlank(sentinelConfigInfo.getMetric().getFileSingleSize())) {
            System.setProperty(SentinelConfig.SINGLE_METRIC_FILE_SIZE, sentinelConfigInfo.getMetric().getFileSingleSize());
        }
        if (StrUtil.isEmpty(System.getProperty(SentinelConfig.TOTAL_METRIC_FILE_COUNT))
            && StrUtil.isNotBlank(sentinelConfigInfo.getMetric().getFileTotalCount())) {
            System.setProperty(SentinelConfig.TOTAL_METRIC_FILE_COUNT, sentinelConfigInfo.getMetric().getFileTotalCount());
        }
        if (StrUtil.isEmpty(System.getProperty(SentinelConfig.COLD_FACTOR))
            && sentinelConfigInfo.getFlow().getColdFactor() > 0) {
            System.setProperty(SentinelConfig.COLD_FACTOR, String.valueOf(sentinelConfigInfo.getFlow().getColdFactor()));
        }
        if (StrUtil.isNotBlank(sentinelConfigInfo.getBlockPage())) {
            SentinelConfig.setConfig(SentinelBaseSystemPropertiesConstants.Sentinel.BLOCK_PAGE_URL_CONF_KEY, sentinelConfigInfo.getBlockPage());
        }

        /*Earlier initialize.*/
        if (sentinelConfigInfo.isEager()) {
            InitExecutor.doInit();
        }
    }

    public SentinelResourceAspect sentinelResourceAspect() {
        return new SentinelResourceAspect();
    }

    public SentinelDataSourceHandler sentinelDataSourceHandler(
            DefaultListableBeanFactory beanFactory, SentinelConfigInfo sentinelConfigInfo,
            Environment env) {
        return new SentinelDataSourceHandler(beanFactory, sentinelConfigInfo, env);
    }

    protected static abstract class SentinelConverterConfigurer {

        protected static abstract class SentinelJsonConfigurer {

            private ObjectMapper objectMapper = new ObjectMapper();

            protected SentinelJsonConfigurer() {
                objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
                        false);
            }

            protected JsonSentinelDataSourceConverter jsonFlowConverter() {
                return new JsonSentinelDataSourceConverter(objectMapper, FlowRule.class);
            }

            protected JsonSentinelDataSourceConverter jsonDegradeConverter() {
                return new JsonSentinelDataSourceConverter(objectMapper, DegradeRule.class);
            }

            protected JsonSentinelDataSourceConverter jsonSystemConverter() {
                return new JsonSentinelDataSourceConverter(objectMapper, SystemRule.class);
            }

            protected JsonSentinelDataSourceConverter jsonAuthorityConverter() {
                return new JsonSentinelDataSourceConverter(objectMapper, AuthorityRule.class);
            }

            protected JsonSentinelDataSourceConverter jsonParamFlowConverter() {
                return new JsonSentinelDataSourceConverter(objectMapper, ParamFlowRule.class);
            }
        }

        protected static abstract class SentinelXmlConfigurer {

            private XmlMapper xmlMapper = new XmlMapper();

            protected SentinelXmlConfigurer() {
                xmlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
                        false);
            }

            protected XmlSentinelDataSourceConverter xmlFlowConverter() {
                return new XmlSentinelDataSourceConverter(xmlMapper, FlowRule.class);
            }

            protected XmlSentinelDataSourceConverter xmlDegradeConverter() {
                return new XmlSentinelDataSourceConverter(xmlMapper, DegradeRule.class);
            }

            protected XmlSentinelDataSourceConverter xmlSystemConverter() {
                return new XmlSentinelDataSourceConverter(xmlMapper, SystemRule.class);
            }

            protected XmlSentinelDataSourceConverter xmlAuthorityConverter() {
                return new XmlSentinelDataSourceConverter(xmlMapper, AuthorityRule.class);
            }

            protected XmlSentinelDataSourceConverter xmlParamFlowConverter() {
                return new XmlSentinelDataSourceConverter(xmlMapper, ParamFlowRule.class);
            }
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy