Catch wind and shadow 2022-06-23 18:13:25 阅读数:121
/**
The provider is running , But don't allow your own business logic , The default degraded data is returned ( Current limiting data )
try (Entry entry = SphU.entry("seckillSkus")) {
// Business logic
} catch(Exception e) {}
<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: '*'
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