spring-cloud-alibaba Can support the use of nacos to persist sentinel gateway configuration?

68de4m5k  于 2022-10-21  发布在  Spring
关注(0)|答案(2)|浏览(174)

I am not sure if this issue should be submitted here, if anyone knows, please let me know.

Describe the solution you'd like

I can modify the gateway's hot parameter current limiting rules in nacos.

This is the serialized JSON of com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule.

Describe alternatives you've considered

I read the official documents and issues, there is no relevant instructions, I found in the source code that there is actually an entrance.

public void postRegister(AbstractDataSource dataSource) {
		switch (this.getRuleType()) {
		case FLOW:
			FlowRuleManager.register2Property(dataSource.getProperty());
			break;
		case DEGRADE:
			DegradeRuleManager.register2Property(dataSource.getProperty());
			break;
		case PARAM_FLOW:
			ParamFlowRuleManager.register2Property(dataSource.getProperty());
			break;
		case SYSTEM:
			SystemRuleManager.register2Property(dataSource.getProperty());
			break;
		case AUTHORITY:
			AuthorityRuleManager.register2Property(dataSource.getProperty());
			break;
		case GW_FLOW:
			GatewayRuleManager.register2Property(dataSource.getProperty());
			break;
		case GW_API_GROUP:
			GatewayApiDefinitionManager.register2Property(dataSource.getProperty());
			break;
		default:
			break;
		}
	}

This is the code in com.alibaba.cloud.sentinel.datasource.config.AbstractDataSourceProperties.
After I completed the configuration, I still lacked a bean, introduced this bean, and completed the dynamic configuration of the Gateway hot parameter current limit.
The following is my configuration content.

@Configuration
public class SentinelFilterConfig {

    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public GlobalFilter sentinelGatewayFilter() {
        return new SentinelGatewayFilter();
    }

    // lacked bean
    @Bean("sentinel-json-gw-flow-converter")
    public JsonConverter<GatewayFlowRule> jsonGatewayFlowConverter() {
        return new JsonConverter<>(new ObjectMapper(), GatewayFlowRule.class);
    }
}
sentinel:
      datasource:
        ds:
          nacos:
            # com.alibaba.cloud.sentinel.datasource.RuleType
            ruleType: gw-flow
            data-type: json
            groupId: ${spring.cloud.nacos.config.group}
            dataId: ${spring.application.name}-sentinel.json
            namespace: ${spring.cloud.nacos.config.namespace}
            server-addr: ${spring.cloud.nacos.config.server-addr}

Can the injection of this bean be added to spring-cloud-alibaba-sentinel-datasource?

我不太清楚这个 issue 该不该在这里提交, 如果有人知道的话请告诉我.

Describe the solution you'd like

我可以在nacos中修改gateway的热点参数限流规则.

这是com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule的序列化JSON.

Describe alternatives you've considered

我翻阅官方文档与issue, 没有相关的说明, 我在源码中发现其实是有入口的

public void postRegister(AbstractDataSource dataSource) {
		switch (this.getRuleType()) {
		case FLOW:
			FlowRuleManager.register2Property(dataSource.getProperty());
			break;
		case DEGRADE:
			DegradeRuleManager.register2Property(dataSource.getProperty());
			break;
		case PARAM_FLOW:
			ParamFlowRuleManager.register2Property(dataSource.getProperty());
			break;
		case SYSTEM:
			SystemRuleManager.register2Property(dataSource.getProperty());
			break;
		case AUTHORITY:
			AuthorityRuleManager.register2Property(dataSource.getProperty());
			break;
		case GW_FLOW:
			GatewayRuleManager.register2Property(dataSource.getProperty());
			break;
		case GW_API_GROUP:
			GatewayApiDefinitionManager.register2Property(dataSource.getProperty());
			break;
		default:
			break;
		}
	}

这是com.alibaba.cloud.sentinel.datasource.config.AbstractDataSourceProperties中的代码.
我配置完成之后还缺少一个bean, 引入这个bean, 完成了动态配置Gateway热点参数限流.
以下是我的配置内容

@Configuration
public class SentinelFilterConfig {

    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public GlobalFilter sentinelGatewayFilter() {
        return new SentinelGatewayFilter();
    }

    // 缺少的bean
    @Bean("sentinel-json-gw-flow-converter")
    public JsonConverter<GatewayFlowRule> jsonGatewayFlowConverter() {
        return new JsonConverter<>(new ObjectMapper(), GatewayFlowRule.class);
    }
}
sentinel:
      datasource:
        ds:
          nacos:
            # com.alibaba.cloud.sentinel.datasource.RuleType
            ruleType: gw-flow
            data-type: json
            groupId: ${spring.cloud.nacos.config.group}
            dataId: ${spring.application.name}-sentinel.json
            namespace: ${spring.cloud.nacos.config.namespace}
            server-addr: ${spring.cloud.nacos.config.server-addr}

这个Bean的注入是否能被加到 spring-cloud-alibaba-sentinel-datasource 中?

nnt7mjpx

nnt7mjpx1#

Add the following dependencies?

<dependency>
		<groupId>com.alibaba.cloud</groupId>
		<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
	</dependency>

	<dependency>
		<groupId>com.alibaba.cloud</groupId>
		<artifactId>spring-cloud-alibaba-sentinel-datasource</artifactId>
	</dependency>

	<dependency>
		<groupId>com.alibaba.csp</groupId>
		<artifactId>sentinel-datasource-nacos</artifactId>
	</dependency>
xsuvu9jc

xsuvu9jc2#

Add the following dependencies?

<dependency>
		<groupId>com.alibaba.cloud</groupId>
		<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
	</dependency>

	<dependency>
		<groupId>com.alibaba.cloud</groupId>
		<artifactId>spring-cloud-alibaba-sentinel-datasource</artifactId>
	</dependency>

	<dependency>
		<groupId>com.alibaba.csp</groupId>
		<artifactId>sentinel-datasource-nacos</artifactId>
	</dependency>

yes, these are my pom configurations.

相关问题