Springboot integrates Sentinel

Catch wind and shadow 2022-06-23 18:13:25 阅读数:121

springbootintegratessentinel

/**

  • 1、 Integrate Sentinel
  • 1)、 Import dependence spring-cloud-starter-alibaba-sentinel
  • 2)、 download sentinel Console
  • 3)、 To configure sentinel Console address information
  • 4)、 Adjust the parameters on the console 、【 By default, all flow control rules are saved in memory , Restart failure 】
  • 2、 Every micro service imports actuator : And cooperate with management.endpoints.web.exposure.include=*
  • 3、 Customize sentinel Data returned by flow control
  • 4、 Use Sentinel To protect the feign The remote invocation , Fuse ;
  • 1)、 Fuse protection of the caller :feign.sentinel.enable=true
  • 2)、 The caller manually specifies the degradation policy of the remote service . The remote service was degraded . Trigger our fuse callback method
  • 3)、 When browsing , Some remote services must be sacrificed . At the service provider ( Remote services ) Specify the degradation policy ;
  •  The provider is running , But don't allow your own business logic , The default degraded data is returned ( Current limiting data )
    
  • 5、 Customize protected resources
  • 1)、 Code
  •  try (Entry entry = SphU.entry("seckillSkus")) {
    
  •  // Business logic
    
  •  } catch(Exception e) {}
    
  • 2)、 Based on annotations
    Configuration to common, Because all services need to be managed , Configuration file each service needs to be configured
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

When it starts : --service port 8888 Set the boot port

 sentinel:
transport:
# To configure sentinel dashboard Address
dashboard: localhost:8080
# Default 8719 port , If it is occupied, it will be automatically transferred from 8719 Start in sequence +1 scanning , Until the unoccupied port is found
port: 8719
# Expose all endpoints
management:
endpoints:
web:
exposure:
include: '*'

 Insert picture description here
 Insert picture description here
Annotate the business classes that need to be restricted ;

 /** * Get the information about the goods that can participate in the second kill * @return */
@SentinelResource(value = "getCurrentSeckillSkusResource",blockHandler = "blockHandler")
@Override
public List<SeckillSkuRedisTo> getCurrentSeckillSkus()

Custom exception information :


/** * @Description: Custom blocking return method * @Created: with IntelliJ IDEA. * @author: Summer foam stops water * @createTime: 2020-07-13 11:30 **/
@Configuration
public class GulimallSeckillSentinelConfig {

public GulimallSeckillSentinelConfig() {

WebCallbackManager.setUrlBlockHandler(new UrlBlockHandler() {

@Override
public void blocked(HttpServletRequest request, HttpServletResponse response, BlockException ex) throws IOException {

R error = R.error(BizCodeEnum.TO_MANY_REQUEST.getCode(), BizCodeEnum.TO_MANY_REQUEST.getMessage());
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json");
response.getWriter().write(JSON.toJSONString(error));
}
});
}
}
版权声明:本文为[Catch wind and shadow]所创,转载请带上原文链接,感谢。 https://javamana.com/2022/174/202206231659400329.html