栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

SpringCloud: Feign整合Spring cloud circuit breaker(resilience4j)

SpringCloud: Feign整合Spring cloud circuit breaker(resilience4j)

1。在pom文件添加相关依赖:



    4.0.0

    cn.edu.tju
    springcloudcircuitbreakerfeign
    1.0-SNAPSHOT
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.6.RELEASE
        
    
    
        3.6.1
        2.10
        UTF-8
        UTF-8
        1.8
        2020.0.2
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        

        
            org.springframework.cloud
            spring-cloud-starter-circuitbreaker-resilience4j
        




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

        
        
            org.springframework.cloud
            spring-cloud-commons
            2.2.9.RELEASE
        


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

    



2.在配置文件application.properties里开启feign的circuit breaker支持:

feign.circuitbreaker.enabled=true
server.port=8091

3.在启动类加上@EnableFeignClients注解
4.编写FeignClient接口,(可以使其抛出错误来进行测试,比如在RequestMapping里配置一个不存在的地址)

@FeignClient(name = "demoservice", url = "http://139.198.xx.xx:9301", fallback = Fallback.class)
public  interface TestClient {

    @RequestMapping(method = RequestMethod.GET, value = "/test")
   String getTime();


}

5.和上一个类的同一个文件里编写用来进行熔断处理的类:

@Component
class Fallback implements TestClient {

    public String getTime() {
        return "exception caught when getting time.";
    }


}

6.编写控制器类,调用Feign接口

package cn.edu.tju.controller;

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

@RestController
public class TestController {
    @Autowired
    private TestClient testClient;
    @RequestMapping("/hi")
    public  String test(){
        String result=testClient.getTime();
        return result;
    }
}

7.运行主类,访问接口:http://localhost:8091/hi,输出如下,

可以看到熔断逻辑被执行了

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/722979.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号