springcloud alibaba(四)OpenFeign实现服务调用

文章目录

OpenFeign实现服务调用
1、什么是OpenFeign

OpenFeign(也常称 Spring Cloud OpenFeign)是 Spring Cloud 生态中的一个声明式的 HTTP 客户端,基于 Netflix Feign 封装而来,旨在简化微服务架构中服务间的 HTTP 远程调用开发。它通过注解化的配置方式,让开发者无需手动编写复杂的 HTTP 请求代码(如 RestTemplate 的拼接 URL、设置请求参数等),仅需定义接口并添加注解,就能实现服务间的远程通信。

2、核心特性

OpenFeign(也常称 Spring Cloud OpenFeign)是 Spring Cloud 生态中的一个声明式的 HTTP 客户端,基于 Netflix Feign 封装而来,旨在简化微服务架构中服务间的 HTTP 远程调用开发。它通过注解化的配置方式,让开发者无需手动编写复杂的 HTTP 请求代码(如 RestTemplate 的拼接 URL、设置请求参数等),仅需定义接口并添加注解,就能实现服务间的远程通信。

2.1、声明式编程

OpenFeign 的核心是接口 + 注解的模式:开发者只需定义一个接口,在接口上添加@FeignClient等注解,指定要调用的远程服务名称,OpenFeign 会自动为该接口生成动态代理实现类,底层完成 HTTP 请求的发送与响应的解析。

2.2、集成 Spring MVC 注解

完全兼容 Spring MVC 的常用注解(如@GetMapping、@PostMapping、@RequestParam、@PathVariable等),开发者无需学习新的注解语法,降低了学习成本。

2.3、负载均衡集成

与 Spring Cloud 的负载均衡组件(如 Ribbon、Spring Cloud LoadBalancer)无缝集成,当远程服务有多个实例时,OpenFeign 会自动实现请求的负载分发。

2.4、熔断器支持

可与 Spring Cloud Circuit Breaker(如 Resilience4j、Sentinel)集成,实现服务调用的熔断、降级,避免服务雪崩。

2.5、请求 / 响应编解码

内置了对 JSON、XML 等数据格式的编解码支持,无需手动处理序列化和反序列化。

2.6、可定制化

支持自定义拦截器(RequestInterceptor)、编码器、解码器、日志级别等,满足个性化的 HTTP 调用需求。

3、OpenFeign的使用
3.1、加入的依赖
xml 复制代码
<!--fegin组件-->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
3.2、在主类上添加注解
java 复制代码
/**
 * 订单启动类
 */
@SpringBootApplication
//@EnableDiscoveryClient
@EnableFeignClients //主类上添加Fegin的注解
public class OrderApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderApplication.class, args);
    }
}
3.3、创建被调用方的Service, 并使用Fegin实现微服务调用
java 复制代码
package com.goblin.feign;

import com.goblin.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * 调用用户微服务
 */
//声明调用的提供者的name
//path是路径, 也可以把path的值写到映射路径中
@FeignClient(name = "service-user"/*, path = "/user"*/)
public interface UserClient {
    @GetMapping("/user/{uid}")
    User getByUid(@PathVariable Long uid);
}
java 复制代码
package com.goblin.feign;

import com.goblin.entity.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * 调用产品微服务
 */
@FeignClient(name = "service-product"/*, path = "/product"*/)
public interface ProductClient {

    @GetMapping("/product/{pid}")
    Product findById(@PathVariable Long pid);
}
3.4、调用方(OrderController)方法代码修改
java 复制代码
   @Resource
   private UserClient userClient;

   @Resource
   private ProductClient productClient;
   
   /**
     * 创建订单
     *
     * @param pid
     * @param uid
     * @return
     */
    @GetMapping("/order/{pid}/{uid}/{byNum}")
    public Order createOrder(@PathVariable Long pid,
                             @PathVariable Long uid,
                             @PathVariable Integer byNum) {

        Order order = new Order();
        //需要远程调用用户微服务获取用户信息
        order.setUid(uid);
        User user = userClient.getByUid(uid);
        log.info("查询到的用户信息: {}", JSON.toJSONString(user));
        order.setUsername(user.getUsername());
        //需要远程调用产品微服务获取产品信息
        order.setPid(pid);
        Product product = productClient.findById(pid);
        log.info("查询到的产品信息: {}", JSON.toJSONString(product));
        order.setPname(product.getPname());
        order.setPprice(0D);
        order.setNumber(byNum);
        orderService.createOrder(order);
        log.info("保存订单成功!");
        return order;
    }
4、重启shop-order服务验证
相关推荐
budingxiaomoli3 小时前
Spring IoC &DI
java·spring·ioc·di
Spider Cat 蜘蛛猫3 小时前
Springboot SSO系统设计文档
java·spring boot·后端
zyk_computer4 小时前
AI 时代,或许 Rust 比 Python 更合适
人工智能·后端·python·ai·rust·ai编程·vibe coding
雨辰AI4 小时前
SpringBoot3 项目国产化改造完整流程|从 MySQL 到人大金仓落地
java·数据库·后端·mysql·政务
带刺的坐椅4 小时前
Java 流程编排新范式 Solon Flow:一个引擎,七种节点,覆盖规则/任务/工作流/AI 编排全场景
java·spring·ai·solon·flow
xiaoshuaishuai85 小时前
C# CDN加速与离线包优化PowerSetting慢问题
开发语言·windows·spring·c#
GreenTea6 小时前
【Rust 2026教程:从零构建 Mini-OLAP 引擎】第 6 章 Benchmark 与优化路线图
后端
Rust语言中文社区6 小时前
【Rust日报】2026-05-14 Pyrefly v1.0 正式发布:快速的 Python 类型检查器和语言服务器
开发语言·后端·python·rust
GreenTea6 小时前
【Rust 2026教程:从零构建 Mini-OLAP 引擎】第 5 章 SQL → 逻辑计划 → 物理计划
后端
GreenTea6 小时前
【Rust 2026教程:从零构建 Mini-OLAP 引擎】第 4 章 哈希聚合:GROUP BY 的核心
后端