【微服务】http客户端Feign

一、Fegin替代RestTemplate

RestTemplate :代码可读性差,编程体验不统一,参数复杂URL难以维护。
Feign :是一个声明式的http客户端,官方地址:https://github.com/OpenFeign/feign,其作用就是帮助我们优雅的实现http请求的发送,解决上面提到的问题。

使用Feign步骤如下:

1、引入依赖

java 复制代码
        <!--Feign的客户端依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

2、在orderservice启动类添加注解开启Feign的功能

3、编写Feign客户端

java 复制代码
package cn.itcast.order.clients;

import cn.itcast.order.pojo.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient("userservice") //发送http请求时需要知道服务名称,在此指定
public interface UserClient {
    @GetMapping("/user/{id}")
    User findById(@PathVariable("id") Long id);
}

修改orderService:

java 复制代码
package cn.itcast.order.service;

import cn.itcast.order.clients.UserClient;
import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;
import cn.itcast.order.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserClient userClient;

    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        //2、利用Feign远程调用
        User user = userClient.findById(order.getUserId());
        //3 封装user到order
        order.setUser(user);
        // 4.返回
        return order;
    }
/*    @Autowired
    private RestTemplate restTemplate;

    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        //2、利用RestTemplate发送http请求,查询用户
        //2.1 url路径
        String url = "http://userservice/user/" + order.getUserId();
        //2.2 发起http请求,实现远程调用(原本返回的是json,告诉它我们要User对象类型)
        User user = restTemplate.getForObject(url, User.class);
        //3 封装user到order
        order.setUser(user);
        // 4.返回
        return order;
    }*/
}

二、自定义配置


全局生效:

三、Feign性能优化---连接池配置

Fegin添加HttpClient依赖

java 复制代码
        <!--引入HttpClient依赖-->
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-httpclient</artifactId>
        </dependency>
java 复制代码
feign:
  httpclient:
    enabled: true # 支持HttpClient的开关
    max-connections: 200 #最大连接数
    max-connections-per-route: 50 #单个路径的最大连接数
相关推荐
会周易的程序员9 小时前
aiDgePLC iec61131 虚拟机 完整使用文档
c++·物联网·架构·st·iec61131
小小龙学IT10 小时前
Boost.Beast 深度实战:基于 Asio 的开源 C++ HTTP/WebSocket 协议库
c++·websocket·http
宇擎智脑科技11 小时前
DeepSeek Harness 架构解析:MCP 和 Skill 如何被统一为 Cordis 插件
架构·deepseek·harness·dsh
这个DBA有点耶12 小时前
分布式数据库到底该不该上?从判断标准到架构选型的实战思考
数据库·架构·dba
楚识科技13 小时前
破除通用瓶颈——企业级OCR定制化开发的架构思维与实战范式
架构·ocr
ryan_99613 小时前
MCP Transport 完整指南:stdio 与 Streamable HTTP 到底怎样传消息
网络·网络协议·http
刘立军13 小时前
RESTful 与契约优先:规范接口定义,统一接口设计范式
后端·架构·ai编程
heimeiyingwang13 小时前
【架构实战】分布式事务:从CAP定理到Seata实战,一文讲透跨服务数据一致性
分布式·架构
独孤九剑打醒他14 小时前
RGB‑TOT 全架构系统仿真:红外波长簇光通信完整验证
架构
这个DBA有点耶15 小时前
从库延迟的“二次放大”效应:一次大事务,拖垮整个读写分离
数据库·mysql·架构