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 Sentinel integrates SpringCloud

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how to integrate Sentinel SpringCloud, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

Spring Cloud Alibaba Sentinel is provided by Alibaba and is committed to providing one-stop solutions for micro services. Spring Cloud Alibaba defaults to Sentinel integration, ServeLet, RestTemplate, FeignClient and Spring Flux. In the ecology of Spring, not only the blank of Hystrix in ServeLet and RestTemplate is not complete, but also it is perfectly compatible with the current-limiting and degraded usage of Hystrix in Feign, and supports flexible configuration and adjustment of current-limiting downgrade rules at run time.

Introduce dependencies:

Com.alibaba.cloud spring-cloud-alibaba-sentinel 2.2.0.RELEASE

Configuration file:

(in the process of getting started, if the JVM parameter used by the application name is set, the integration SpringCloud does not need that. After the application name is configured in the configuration file, the Sentinel will automatically load)

# set the name of the application spring: application: name: springCloudSentinel cloud: sentinel: transport: # set the host address and port number of the Sentinel console dashboard: localhost:9000

Write test Controller, add Sentinel_Cloud resource limit test to the console

@ SentinelResource (value = "Sentinel_Cloud", blockHandler = "exceptionHandler") @ GetMapping ("/ sentinelCloud") public String sentinelCloud () {/ / use the current limit rule return "Sentinel_Cloud, successfully call";}

The method called when the current is limited:

/ * define the handler function for downgrade / current limit * * @ param exception * @ return * / public String exceptionHandler (BlockException exception) {exception.printStackTrace (); return "Sentinel_Cloud, access current limit";}

Sentinel integrates Feign (OpenFeign)

Sentinel adapts to the Feign component. If you want to use it, you need two more steps in addition to referencing spring-cloud-starter-alibaba-sentinel 's dependencies:

Configure to open Sentinel's support for Feign: feign.sentinel.enable=true

Adding spring-cloud-starter-openfeign dependencies enables the Sentinel starter automation configuration class to take effect.

# set the name of the application spring: application: name: springCloudSentinel cloud: sentinel: transport: # set the host address and port number of the Sentinel console dashboard: localhost:9000 # enable Sentinel support for Feign feign: sentinel: enabled: true

Server caller Controller

@ GetMapping ("/ feignHello") public String feignHello () {return feignClient.feignHello ();}

Service provider FeignClient

@ FeignClient (contextId = "testFeignClient", value = "name of the service in the registry", fallback = FeignFallbackService.class) public interface TestFeignClient {/ * * OpenFeign remote invocation method * * @ return * / @ GetMapping ("/ test/feignHello") String feignHello ();}

Provide an implementation class of FeignClient interface as a method to handle current limitation

@ Servicepublic class FeignFallbackService implements TestFeignClient {@ Override public String feignHello () {return "Feign remote call limit";}}

Add a current limit rule to the Sentinel console:

Request method: http:// service module registry name / test/feignHello

The above is all the content of the article "how Sentinel integrates SpringCloud". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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