Add Sentinel SpringMVC adapter module - #1104
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1104 +/- ##
============================================
+ Coverage 42.92% 43.13% +0.21%
- Complexity 1502 1535 +33
============================================
Files 320 327 +7
Lines 9400 9514 +114
Branches 1283 1294 +11
============================================
+ Hits 4035 4104 +69
- Misses 4870 4902 +32
- Partials 495 508 +13
Continue to review full report at Codecov.
|
8d0f81c to
b0dc5a1
Compare
| <module>sentinel-web-servlet</module> | ||
| <module>sentinel-dubbo-adapter</module> | ||
| <module>sentinel-apache-dubbo-adapter</module> | ||
| <module>sentinel-grpc-adapter</module> |
There was a problem hiding this comment.
What is the reason for deleting this sentinel-grpc-adapter module?
There was a problem hiding this comment.
Miss class in module: sentinel-grpc-adapter
| protected void setEntryContainerInReqeust(HttpServletRequest request, String name, EntryContainer entryContainer) { | ||
| Object attrVal = request.getAttribute(name); | ||
| if (attrVal != null) { | ||
| throw new SentinelSpringMvcException("Already exist attribute name '" + name + "' in request"); |
There was a problem hiding this comment.
It seems to me that throwing an exception directly is not very friendly.
| protected EntryContainer getEntryContainerInReqeust(HttpServletRequest request, String attrKey) { | ||
| Object entityContainerObject = request.getAttribute(attrKey); | ||
| if (entityContainerObject == null) { | ||
| throw new SentinelSpringMvcException("EntryContainer is null in request"); |
| if (entryContainer.getUrlEntry() != null) { | ||
| entryContainer.getUrlEntry().exit(); | ||
| } | ||
| removeEntryContainerInReqeust(request, config.getRequestAttributeName()); |
There was a problem hiding this comment.
First exit httpMethodUrlEntry and then exit urlEntry.
And Trace the Exception ex at here may be better.
| ContextUtil.enter(SPRING_MVC_CONTEXT_NAME); | ||
| Entry entry = SphU.entry(config.getTotalResourceName()); | ||
| final EntryContainer entryContainer = new EntryContainer().setUrlEntry(entry); | ||
| setEntryContainerInReqeust(request, config.getRequestAttributeName(), entryContainer); |
There was a problem hiding this comment.
It might be better to use a new property such as totalUrlEntry
There was a problem hiding this comment.
Good suggestion, will modify it
| } | ||
| removeEntryContainerInReqeust(request, config.getRequestAttributeName()); | ||
| } | ||
| ContextUtil.exit(); |
There was a problem hiding this comment.
Trace the Exception ex at here may be better.
| /** | ||
| * @Author kaizi2009 | ||
| */ | ||
| public class DefaultUrlCleaner implements UrlCleaner { |
There was a problem hiding this comment.
DefaultUrlCleaner is not necessary, in my opinion
b0dc5a1 to
2b406f0
Compare
kaizi2009
left a comment
There was a problem hiding this comment.
There is one question left to discuss: Order of 'SentinelInterceptor' and 'SentinelTotalInterceptor'.
I don't think the order matters, what is your detailed analysis?
| protected EntryContainer getEntryContainerInReqeust(HttpServletRequest request, String attrKey) { | ||
| Object entityContainerObject = request.getAttribute(attrKey); | ||
| if (entityContainerObject == null) { | ||
| throw new SentinelSpringMvcException("EntryContainer is null in request"); |
| if (entryContainer.getUrlEntry() != null) { | ||
| entryContainer.getUrlEntry().exit(); | ||
| } | ||
| removeEntryContainerInReqeust(request, config.getRequestAttributeName()); |
| } | ||
| removeEntryContainerInReqeust(request, config.getRequestAttributeName()); | ||
| } | ||
| ContextUtil.exit(); |
| /** | ||
| * @Author kaizi2009 | ||
| */ | ||
| public class DefaultUrlCleaner implements UrlCleaner { |
There was a problem hiding this comment.
Dear @zhaoyuguang ! Please review changes, I have two different views:
- The order of the interceptors does not matter.
- Handle
BlockExceptioninspring MVC, because spring MVC's exception handling is more customized
make sense |
011fda0 to
58177bc
Compare
|
👍 I'll take a look these days. |
181ad0f to
16a4d67
Compare
kaizi2009
left a comment
There was a problem hiding this comment.
- Optimization configuration class.
- Optimization AbstractSentinelInterceptor
0e5c6fb to
a57ef22
Compare
a57ef22 to
d9a6a42
Compare
| @Override | ||
| public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) | ||
| throws Exception { | ||
| Entry urlEntry = null; |
There was a problem hiding this comment.
Maybe here only one entry is needed. If the httpMethodSpecify is enabled, then the resource name could be METHOD:path, or the resource name could be path.
There was a problem hiding this comment.
I agree with your suggestion. I will modify.
| httpMethodUrlEntry = SphU.entry(request.getMethod().toUpperCase() + COLON + resourceName, | ||
| EntryType.IN); | ||
| } | ||
| final EntryContainer entryContainer = new EntryContainer().setUrlEntry(urlEntry) |
There was a problem hiding this comment.
Since only one entry is needed, maybe we could directly carry the Entry in the request attribute.
There was a problem hiding this comment.
I agree with your suggestion. I will modify.
a2947e7 to
2309973
Compare
| */ | ||
| @ControllerAdvice | ||
| @Order(0) | ||
| public class SentinelSringMvcBlockHandlerConfig { |
| import org.springframework.web.servlet.ModelAndView; | ||
|
|
||
| /** | ||
| * @Author kaizi2009 |
There was a problem hiding this comment.
Please format all your file headers: @Author -> @author
| } | ||
| } | ||
|
|
||
| protected Entry getEntryInReqeust(HttpServletRequest request, String attrKey) { |
…into sentinel-spring-webmvc-adapter
|
Nice work! I'll improve the code later. Maybe we could replace the |
|
Thanks for contributing! |
|
@sczyh30 My original idea was: My idea now is:
|
|
Need to create new PR? |
|
@sczyh30 What is your suggestion? |
Feel free to submit a new PR (don't forget to update the relevant part of the document :) |
|
It is better to throw. |
- Add sentinel-spring-webmvc-adapter module and demo
Why we need it?
We expect Sentinel's target is '/foo/{id}'
Issue
#1074
How to verify it
Implements org.springframework.web.servlet.HandlerInterceptor
Junit:
module: sentinel-spring-webmvc-adapter
com.alibaba.csp.sentinel.adapter.spring.webmvc.TestInterceptor