Feign 调用第三方接口示例,FeignClient参数动态配置url、超时时间配置

Feign是一个灵感来自于Retrofit、JAXRS-2.0、WebSocket的Java Http客户端,Feign的主要目标是降低大家使用Http API的复杂性”。

其实,Feign底层依赖于Java的动态代理机制,对原生Java Socket或者Apache HttpClient进行封装,实现了基于Http协议的远程过程调用。当然,Feign还在此基础上实现了负载均衡、熔断等机制。

Feign超时时间配置

默认超时时间1秒!

feign:
  client:
    config:
      default:
        #不设置connectTimeout会导致readTimeout设置不生效
        connectTimeout: 3000
        readTimeout: 6000

使用@FeignClient注解时,只用name标签会怎样?

@FeignClient注解,name出现时,url必须与之同时出现

注解@FeignClient

动态url,则用url = ${}即可,需要注意的是,配置文件需在启动类所在的resource目录下!

// 注意:这里的url属性值不能为空字符串,但是可以设置为任意字符串值,在这里设置为“EMPTY”
@FeignClient(value = "CallbackAPI", url = "EMPTY", configuration = CallbackConfiguration.class)
public interface CallbackAPI {
    /**
     * 统一回调接口方法,请求消息体格式为JSON,响应消息体格式也为JSON
     * @param host 接口主机地址,如:http://localhost:8080
     * @param path 接口路径,如:/test/hello
     * @param queryMap 动态URL参数集合
     * @param body 请求消息体对象
     * @return
     */
    @RequestMapping(value = "{path}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    Object callback(URI host,
                    @PathVariable("path") String path,
                    @SpringQueryMap Map<String, Object> queryMap,
                    @RequestBody Object body);
    /**
     * 统一回调接口方法,请求消息体格式为JSON,响应消息体格式也为JSON
     * @param uri 完整的请求路径地址,如:http://localhost:8080/test/hello
     * @param queryMap 动态URL参数集合
     * @param body 请求消息体对象
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    Object callback(URI uri,
                    @SpringQueryMap Map<String, Object> queryMap,
                    @RequestBody Object body);
}
 
// 回调接口配置
public class CallbackConfiguration {
    @Bean
    public Encoder feignEncoder() {
        return new GsonEncoder();
    }
 
    @Bean
    public Decoder feignDecoder() {
        return new GsonDecoder();
    }
 
    @Bean
    public Retryer feignRetryer() {
        return new Retryer.Default();
    }
 
    @Bean
    public Logger feignLogger() {
        return new Slf4jLogger();
    }
 
    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.FULL;
    }
}
@Autowired
CallbackAPI callbackAPI;
 
String uri = "http://localhost:8080";
Map<String, Object> queryMap = new HashMap<>(0);
queryMap.put("k", "v");
Subject subject = Subject.builder().id(10).build();
// 请求主机地址与路径分开传递
this.callbackAPI.callback(URI.create(uri), "/test/simple/post/json", queryMap, subject);
// 直接将完整请求完整路径作为uri类型参数
this.callbackAPI.callback(URI.create("http://localhost:8080/test/simple/post/json"), queryMap, subject);

注解@RequestLine

 使用步骤        

1.  FeignClient 中不要写url, 使用 @RequestLine修饰方法

2. 调用地方必须引入  FeignClientConfiguration, 必须有Decoder, Encoder

3. 调用类必须以构建函数(Constructor) 的方式注入 FeignClient 类

4. 传入URL作为参数;

@FeignClient(name = "xxxxClient")
public interface XxxFeignClient {


    @RequestLine("POST")
    ResponseDto postMethod(URI baseUri, ApproveNotifyDto notifyDto);

    @RequestLine("GET")
    ResponseDto getMethod(URI baseUri, XxxDto xxxDto);
  
}
@Slf4j
@Component
@Import(FeignClientsConfiguration.class)
public class CallerService {

    private XxxFeignClient xxxFeignClient;

    @Autowired
    public CallerService(Decoder decoder, Encoder encoder) {
        xxxFeignClient = Feign.builder()
                //.client(client)
                .encoder(encoder)
                .decoder(decoder)
                .target(Target.EmptyTarget.create(XxxFeignClient.class));
    }

    public ResponseDto postMethod(String url, XxxxDto dto) throws URISyntaxException {
        return xxxFeignClient.postMethod(new URI(url), dto);
    }


    public String test() throws URISyntaxException {
        String url = "http://localhost:30866/";
        return xxxFeignClient.getMethod(new URI(url));

    }


}

  • 0
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在使用 Feign 进行服务间通讯时,为了避免因服务调用超时导致的问题,需要对超时时间进行配置Feign 提供了两种方式进行超时时间配置,分别是全局配置和单个请求配置。 全局配置: 在 Feign配置文件中添加以下属性进行全局配置: ``` feign: client: config: default: connectTimeout: 5000 readTimeout: 5000 ``` 其中,`connectTimeout` 表示连接超时时间,`readTimeout` 表示读取超时时间,单位均为毫秒。 单个请求配置: 在定义 Feign 接口方法时,可以使用 `@FeignClient` 注解的 `configuration` 属性指定一个配置类,并在该配置类中进行单个请求的超时时间配置示例代码如下: ``` @FeignClient(name = "example", configuration = ExampleFeignConfig.class) public interface ExampleFeignClient { @GetMapping("/example") @Headers("Content-Type: application/json") @RequestLine("GET /example") public String getExample(); } @Configuration public class ExampleFeignConfig { @Bean public Request.Options options() { return new Request.Options(5000, 5000); } } ``` 在上述示例中,`ExampleFeignConfig` 类中的 `options` 方法返回一个 `Request.Options` 对象,其中第一个参数表示连接超时时间,第二个参数表示读取超时时间,单位均为毫秒。 需要注意的是,单个请求配置会覆盖全局配置。所以如果在全局配置中已经指定了超时时间,而在单个请求配置中又指定了不同的超时时间,那么实际生效的是单个请求配置中指定的超时时间

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值