目录
一、前言
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