我们一般在代码中调用http请求时,都是封装了http调用类,底层自己定义请求头,在写的时候,也是需要对返回的值进行json解析,很不方便。
name:name属性会作为微服务的名称,用于服务发现
url:host的意思,不用加http://前缀
decode404:当发生http 404错误时,如果该字段位true,会调用decoder进行解码,否则抛出FeignException
使用流程:
(1)创建接口类(FeignApi),来统一规范需要调用的第三方接口
@FeignClient(name = "aaa", url = "localhost:8080", decode404 = true)
public interface FeignApi {
/**
* http请求
*/
@PostMapping("/api/xxxx/baiduaaa")
ResponseResult<ResponseVo> getSomeMoneyForYourSelfAAA(@RequestBody AAAParam param);
/**
* 模仿上面写的Get方式请求
*/
@GetMapping("/api/xxxx/baidubbb")
ResponseResult<ResponseVo> getSomeMoneyForYourSelfBBB(@RequestBody AAAParam param);
}
(2)在启动类加上注解,会去扫包注册Bean
@EnableFeignClients(basePackages = {"com.aaa"})
(3)业务代码调用处:
ResponseResult<ResponseVo> response = pmsFeignApi.getSomeMoneyForYourSelfAAA(param);
END