Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add sentinel plugin #331

Merged
merged 1 commit into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions script/soul.sql
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ INSERT IGNORE INTO `plugin` (`id`, `name`,`role`,`config`,`enabled`, `date_creat
INSERT IGNORE INTO `plugin` (`id`, `name`,`role`,`config`,`enabled`, `date_created`, `date_updated`) VALUES ('7', 'monitor', '0','{"metricsName":"prometheus","host":"localhost","port":"9190","async":"true"}','0', '2018-06-25 13:47:57', '2018-06-25 13:47:57');
INSERT IGNORE INTO `plugin` (`id`, `name`,`role`, `enabled`, `date_created`, `date_updated`) VALUES ('8', 'springCloud','0', '0', '2018-06-25 13:47:57', '2018-06-25 13:47:57');
INSERT IGNORE INTO `plugin` (`id`, `name`,`role`, `enabled`, `date_created`, `date_updated`) VALUES ('9', 'hystrix', '0','0', '2020-01-15 10:19:10', '2020-01-15 10:19:10');
INSERT IGNORE INTO `plugin` (`id`, `name`,`role`, `enabled`, `date_created`, `date_updated`) VALUES ('10', 'sentinel', '0','0', '2020-11-09 01:19:10', '2020-11-09 01:19:10');

/**user**/
INSERT IGNORE INTO `dashboard_user` (`id`, `user_name`, `password`, `role`, `enabled`, `date_created`, `date_updated`) VALUES ('1', 'admin', '123456', '1', '1', '2018-06-23 15:12:22', '2018-06-23 15:12:23');
Expand Down
1 change: 1 addition & 0 deletions soul-admin/src/main/resources/META-INF/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ INSERT IGNORE INTO `plugin` (`id`, `name`,`role`,`config`,`enabled`, `date_creat
INSERT IGNORE INTO `plugin` (`id`, `name`,`role`,`config`,`enabled`, `date_created`, `date_updated`) VALUES ('7', 'monitor', '0','{"metricsName":"prometheus","host":"localhost","port":"9190","async":"true"}','0', '2018-06-25 13:47:57', '2018-06-25 13:47:57');
INSERT IGNORE INTO `plugin` (`id`, `name`,`role`, `enabled`, `date_created`, `date_updated`) VALUES ('8', 'springCloud','0', '0', '2018-06-25 13:47:57', '2018-06-25 13:47:57');
INSERT IGNORE INTO `plugin` (`id`, `name`,`role`, `enabled`, `date_created`, `date_updated`) VALUES ('9', 'hystrix', '0','0', '2020-01-15 10:19:10', '2020-01-15 10:19:10');
INSERT IGNORE INTO `plugin` (`id`, `name`,`role`, `enabled`, `date_created`, `date_updated`) VALUES ('10', 'sentinel', '0','0', '2020-11-09 01:19:10', '2020-11-09 01:19:10');

/**user**/
INSERT IGNORE INTO `dashboard_user` (`id`, `user_name`, `password`, `role`, `enabled`, `date_created`, `date_updated`) VALUES ('1', 'admin', '123456', '1', '1', '2018-06-23 15:12:22', '2018-06-23 15:12:23');
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.dromara.soul.common.dto.convert;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

/**
* This is SentinelHandle.
*
* @author tydhot
*/
@Getter
@Setter
@EqualsAndHashCode
public class SentinelHandle {

/**
* grade.
*/
private Integer grade;

/**
* count.
*/
private Integer count;

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public enum PluginEnum {
*/
HYSTRIX(45, 0, "hystrix"),

/**
* Sentinel plugin enum.
*/
SENTINEL(45, 0, "sentinel"),

/**
* Divide plugin enum.
*/
Expand Down
1 change: 1 addition & 0 deletions soul-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<module>soul-plugin-rewrite</module>
<module>soul-plugin-waf</module>
<module>soul-plugin-global</module>
<module>soul-plugin-sentinel</module>
</modules>


Expand Down
40 changes: 40 additions & 0 deletions soul-plugin/soul-plugin-sentinel/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>soul-plugin</artifactId>
<groupId>org.dromara</groupId>
<version>2.2.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>soul-plugin-sentinel</artifactId>

<dependencies>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-plugin-base</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-reactor-adapter</artifactId>
<version>1.7.0</version>
</dependency>

<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>1.7.0</version>
</dependency>

<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-webflux-adapter</artifactId>
<version>1.7.0</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.dromara.soul.plugin.sentinel;

import com.alibaba.csp.sentinel.adapter.reactor.SentinelReactorTransformer;
import lombok.extern.slf4j.Slf4j;
import org.dromara.soul.common.constant.Constants;
import org.dromara.soul.common.dto.RuleData;
import org.dromara.soul.common.dto.SelectorData;
import org.dromara.soul.common.enums.PluginEnum;
import org.dromara.soul.plugin.api.SoulPluginChain;
import org.dromara.soul.plugin.api.context.SoulContext;
import org.dromara.soul.plugin.base.AbstractSoulPlugin;
import org.dromara.soul.plugin.sentinel.handler.SentinelRuleHandle;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
* Sentinel Plugin.
*
* @author tydhot
*/
@Slf4j
public class SentinelPlugin extends AbstractSoulPlugin {

@Override
protected Mono<Void> doExecute(final ServerWebExchange exchange, final SoulPluginChain chain, final SelectorData selector, final RuleData rule) {
final SoulContext soulContext = exchange.getAttribute(Constants.CONTEXT);
assert soulContext != null;
String resourceName = SentinelRuleHandle.getResourceName(rule);
return chain.execute(exchange).transform(new SentinelReactorTransformer<>(resourceName));
}

@Override
public String named() {
return PluginEnum.SENTINEL.getName();
}

@Override
public int getOrder() {
return PluginEnum.SENTINEL.getCode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.dromara.soul.plugin.sentinel.handler;

import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.dromara.soul.common.dto.RuleData;
import org.dromara.soul.common.dto.convert.SentinelHandle;
import org.dromara.soul.common.enums.PluginEnum;
import org.dromara.soul.common.utils.GsonUtils;
import org.dromara.soul.plugin.base.handler.PluginDataHandler;

import java.util.List;
import java.util.stream.Collectors;

/**
* Sentinel rule handle.
*
* @author tydhot
*/
public class SentinelRuleHandle implements PluginDataHandler {

@Override
public void handlerRule(final RuleData ruleData) {
SentinelHandle sentinelHandle = GsonUtils.getInstance().fromJson(ruleData.getHandle(), SentinelHandle.class);
FlowRule rule = new FlowRule(getResourceName(ruleData));
rule.setCount(sentinelHandle.getCount());
rule.setGrade(sentinelHandle.getGrade());
List<FlowRule> rules = FlowRuleManager.getRules()
.stream()
.filter(r -> r.getResource().equals(getResourceName(ruleData)))
.collect(Collectors.toList());
rules.add(rule);
FlowRuleManager.loadRules(rules);
}

@Override
public void removeRule(final RuleData ruleData) {
FlowRuleManager.loadRules(FlowRuleManager.getRules()
.stream()
.filter(r -> r.getResource().equals(getResourceName(ruleData)))
.collect(Collectors.toList()));
}

@Override
public String pluginNamed() {
return PluginEnum.SENTINEL.getName();
}

/**
* return sentinel resource name.
*
* @param ruleData ruleData
* @return string string
*/
public static String getResourceName(final RuleData ruleData) {
return ruleData.getSelectorId() + "_" + ruleData.getName();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<module>soul-spring-boot-starter-plugin-sign</module>
<module>soul-spring-boot-starter-plugin-waf</module>
<module>soul-spring-boot-starter-plugin-rewrite</module>
<module>soul-spring-boot-starter-plugin-sentinel</module>
</modules>


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>soul-spring-boot-starter-plugin</artifactId>
<groupId>org.dromara</groupId>
<version>2.2.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>soul-spring-boot-starter-plugin-sentinel</artifactId>

<dependencies>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-plugin-sentinel</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.dromara.soul.springboot.starter.plugin.sentinel;

import org.dromara.soul.plugin.base.handler.PluginDataHandler;
import org.dromara.soul.plugin.sentinel.SentinelPlugin;
import org.dromara.soul.plugin.sentinel.handler.SentinelRuleHandle;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
* Sentinel plugin configuration.
*
* @author tydhot
*/
@Configuration
public class SentinelPluginConfiguration {

/**
* Sentinel plugin.
*
* @return the soul plugin
*/
@Bean
public SentinelPlugin sentinelPlugin() {
return new SentinelPlugin();
}

/**
* Sentinel plugin data handler plugin data handler.
*
* @return the plugin data handler
*/
@Bean
public PluginDataHandler sentinelRuleHandle() {
return new SentinelRuleHandle();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.dromara.soul.springboot.starter.plugin.sentinel.SentinelPluginConfiguration
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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.
#

provides: soul-spring-boot-starter-plugin-sentinel