Browse Source

nacos持久化

master
wangjx 3 years ago
parent
commit
def563711f
4 changed files with 13 additions and 3 deletions
  1. +2
    -3
      sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/v2/FlowControllerV2.java
  2. +1
    -0
      sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/discovery/SimpleMachineDiscovery.java
  3. +5
    -0
      sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/FlowRuleNacosProvider.java
  4. +5
    -0
      sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/FlowRuleNacosPublisher.java

+ 2
- 3
sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/v2/FlowControllerV2.java View File

@@ -70,7 +70,7 @@ public class FlowControllerV2 {
@GetMapping("/rules") @GetMapping("/rules")
@AuthAction(PrivilegeType.READ_RULE) @AuthAction(PrivilegeType.READ_RULE)
public Result<List<FlowRuleEntity>> apiQueryMachineRules(@RequestParam String app) { public Result<List<FlowRuleEntity>> apiQueryMachineRules(@RequestParam String app) {
System.out.println("FlowControllerV2 apiQueryMachineRules app is " + app);
if (StringUtil.isEmpty(app)) { if (StringUtil.isEmpty(app)) {
return Result.ofFail(-1, "app can't be null or empty"); return Result.ofFail(-1, "app can't be null or empty");
} }
@@ -163,8 +163,7 @@ public class FlowControllerV2 {
@PutMapping("/rule/{id}") @PutMapping("/rule/{id}")
@AuthAction(AuthService.PrivilegeType.WRITE_RULE) @AuthAction(AuthService.PrivilegeType.WRITE_RULE)


public Result<FlowRuleEntity> apiUpdateFlowRule(@PathVariable("id") Long id,
@RequestBody FlowRuleEntity entity) {
public Result<FlowRuleEntity> apiUpdateFlowRule(@PathVariable("id") Long id, @RequestBody FlowRuleEntity entity) {
if (id == null || id <= 0) { if (id == null || id <= 0) {
return Result.ofFail(-1, "Invalid id"); return Result.ofFail(-1, "Invalid id");
} }


+ 1
- 0
sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/discovery/SimpleMachineDiscovery.java View File

@@ -42,6 +42,7 @@ public class SimpleMachineDiscovery implements MachineDiscovery {


@Override @Override
public long addMachine(MachineInfo machineInfo) { public long addMachine(MachineInfo machineInfo) {
logger.info("SimpleMachineDiscovery addMachine");
AssertUtil.notNull(machineInfo, "machineInfo cannot be null"); AssertUtil.notNull(machineInfo, "machineInfo cannot be null");
AppInfo appInfo = apps.computeIfAbsent(machineInfo.getApp(), o -> new AppInfo(machineInfo.getApp(), machineInfo.getAppType())); AppInfo appInfo = apps.computeIfAbsent(machineInfo.getApp(), o -> new AppInfo(machineInfo.getApp(), machineInfo.getAppType()));
appInfo.addMachine(machineInfo); appInfo.addMachine(machineInfo);


+ 5
- 0
sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/FlowRuleNacosProvider.java View File

@@ -20,6 +20,8 @@ import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider; import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
import com.alibaba.csp.sentinel.datasource.Converter; import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.nacos.api.config.ConfigService; 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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -33,6 +35,8 @@ import java.util.List;
@Service @Service
public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleEntity>> { public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleEntity>> {


@Autowired
private static Logger logger = LoggerFactory.getLogger(FlowRuleNacosProvider.class);
@Autowired @Autowired
@Qualifier("nacosConfiguration") @Qualifier("nacosConfiguration")
private ConfigService nacosConfiguration; private ConfigService nacosConfiguration;
@@ -43,6 +47,7 @@ public class FlowRuleNacosProvider implements DynamicRuleProvider<List<FlowRuleE


@Override @Override
public List<FlowRuleEntity> getRules(String appName) throws Exception { public List<FlowRuleEntity> getRules(String appName) throws Exception {
logger.info("FlowRuleNacosProvider getRules [appName] " + appName);
String dataId=new StringBuilder(appName).append(NacosConstants.DATA_ID_POSTFIX).toString(); String dataId=new StringBuilder(appName).append(NacosConstants.DATA_ID_POSTFIX).toString();
String rules = nacosConfiguration.getConfig(dataId,nacosProperties.getGroupId(),3000); String rules = nacosConfiguration.getConfig(dataId,nacosProperties.getGroupId(),3000);
//log.info("pull FlowRule from nacos Config:"+rules); //log.info("pull FlowRule from nacos Config:"+rules);


+ 5
- 0
sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/FlowRuleNacosPublisher.java View File

@@ -21,6 +21,8 @@ import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
import com.alibaba.csp.sentinel.datasource.Converter; import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.util.AssertUtil; import com.alibaba.csp.sentinel.util.AssertUtil;
import com.alibaba.nacos.api.config.ConfigService; 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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -33,6 +35,8 @@ import java.util.List;
@Service @Service
public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> { public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRuleEntity>> {


@Autowired
private static Logger logger = LoggerFactory.getLogger(FlowRuleNacosPublisher.class);
@Autowired @Autowired
@Qualifier("nacosConfiguration") @Qualifier("nacosConfiguration")
private ConfigService nacosConfiguration; private ConfigService nacosConfiguration;
@@ -44,6 +48,7 @@ public class FlowRuleNacosPublisher implements DynamicRulePublisher<List<FlowRul
@Override @Override
public void publish(String appName, List<FlowRuleEntity> rules) throws Exception { public void publish(String appName, List<FlowRuleEntity> rules) throws Exception {


logger.info("FlowRuleNacosPublisher publish [appName] " + appName);
AssertUtil.notEmpty(appName, "appName不能为空"); AssertUtil.notEmpty(appName, "appName不能为空");
if(rules == null) { if(rules == null) {
return; return;


Loading…
Cancel
Save