feign远程调用怎么用restful(feign调用第三方接口)
feign远程调用怎么用restful,原文标题:Feign 接入第三方restful api 入门。通常来说,很多人用feign是用于内部环境的spri
feign远程调用怎么用restful,原文标题:Feign 接入第三方restful api 入门。
通常来说,很多人用feign是用于内部环境的spring cloud微服务调用。但feign其实是封装了http请求,那调用外部restful api是没有问题的。
在此讲下集成步骤,还有几种配置方法,以及一些注意点。
包引入
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>3.1.1</version></dependency>
这里要注意的是,和springboot版本匹配问题. 一开始使用了2.* 的feign版本,而我的springboot 是 2.6.5,然后报spring boot configration类找不到。更新feign到3.1.1就可以了。
feign编写
这个各种文章都有,
- 在Application类,增加注解 @EnableFeignclients
- 编写ApiClient interface
- 注入实例就可以调用了
- 请求得到的对象,可以直接反序列化为自定义的类。这个还挺方便。当然,也可以返回String,自己做json反序列化
- 当返回的状态不是200时,会异常的形式返回,这个需要处理
代码示例:
@SpringBootApplication@EnableFeignClientspublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}@FeignClient(name = "api", url = "https://abc.abc.com/api")public interface AbcApi { @requestMapping(value = "/", method = RequestMethod.POST) String query(@RequestParam Map<String, Object> param);}@SpringBootTest@Slf4jclass KaolaApiTest { @Resource AbcApi api; @Test void query() { Map<String, Object> param = new HashMap<>(); param.put("sortType", "1"); param.put("pageIndex", "1"); try { String responseEntity = api.query(param); log.info("responseEntity {}", responseEntity); } catch (Exception e){ // 非200的返回 log.warn("fail to request {}", e); } }}
还是比较简洁的,不过,似乎依赖spring框架比较多,不知道在其他环境集成是不是还是这么简单。
Request配置及统一处理
因为配置请求参数,和统一的业务处理我是放在一起的,所以这里也一起说明。
API请求的参数,主要包括像格式、字符集等。而统一业务处理,比如像加签、加密这些。
feign的配置有三种方式,按照作用域如下
- 全局配置
- 实例配置
- 单个请求配置
根据我的日志打印,是先调用2的实例配置,再调用全局配置。假如两个都配置,需要注意这个优先级。
======= local ApiInterceptor start ========
======= local ApiInterceptor end ========
======= global ApiInterceptor start ========
======= global ApiInterceptor end ========
全局配置
使用 @Configuration 的配置注入,并且需要 实现 RequestInterceptor 接口。
作用域是全局的,所以不适合做业务相关的处理。不过你假如只有一个client配置,写这里也无妨。
业务处理的方式,是获取body或者query参数,处理后,再放到header, query, body中去。
@Configuration // a global feign client interceptorpublic class FeignConfig implements RequestInterceptor { @Override public void apply(RequestTemplate template) { log.info("======= FeignClientsConfigurationInterceptor ========"); template.header("Content-Type", "application/json;charset=utf-8"); // 读取业务参数 template.queries or template.body JSONObject requestBody= (JSONObject) JSON.parse(new String(template.body())); String appid= (String) requestBody.get("appid"); // 处理业务数据 bodyText,并写回 template.body(bodyText); }}
实例配置
因为全局配置会影响所有的feign,所以假如项目中有不同的feign client,使用的时候不应该采用全局。
实例配置,自己建立一个interceptor,然后在ApiClient中配置
注意,实例的配置不应该加入@Configuration 的注解 ,防止被注入。
public class ApiConfig { @Bean public RequestInterceptor apiInterceptor(){ return template -> { log.info("======= apiInterceptor start ========"); handleRequestType(template); // 处理方式同全局 log.info("======= apiInterceptor end ========"); }; }}@FeignClient(name = "api", url = "https://abc.abc.com/api", configuration = ApiConfig.class) // 这里配置上public interface AbcApi { @RequestMapping(value = "/", method = RequestMethod.POST) String query(@RequestParam Map<String, Object> param);}
单个请求配置
这种配置方式就是直接在requestMap上增加,其实之前写的 method 就是如此。还可以增加其他的,比如如下,就是请求乱码可以增加一下header。
@FeignClient(name = "api", url = "https://abc.abc.com/api")public interface AbcApi { @RequestMapping(value = "/", method = RequestMethod.POST, produces = "application/json;charset=UTF-8", consumes = "application/json;charset=UTF-8") String query(@RequestParam Map<String, Object> param);}
好了,集成入门就到此。简单使用应该问题不大了。假如生产使用,还需要测试性能,连接池等问题。
我是窝牛,专注于各种大杂烩,各种都写,都写不好。欢迎讨论交流。
本文《feign远程调用怎么用restful(feign调用第三方接口)》由网赚联盟( wangzhuan.org.cn )整理或原创,感谢您的阅读。随机文章
SEO教程友情链接交换
GEO培训
SEO小小课堂网
SEO教程
站长导航
友情链接交换
搜素引擎算法
百度搜索“网赚联盟”即可找到本站,微信搜索“小小课堂网”关注小小课堂网公众号。网赚联盟( wangzhuan.org.cn )欢迎用户投稿,发布者:窝牛大杂烩,文章版权归作者所有,投稿文章不代表网赚联盟立场,中二少年发布为网赚联盟原创文章,转载请注明出处:https://wangzhuan.org.cn/900170.html

微信扫一扫
支付宝扫一扫