广州网站建设网络推广公司/网络营销人员招聘
因为sentinel 的功能就是保护服务的可用,有流量控制,线程数隔离,限流,熔断,降级,种种都是为了系统的可用性。下面我们来简单的使用一下sentinel
一, 首先还是加入依赖和配置yml文件
详情可以通过上篇文章查看。
二,设置熔断方法
2.1 编写代码
在服务消费者添加熔断方法,主要是使用sentinel的@SentinelResource 注解,参数有
@GetMapping("/buy/{id}")
@SentinelResource(value="order",blockHandler = "orderblockHandler",fallback
= "orderfallback")
public Product order(@PathVariable Long id) {return restTemplate.getForObject("http://shop-service-product/product/1", Product.class);
}
//降级方法
public Product orderblockHandler(Long id) {Product product = new Product();product.setId(-1l);product.setProductName("触发熔断降级方法");return product;
}
//降级方法
public Product orderfallback(Long id) {Product product = new Product();product.setId(-1l);product.setProductName("触发抛出异常方法");return product;
}
2.2 配置sentinel 页面,设置熔断时间,次数
注意,我使用的sentinel 1.8.3 并没有降级按钮,又换回了sentinel 1.6.3,sentinel 的降级策略才生效