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服务验证
相关推荐
逸风尊者5 分钟前
开发可掌握的知识:推荐系统
java·后端·算法
Violet_YSWY9 分钟前
阿里巴巴状态码
后端
灵魂猎手14 分钟前
Antrl4 入门 —— 使用Antrl4实现一个表达式计算器
java·后端
zhonghua88101620 分钟前
spring ai alibab agent之ReactAgent深度解读
java·人工智能·spring
moxiaoran575323 分钟前
Go语言的递归函数
开发语言·后端·golang
IT 行者1 小时前
Spring Security 7.0 新特性详解
java·后端·spring
华仔啊1 小时前
Java 的金额计算用 long 还是 BigDecimal?资深程序员这样选
java·后端
运维@小兵1 小时前
Spring AI系列——开发MCP Server和MCP Client(SSE方式)
java·人工智能·spring
IT 行者1 小时前
Spring Security 7.0 迁移指南
java·数据库·spring