通常来说,很多人用feign是用于内部环境的springcloud微服务调用。但feign其实是封装了http请求,那调用外部restfulapi是没有问题的。 在此讲下集成步骤,还有几种配置方法,以及一些注意点。包引入dependencygroupIdorg。springframework。cloudgroupIdspringcloudstarteropenfeignartifactIdversion3。1。1versiondependency 这里要注意的是,和springboot版本匹配问题。一开始使用了2。的feign版本,而我的springboot是2。6。5,然后报springbootconfigration类找不到。更新feign到3。1。1就可以了。feign编写 这个各种文章都有,在Application类,增加注解EnableFeignClients编写ApiClientinterface注入实例就可以调用了请求得到的对象,可以直接反序列化为自定义的类。这个还挺方便。当然,也可以返回String,自己做json反序列化当返回的状态不是200时,会异常的形式返回,这个需要处理 代码示例:SpringBootApplicationEnableFeignClientspublicclassApplication{publicstaticvoidmain(String〔〕args){SpringApplication。run(Application。class,args);}}FeignClient(nameapi,urlhttps:abc。abc。comapi)publicinterfaceAbcApi{RequestMapping(value,methodRequestMethod。POST)Stringquery(RequestParamMapString,Objectparam);}SpringBootTestSlf4jclassKaolaApiTest{ResourceAbcApiapi;Testvoidquery(){MapString,ObjectparamnewHashMap();param。put(sortType,1);param。put(pageIndex,1);try{StringresponseEntityapi。query(param);log。info(responseEntity{},responseEntity);}catch(Exceptione){非200的返回log。warn(failtorequest{},e);}}} 还是比较简洁的,不过,似乎依赖spring框架比较多,不知道在其他环境集成是不是还是这么简单。Request配置及统一处理 因为配置请求参数,和统一的业务处理我是放在一起的,所以这里也一起说明。 API请求的参数,主要包括像格式、字符集等。而统一业务处理,比如像加签、加密这些。 feign的配置有三种方式,按照作用域如下全局配置实例配置单个请求配置 根据我的日志打印,是先调用2的实例配置,再调用全局配置。假如两个都配置,需要注意这个优先级。 localApiInterceptorstart localApiInterceptorend globalApiInterceptorstart globalApiInterceptorend全局配置 使用Configuration的配置注入,并且需要实现RequestInterceptor接口。 作用域是全局的,所以不适合做业务相关的处理。不过你假如只有一个client配置,写这里也无妨。 业务处理的方式,是获取body或者query参数,处理后,再放到header,query,body中去。ConfigurationaglobalfeignclientinterceptorpublicclassFeignConfigimplementsRequestInterceptor{Overridepublicvoidapply(RequestTemplatetemplate){log。info(FeignClientsConfigurationInterceptor);template。header(ContentType,applicationjson;charsetutf8);读取业务参数template。queriesortemplate。bodyJSONObjectrequestBody(JSONObject)JSON。parse(newString(template。body()));Stringappid(String)requestBody。get(appid);处理业务数据bodyText,并写回template。body(bodyText);}}实例配置 因为全局配置会影响所有的feign,所以假如项目中有不同的feignclient,使用的时候不应该采用全局。 实例配置,自己建立一个interceptor,然后在ApiClient中配置 注意,实例的配置不应该加入Configuration的注解,防止被注入。publicclassApiConfig{BeanpublicRequestInterceptorapiInterceptor(){returntemplate{log。info(apiInterceptorstart);handleRequestType(template);处理方式同全局log。info(apiInterceptorend);};}}FeignClient(nameapi,urlhttps:abc。abc。comapi,configurationApiConfig。class)这里配置上publicinterfaceAbcApi{RequestMapping(value,methodRequestMethod。POST)Stringquery(RequestParamMapString,Objectparam);} 单个请求配置 这种配置方式就是直接在requestMap上增加,其实之前写的method就是如此。还可以增加其他的,比如如下,就是请求乱码可以增加一下header。FeignClient(nameapi,urlhttps:abc。abc。comapi)publicinterfaceAbcApi{RequestMapping(value,methodRequestMethod。POST,producesapplicationjson;charsetUTF8,consumesapplicationjson;charsetUTF8)Stringquery(RequestParamMapString,Objectparam);} 好了,集成入门就到此。简单使用应该问题不大了。假如生产使用,还需要测试性能,连接池等问题。 我是窝牛,专注于各种大杂烩,各种都写,都写不好。欢迎讨论交流。