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

feat: Add support for sentinel opensergo datasource #3142

Open
wants to merge 5 commits into
base: 2.2.x-ospp2023
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ spring.cloud.sentinel.datasource.ds4.file.rule-type=system

spring.cloud.sentinel.datasource.ds5.file.file=classpath: param-flow.json
spring.cloud.sentinel.datasource.ds5.file.rule-type=param_flow
spring.cloud.sentinel.datasource.ds5.opensergo.rule-type=param_flow
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
<artifactId>spring-cloud-alibaba-sentinel-datasource</artifactId>
<name>Spring Cloud Alibaba Sentinel DataSource</name>

<properties>
<opensergo-datasource.version>0.1.0-beta</opensergo-datasource.version>
</properties>

<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
Expand Down Expand Up @@ -81,6 +85,12 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-opensergo</artifactId>
<version>${opensergo-datasource.version}</version>
</dependency>
Comment on lines +88 to +92
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whether to use optional, the integration of opensergo is at the discretion of the user.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this dependency, I can not use the OpenSergoDataSourceGroup


<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public class DataSourcePropertiesConfiguration {

private ConsulDataSourceProperties consul;

private OpenSergoDataSourceProperties opensergo;

public DataSourcePropertiesConfiguration() {
}

Expand Down Expand Up @@ -125,6 +127,14 @@ public void setRedis(RedisDataSourceProperties redis) {
this.redis = redis;
}

public OpenSergoDataSourceProperties getOpensergo() {
return opensergo;
}

public void setOpensergo(OpenSergoDataSourceProperties opensergo) {
this.opensergo = opensergo;
}

@JsonIgnore
public List<String> getValidField() {
return Arrays.stream(this.getClass().getDeclaredFields())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* 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
*
* https://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.cloud.sentinel.datasource.config;

import java.util.HashSet;
import java.util.Set;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.alibaba.cloud.sentinel.datasource.RuleType;
import com.alibaba.cloud.sentinel.datasource.factorybean.OpenSergoDataSourceFactoryBean;
import com.alibaba.csp.sentinel.datasource.OpenSergoDataSourceGroup;
import com.alibaba.csp.sentinel.datasource.OpenSergoSentinelConstants;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.util.AppNameUtil;

import org.springframework.util.CollectionUtils;

/**
* OpenSergo Properties class Using by {@link DataSourcePropertiesConfiguration} and
* {@link OpenSergoDataSourceFactoryBean}.
*
* @author musi
* @author <a href="[email protected]"></a>
*/
public class OpenSergoDataSourceProperties extends AbstractDataSourceProperties {
private static final String FLOW = "flow";
private static final String DEGRADE = "degrade";
private String host = "127.0.0.1";

private int port = 10246;

private String namespace = "default";

private String app = AppNameUtil.getAppName();

private Set<String> enabledRules = new HashSet<>();
Copy link

@jnan806 jnan806 Apr 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe can use Set<RuleType>

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, It is better to use Enum type. I will fix it!


public OpenSergoDataSourceProperties() {
super(OpenSergoDataSourceFactoryBean.class.getName());
}

public void postRegister(OpenSergoDataSourceGroup dataSourceGroup) {
// TODO: SystemRule and ParamFlowRule
if (enabledRules.contains(FLOW)) {
FlowRuleManager.register2Property(dataSourceGroup.subscribeFlowRules());
}
if (enabledRules.contains(DEGRADE)) {
DegradeRuleManager.register2Property(dataSourceGroup.subscribeDegradeRules());
}
// When there is no enabled-rules, try ruleType
RuleType ruleType = getRuleType();
switch (ruleType) {
case FLOW:
FlowRuleManager.register2Property(dataSourceGroup.subscribeFlowRules());
break;
case DEGRADE:
DegradeRuleManager.register2Property(dataSourceGroup.subscribeDegradeRules());
break;
}
Comment on lines +66 to +74
Copy link

@jnan806 jnan806 Apr 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As your comment, these code lines should be wrapped in condition enabledRules is empty.
Because the ruleType was annotationed by NotNull in AbstractDataSourceProperties ,so if enabledRules contains the ruleType, the subscribeConfig action will be invoked twice.

}

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

public String getNamespace() {
return namespace;
}

public void setNamespace(String namespace) {
this.namespace = namespace;
}

public String getApp() {
return app;
}

public void setApp(String app) {
this.app = app;
}

public Set<String> getEnabledRules() {
return enabledRules;
}

public void setEnabledRules(Set<String> enabledRules) {
this.enabledRules = enabledRules;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright 2013-2018 the original author or authors.
*
* 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
*
* https://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.cloud.sentinel.datasource.factorybean;

import java.util.Set;

import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.datasource.OpenSergoDataSourceGroup;

import org.springframework.beans.factory.FactoryBean;

/**
* A {@link FactoryBean} for creating {@link OpenSergoDataSourceGroup} instance.
*
* @author musi
* @author <a href="[email protected]"></a>
* @see OpenSergoDataSourceGroup
*/
public class OpenSergoDataSourceFactoryBean
implements FactoryBean<OpenSergoDataSourceGroup> {

private String host;

private int port;

private String namespace;

private String app;

private Set<String> enabledRules;

private Converter converter;

@Override
public OpenSergoDataSourceGroup getObject() throws Exception {
return new OpenSergoDataSourceGroup(host, port, namespace, app);
}

@Override
public Class<?> getObjectType() {
return OpenSergoDataSourceGroup.class;
}

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

public String getNamespace() {
return namespace;
}

public void setNamespace(String namespace) {
this.namespace = namespace;
}

public String getApp() {
return app;
}

public void setApp(String app) {
this.app = app;
}

public Set<String> getEnabledRules() {
return enabledRules;
}

public void setEnabledRules(Set<String> enabledRules) {
this.enabledRules = enabledRules;
}

public Converter getConverter() {
return converter;
}

public void setConverter(Converter converter) {
this.converter = converter;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ apollo = com.alibaba.csp.sentinel.datasource.apollo.ApolloDataSource
zk = com.alibaba.csp.sentinel.datasource.zookeeper.ZookeeperDataSource
redis = com.alibaba.csp.sentinel.datasource.redis.RedisDataSource
consul = com.alibaba.csp.sentinel.datasource.consul.ConsulDataSource
opensergo = com.alibaba.csp.sentinel.datasource.OpenSergoDataSourceGroup
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@

import com.alibaba.cloud.sentinel.SentinelProperties;
import com.alibaba.cloud.sentinel.datasource.config.AbstractDataSourceProperties;
import com.alibaba.cloud.sentinel.datasource.config.OpenSergoDataSourceProperties;
import com.alibaba.cloud.sentinel.datasource.converter.JsonConverter;
import com.alibaba.cloud.sentinel.datasource.converter.XmlConverter;
import com.alibaba.csp.sentinel.datasource.AbstractDataSource;
import com.alibaba.csp.sentinel.datasource.OpenSergoDataSourceGroup;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -206,11 +208,26 @@ private void registerBean(final AbstractDataSourceProperties dataSourcePropertie
this.beanFactory.registerBeanDefinition(dataSourceName,
builder.getBeanDefinition());
// init in Spring
AbstractDataSource newDataSource = (AbstractDataSource) this.beanFactory
.getBean(dataSourceName);

// register property in RuleManager
dataSourceProperties.postRegister(newDataSource);
Object newDataSource = this.beanFactory.getBean(dataSourceName);
if (newDataSource instanceof AbstractDataSource) {
// register property in RuleManager
dataSourceProperties.postRegister((AbstractDataSource) newDataSource);
}
if (newDataSource instanceof OpenSergoDataSourceGroup) {
// Properties must be OpenSergoDataSourceProperties
if (!(dataSourceProperties instanceof OpenSergoDataSourceProperties)) {
return;
}
OpenSergoDataSourceProperties openSergoDataSourceProperties = (OpenSergoDataSourceProperties) dataSourceProperties;
try {
OpenSergoDataSourceGroup dataSourceGroup = (OpenSergoDataSourceGroup) newDataSource;
dataSourceGroup.start();
openSergoDataSourceProperties.postRegister(dataSourceGroup);
}
catch (Exception e) {
log.error("Error on register to OpenSergo data source", e);
}
}
}

}