fegin 单客户端配置类方式设置配置

36 篇文章 2 订阅
3 篇文章 0 订阅

Fegin 单客户端配置类方式设置配置

个人所知的这种方式可以配置的属性为: encoder , decoder, Contract , Logger , Retryer , ErrorDecoder , Request.Options , RequestInterceptor

官方文档

其中重点是: @FeignClient(name = "stores", configuration = FooConfiguration.class) ,

也可以使用增加方式: FooConfiguration只配置需要的配置

@FeignClient(value = "stores", configuration = {FooConfiguration.class,DefaultEncoder.class, DefaultDecoder.class})

并且FooConfiguration配置类上, 无需标明@Configuration, 标明后就是全局配置了!

个人例子, 设置单个客户端读取超时

fegin接口

@FeignClient(value = "account-service-api", url = "${account-service.url}", path = "/accountActiveInfo",
        configuration = AccountFeignRequest.class) // 使用自定义组合配置类
public interface AccountActiveInfoApi  {

    /**
     * 查询不活跃用户
     *
     * @param accountQueryRqt
     * @author: ZhiHao
     * @date: 2022/5/25
     */
    @PostMapping("/inactiveQuery")
    AccountActiveInfoRsp<AccountRsp> inactiveQuery(@RequestBody InactiveQueryRqt accountQueryRqt);
}

AccountFeignRequest 配置类

注意无需配置@Configuration 注解, 配置后是会到 父Bean工厂上下文中

public class AccountFeignRequest {

    @Bean
    public Request.Options feignRequestOptions() {
        return new Request.Options(10000,333*1000);
    }

    @Bean
    public Encoder defaultEncoder() {
        return new DefaultEncoder();
    }

    @Bean
    public Decoder defaultDecoder() {
        return new DefaultDecoder();
    }

    @Bean
    public ErrorDecoder defaultErrorDecode() {
        return new DefaultErrorDecode();
    }
}

结果:

jJ2Gz6.png

扩展:

配置隔离实现原理:

NamedContextFactory (比较好的说明1说明2)

不同微服务的客户端有不同的以及相同的配置有不同的 Bean,也有相同的 Bean。所以,我们可以针对每一个微服务将他们的 Bean 所处于 ApplicationContext 独立开来,不同微服务客户端使用不同的 ApplicationContext。NamedContextFactory 就是用来实现这种机制的

1

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

懵懵懂懂程序员

如果节省了你的时间, 请鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值