@@ -101,7 +101,7 @@ | |||||
<dependency> | <dependency> | ||||
<groupId>com.alibaba.csp</groupId> | <groupId>com.alibaba.csp</groupId> | ||||
<artifactId>sentinel-datasource-nacos</artifactId> | <artifactId>sentinel-datasource-nacos</artifactId> | ||||
<scope>test</scope> | |||||
<!--scope>test</scope --> | |||||
</dependency> | </dependency> | ||||
<!-- for Apollo rule publisher sample --> | <!-- for Apollo rule publisher sample --> | ||||
<dependency> | <dependency> | ||||
@@ -135,6 +135,18 @@ | |||||
<version>1.16.1</version> | <version>1.16.1</version> | ||||
<scope>test</scope> | <scope>test</scope> | ||||
</dependency> | </dependency> | ||||
<!--lombok 注解--> | |||||
<dependency> | |||||
<groupId>org.projectlombok</groupId> | |||||
<artifactId>lombok</artifactId> | |||||
<optional>true</optional> | |||||
</dependency> | |||||
<dependency> | |||||
<groupId>org.springframework.boot</groupId> | |||||
<artifactId>spring-boot-configuration-processor</artifactId> | |||||
<optional>true</optional> | |||||
</dependency> | |||||
</dependencies> | </dependencies> | ||||
<build> | <build> | ||||
@@ -0,0 +1,12 @@ | |||||
package com.alibaba.csp.sentinel.dashboard.common; | |||||
/** | |||||
* @program: sentinel-parent | |||||
* @description: Nacos常量 | |||||
* @author: wangjx | |||||
* @create: 2021-06-24 16:12 | |||||
**/ | |||||
public class NacosConstants { | |||||
public static final String DATA_ID_POSTFIX="-sentinel-flow"; | |||||
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.FlowRuleEntity; | |||||
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.config.annotation.NacosConfigurationProperties; | |||||
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(NacosConfigurationProperties.class) | |||||
public class NacosConfiguration { | |||||
@Bean | |||||
public Converter<List<FlowRuleEntity>, String> flowRuleEntityEncoder() { | |||||
return JSON::toJSONString; | |||||
} | |||||
@Bean | |||||
public Converter<String,List<FlowRuleEntity>> flowRuleEntityDecoder() { | |||||
return s -> JSON.parseArray(s, FlowRuleEntity.class); | |||||
} | |||||
@Bean | |||||
public ConfigService nacosConfiguration(NacosPropertiesConfiguration nacosPropertiesConfiguration) throws NacosException { | |||||
Properties properties = new Properties(); | |||||
properties.put(PropertyKeyConst.SERVER_ADDR,nacosPropertiesConfiguration.getServerAddr()); | |||||
properties.put(PropertyKeyConst.NAMESPACE,nacosPropertiesConfiguration.getNamespace()); | |||||
return ConfigFactory.createConfigService(properties); | |||||
} | |||||
} |
@@ -0,0 +1,45 @@ | |||||
/* | |||||
* 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 org.springframework.beans.factory.annotation.Configurable; | |||||
import org.springframework.boot.context.properties.ConfigurationProperties; | |||||
import lombok.Getter; | |||||
import lombok.Setter; | |||||
import org.springframework.stereotype.Component; | |||||
/** | |||||
* @author King Wang | |||||
*/ | |||||
@Component | |||||
@Configurable | |||||
@ConfigurationProperties(prefix="sentinel.nacos") | |||||
public class NacosPropertiesConfiguration { | |||||
@Getter | |||||
@Setter | |||||
private String serverAddr; | |||||
@Getter | |||||
@Setter | |||||
private String dataId; | |||||
@Getter | |||||
@Setter | |||||
private String groupId; | |||||
@Getter | |||||
@Setter | |||||
private String namespace; | |||||
} |
@@ -59,10 +59,12 @@ public class FlowControllerV2 { | |||||
private InMemoryRuleRepositoryAdapter<FlowRuleEntity> repository; | private InMemoryRuleRepositoryAdapter<FlowRuleEntity> repository; | ||||
@Autowired | @Autowired | ||||
@Qualifier("flowRuleDefaultProvider") | |||||
// @Qualifier("flowRuleDefaultProvider") | |||||
@Qualifier("flowRuleNacosProvider") | |||||
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider; | private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider; | ||||
@Autowired | @Autowired | ||||
@Qualifier("flowRuleDefaultPublisher") | |||||
// @Qualifier("flowRuleDefaultPublisher") | |||||
@Qualifier("flowRuleNacosPublisher") | |||||
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher; | private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher; | ||||
@GetMapping("/rules") | @GetMapping("/rules") | ||||
@@ -0,0 +1,57 @@ | |||||
/* | |||||
* 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; | |||||
import com.alibaba.csp.sentinel.dashboard.common.NacosConstants; | |||||
import com.alibaba.csp.sentinel.dashboard.config.NacosPropertiesConfiguration; | |||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity; | |||||
import com.alibaba.csp.sentinel.datasource.Converter; | |||||
import com.alibaba.nacos.api.config.ConfigService; | |||||
import lombok.extern.log4j.Log4j; | |||||
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 | |||||
*/ | |||||
@Log4j | |||||
@Service | |||||
public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleEntity>> { | |||||
@Qualifier("configService") | |||||
@Autowired | |||||
private ConfigService configService; | |||||
@Autowired | |||||
private NacosPropertiesConfiguration nacosPropertiesConfiguration; | |||||
@Autowired | |||||
private Converter<String, List<FlowRuleEntity>> converter; | |||||
@Override | |||||
public List<FlowRuleEntity> getRules(String appName) throws Exception { | |||||
String dataId=new StringBuilder(appName).append(NacosConstants.DATA_ID_POSTFIX).toString(); | |||||
String rules = configService.getConfig(dataId,nacosPropertiesConfiguration.getGroupId(),3000); | |||||
log.info("pull FlowRule from nacos Config:"+rules); | |||||
if (rules.isEmpty()) { | |||||
return new ArrayList<>(); | |||||
} else { | |||||
return converter.convert(rules); | |||||
} | |||||
} | |||||
} |
@@ -0,0 +1,57 @@ | |||||
/* | |||||
* 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; | |||||
import com.alibaba.csp.sentinel.dashboard.common.NacosConstants; | |||||
import com.alibaba.csp.sentinel.dashboard.config.NacosPropertiesConfiguration; | |||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity; | |||||
import com.alibaba.csp.sentinel.datasource.Converter; | |||||
import com.alibaba.csp.sentinel.util.AssertUtil; | |||||
import com.alibaba.nacos.api.config.ConfigService; | |||||
import lombok.extern.log4j.Log4j; | |||||
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 | |||||
*/ | |||||
@Log4j | |||||
@Service | |||||
public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> { | |||||
@Qualifier("configService") | |||||
@Autowired | |||||
private ConfigService configService; | |||||
@Autowired | |||||
private NacosPropertiesConfiguration nacosPropertiesConfiguration; | |||||
@Autowired | |||||
private Converter<List<FlowRuleEntity>,String> converter; | |||||
@Override | |||||
public void publish(String appName, List<FlowRuleEntity> rules) throws Exception { | |||||
AssertUtil.notEmpty(appName, "appName不能为空"); | |||||
if(rules == null) { | |||||
return; | |||||
} | |||||
String dataId=new StringBuilder(appName).append(NacosConstants.DATA_ID_POSTFIX).toString(); | |||||
configService.publishConfig(dataId,nacosPropertiesConfiguration.getGroupId(),converter.convert(rules)); | |||||
} | |||||
} |
@@ -24,4 +24,7 @@ auth.password=sentinel | |||||
# Inject the dashboard version. It's required to enable | # Inject the dashboard version. It's required to enable | ||||
# filtering in pom.xml for this resource file. | # filtering in pom.xml for this resource file. | ||||
# sentinel.dashboard.version=@project.version@ | # sentinel.dashboard.version=@project.version@ | ||||
sentinel.dashboard.version=1.8.2 | |||||
sentinel.dashboard.version=1.8.2 | |||||
sentinel.nacos.serverAddr=172.19.42.44:8848 | |||||
sentinel.nacos.namespace= | |||||
sentinel.nacos.group-id=DEFAULT_GROUP |
@@ -10,10 +10,11 @@ spring: | |||||
sentinel: | sentinel: | ||||
transport: | transport: | ||||
dashboard: 172.19.192.44:7777 | dashboard: 172.19.192.44:7777 | ||||
port: 8719 | |||||
datasource: | datasource: | ||||
- nacos: | - nacos: | ||||
server-addr: 172.19.42.44:8848 | server-addr: 172.19.42.44:8848 | ||||
data-id: ${spring.application.name}-sentinel | |||||
data-id: ${spring.application.name}-sentinel-flow | |||||
group-id: DEFAULT_GROUP | group-id: DEFAULT_GROUP | ||||
data-type: json | data-type: json | ||||
rule-type: flow | rule-type: flow |
@@ -13,7 +13,7 @@ spring: | |||||
datasource: | datasource: | ||||
- nacos: | - nacos: | ||||
server-addr: 172.19.42.44:8848 | server-addr: 172.19.42.44:8848 | ||||
data-id: ${spring.application.name}-sentinel | |||||
data-id: ${spring.application.name}-sentinel-flow | |||||
group-id: DEFAULT_GROUP | group-id: DEFAULT_GROUP | ||||
data-type: json | data-type: json | ||||
rule-type: flow | rule-type: flow |