千家信息网

Spring Cloud声明式客户端调用工具Feign怎么用

发表于:2025-02-12 作者:千家信息网编辑
千家信息网最后更新 2025年02月12日,Spring Cloud声明式客户端调用工具Feign怎么用,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。一、简介 feign 整合了r
千家信息网最后更新 2025年02月12日Spring Cloud声明式客户端调用工具Feign怎么用

Spring Cloud声明式客户端调用工具Feign怎么用,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

一、简介 feign 整合了rabbion 和 hytrix,完美兼容了spring mvc,使服务调用变得简单。 二、Ribbon和Feign的区别

Spring Cloud中支持两种客户端调用工具:

1.是Ribbon中的RestTemplate

2.是Feign

Ribbon :是一个基于 HTTP 和 TCP 客户端的负载均衡器

Feign: Spring Cloud Netflix 的微服务都是以 HTTP 接口的形式暴露的,所以可以用 Apache 的 HttpClient 或 Spring 的 RestTemplate 去调用,

是在Ribbon的基础上进一步进行Http封装的Http客户端,它是一种以接口+注解 形式实现微服务调用的声明式客户端调用工具。

它还整合了Spring Cloud Ribbon和Spring Cloud Hystrix。

特别指出:可以查看spring-cloud-netflix-core的jar包层级结构,清晰看出Netflix、Feign和Ribbon的关系

**总结:**Ribbon的RestTemplate接口调用方式,一般不用;

Feign声明式接口调用方式,建议使用

二、实战使用

A、Jar包依赖

Spring Boot 1.x版本依赖Feign

                    org.springframework.cloud            spring-cloud-starter-feign        

Spring Boot 2.x版本依赖Feign

                   org.springframework.cloud            spring-cloud-starter-openfeign        

**注意:**Spring Boot1.x和2.x两个版本引用Feign的名称不一样

B、Feign接口声明

@FeignClient(name = "tigbss-report")public interface ReportFeignService {    @RequestMapping(value = "/openApi/manage/queryReportDetailByReportAndVersionId", method = RequestMethod.GET)    ResponseObject queryRptDtail(@RequestParam(value = "reportId") String reportId,                                 @RequestParam(value = "versionId") String versionId,                                 @RequestParam(value = "systemLabel") Integer systemLabel);

注意:

**1、声明的是****接口,**不是类

2、@FeignClient(name = "tigbs-report"),@RequestMapping,这两个注解是必须的

3、接口类上不能加@RestController,因为接口是抽象类,不能被实例化

C、Feign接口被调用入口

@RestController@RequestMapping("api/operate/")public class ReportManageController {    @Autowired    private ReportFeignService reportFeignService;       /**          * @param reportId  报表id     * @param versionId 报表版本id     * @return ResponseObject     */    @ApiOperation(value = "查询报表详情", notes = "查询报表详情 ")    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "reportId", value = "报表ID", required = true, dataType = "String"),            @ApiImplicitParam(paramType = "query", name = "versionId", value = "报表版本ID", required = true, dataType = "String"),            @ApiImplicitParam(paramType = "query", name = "systemLabel", value = "系统标识", required = true, dataType = "int")})    @RequestMapping(value = "reportDetail", method = RequestMethod.GET)    public ResponseObject reportDetail(@RequestParam(value = "reportId") String reportId,                                       @RequestParam(value = "versionId") String versionId) {        return reportFeignService.queryRptDtail(reportId, versionId, 3);    }

D、启动类加上启动Feign客户端注解@EnableFeignClients

// 启用自动配置@ComponentScan(value = { "com.drpp", "com.framework.workflow" })@EnableFeignClients(basePackages = { "com.drpp" })@SpringBootApplication@EnableTransactionManagement@ImportResource(locations = { "classpath:spring-res.xml" })@EnableAutoConfiguration@EnableDiscoveryClient@EnableHystrix@EnableEurekaClient@ServletComponentScan({ "com.drpp" })public class CmsApplication  extends SpringBootServletInitializer {    public static void main(String[] args) {        SpringApplication.run(CmsApplication.class, args);    }    @Override    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {        return builder.sources(CmsApplication.class);    }    @Bean    public PlatformTransactionManager transactionManager(DataSource dataSource) {        return new DataSourceTransactionManager(dataSource);    }}

到此,Feign客户端调用的实战使用就结束了,就是那么so easy~~,继续加油吧~~

看完上述内容,你们掌握Spring Cloud声明式客户端调用工具Feign怎么用的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!

0