From 8f758ceec7dc99c69a45172e2fc4037bbbd32b4a Mon Sep 17 00:00:00 2001 From: wangjx <1609724385@qq.com> Date: Sat, 26 Jun 2021 22:29:19 +0800 Subject: [PATCH] =?UTF-8?q?nacos=E6=8C=81=E4=B9=85=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dashboard/common/NacosConstants.java | 1 + .../NacosDegradePropertiesConfiguration.java | 56 +++++++++++++++++ .../rule/nacos/DegradeRuleNacosProvider.java | 62 +++++++++++++++++++ .../rule/nacos/DegradeRuleNacosPublisher.java | 60 ++++++++++++++++++ 4 files changed, 179 insertions(+) create mode 100644 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/config/NacosDegradePropertiesConfiguration.java create mode 100644 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/DegradeRuleNacosProvider.java create mode 100644 sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/DegradeRuleNacosPublisher.java diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/common/NacosConstants.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/common/NacosConstants.java index a665ee36..b1bf3029 100644 --- a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/common/NacosConstants.java +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/common/NacosConstants.java @@ -8,5 +8,6 @@ package com.alibaba.csp.sentinel.dashboard.common; **/ public class NacosConstants { public static final String DATA_ID_POSTFIX="-sentinel-flow"; + public static final String DATA_ID_DEGRADE_POSTFIX="-sentinel-degrade"; public static final String GROUP_ID="DEFAULT_GROUP"; } diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/config/NacosDegradePropertiesConfiguration.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/config/NacosDegradePropertiesConfiguration.java new file mode 100644 index 00000000..c4b4aefb --- /dev/null +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/config/NacosDegradePropertiesConfiguration.java @@ -0,0 +1,56 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.dashboard.config; + +import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity; +import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity; +import com.alibaba.csp.sentinel.dashboard.rule.nacos.NacosProperties; +import com.alibaba.csp.sentinel.datasource.Converter; +import com.alibaba.fastjson.JSON; +import com.alibaba.nacos.api.PropertyKeyConst; +import com.alibaba.nacos.api.config.ConfigFactory; +import com.alibaba.nacos.api.config.ConfigService; +import com.alibaba.nacos.api.exception.NacosException; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.List; +import java.util.Properties; + +@Configuration +@EnableConfigurationProperties(NacosProperties.class) +public class NacosDegradePropertiesConfiguration { + + @Bean + public Converter, String> degradeRuleEntityEncoder() { + return JSON::toJSONString; + } + + @Bean + public Converter> degradeRuleEntityDecoder() { + return s -> JSON.parseArray(s, DegradeRuleEntity.class); + } + + @Bean + public ConfigService nacosDegradeConfiguration(NacosProperties nacosProperties) throws NacosException { + Properties properties = new Properties(); + properties.put(PropertyKeyConst.SERVER_ADDR,nacosProperties.getServerAddr()); + properties.put(PropertyKeyConst.NAMESPACE,nacosProperties.getNamespace()); + return ConfigFactory.createConfigService(properties); + } + +} diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/DegradeRuleNacosProvider.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/DegradeRuleNacosProvider.java new file mode 100644 index 00000000..b54d5005 --- /dev/null +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/DegradeRuleNacosProvider.java @@ -0,0 +1,62 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.dashboard.rule.nacos; + +import com.alibaba.csp.sentinel.dashboard.common.NacosConstants; +import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity; +import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity; +import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider; +import com.alibaba.csp.sentinel.datasource.Converter; +import com.alibaba.nacos.api.config.ConfigService; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author Eric Zhao + */ +@Service +public class DegradeRuleNacosProvider implements DynamicRuleProvider> { + + @Autowired + private static Logger logger = LoggerFactory.getLogger(DegradeRuleNacosProvider.class); + @Autowired + @Qualifier("nacosDegradeConfiguration") + private ConfigService nacosDegradeConfiguration; + @Autowired + private NacosProperties nacosProperties; + @Autowired + private Converter> converter; + + @Override + public List getRules(String appName) throws Exception { + logger.info("DegradeRuleNacosProvider getRules [appName] " + appName); + String dataId=new StringBuilder(appName).append(NacosConstants.DATA_ID_DEGRADE_POSTFIX).toString(); + String rules = nacosDegradeConfiguration.getConfig(dataId,nacosProperties.getGroupId(),3000); + //log.info("pull FlowRule from nacos Config:"+rules); + if (StringUtils.isEmpty(rules)) { + return new ArrayList<>(); + } else { + return converter.convert(rules); + } + } +} diff --git a/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/DegradeRuleNacosPublisher.java b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/DegradeRuleNacosPublisher.java new file mode 100644 index 00000000..bbc5ba4a --- /dev/null +++ b/sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/DegradeRuleNacosPublisher.java @@ -0,0 +1,60 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.dashboard.rule.nacos; + +import com.alibaba.csp.sentinel.dashboard.common.NacosConstants; +import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity; +import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity; +import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher; +import com.alibaba.csp.sentinel.datasource.Converter; +import com.alibaba.csp.sentinel.util.AssertUtil; +import com.alibaba.nacos.api.config.ConfigService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * @author Eric Zhao + */ +@Service +public class DegradeRuleNacosPublisher implements DynamicRulePublisher> { + + @Autowired + private static Logger logger = LoggerFactory.getLogger(DegradeRuleNacosPublisher.class); + @Autowired + @Qualifier("nacosDegradeConfiguration") + private ConfigService nacosDegradeConfiguration; + @Autowired + private NacosProperties nacosProperties; + @Autowired + private Converter,String> converter; + + @Override + public void publish(String appName, List rules) throws Exception { + + logger.info("DegradeRuleNacosPublisher publish [appName] " + appName); + AssertUtil.notEmpty(appName, "appName不能为空"); + if(rules == null) { + return; + } + String dataId=new StringBuilder(appName).append(NacosConstants.DATA_ID_DEGRADE_POSTFIX).toString(); + nacosDegradeConfiguration.publishConfig(dataId,nacosProperties.getGroupId(),converter.convert(rules)); + } +}