SpringBoot集成Feign组件调用第三方接口
一、 POM文件引入
- 版本控制
<properties> <java.version>1.8</java.version> <!-- openfeign --> <fegin.version>2.2.6.RELEASE</fegin.version> <fegin.http.client.version>11.0</fegin.http.client.version> </properties>
- 依赖管理
<dependencyManagement> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>${fegin.version}</version> </dependency> <!-- https://mvnrepository.com/artifact/io.github.openfeign/feign-httpclient --> <dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-httpclient</artifactId> <version>${fegin.http.client.version}</version> </dependency> </dependencies> </dependencyManagement>
- 引入
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>io.github.openfeign</groupId> <artifactId>feign-httpclient</artifactId> </dependency> </dependencies>
二、项目整体结构示意
1、启动类
import com.yz.yz.adapter.annotations.EnableAdapter; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.PropertySource; /** * @author xy */ //使用caffeine缓存 @EnableCaching @EnableAdapter //扫描rmi模块的interface @EnableFeignClients(basePackages = {"com.yz.yzkl.*"}) @SpringBootApplication //扫描其他非启动类包下的组件 @ComponentScan({"com.yz.yzkl.*"}) @PropertySource(ignoreResourceNotFound = true,value = {"",""}) public class YdyjApplication { public static void main(String[] args) { SpringApplication.run(YdyjApplication.class, args); } @Bean public ConfigurableServletWebServerFactory webServerFactory() { TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(); factory.addConnectorCustomizers(connector -> connector.setProperty("relaxedQueryChars", "|{}[]")); return factory; } }
2、yml 配置
feign: httpclient: enabled: true
3、rmi模块代码
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; /** * YjgjService * * @author xy * @version 1.0 * @description 啊啊 * @date 2020/12/10 16:25 */ //url 中的值 自己去yaml文件中配置就可以 也可以配置在配置中心 @FeignClient( name = "yjzService" ,url="${yz.yz.url}") public interface YzService { @RequestMapping(value = "/api/v1/y/{pathvariable1}/example", method = RequestMethod.POST) String getNR(@PathVariable("pathvariable1") String pathvariable1, @RequestParam("rqParam") String rqParam); }
4、Controller调用代码
/** * SearchService * * @author xuyang * @version 1.0 * @description 搜索业务类 * @date 2020/12/10 16:05 */ @Slf4j @Service public class SearchService { @Resource YzService yzService; public List<SearchParam> searchNrData(String keyword, String param1) { List<SearchParam> searchParams = Lists.newArrayList();; String nr = null; List<QwjsResult> qwjsResults = null; try { log.info("开始调用接口进行搜索,ajbh = {},keyword = {}",param1,keyword); nr = yjgjService.getNR(ajbh, key); log.info("调用结束,返回结果 nr = {}",nr); if(Objects.isNull(nr)){ log.error("Remote result is null "); return Lists.newArrayList(); } qwjsResults = JSONArray.parseArray(nr, QwjsResult.class); } catch (Exception e) { log.error("Remote 数据转换失败 error {}", e.getMessage(), e); } if(CollectionUtils.isNotEmpty(qwjsResults)){ searchParams = qwjsResults.stream().map(ConvertUtils::convertA2B).collect(Collectors.toList()); } return searchParams; } }
三、 一些说明
如果我们只需要使用Feign的功能按照上面引入spring-cloud-starter-openfeign就可以 之所以 引入feign-httpclient 是因为 默认情况下feign通过jdk中的HttpURLConnection向下游服务发起http请求
会有问题存在【没有连接池管理所有连接】,所以通过配置开启feign-httpclient来管理连接提升性能。
版权声明:本文为YangzaiLeHeHe原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。