V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
wmhack
V2EX  ›  问与答

用 feign 有没有好用的挡板工具呢?

  •  
  •   wmhack · 2019-01-05 22:07:49 +08:00 · 1826 次点击
    这是一个创建于 1909 天前的主题,其中的信息可能已经有所发展或是发生改变。

    项目用的 feign,对接第三方接口时,经常遇到对方接口出错。所以想在 feign 进出的时候,做一个挡板,挡板开关打开时,就执行挡板 mock 的代码直接返回。

    2 条回复    2019-01-06 20:50:08 +08:00
    feiyuanqiu
        1
    feiyuanqiu  
       2019-01-06 20:02:59 +08:00
    有个不完善的方案:

    1. 新增一个挡板配置(@Configuration ),用于提供挡板 feign-client。设置这个 bean 优先级为 @Primary,同时设置只有在指定的属性存在时才生效

    @Configuration
    public class FeignStubConfig {

    @Bean
    @Primary
    @ConditionalOnProperty("use-feign-client-stub")
    public UserClient userClient() {
    return id -> new User(id, "test");
    }
    }


    2. 配置 @FeignClient 的 primary=false,否则在挡板配置生效时,会与挡板 feign-client 冲突

    @FeignClient(value = "user-v1", primary = false)
    public interface UserClient {

    @GetMapping("/users/{id}")
    User get(@PathVariable("id") String id);
    }


    3. 启动应用时指定使用挡板( java -jar app.jar --use-feign-client-stub )
    wmhack
        2
    wmhack  
    OP
       2019-01-06 20:50:08 +08:00
    @feiyuanqiu 非常感谢,这个方案很棒。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1431 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 17:27 · PVG 01:27 · LAX 10:27 · JFK 13:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.