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

@SentinelResource stay controller Can use,but service can not use #1053

Closed
liduanwen opened this issue Sep 19, 2019 · 32 comments
Closed

@SentinelResource stay controller Can use,but service can not use #1053

liduanwen opened this issue Sep 19, 2019 · 32 comments
Labels
area/annotation Issues or PRs related to annotation support kind/question Category issues related to questions or problems

Comments

@liduanwen
Copy link

liduanwen commented Sep 19, 2019

Hello, why can this annotation be used in controller, but not in service
,I found that the breakpoint in the controller would follow the invokeResourceWithSentinel method in the SentinelResourceAspect class, but the service would not follow the invokeResourceWithSentinel method.controller and service are both in one project

@SentinelResource(value = "hello3", blockHandler = "handleException", blockHandlerClass = {ExceptionUtil.class})
@RequestMapping("/hello3")
public Map<String,Object> test2() {
Map<String,Object> map=new HashMap<>();
map.put("method","test2");
map.put("msg","自定义限流逻辑处理");
return map;
}
@Service
public class TestServiceImpl implements TestService {
protected transient final Logger log = LoggerFactory.getLogger(getClass());

@SentinelResource(value = "hello3", blockHandler = "handleException", blockHandlerClass = {ExceptionUtil.class})
@Override
public Map<String,Object> test(String str) {
    System.out.println("str=" + str);
    Map<String,Object> map=new HashMap<>();
    map.put("method","test2");
    map.put("msg","自定义限流逻辑处理");
    return  map;
}
@sentinel-bot sentinel-bot added the kind/bug Category issues or prs related to bug. label Sep 19, 2019
@sczyh30 sczyh30 added area/annotation Issues or PRs related to annotation support kind/question Category issues related to questions or problems and removed kind/bug Category issues or prs related to bug. labels Sep 19, 2019
@kaori-seasons
Copy link
Contributor

kaori-seasons commented Sep 20, 2019

@liduanwen

BlockHandlerClass is the Class object of the corresponding class. Note that the corresponding function must be a static function, otherwise it cannot be parsed.

@kaori-seasons
Copy link
Contributor

I tried demo here and it works.

https://github.com/dyc87112/SpringCloud-Learning/tree/master/4-Finchley/alibaba-sentinel-annotation

@sczyh30
Copy link
Member

sczyh30 commented Sep 21, 2019

Please ensure your blockHandler method matches the restriction described here: https://github.com/alibaba/Sentinel/wiki/注解支持#sentinelresource-注解

@cdfive
Copy link
Collaborator

cdfive commented Sep 21, 2019

You mean that the blockHandler take no effect?

In controller, the method signature is Map<String,Object> test2();
In service, the method signature is Map<String,Object> test(String str);

But they both use
blockHandler = "handleException", blockHandlerClass = {ExceptionUtil.class}
You can refer to the wiki page @sczyh30 mentioned.

@liduanwen
Copy link
Author

Sorry, I know the problem because the service method does not match the parameter type of the exception method.I have another problem, because the architecture I'm working on now is spring boot + sentinel, and I'm introducing Spring cloud that relies on direct use. We also have the old project Spring mvc, which directly introduces sentinel-core package and nacos. sentinel is not available. I would like to ask if there is Spring MVC + sentinel demo, is it necessary to initialize sentinel? I look forward to your answer. Thanks very much.

@jasonjoo2010
Copy link
Collaborator

Sorry, I know the problem because the service method does not match the parameter type of the exception method.I have another problem, because the architecture I'm working on now is spring boot + sentinel, and I'm introducing Spring cloud that relies on direct use. We also have the old project Spring mvc, which directly introduces sentinel-core package and nacos. sentinel is not available. I would like to ask if there is Spring MVC + sentinel demo, is it necessary to initialize sentinel? I look forward to your answer. Thanks very much.

If you mean SpringMVC under containers like tomcat (which is also driven by a web.xml) you can refer to the adaption of CommonFilter (Web Servlet)

https://github.com/alibaba/Sentinel/wiki/Adapters-to-Popular-Framework#web-servlet

@liduanwen
Copy link
Author

好的,我再试试,非常感谢。 @jasonjoo2010

@liduanwen
Copy link
Author

Hello, I just looked at the website below. I saw dubbo. Our framework is spring MVC + dubbo. I looked directly at this website at https://github.com/alibaba/Sentinel/wiki/Adapters-to-Popular-Framework#dubbo. I also looked at sentinel-demo-dubbo project, and did not configure sentinel address. There is no place to configure the address in demo. I look forward to your answer. Thank you very much. @jasonjoo2010

@jasonjoo2010
Copy link
Collaborator

Hello, I just looked at the website below. I saw dubbo. Our framework is spring MVC + dubbo. I looked directly at this website at https://github.com/alibaba/Sentinel/wiki/Adapters-to-Popular-Framework#dubbo. I also looked at sentinel-demo-dubbo project, and did not configure sentinel address. There is no place to configure the address in demo. I look forward to your answer. Thank you very much. @jasonjoo2010

The adapters are designed easy to use / out-of-box. So the resource name are generated automatically base on url pattern (servlet) / class.method (dubbo). You can introduce the suitable adapter to you project and load some requests to it after boot it up. If everything goes correctly you should check the entries on you dashboard like the applications you saw before.

@liduanwen
Copy link
Author

Hello, Dubbo on our side is 2.5.3. I just need to introduce sentinel-dubbo-adapter dependency. I can use it out of the box without configuration initialization, right? But I found that sentinel's management platform did not have this project. @jasonjoo2010

@sczyh30
Copy link
Member

sczyh30 commented Sep 23, 2019

Dubbo 2.5.3 is too old that sentinel-dubbo-adapter may not support. You may introduce the dependency and see whether it will take effect.

See https://github.com/alibaba/Sentinel/wiki/主流框架的适配#dubbo for more information.

@jasonjoo2010
Copy link
Collaborator

Hello, Dubbo on our side is 2.5.3. I just need to introduce sentinel-dubbo-adapter dependency. I can use it out of the box without configuration initialization, right? But I found that sentinel's management platform did not have this project. @jasonjoo2010

You should also follow the general integration. eg. dashboard.server

Refer to
https://github.com/alibaba/Sentinel/wiki/Dashboard#32-configure-dashboard-ip-address

@liduanwen
Copy link
Author

Sorry, I really don't know much about springmvc + sentinel, because I see the document that says - Dcsp. sentinel. dashboard. server = consoleIp: port after the command boot, but I want to write Dcsp. sentinel. dashboard. server in the dev-filter. properties configuration file, I normally write the address. In the configuration file, do I have to read out the Dcsp. sentinel. dashboard. server of the configuration file and put it in the initialization? That's what I imagined, because I remember you said it was out of the box before. So a little confused, I'm very sorry. @jasonjoo2010

@jasonjoo2010
Copy link
Collaborator

Sorry, I really don't know much about springmvc + sentinel, because I see the document that says - Dcsp. sentinel. dashboard. server = consoleIp: port after the command boot, but I want to write Dcsp. sentinel. dashboard. server in the dev-filter. properties configuration file, I normally write the address. In the configuration file, do I have to read out the Dcsp. sentinel. dashboard. server of the configuration file and put it in the initialization? That's what I imagined, because I remember you said it was out of the box before. So a little confused, I'm very sorry. @jasonjoo2010

Never mind and take it easy.

Out-of-box hide the integrating details for specific framework. But you still need to let it know who to report / heartbeat with.
So a dashboard address should be specified in command line arguments at least.
And if you need further datasource integrating you still should refer to datasource integration, too.

Is it appeared in your dashboard sidebar now?

@liduanwen
Copy link
Author

Hello, I configure it by command and it appears in sentinel console. If I put it in properties, then I have to initialize @bean and get the parameters of properties in @bean, right? Thank you very much.
1
2 @jasonjoo2010

@jasonjoo2010
Copy link
Collaborator

Hello, I configure it by command and it appears in sentinel console. If I put it in properties, then I have to initialize @bean and get the parameters of properties in @bean, right? Thank you very much.
1
2 @jasonjoo2010

Nope, it's not quite that simple if you want use properties file.
There is only one principle you must follow: You must set it into System.properties BEFORE InitExecutor is called.
So you can put it into your bootstrap class(eg. Main class), or make sure you haven't imported any class related sentinel when you call System.putProperty().
So you can do that in your bean but if you find it makes no sense please check whether InitExecutor is called before that.

@liduanwen
Copy link
Author

Hello, I have another question. I'm sorry. Because I am doing (spring MVC + Dubbo + zookeeper / spring boot + Dubbo + zookeeper) + sentinel + Nacos (because our original registry is zookeeper, not nacos, Nacos is only persistent as sentinel in this area). I also need to integrate Nacos into the project to persist configuration rules and degradation rules. Is this configuration address configured with sentinel on JVM parameters? Thanks very much.

@jasonjoo2010
Copy link
Collaborator

Hello, I have another question. I'm sorry. Because I am doing (spring MVC + Dubbo + zookeeper / spring boot + Dubbo + zookeeper) + sentinel + Nacos (because our original registry is zookeeper, not nacos, Nacos is only persistent as sentinel in this area). I also need to integrate Nacos into the project to persist configuration rules and degradation rules. Is this configuration address configured with sentinel on JVM parameters? Thanks very much.

Nope, you should do it yourself.

Please refer to zookeeper version code example:
https://github.com/yoloho/enhanced-spring/blob/master/src/main/java/com/yoloho/enhanced/spring/support/SentinelZookeeperDataSourceInit.java
https://github.com/yoloho/enhanced-spring/blob/master/src/main/java/com/yoloho/enhanced/spring/support/SentinelZookeeperWritableDataSourceInit.java

@liduanwen
Copy link
Author

Hello, maybe I am not very clear. Nacos is only a persistent tool for sentinel. In fact, the purpose of Nacos is equivalent to sentinel. It is only a registered and persistent tool. I mean - Dcsp. sentinel. dashboard. server = localhost: 8080, is sentinel configured like this? Is Nacos also configured like this? I want to ask these questions.

@jasonjoo2010
Copy link
Collaborator

Hello, maybe I am not very clear. Nacos is only a persistent tool for sentinel. In fact, the purpose of Nacos is equivalent to sentinel. It is only a registered and persistent tool. I mean - Dcsp. sentinel. dashboard. server = localhost: 8080, is sentinel configured like this? Is Nacos also configured like this? I want to ask these questions.

They are not the same.
Dashboard is a console for projects having integrated sentinel.
Nacos is a data source for sentinel.

Dashboard communicates with projects using http api, not Nacos.
That's two things totally.
If you want to use them together without so much modification, you can register both readable and writable data source in project side and operate using dashboard. Then you can add a rule to one node of application but all other nodes will receive the subscription from their readable data source.

@liduanwen
Copy link
Author

Hello, I probably understand the difference between Nacos and sentinel, but I don't quite understand your words. I don't know exactly what to do. I'm sorry to delay you for so long.The following paragraph
remarks:Then you can add a rule to one node of application but all other nodes will receive the subscription from their readable data source

@liduanwen
Copy link
Author

liduanwen commented Sep 23, 2019

Hello, I built a demo myself. I used spring boot + sentinel + Nacos for my project, and then I introduced these two dependencies directly. Figure 1. This integrates sentinel, nacos, and configuration address Figure 2. Then I found a problem, spring MVC + sentinel, with no Nacos configuration. I added an example to the configuration list below configuration management in nacos, Figure 3. See this traffic rule in the sentinel console. The project using spring MVC did not see it. Is it because of Nacos configuration?

Figure one:1
Figure two:2
Figure three:
3

@jasonjoo2010
Copy link
Collaborator

So do you manage the rules by nacos console not dashboard?

If so, there is only one more step in your legacy projects(SpringMVC): Register the readable data sources.

@jasonjoo2010
Copy link
Collaborator

@liduanwen
Copy link
Author

Okay, I downloaded your code and read it. I'll write it first. Thank you very much for your help during the day. I've been bothering you for so long. @jasonjoo2010

@jasonjoo2010
Copy link
Collaborator

Okay, I downloaded your code and read it. I'll write it first. Thank you very much for your help during the day. I've been bothering you for so long. @jasonjoo2010

It's okay. If you still don't make it work you are welcome to update it.

@liduanwen
Copy link
Author

liduanwen commented Sep 23, 2019

Hello, again, I'll change Zookeeper Writable Data Source Zookeeper to nacos, or use this, Figure 1? Because this Zookeeper Writable Data Source is used in Figure 2.
Figure 1:

1

Figure 2:
2

@jasonjoo2010

@jasonjoo2010
Copy link
Collaborator

Hello, again, I'll change Zookeeper Writable Data Source Zookeeper to nacos, or use this, Figure 1? Because this Zookeeper Writable Data Source is used in Figure 2.
Figure 1:

1

Figure 2:
2

@jasonjoo2010

No need. If you use nacos console to manage rules you will only need readable datasource.

Just register readable datasource in you project is enough. And readable datasource of nacos is already provided by Sentinel officially. The artifactId should be sentinel-datasource-nacos. Pay attention to use the SAME version for all related libraries of sentinel.

@liduanwen
Copy link
Author

Hello, thank you very much for your technical support in these two days. I have added the JSON format of process rules to the Nacos console in spring MVC + sentinel + Nacos project. I can see the configuration in the list of sentinel flow control rules. Thank you again.That's what I wrote in the project. @jasonjoo2010
for example:
1

@jasonjoo2010
Copy link
Collaborator

at I wrote

So is the problem solved?
You will be welcome to Sentinel Community.

@liduanwen
Copy link
Author

Yes, it's settled. Thank you very much. @jasonjoo2010

@sczyh30
Copy link
Member

sczyh30 commented Sep 24, 2019

👍

CST11021 pushed a commit to CST11021/Sentinel that referenced this issue Nov 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/annotation Issues or PRs related to annotation support kind/question Category issues related to questions or problems
Projects
None yet
Development

No branches or pull requests

6 participants