java练习生 使用feign

一、添加依赖

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

二、添加配置(可选)(application.yml)

参考:https://blog.csdn.net/hanzhen2010/article/details/116303368?spm=1001.2101.3001.6650.9&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-9.highlightwordscore&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7Edefault-9.highlightwordscore

参考:https://blog.csdn.net/weixin_33686714/article/details/92397143

三、创建FeignClient服务接口

参考:https://www.cnblogs.com/smiler/p/10689894.html

声明接口之后,在代码中通过@Resource注入之后即可使用。@FeignClient标签的常用属性如下:

  • name:指定FeignClient的名称,如果项目使用了Ribbon,name属性会作为微服务的名称,用于服务发现
  • url: url一般用于调试,可以手动指定@FeignClient调用的地址
  • decode404:当发生http 404错误时,如果该字段位true,会调用decoder进行解码,否则抛出FeignException
  • configuration: Feign配置类,可以自定义Feign的Encoder、Decoder、LogLevel、Contract
  • fallback: 定义容错的处理类,当调用远程接口失败或超时时,会调用对应接口的容错逻辑,fallback指定的类必须实现@FeignClient标记的接口
  • fallbackFactory: 工厂类,用于生成fallback类示例,通过这个属性我们可以实现每个接口通用的容错逻辑,减少重复的代码
  • path: 定义当前FeignClient的统一前缀

注意:name和value属性互为别名,任选一个使用即可。服务名不能有下划线,否则会提示【Service id not legal hostname】

1.最简单的直接访问某个接口

import hzq.maven.demo.springboot.models.dto.CalculateRequest;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 调用指定url下的接口
 */
@FeignClient(url="${http.test_service_url}", value = "demo-api")
public interface TestApi {
    @GetMapping(value = "/demo_api/calculate/test/get")
    String funA();
}

 2.访问微服务中指定服务的接口

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

/**
 * 如果没有配置url属性,value或name属性则会作为微服务的名称,用于服务发现
 */
@FeignClient(value = "fund-command")
public interface TestApi2 {
    @PostMapping(value = "/api/fund/command/daily-trading/callback")
    String funA();
}

 3.增加 Hystrix 断路熔断机制

断路器必须实现@FeignClient标记的接口。实现方法作为断路器的回调方法。需要使用@Component注解,保证能被Spring容器扫描到。

配置中需要指定【feign.hystrix.enabled = true】,否则断路器无法生效。

/**
 * test服务断路器
 */
@Component
public class TestApiHystrix implements TestApi {
    @Override
    public double funA(CalculateRequest request){
        return 0.001;
    }
}
/**
 * 调用指定url下的接口,增加fallback断路器
 */
@FeignClient(url="${http.test_service_url}", value = "test-1", fallback = TestApiHystrix.class)
public interface TestApi {
    @PostMapping(value = "/springboot/calculate/add3")
    double funA(CalculateRequest request);
}

Feign请求超时问题
Hystrix默认的超时时间是1秒,如果超过这个时间尚未响应,将会进入fallback代码。而首次请求往往会比较慢(因为Spring的懒加载机制,要实例化一些类),这个响应时间可能就大于1秒了
解决方案有三种,以feign为例。
方法一
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
该配置是让Hystrix的超时时间改为5秒
方法二
hystrix.command.default.execution.timeout.enabled: false
该配置,用于禁用Hystrix的超时时间
方法三
feign.hystrix.enabled: false
该配置,用于索性禁用feign的hystrix。不推荐使用。

四、使用

1.在main主入口类添加FeignClients启用注解:

@EnableFeignClients

2.注入及使用

    @Resource
    private TestApi testApi;

    public void tfunA()
    {
        var re = testApi.funA();
    }

扩展:https://zhuanlan.zhihu.com/p/149693905?from_voters_page=true

原文地址:https://www.cnblogs.com/ariter/p/15741322.html