wmhack
V2EX  ›  问与答

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

  •  
  •   wmhack · Jan 5, 2019 · 2616 views
    This topic created in 2685 days ago, the information mentioned may be changed or developed.

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

    2 replies    2019-01-06 20:50:08 +08:00
    feiyuanqiu
        1
    feiyuanqiu  
       Jan 6, 2019
    有个不完善的方案:

    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
       Jan 6, 2019
    @feiyuanqiu 非常感谢,这个方案很棒。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5860 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 01:59 · PVG 09:59 · LAX 18:59 · JFK 21:59
    ♥ Do have faith in what you're doing.