SpringCloud: feign整合sentinel实现降级

一、加依赖:



    4.0.0

    cn.edu.tju
    springbootcircuitbreaker
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.6
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                2021.0.1
                pom
                import
            

            
                com.alibaba.cloud
                spring-cloud-alibaba-dependencies
                2021.0.1.0
                pom
                import
            
        
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        

        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        

        
            org.springframework.cloud
            spring-cloud-starter-loadbalancer
        



        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-sentinel
        
        
            com.alibaba.csp
            sentinel-datasource-nacos
        

        
            org.springframework.cloud
            spring-cloud-starter-bootstrap
        

        
            com.google.guava
            guava
            30.0-jre
        

        
            com.google.code.gson
            gson
            2.8.6
        

    



二、配置文件启用feign对sentinel的支持:

feign:
  sentinel:
    enabled: true

三、定义被调用的接口:

package cn.edu.tju.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class StockController {
    @RequestMapping("/getStock")
    public String getStock(){
        int t = 1 / 0;
        return "getStock";
    }
}

四、定义FeignClient及降级类:

package cn.edu.tju.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient(value = "circuit-breaker", fallback = MyFallbackService.class)
public interface StockService {
    @RequestMapping("/getStock")
    String getStock();
}

package cn.edu.tju.service;

import org.springframework.stereotype.Component;

@Component
public class MyFallbackService implements StockService{
    @Override
    public String getStock() {
        return "degraded get stock";
    }
}

五、定义controller,注入@FeignClient

package cn.edu.tju.controller;

import cn.edu.tju.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DegradeController {
    @Autowired
    private StockService stockService;

    @RequestMapping("/getDegrade")
    public String getDegrade(){
        return stockService.getStock();
    }
}

六、接口调用结果:
SpringCloud: feign整合sentinel实现降级_第1张图片

你可能感兴趣的:(SpringCloud,spring,cloud,sentinel,spring,boot)