Sentinel整合openFeign AND Sentinel持久化配置

server: port: 8888 spring: application: name: nacos-customer-openfegin-8888 cloud: nacos: discovery: server-addr: localhost:8848 management: endpoints: web: exposure: include: "*"
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>






ribbon: ReadTimeout: 5000 ConnectTimeout: 5000













@RestController public class FeignController { @Autowired FeignService feignService; @GetMapping("/getInfo/{id}") @SentinelResource(value = "getInfo",blockHandler = "blockHandlerRes",fallback = "fallbackHandler") public JsonResult<String> getInfo(@PathVariable Long id){ if(id>3){ throw new RuntimeException("没有该id"); } return feignService.mysql(id); } public JsonResult<String> blockHandlerRes(Long id, BlockException e){ return new JsonResult<>(200,"sentinel服务不可用"); } public JsonResult<String> fallbackHandler(Long id,Throwable throwable ){ return new JsonResult<>(200,"运行时异常服务不可用"); } }
@Service @FeignClient(value = "nacos-provider",fallback = FeignServiceImpl.class) public interface FeignService { @GetMapping("/info/{id}") public JsonResult<String> mysql(@PathVariable("id") Long id); }
@Component public class FeignServiceImpl implements FeignService{ @Override public JsonResult<String> mysql(Long id) { return new JsonResult<>(400,"服务降级返回!"); } }
server: port: 8084 spring: cloud: sentinel: transport: dashboard: localhost:8080 port: 8719 nacos: discovery: server-addr: localhost:8848 application: name: nacos-customer service-url: nacos-user-service: http://nacos-provider feign: sentinel: enabled: true
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
----------------------------------------------------------------------------------------------------------------------------------------------------------

<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<version>1.8.3</version>
</dependency>




spring: application: name: springcloud-order-8899 cloud: nacos: discovery: server-addr: localhost:8848 sentinel: transport: dashboard: localhost:8080 datasource: #配置sentinel持久化 nscos: nacos: username: nacos password: nacos server-addr: localhost:8848 group-id: DEFAULT_GROUP data-id: order-sentinel.json rule-type: flow #限流 data-type: json profiles: active: dev server: port: 8899
[ { "resource": "test1", "limitApp": "default", "grade": 1, "count": 2, "strategy": 0, "controlBehavior": 0, "clusterMode": false } ] ---------------具体内容含义----------------- resource:资源名称; limitApp:来源应用; grade:阈值类型,0表示线程数,1表示QPS; count:单机阈值; strategy:流控模式,0表示直接,1表示关联,2表示链路; controlBehavior:流控效果,0表示快速失败,1表示Warm Up,2表示排队等待; clusterMode:是否集群。 ```
@RestController public class OrderController { @GetMapping("/getTest1") @SentinelResource(value = "test1") public String test1() { return "test1"; } }
后面一定要加nacos的username和password否则会报空指针异常

浙公网安备 33010602011771号