@@ -8,5 +8,6 @@ package com.alibaba.csp.sentinel.dashboard.common; | |||||
**/ | **/ | ||||
public class NacosConstants { | public class NacosConstants { | ||||
public static final String DATA_ID_POSTFIX="-sentinel-flow"; | 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"; | public static final String GROUP_ID="DEFAULT_GROUP"; | ||||
} | } |
@@ -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<List<DegradeRuleEntity>, String> degradeRuleEntityEncoder() { | |||||
return JSON::toJSONString; | |||||
} | |||||
@Bean | |||||
public Converter<String,List<DegradeRuleEntity>> 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); | |||||
} | |||||
} |
@@ -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<List<DegradeRuleEntity>> { | |||||
@Autowired | |||||
private static Logger logger = LoggerFactory.getLogger(DegradeRuleNacosProvider.class); | |||||
@Autowired | |||||
@Qualifier("nacosDegradeConfiguration") | |||||
private ConfigService nacosDegradeConfiguration; | |||||
@Autowired | |||||
private NacosProperties nacosProperties; | |||||
@Autowired | |||||
private Converter<String, List<DegradeRuleEntity>> converter; | |||||
@Override | |||||
public List<DegradeRuleEntity> 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); | |||||
} | |||||
} | |||||
} |
@@ -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<List<DegradeRuleEntity>> { | |||||
@Autowired | |||||
private static Logger logger = LoggerFactory.getLogger(DegradeRuleNacosPublisher.class); | |||||
@Autowired | |||||
@Qualifier("nacosDegradeConfiguration") | |||||
private ConfigService nacosDegradeConfiguration; | |||||
@Autowired | |||||
private NacosProperties nacosProperties; | |||||
@Autowired | |||||
private Converter<List<DegradeRuleEntity>,String> converter; | |||||
@Override | |||||
public void publish(String appName, List<DegradeRuleEntity> 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)); | |||||
} | |||||
} |