https://github.com/alibaba/Sentinel/releases/download/1.6.3/sentinel-dashboard-1.6.3.jar
java -Dserver.port=9090 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.6.3.jar
com.alibaba.cloud
spring-cloud-alibaba-dependencies
2.1.0.RELEASE
pom
import
feign和rest子工程引入:
com.alibaba.cloud
spring-cloud-starter-alibaba-sentinel
配置文件放入:
spring:
cloud:
sentinel:
transport:
dashboard: localhost:9090 #sentinel控制台的请求地址
分别启动两个子项目一个是9003端口,一个是9004。注册中心、以及消费者。
先分别访问两个接口:
之后刷新:
就出来了。对应的信息。
修改rest的order-service
package cn.itcast.order.controller;
import cn.itcast.order.entity.Product;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private RestTemplate restTemplate;
/**
* @SentinelResource
* blockHandler : 声明熔断时调用的降级方法
* fallback : 抛出异常执行的降级方法
* value : 自定义的资源名称
* * 不设置:当前全类名.方法名
*/
@SentinelResource(value="orderFindById",blockHandler = "orderBlockHandler",fallback = "orderFallback")
@RequestMapping(value = "/buy/{id}",method = RequestMethod.GET)
public Product findById(@PathVariable Long id) {
if(id != 1) {
throw new RuntimeException("错误");
}
return restTemplate.getForObject("http://service-product/product/1",Product.class);
}
/**
* 定义降级逻辑
* hystrix和sentinel
* 熔断执行的降级方法
* 抛出异常执行的降级方法
*/
public Product orderBlockHandler(Long id) {
Product product = new Product();
product.setProductName("触发熔断的降级方法");
return product;
}
public Product orderFallback(Long id) {
Product product = new Product();
product.setProductName("抛出异常执行的降级方法");
return product;
}
}
这里我们有两个方法,分别是熔断执行的降级方法,和抛出异常执行的降级方法。在hystrix中这个是不区分的,但是在sentinel是区分开的。
配置降级规则:
输入:
大家有没有发现,如果我们重启项目之后,再sentinel中配置的东西就都没了。我们可以搞一个本地配置。
写一个json
[
{
"resource": "orderFindById",
"controlBehavior": 0,
"count": 1,
"grade": 1,
"limitApp": "default",
"strategy": 0
}
]
然后在配置文件中加入,就可以了
cloud:
sentinel:
datasource:
ds1:
file:
file: classpath:flowrule.json
data-type: json
rule-type: flow
package cn.itcast.order.exception;
import cn.itcast.order.entity.Product;
import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import feign.Feign;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
public class ExceptionUtils {
/**
* 静态方法
* 返回值: SentinelClientHttpResponse
* 参数 : request , byte[] , clientRquestExcetion , blockException
*/
//限流熔断业务逻辑
public static SentinelClientHttpResponse handleBlock(HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
Product product = new Product();
product.setProductName("限流熔断");
return new SentinelClientHttpResponse(JSON.toJSONString(product));
}
//异常降级业务逻辑
public static SentinelClientHttpResponse handleFallback(HttpRequest request, byte[] body,
ClientHttpRequestExecution execution, BlockException ex) {
Product product = new Product();
product.setProductName("异常降级");
return new SentinelClientHttpResponse(JSON.toJSONString(product));
}
}
启动类加入:
package cn.itcast.order;
import cn.itcast.order.exception.ExceptionUtils;
import com.alibaba.cloud.sentinel.annotation.SentinelRestTemplate;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EntityScan("cn.itcast.order.entity")
public class RestOrderApplication {
/**
* sentinel支持对restTemplate的服务调用使用sentinel方法.在构造
* RestTemplate对象的时候,只需要加载@SentinelRestTemplate即可
*
* 资源名:
* httpmethod:schema://host:port/path :协议、主机、端口和路径
* httpmethod:schema://host:port :协议、主机和端口
*
* @SentinelRestTemplate:
* 异常降级
* fallback : 降级方法
* fallbackClass : 降级配置类
* 限流熔断
* blockHandler
* blockHandlerClass
*/
@LoadBalanced
@Bean
@SentinelRestTemplate(fallbackClass = ExceptionUtils.class,fallback = "handleFallback",
blockHandler = "handleBlock" ,blockHandlerClass = ExceptionUtils.class
)
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RestOrderApplication.class,args);
}
}
之后就不需要之前 @SentinelResource(value="orderFindById",blockHandler = "orderBlockHandler",fallback = "orderFallback") 的配置了。
配置sentinel