In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "RestTemplate and Feign Analysis in Sentinel". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "RestTemplate and Feign Analysis in Sentinel".
Sentinel API
Github: WIKI
Sphu (indicates the name of the resource to be protected)
Tracer (indicates the call source and the API for exception statistics)
ContextUtil (indicates entry into the call chain)
Flow control rules (for source attributes)
@ GetMapping ("/ test-sentinel-api") public String testSentinelAPI (@ RequestParam (required = false) String a) {String resourceName = "test-sentinel-api"; ContextUtil.enter (resourceName, "user-center-service"); / / define a sentinel protected resource with the name test-sentinel-api Entry entry = null; try {entry = SphU.entry (resourceName); / /. The protected business logic processes if (StringUtils.isEmpty (a)) {/ / Sentinel only counts the subclasses of BlockException & BlockException by default. If you want to count other exception information, add Tracer throw new IllegalArgumentException ("An is not empty.");} return a / / block Exception: if the protected resource is restricted or degraded, an exception will be thrown} catch (BlockException e) {log.error ("I am restricted!! {} ", e); return" I am restricted! " ;} catch (IllegalArgumentException argEx) {/ / Statistics current exception occurrence / proportion Tracer.trace (argEx); return "illegal parameter information";} finally {if (entry! = null) {entry.exit ();} ContextUtil.exit ();}}
Demotion rule
@ GetMapping ("/ test-sentinel-api") public String testSentinelAPI (@ RequestParam (required = false) String a) {/ / defines a sentinel protected resource with the name test-sentinel-api Entry entry = null; try {entry = SphU.entry ("test-sentinel-api"); / /. The protected business logic processes if (StringUtils.isEmpty (a)) {/ / Sentinel only counts the subclasses of BlockException & BlockException by default. If you want to count other exception information, add Tracer throw new IllegalArgumentException ("An is not empty.");} return a / / block Exception: if the protected resource is restricted or degraded, an exception will be thrown} catch (BlockException e) {log.error ("I am restricted!! {} ", e); return" I am restricted! " ;} catch (IllegalArgumentException argEx) {/ / count current exception occurrence / proportion Tracer.trace (argEx); return "illegal parameter information";} finally {if (entry! = null) {entry.exit ();}
Sentinel Annotation
Source code: com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect & com.alibaba.csp.sentinel.annotation.aspectj.AbstractSentinelAspectSupport
SentinelResource uses this annotation to reconstruct the above method
GetMapping ("/ test-sentinel-resource") @ SentinelResource (value = "test-sentinel-api", blockHandler = "blockException", fallback = "fallback") public String testSentinelResource (@ RequestParam (required = false) String a) {/ / The protected business logic processes if (StringUtils.isEmpty (a)) {/ / Sentinel only counts the subclasses of BlockException & BlockException by default. If you want to count other exception information, add Tracer throw new IllegalArgumentException ("An is not empty.");} return a } / * testSentinelResource BlockException method * / public String blockException (String a, BlockException e) {log.error ("current limit, {}", e); return "blockHandler corresponds to" current limit rules " } / * * testSentinelResource fallback method * {@ link SentinelResource} # fallback cannot be replenished in the version < 1.6. BlockException * / public String fallback (String a) {return "fallback corresponds to downgrade rules";}
RestTemplate integrates Sentinel
Use @ SentinelRestTemplate.
Resttemplate.sentinel.enabled can toggle whether the annotation is enabled or not. (the development phase is very meaningful. )
Source code: com.springframework.cloud.alibaba.sentinel.custom.SentinelBeanPostProcessor
@ Bean@LoadBalanced@SentinelRestTemplatepublic RestTemplate restTemplate () {return new RestTemplate ();} @ Autowiredprivate RestTemplate restTemplate;...Feign integrate Sentinel
Add feign.sentinel.enabled: true to the configuration file to open it.
Write fallback classes to implement feign client
@ Componentpublic class UserCenterFeignClientFallback implements IUserCenterFeignClient {@ Override public UserDTO findById (Long userId) {UserDTO userDTO = new UserDTO (); userDTO.setWxNickname (default user); return userDTO } @ Slf4j@Componentpublic class UserCenterFeignClientFallbackFactory implements FallbackFactory {@ Override public IUserCenterFeignClient create (Throwable cause) {return new IUserCenterFeignClient () {@ Override public UserDTO findById (Long userId) {log.warn ("remote calls are restricted / degraded, {}", cause); UserDTO userDTO = new UserDTO (); userDTO.setWxNickname ("default user") Return userDTO;}}
Apply fallback class
/ * * IUserCenterFeignClient for defines user-center feign client * fallbackFactory to get exception information * fallback cannot get exception information * * @ author Isaac.Zhang | Ruochu * @ since 2019-7-15 * / @ FeignClient (name = "user-center", / / fallback = UserCenterFeignClientFallback.class, fallbackFactory = UserCenterFeignClientFallbackFactory.class) public interface IUserCenterFeignClient {@ GetMapping (path = "/ users/ {userId}") public UserDTO findById (@ PathVariable Long userId);}
Start the application and set the flow control rules. The results are shown below.
{id: 1,... WxNickName: "default user"}
Thank you for reading, the above is the content of "RestTemplate and Feign Analysis in Sentinel". After the study of this article, I believe you have a deeper understanding of the problem of RestTemplate and Feign analysis in Sentinel, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.