springcloud篇2-feign、gateway 一、Feign(http客户端,替代RestTemplate)1.1 简介之前不同的服务之间进行远程调用使用的是RestTemplate。存在下面的问题:(1)代码可读性差,编程体验不统一;(2)参数复杂,URL难以维护。Feign(发音“奋”)是一个声明式的http客户端,作用是帮助我们便捷地实现http请求的发送。1.2 使用步骤上图中写了一个接口UserClient,该接口中包含所有对user-service服务的远程调用,@FeignClient指明服务名,基于SpringMVC的注解(包括@GetMapping,@PathVariable等)声明远程调用的信息。说明:@FeignClient注解说明远程调用地服务名称。例如:(1)在order-service项目中添加feign客户端依赖并开启自动装配功能!--feign客户端依赖--dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-openfeign/artifactId/dependency(2)在order-service中新建接口声明importcn.itcast.order.pojo.User;importorg.springframework.cloud.openfeign.FeignClient;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.PathVariable;@FeignClient("userservice")publicinterfaceUserClient{@GetMapping("/user/{id}")UserfindById(@PathVariable("id")Longid);}(3)在order-service中使用声明的接口发起远程调用@AutowiredprivateUserClientuserClient;publicOrderqueryOrderById(LongorderId){// 1.查询订单Orderorder=orderMapper.findById(orderId);Useruser=userClient.findById(order.getUserId());order.setUser(user);returnorder;}1.3 自定义配置说明:(1)日志级别NONE-没有日志BASIC-记录请求的开始和结束时间及耗时多久HEADERS-记录请求的开始和结束时及耗时多久、请求头和响应头FULL-记录请求的开始和结束时、请求头和响应头、请求体和响应体1.3.1 修改日志文件的级别例如:在order-serivce配置,feign:client:config:default:loggerLevel:Full日志查看:注意:全局配置的注解写在Application类(例如order-service的OrderApplication类)上,局部配置的注解写在Feign的配置类上(例如order-service的UserClient类)。例如,在order-service中进行配置,声明一个Bean:publicclassDefaultFeignConfiguration{@BeanpublicLogger.LevellogLevel(){returnLogger.Level.BASIC;}}加全局有效注解:@EnableFeignClients(defaultConfiguration=DefaultFeignConfiguration.class)1.4 性能优化Feign底层的客户端实现(三种模式):URLConnection性能不太好,也不支持连接池。建立连接池以后可以减少连接创建和销毁的性能损耗(连接创建时要三次握手,断开时要四次挥手)。对Feign性能的优化,主要是对底层客户端的改变。另外,Feign的日志级别也会影响性能,最佳是设置为basic或none。1.4.1 连接池配置在orderservice服务中加依赖:!-- 引入HttpClient依赖--dependencygroupIdio.github.openfeign/groupIdartifactIdfeign-httpclient/artifactId/dependencyfeign:httpclient:enabled:true# 支持httpClient的开关max-connections:200# 最大连接数max-connections-per-route:50# 单个路径的最大连接数1.5 最佳实践(企业级实践经验)观察可以看出,order-service发请求的配置与user-service接收的配置一致。order-service的配置如下:user-service的配置如下:1.5.1 给消费者的FeignClient和提供者的controller定义统一的父接口作为标准

相关新闻

最新新闻

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现 【免费下载链接】serenity The Serenity Operating System 🐞 项目地址: https://gitcode.com/GitHub_Trending/se/serenity 导读 本文以 getopt(3) 手册 为核心&a…

2026/10/1 19:32:24
轻量服务器还是ECS?大促云服务器选购与避坑实战指南

轻量服务器还是ECS?大促云服务器选购与避坑实战指南

每年大促节点,群里永远有人在问同一个问题:“38元的轻量服务器到底怎么抢?为什么我每次点进去都是已售罄?68元直购和99元的ECS我到底选哪个?”作为一个常年帮团队和自己采购云服务器的老用户,我太清楚这种纠…

2026/9/30 21:32:07
为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南 【免费下载链接】agents Multi-harness agentic plugin marketplace for Claude Code, Codex, Cursor, OpenCode, GitHub Copilot, and Google Antigravity 项目地址:…

2026/10/2 15:29:32
PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署 【免费下载链接】PaddleOCR Turn any PDF or image document into structured data for your AI. A powerful, lightweight OCR toolkit that bridges the gap between i…

2026/10/1 19:32:23
Spring源码解析:构造器注入的类型转换与候选匹配机制

Spring源码解析:构造器注入的类型转换与候选匹配机制

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/1 19:32:35
openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由 【免费下载链接】openai-agents-python A lightweight, powerful framework for multi-agent workflows 项目地址: https://gitcode.com/GitHub_Trending/op/openai-agents-pyth…

2026/9/30 21:32:11

日新闻

周新闻

月新闻