• springboot2 接口化 http 跨项目调用之 fegin


    1,依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    

    2,启动类添加注解:@EnableFeignClients

    3,添加配置启动 fegin

    # feign.hystrix.enabled: 启动 fegin 远程调用的熔断机制
    feign:
      hystrix:
        enabled: true
    

    4,添加接口,其中 @FeignClient 注解中的 name 为被调用项目的名称,fallback 指定容错的实现类

    package com.hwq.web.server.fegin;
    
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.GetMapping;
    
    @FeignClient(name = "global-api", fallback = SnowFlakeFeginImpl.class)
    public interface SnowFlakeFegin {
    
    	@GetMapping("snow-flake/id")
    	String getId();
    
    }
    

    5,容错的实现类

    package com.hwq.web.server.fegin;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class SnowFlakeFeginImpl implements SnowFlakeFegin {
    
    	@Override
    	public String getId() {
    		throw new RuntimeException("获取主键失败");
    	}
    
    }
    
  • 相关阅读:
    springboot启动只显示图标不报错
    tmux常用
    ubuntu+anaconda+mxnet环境配置
    OpenCV学习笔记(二)
    c++基础
    c++算法实现(一)
    pytorch使用不完全文档
    ubuntu上传到百度网盘
    pickel加速caffe读图
    caffe常用
  • 原文地址:https://www.cnblogs.com/lovling/p/13206278.html
Copyright © 2020-2023  润新知