OpenFeign入门教程

引入Jar包

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

启动Feign功能

在启动类上添加@EnableFeignClients 注解

@SpringBootApplication
@EnableFeignClients
public class Application {
   
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

创建一个FeignClient

@FeignClient("stores")
public interface StoreClient {
    @RequestMapping(method = RequestMethod.GET, value = "/stores")
    List<Store> getStores();

    @RequestMapping(method = RequestMethod.GET, value = "/stores")
    Page<Store> getStores(Pageable pageable);

    @RequestMapping(method = RequestMethod.POST, value = "/stores/{storeId}", consumes = "application/json")
    Store update(@PathVariable("storeId") Long storeId, Store store);
}

配置FeignClient

如果同时存在java方式配置和文件方式配置,默认情况下配置文件方式会覆盖java配置方式,如果想要将优先级反转过来则可以通过属性:feign.client.default-to-properties 设置为false 即可。

java方式配置
@FeignClient(name = "stores", configuration = FooConfiguration.class)
public interface StoreClient {
    //..
}


@Configuration
public class FooConfiguration {
    @Bean
    public Contract feignContract() {
        return new feign.Contract.Default();
    }

    @Bean
    public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
        return new BasicAuthRequestInterceptor("user", "password");
    }
}

configuration 属性所指定的java类 需要在@ComponentScan 扫描的路径外,否则将会成为多个feignClient的公有配置。也可以使用排除属性将制定的配置类或包排除在外

配置文件方式配置
feign:
  client:
    config:
      stores: # 这里配置你自己的@feignclient 注解中制定的name属性的值,如我这里叫 stores
        connectTimeout: 5000
        readTimeout: 5000
        loggerLevel: full
        errorDecoder: com.example.SimpleErrorDecoder
        retryer: com.example.SimpleRetryer
        requestInterceptors:
          - com.example.FooRequestInterceptor
          - com.example.BarRequestInterceptor
        decode404: false
        encoder: com.example.SimpleEncoder
        decoder: com.example.SimpleDecoder
        contract: com.example.SimpleContract

配置一个公用的默认配置

java方式配置

通过指定 @EnableFeignClients 注解中的 defaultConfiguration 属性,该属性是一个对象数组,可指定多个

@SpringBootApplication
@EnableFeignClients(defaultConfiguration = {CustomConfiguration.class})
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}
配置文件方式配置
feign:
  client:
    config:
      default:
        connectTimeout: 5000
        readTimeout: 5000
        loggerLevel: basic

如何配置多个相同名称的FeignClient

通过指定不同的contextId 属性类创建多个相同名称的feignclient

@FeignClient(contextId = "fooClient", name = "stores", configuration = FooConfiguration.class)
public interface FooClient {
    //..
}
@FeignClient(contextId = "barClient", name = "stores", configuration = BarConfiguration.class)
public interface BarClient {
    //..
}

通过java方式创建一个FeignClient

FeignClientsConfiguration.class 是springcloud 提供的默认配置,可替换成自定的配置类
PROD-SVC 是服务器的名称 例如 eureka的application 名称

@Import(FeignClientsConfiguration.class)
class FooController {

    private FooClient fooClient;

    private FooClient adminClient;

        @Autowired
    public FooController(Decoder decoder, Encoder encoder, Client client, Contract contract) {
        this.fooClient = Feign.builder().client(client)
                .encoder(encoder)
                .decoder(decoder)
                .contract(contract)
                .requestInterceptor(new BasicAuthRequestInterceptor("user", "user"))
                .target(FooClient.class, "https://PROD-SVC");

        this.adminClient = Feign.builder().client(client)
                .encoder(encoder)
                .decoder(decoder)
                .contract(contract)
                .requestInterceptor(new BasicAuthRequestInterceptor("admin", "admin"))
                .target(FooClient.class, "https://PROD-SVC");
    }
}

使用降级功能

由于 openfeign中引入了hystrix-core包,所以可通过 feign.hystrix.enabled 属性开启hystrix 默认是关闭状态
在这里插入图片描述
例如:

@FeignClient(name = "hello", fallback = HystrixClientFallback.class)
protected interface HystrixClient {
    @RequestMapping(method = RequestMethod.GET, value = "/hello")
    Hello iFailSometimes();
}

static class HystrixClientFallback implements HystrixClient {
    @Override
    public Hello iFailSometimes() {
        return new Hello("fallback");
    }
}

如果需要知道触发降级的原因(也就是报了啥错)使用fallbackFactory 属性配置:

@FeignClient(name = "hello", fallbackFactory = HystrixClientFallbackFactory.class)
protected interface HystrixClient {
    @RequestMapping(method = RequestMethod.GET, value = "/hello")
    Hello iFailSometimes();
}

@Component
static class HystrixClientFallbackFactory implements FallbackFactory<HystrixClient> {
    @Override
    public HystrixClient create(Throwable cause) {
        return new HystrixClient() {
            @Override
            public Hello iFailSometimes() {
                return new Hello("fallback; reason was: " + cause.getMessage());
            }
        };
    }
}

问题

之前遇到的问题,当我使用配置文件方式来管理feignclient的相关配置时,同时开启 hystrix功能。这时feignclient的配置无效。 官网中暂时没有找到相关说明。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值