spring-cloud-starter-alibaba-sentinel 中 SentinelProperties 应用配置属性与 SentinclConfig 配置属性之间的优先级关系
- Dominant language
- Java
- Stars
- 23.1k
- Forks
- 8.1k
- PR merge metrics
- No merged PRs in 30d
Description
## Issue Description
Type: *bug report* or *feature request*
### Describe what happened (or what feature you want)
在依赖了`spring-cloud-starter-alibaba-sentinel` 之后,`SentinelAutoConfiguration` 的自动配置关联着`SentinelProperties`,在初始化方法`init`中,存在这样的代码:
```java
if (StringUtils.isEmpty(System.getProperty(SentinelConfig.APP_NAME_PROP_KEY))
&& StringUtils.hasText(projectName)) {
System.setProperty(SentinelConfig.APP_NAME_PROP_KEY, projectName);
}
```
若系统环境中没有对应的属性配置,则将`SentinelProperties` 中对应的属性添加到系统属性中。
但是系统属性的优先级是非常高的,所以在`sentinel-core` 中的`SentinelConfig` 加载的时候会取系统属性替换掉配置文件中的属性。
在这里都是没有问题的。
问题是,`SentinelConfig` 的调用与`SentinelAutoConfiguration` 的调用先后顺序,会影响到配置的优先级问题。
如果`SentinelConfig` 静态代码块早于`SentinelAutoConfiguration#init`方法 那么`SentinelProperties` 中对应的几个属性将不会被sentinel 识别。
反之,则会被识别。
由于`SentinelConfig` 中的加载操作是以静态代码块的形式存在的,你永远无法预测它的调用会在什么时候触发。更无法确定随着工程的开发进度,一不小心将它俩的调用顺序颠倒了,那么整个系统的配置将会被颠覆。
如果是普通应用我只在一个地方添加配置,自然不会有影响。但是如果是基础jar 包的提供,使用的人那你就各不相同了。
我感觉这是一个问题,为什么`SentinelAutoConfiguration#init` 中不是直接调用`SentinelConfig.setConfig(..)` 方法?
同时判断条件也应该是`SentinelConfig.getConfig()`。
### Describe what you expected to happen
### How to reproduce it (as minimally and precisely as possible)
1.
2.
3.
### Tell us your environment
我这边用的是sentinel 1.8.1 版本
### Anything else we need to know?
Contributor guide
Research direction
Start by tracing SentinelAutoConfiguration#init, SentinelProperties, and SentinelConfig in the Sentinel 1.8.1 integration. Reproduce initialization with SentinelConfig loading before and after SentinelAutoConfiguration#init, then verify that application properties have consistent precedence regardless of initialization order.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100