Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use @ SentinelResource annotations

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use the @ SentinelResource annotation, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let the editor take you to know it.

@ SentinelResource comment

Note: the private method is not supported in the annotation method.

Introduction to annotations

SentinelResource is used to define resources and provides optional exception handling and fallback configuration items.

The @ SentinelResource annotation contains the following attributes:

1.value: resource name, required (cannot be empty)

2.entryType:entry type, optional (default is EntryType.OUT)

3.blockHandler / blockHandlerClass: blockHandler corresponds to the name of the function that handles BlockException. Optional. The access scope of the blockHandler function needs to be public, the return type needs to match the original method, the parameter type needs to match the original method, and finally an additional parameter is added, the type is BlockException. The blockHandler function needs to be in the same class as the original method by default. If you want to use a function of another class, you can specify blockHandlerClass as the Class object of the corresponding class. Note that the corresponding function must be a static function, otherwise it cannot be parsed.

The 4.fallback/fallbackClass:fallback function name, optional, is used to provide fallback handling logic when an exception is thrown. The fallback function can handle all types of exceptions (except those excluded from exceptionsToIgnore). Fallback function signature and location requirements:

The return value type must be the same as the original function return value type

The method parameter list needs to be consistent with the original function, or an additional parameter of type Throwable can be used to receive the corresponding exception.

The fallback function needs to be in the same class as the original method by default. If you want to use a function of another class, you can specify fallbackClass as the Class object of the corresponding class. Note that the corresponding function must be a static function, otherwise it cannot be parsed.

5.defaultFallback (since 1.6.0): the default fallback function name, optional, is usually used for general fallback logic (that is, it can be used for many services or methods). The default fallback function handles all types of exceptions (except those excluded from exceptionsToIgnore). If both fallback and defaultFallback are configured, only fallback will take effect. DefaultFallback function signature requirements:

The return value type must be the same as the original function return value type

The method parameter list needs to be empty, or an additional parameter of type Throwable can be used to receive the corresponding exception.

The defaultFallback function needs to be in the same class as the original method by default. If you want to use a function of another class, you can specify fallbackClass as the Class object of the corresponding class. Note that the corresponding function must be a static function, otherwise it cannot be parsed.

5.exceptionsToIgnore (since 1.6.0): used to specify which exceptions are excluded, will not be counted in the exception statistics, will not be entered into the fallback logic, but will be thrown as is.

Starting with version 1.8.0, defaultFallback supports configuration at the class level.

Note: the fallback function before 1.6.0 only handles degraded exceptions (DegradeException), not business exceptions.

In particular, if both blockHandler and fallback are configured, it will only enter the blockHandler processing logic when it is degraded by current restriction and thrown out of BlockException. If blockHandler, fallback and defaultFallback are not configured, the BlockException will be thrown directly when the current is degraded (if the method itself does not have a defined throws BlockException, it will be wrapped with a layer of UndeclaredThrowableException by JVM).

Annotation usage

Code writing

Code structure

SentinelController/** * Notes @ SentinelResource Learning * @ author Milo Lee * @ date 2021-03-23 11:33 * / @ RestController public class SentinelController {@ Autowired private ISentinelService service; @ GetMapping (value = "/ hello/ {s}") public String apiHello (@ PathVariable long s) {return service.hello (s) } ISentinelService/** * @ author Milo Lee * @ date 2021-03-23 11:34 * / public interface ISentinelService {String hello (long s) } SentinelServiceImpl/** * @ author Milo Lee * @ date 2021-03-23 11:34 * / @ Service @ Slf4j public class SentinelServiceImpl implements ISentinelService {/ * Sentinel provides @ SentinelResource annotations to define the resource * @ param s * @ return * / @ Override / / value: resource name Required / / blockHandler corresponds to the name of the function handling BlockException / / fallback is used to provide fallback handling logic @ SentinelResource (value = "hello", blockHandler = "exceptionHandler", fallback = "helloFallback") public String hello (long s) {log.error ("hello: {}", s) when an exception is thrown Return String.format ("Hello at% d", s);} / / Fallback function with the same signature as the original function or with an argument of type Throwable. Public String helloFallback (long s) {log.error ("helloFallback: {}", s); return String.format ("Halooooo% d", s);} / / Block exception handler, the last parameter is one more BlockException, and the rest is the same as the original function. Public String exceptionHandler (long s, BlockException ex) {/ / Do some log here. Log.error ("exceptionHandler: {}", s); ex.printStackTrace (); return "Oops, error occurred at" + s;}}

Start our project and visit our test methods (refresh a few more times to see the control panel)

Current limiting test

Now let's configure a flow control rule for our resource: hello, as shown in the following figure

Configuration succeeded:

When we go back to our page and quickly refresh the page, we will find that occasionally the following information is displayed, indicating that the flow control rules we configured have been successfully intercepted.

Console log:

According to what we have learned above, if it exceeds the QPS we configured, the code will throw a BlockException exception. Why is there FlowException in the code? by looking at the source code, we will find that FlowException is actually a child of BlockException.

After the above testing, we find that the current limit is achieved by successfully implementing the annotation development.

Downgrade test

Before we downgrade the test, we need to modify our code

Front:

@ SentinelResource (value = "hello", blockHandler = "exceptionHandler", fallback = "helloFallback")

After:

@ SentinelResource (value = "hello", fallback = "helloFallback")

Configure flow control rules

Configure downgrade rules:

Testing method

According to the rules we configured, if qps > 1, the BlockException will definitely start. If the demotion rule determines that the exception rate exceeds 20% of the number of requests, the demotion will be triggered automatically?

My click rate: uniform-to-fast?

When I started to click at a uniform speed, I did not enter the helloFallback method. When I clicked quickly, I entered the helloFallback method, indicating that the downgrade rule takes effect, triggers the downgrade, and enters the callback function helloFallback.

Thank you for reading this article carefully. I hope the article "how to use @ SentinelResource annotations" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report