Pārlūkot izejas kodu

nacos持久化

master
wangjx pirms 3 gadiem
vecāks
revīzija
47fea40933
10 mainītis faili ar 251 papildinājumiem un 6 dzēšanām
  1. +13
    -1
      sentinel-dashboard/pom.xml
  2. +12
    -0
      sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/common/NacosConstants.java
  3. +56
    -0
      sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/config/NacosConfiguration.java
  4. +45
    -0
      sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/config/NacosPropertiesConfiguration.java
  5. +4
    -2
      sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/v2/FlowControllerV2.java
  6. +57
    -0
      sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/FlowRuleNacosProvider.java
  7. +57
    -0
      sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/FlowRuleNacosPublisher.java
  8. +4
    -1
      sentinel-dashboard/src/main/resources/application.properties
  9. +2
    -1
      sentinel-dashboard/src/main/resources/bootstrap-test.yaml
  10. +1
    -1
      sentinel-dashboard/src/main/resources/bootstrap.yaml

+ 13
- 1
sentinel-dashboard/pom.xml Parādīt failu

@@ -101,7 +101,7 @@
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<scope>test</scope>
<!--scope>test</scope -->
</dependency>
<!-- for Apollo rule publisher sample -->
<dependency>
@@ -135,6 +135,18 @@
<version>1.16.1</version>
<scope>test</scope>
</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>

<build>


+ 12
- 0
sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/common/NacosConstants.java Parādīt failu

@@ -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";
}

+ 56
- 0
sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/config/NacosConfiguration.java Parādīt failu

@@ -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);
}

}

+ 45
- 0
sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/config/NacosPropertiesConfiguration.java Parādīt failu

@@ -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;

}

+ 4
- 2
sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/v2/FlowControllerV2.java Parādīt failu

@@ -59,10 +59,12 @@ public class FlowControllerV2 {
private InMemoryRuleRepositoryAdapter<FlowRuleEntity> repository;

@Autowired
@Qualifier("flowRuleDefaultProvider")
// @Qualifier("flowRuleDefaultProvider")
@Qualifier("flowRuleNacosProvider")
private DynamicRuleProvider<List<FlowRuleEntity>> ruleProvider;
@Autowired
@Qualifier("flowRuleDefaultPublisher")
// @Qualifier("flowRuleDefaultPublisher")
@Qualifier("flowRuleNacosPublisher")
private DynamicRulePublisher<List<FlowRuleEntity>> rulePublisher;

@GetMapping("/rules")


+ 57
- 0
sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/FlowRuleNacosProvider.java Parādīt failu

@@ -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);
}
}
}

+ 57
- 0
sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/FlowRuleNacosPublisher.java Parādīt failu

@@ -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));
}
}

+ 4
- 1
sentinel-dashboard/src/main/resources/application.properties Parādīt failu

@@ -24,4 +24,7 @@ auth.password=sentinel
# Inject the dashboard version. It's required to enable
# filtering in pom.xml for this resource file.
# 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

+ 2
- 1
sentinel-dashboard/src/main/resources/bootstrap-test.yaml Parādīt failu

@@ -10,10 +10,11 @@ spring:
sentinel:
transport:
dashboard: 172.19.192.44:7777
port: 8719
datasource:
- nacos:
server-addr: 172.19.42.44:8848
data-id: ${spring.application.name}-sentinel
data-id: ${spring.application.name}-sentinel-flow
group-id: DEFAULT_GROUP
data-type: json
rule-type: flow

+ 1
- 1
sentinel-dashboard/src/main/resources/bootstrap.yaml Parādīt failu

@@ -13,7 +13,7 @@ spring:
datasource:
- nacos:
server-addr: 172.19.42.44:8848
data-id: ${spring.application.name}-sentinel
data-id: ${spring.application.name}-sentinel-flow
group-id: DEFAULT_GROUP
data-type: json
rule-type: flow

Notiek ielāde…
Atcelt
Saglabāt