-
Notifications
You must be signed in to change notification settings - Fork 8k
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
integration transport module with spring-mvc #1957
Conversation
Good idea! I think it can really solve the problem in #1565. @Bean
public SentinelApiHandlerMapping sentinelApiHandlerMapping() {
return new SentinelApiHandlerMapping();
}
@Bean
public SentinelApiHandlerAdapter sentinelApiHandlerAdapter() {
return new SentinelApiHandlerAdapter();
} Besides, Could you please add a quick-start demo for this new module |
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-webmvc</artifactId> | ||
<version>${spring.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>javax.servlet-api</artifactId> | ||
<version>${servlet.api.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.httpcomponents</groupId> | ||
<artifactId>httpclient</artifactId> | ||
<version>${apache.httpclient.version}</version> | ||
</dependency> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about making spring-webmvc
,javax.servlet-api
scope provided
, since user may use different version of spring or spring-boot, the version may conflicts. Another way is that user can exclude them manually.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes I will change their scope to provided
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tend to create new demo module. I think sentinel-transport-spring-mvc and sentinel-spring-webmvc-adapter have different focus, it is better to separate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It had a point, a separate demo will make it easier for new users to understand how to use this new module.
A small suggestion for reference and discussion: If user choose to use In step7 of how to verify it, we need to config the user application port 'csp.sentinel.api.port', We can get the web port and config it with @Component
public class SpringMvcHttpHeartbeatSender implements HeartbeatSender, ApplicationListener<WebServerInitializedEvent> {
...
@Override
public void onApplicationEvent(WebServerInitializedEvent event) {
int port = event.getWebServer().getPort();
RecordLog.info("[SpringMvcHttpHeartbeatSender]get the web port: {}", port);
TransportConfig.setRuntimePort(port);
} The |
I find a class named ServerPortInfoApplicationContextInitializer, it is provided by spring-boot and is effective by default. he listen WebServerInitializedEvent and set the port to environment use key 'local.server.port'
so I think we can get port from environment use key 'local.server.port'.
I suggest when get port, the user config hava High priority, in some cases application actual running ports is different from external system accessed
|
Yes, user config should have Higher priority. Another way is: if (TransportConfig.getPort() == null) {
TransportConfig.setRuntimePort(env.getProperty("local.server.port"));
}
.setParameter("port", TransportConfig.getPort()) This may more clear and readable, since we can call
|
TransportConfig.getPort method logic, it get runtimePort first, user config second. |
if (TransportConfig.getPort() == null) {
TransportConfig.setRuntimePort(env.getProperty("local.server.port"));
}
Before |
ok I get it, and I tend to use this way.
This have a problem, [environment.getProperty("local.server.port")] return is null, because setEnvironment is before than ServerPortInfoApplicationContextInitializer set the value. So I want to use another solution, the code is
how about this way |
@shenbaoyong 你看我最开始发的那个可行么,代码比较少。 @Component
public class SpringMvcHttpHeartbeatSender implements HeartbeatSender, ApplicationListener<WebServerInitializedEvent> {
...
@Override
public void onApplicationEvent(WebServerInitializedEvent event) {
int port = event.getWebServer().getPort();
RecordLog.info("[SpringMvcHttpHeartbeatSender]get the web port: {}", port);
TransportConfig.setRuntimePort(port);
} 新的那个看着步骤稍微有点复杂,本机测过没问题的话就按新的提吧,殊途同归哈: ) |
@cdfive 我本机测试了,没有问题,我按照我最新的那种方式提交了。 |
我觉得你这种实现方式比我之前建议的那种更好,这样不用依赖spring-boot,并且能自动区分是spring-boot还是单纯的spring-mvc。demo本机运行了没问题。通过反射判断可行,我没有找到其他方法,也想的是通过这个。 最后一个小建议是 在启动 还有 |
It works well, I've no futher idea. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work, Baoyong!
I dropped some comments and please have a look when you have time.
Thanks.
...-spring-mvc/src/main/java/com/alibaba/csp/sentinel/transport/command/SentinelApiHandler.java
Outdated
Show resolved
Hide resolved
...-spring-mvc/src/main/java/com/alibaba/csp/sentinel/transport/command/SentinelApiHandler.java
Outdated
Show resolved
Hide resolved
...-spring-mvc/src/main/java/com/alibaba/csp/sentinel/transport/command/SentinelApiHandler.java
Outdated
Show resolved
Hide resolved
...-spring-mvc/src/main/java/com/alibaba/csp/sentinel/transport/command/SentinelApiHandler.java
Outdated
Show resolved
Hide resolved
@shenbaoyong Nice work. As the CI indicated, you may need to polish your command center implementation with the nouveau SPI mechanism (introduced in #1383) |
...-spring-mvc/src/main/java/com/alibaba/csp/sentinel/transport/command/SentinelApiHandler.java
Outdated
Show resolved
Hide resolved
…baba#1905) * Fix issue of memory leak in metric.js
* Add `@Spi` annotation as the general annotation for SPI definition. * Add isDefault in @SPI, add loadDefaultInstance and improve loadFirstInstanceOrDefault method, improve test cases * Add SpiLoaderException class for thrown when something goes wrong while loading Provider * Rearrange packages of base SPI mechanism NOTE: this PR contains breaking changes regarding API.
Signed-off-by: LIU Ming <hit_oak_tree@126.com>
…y-rls module (alibaba#2003) Signed-off-by: LIU Ming <hit_oak_tree@126.com>
- The demo was unable to run and stop because of missing namesrv configuration, and now fixed.
Remove unused code in DefaultBlockExceptionHandler#handle
Signed-off-by: Eric Zhao <sczyh16@gmail.com>
Signed-off-by: Eric Zhao <sczyh16@gmail.com>
Signed-off-by: Eric Zhao <sczyh16@gmail.com>
Signed-off-by: Eric Zhao <sczyh16@gmail.com>
Signed-off-by: Eric Zhao <sczyh16@gmail.com>
@jasonjoo2010
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no further concerns based on the previous review.
So LGTM now.
But there are so many changes introduced since the last review I don't know what happened. Is there any changes on other branches which have not merged yet that was merged into this PR?
There are absolutely some tests that are not well designed like simulating the racing conditions that are not stable. So just ignore them or if you can improve one or more of them you could submit another pull request kindly. |
Nice work. Thanks for contributing and look forward to more! |
Refer #1752 |
Add sentinel-transport-spring-mvc module (alibaba#1957)
DefaultMQProducer didn't shutdown completely
Describe what this PR does / why we need it
Fixes #1565
Does this pull request fix one issue?
Fixes #1565
Describe how you did it
I implements tow interfaces of spring-mvc(SentinelApiHandlerAdapter-HandlerAdapter SentinelApiHandlerMapping-HandlerMapping), mapping the api of dashboard want to visit to SentinelApiHandler(CommandHandler)。
the part of heartbeat, I just copy from sentinel-transport-netty-http(use HttpClient lib)
Describe how to verify it
Special notes for reviews