1. 程式人生 > 其它 >Sentinel整合openFeign AND Sentinel持久化配置

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否則會報空指標異常