当前位置: 首页> 汽车> 时评 > (超详细)SpringCloud集成OpenFeign实现服务调用

(超详细)SpringCloud集成OpenFeign实现服务调用

时间:2025/7/9 23:41:49来源:https://blog.csdn.net/aoyuanyuan0921/article/details/139152497 浏览次数: 0次

文章目录

    • 概要
    • 整体架构流程
    • 技术名词解释
    • 技术细节
    • 小结

概要

        OpenFeign 是一个声明式的 HTTP 客户端,它使得我们可以用接口的方式调用 HTTP 服务,极大地简化了 REST 客户端的编写。在 Spring Cloud 中,集成了 OpenFeign,使得开发者可以更加方便地调用其他微服务的接口。

(通俗的说就是,在springcloud中可以直接在一个模块中调用另一个模块的接口,实现别的接口的功能)。 

特点

二、主要特点

  1. 声明式 HTTP 客户端:通过定义接口和注解即可实现 HTTP 调用。
  2. 与 Spring Cloud 无缝集成:支持 Spring MVC 注解,如 @RequestMapping、@GetMapping、@PostMapping 等。
  3. 负载均衡支持:结合 Ribbon 可以实现客户端负载均衡。
  4. 可插拔的解码和编码器:支持 JSON、XML 等格式的请求和响应。
  5. 日志记录:内置日志系统,可以方便地记录 HTTP 请求和响应。

集成流程

第一步,在脑子里想好哪一个是生产者(被调用的方法所在模块),哪一个是消费者(调用另一个模块的方法)
第二步,确定好要使用的注册中心(eureka或者nacos都可以,要保证生产者和消费者所在的服务可以被注册到注册中心中以供调用)

生产者模块(这里举得例子是商品模块作为生产者),引入nacos依赖注册中心,将服务注册上去。

        <dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><exclusions><exclusion><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId></exclusion></exclusions></dependency>

消费者模块

nacos依赖(将消费者模块注册到nacos中)

        <dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><exclusions><exclusion><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId></exclusion></exclusions></dependency>
第三步在消费者魔模块中引入openfeign依赖
         <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency>
第四步在消费者模块的启动类中加入注解

@FeignClient(value = "jingxi-mall")
public interface OpenFeignService {@GetMapping("/product/tb/{id}")Result<OpenFeignDto> getTypeById(@PathVariable("id") Integer id);@GetMapping("/product/tb/selectAll")public List<OpenFeignDto>  selectAll();@GetMapping("/product/tb/selectByIds")public List<OpenFeignDto>  selectAllByIds(@RequestParam Integer[] ids);}
第五步在消费者模块(jx-order模块)中引入对接到生产者模块(jx-mall)的方法

@FeignClient(value = "jingxi-mall")声音该消费者引用的是哪一个生产者模块(value中代码对应的是需要绑定的生产者在服务注册中心的名字)

需要注意是的不能加入@RequestMapping注解,否则会出现报错

该接口中的方法名,方法的Rest请求风格要和生产者中对应的方法对应

@FeignClient(value = "jingxi-mall")
public interface OpenFeignService {@GetMapping("/product/tb/{id}")Result<OpenFeignDto> getTypeById(@PathVariable("id") Integer id);@GetMapping("/product/tb/selectAll")public List<OpenFeignDto>  selectAll();@GetMapping("/product/tb/selectByIds")public List<OpenFeignDto>  selectAllByIds(@RequestParam Integer[] ids);}
生产者(ji-mall模块)中的方法
@Resourceprivate OpenFeignService openFeignService;@Resourceprivate JxCountService jxCountService;//分页查询排行榜(当日的榜单)@GetMapping("/selectAll")public List<TbProduct>  selectAll(){List<TbProduct> list = iTbProductService.getAll();return list;}

第六步,消费者模块调用

@RequestMapping("/order/sort")
public class OrderCountController {@Resourceprivate OpenFeignService openFeignService;@Resourceprivate JxCountService jxCountService;//分页查询排行榜(当日的榜单)@GetMapping("/countSort")public PageResponse getDto2(@RequestParam(required = false,defaultValue = "1") Integer pageNo,@RequestParam(required = false,defaultValue = "10") Integer pageSize){List<OpenFeignDto> openFeignDtos = openFeignService.selectAll();List<OrderCountDto> orderCountDtos = jxCountService.prepareSort(openFeignDtos);List<RedisOrderSort> list = jxCountService.queryToSortSum(orderCountDtos);
// 计算总页数int total = list.size();int pageCount = (total + pageSize - 1) / pageSize;// 计算起始索引int start = Math.max((pageNo - 1) * pageSize, 0);// 计算结束索引int end = Math.min(start + pageSize, total);// 获取当前页的数据List<RedisOrderSort> pageItems = list.subList(start, end);// 创建PageInfo对象PageInfo<RedisOrderSort> pageInfo = new PageInfo<>();pageInfo.setTotal(total);pageInfo.setPages(pageCount);pageInfo.setPageSize(pageSize);pageInfo.setPageNum(pageNo);pageInfo.setList(pageItems);pageInfo.setSize(pageItems.size());return PageResponse.suc(pageInfo);}

第七步,启动消费者和生产者模块

可以发现,该消费者模块已经可以成功嗲调用生产者模块中的.selectAll方法。

小结

通过集成 OpenFeign,Spring Cloud 应用可以方便地实现微服务之间的调用,简化了 REST 客户端的编写。结合服务发现、负载均衡等功能,能够显著提升微服务架构的开发效率和可靠性。在实际应用中,可以根据具体需求对 Feign 客户端进行定制化配置,以满足各种复杂的调用场景。

关键字:(超详细)SpringCloud集成OpenFeign实现服务调用

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: