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服务验证
相关推荐
woniu_maggie13 小时前
SAP CPI 开发RFC适配器的Integration Flow
后端
树獭叔叔14 小时前
Agent 记忆系统设计全景:从短期对话到长期知识沉淀
后端·aigc·openai
回到原点的码农14 小时前
maven导入spring框架
数据库·spring·maven
￰meteor14 小时前
23种设计模式 -【工厂方法】
后端
Predestination王瀞潞15 小时前
Java EE3-我独自整合(第一章:Spring入门)
java·spring·java-ee
bjzhang7515 小时前
SpringCloud——国产化改造,项目对接 TongWeb 嵌入版
后端·spring·spring cloud
光影少年15 小时前
平时如何学习新技术?
后端·学习·前端框架
无名-CODING15 小时前
SpringCloud 网关与熔断:Gateway + Sentinel 快速入门
spring cloud·gateway·sentinel
csdn2015_16 小时前
springboot controller 参数可以是List吗
spring boot·后端·list
Memory_荒年16 小时前
Dubbo调优实战:从QPS 1000到10000的惊险过山车之旅
java·后端·dubbo