springCloud的Feign在项目中的使用总结——https请求、结合hystrix、httpclient配置等

本文详细介绍了SpringCloud Feign在实际项目中的应用,包括发送POST请求参数传递、访问HTTPS地址、使用HttpClient替换底层连接以及集成Hystrix进行超时回调。此外,还探讨了为何即使有Hystrix控制最大连接数,仍需要自定义连接池配置的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、前言

Feign目前在网上能找的的有两种使用方式,一种是基于springcloud,使用@feignClient+@requestMapping。一种是github上的开源项目,使用@RequestLine注解。

此次示例使用的是第一种方式,但是并没有使用springcloud的服务中心eureka。主要记录了使用feign发送请求参数的传递,访问https地址、httpclient替换feign底层的urlConection,集成hystrix进行超时回调。

二、具体使用

在具体使用之前需要进行相关jar的依赖添加:

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
        <!-- 使用Apache HttpClient替换Feign原生httpclient -->
        <dependency>
            <!--不是netfix包下了,应该是独立出来开源了-->
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-httpclient</artifactId>
            <!-- <version>9.5.1</version>   这里可以不指定version spring-boot-parent中已经有版本定义-->
        </dependency>

feign-httpclient用于httpclient替换feign底层http请求。
spring-cloud-starter-netflix-ribbon在进行https地址调用的时候会用到。相关的application.yml配置在具体demo按需要添加。

1. feign发送post请求参数的传递

@FeignClient(name="feignService",url="${dlc.amap.ip}")
public interface FeignHttpsService {
   
   
    
    @RequestMapping(value="${dlc.amap.address}={sign}",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    String sendPost(@RequestBody String str, @PathVariable String sign);
    
    @RequestMapping(value="helloget",method = RequestMethod.GET)
    String sendGet();
    
}

定义一个接口,接口中有发送get和post两种请求的方法,此接口不需要写实现方法,在项目中使用@autowired注解引入进行使用即可。

@RestController
public class FeignController {
   
   

    @Autowired
    
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值