java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar xxxxx.jar
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
spring:cloud:sentinel:transport:port: 8719dashboard: localhost:8080
package com.slj.order.service.Impl;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.slj.entry.Product;
import com.slj.order.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
/**
* @program: springcloud-parent
* @description:
* @author: 孙路军
* @create: 2021-07-15 10:39
**/
@Service
public class ProductServiceImpl_blank implements ProductService {
@Autowired
private RestTemplate restTemplate;
@Override
@SentinelResource(value = "findByid", blockHandler = "selectByIdBlock", fallback = "selectByIdBack")
public Product findByid(Integer pid) {
if(pid==1){
throw new RuntimeException("pid不能为1");
}
return restTemplate.getForObject("http://shop-product/product/findById?pid=" + pid, Product.class);
}
public Product selectByIdBlock(Integer pid,BlockException e){
Product product=new Product();
product.setPid(pid);
product.setPname("熔断");
return product;
}
public Product selectByIdBack(Integer pid,Throwable e){
Product product=new Product();
product.setPid(pid);
product.setPname("异常");
return product;
}
}


blockHandlerMethod方法里


降级是通常是为异常设置的,设置好降级之后,一般我们要删除流控设置的东西。要不然时间间隔效果看不出来。设置id为1的时候出现异常

多次访问id为1就会出现下图所示

设置id为2的不是异常,没过间隔30秒访问id为2也会出现

过了30秒之后才会成功访问

spring:cloud:sentinel:datasource:ds1:fifile:fifile: classpath:flflowRule.jsondata-type: jsonrule-type: flflow
[{"resource":"fifindByid","count":1,"grade":1,"limitApp":"default","strategy":0,"controlBehavior":0}]

创建util类ExceptionUtil
package com.slj.order.util;
import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import com.slj.entry.Product;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;
/**
* @program: springcloud-parent
* @description: gongjulei
* @author: 孙路军
* @create: 2021-07-15 16:44
**/
@Component
public class ExceptionUtil {
public static ClientHttpResponse fallback(HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException exception) {
return new SentinelClientHttpResponse(JSON.toJSONString(new Product(1,"服务流量控制-兜底的数据",5555.0,1000)));
}
public static ClientHttpResponse handlerException(HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException exception) {
return new SentinelClientHttpResponse(JSON.toJSONString(new Product(1,"服务熔断降级控制-兜底的数据",6666.0,1000)));
}
}


openfeign的支持
在配置里多加3行代码,是openfeign让sentinel管理所需要的
添加依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency>
spring:cloud:sentinel:transport:dashboard: localhost:8080feign:sentinel:enabled: true
创建一个实现类继承所需要的openfeign的方法
openfeign的接口




4310



被折叠的 条评论
为什么被折叠?



