Spring Cloud Alibaba 教程 Fegin 篇

2023-10-29 19:36

本文主要是介绍Spring Cloud Alibaba 教程 Fegin 篇,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Spring Cloud Alibaba 教程 | Feign 篇

写在前面的话: 本笔记在参考网上视频以及博客的基础上,只作为个人学习笔记,如有侵权联系删除,谢谢!

1、Feign替代RestTemplate

1.1 引入依赖

<!-- Feign 客户端依赖 -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
**1.2  添加注解**

启动类添加注解**@EnableFeignClients**开启Feign的功能

@SpringBootApplication
@EnableFeignClients
public class NacosConsumerApplication {public static void main(String[] args) {SpringApplication.run(NacosConsumerApplication.class,args);}
}   

1.3 编写客户端,使用@FeignClient(value = “服务提供方服务名”)

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;@FeignClient(value = "nacos-provider") //对应的服务提供方的spring.application.name配置
public interface ServiceFeginClient {@GetMapping("/nacos-provider/sayHello/getConfig") // 服务提供方的restful请求路径String getConfig();@GetMapping("/nacos-provider/sayHello/test/{name}")// nacos-provider为服务提供方的context-path,无则不写String sayHello(@PathVariable("name") String name);}

这个客户端主要是基于SpringMVC的注解来声明远程调用的信息,比如:

  • 服务名称:nacos-provider
  • 请求方式:GET
  • 请求路径:/nacos-provider/sayHello/test/{name}
  • 请求参数: String name
  • 返回值类型:String

这样,Feign就可以帮助我们发送http请求,无需自己使用RestTemplate来发送了。

1.4 使用Feign客户端


import com.cwh.fegin.ServiceFeginClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("hello")
public class ConsumerController {@Autowiredprivate ServiceFeginClient serviceFeginClient; //编写的客户端@RequestMapping("sayHi/{name}")public String sayHello(@PathVariable("name") String name){return serviceFeginClient.sayHello(name);}@RequestMapping("getConfig")public String getConfig(){return serviceFeginClient.getConfig();}}

1.5 总结

1、引入依赖

2、启动类上加注解@EnableFeignClients

3、使用@FeignClient编写客户端

4、使用FeignClient中定义的方法代替RestTemplate

2、Feign使用优化

Feign底层发起http请求,依赖于其它的框架。其底层客户端实现包括:

•URLConnection:默认实现,不支持连接池

•Apache HttpClient :支持连接池

•OKHttp:支持连接池

因此提高Feign的性能主要手段就是使用连接池代替默认的URLConnection。

这里我们用Apache的HttpClient来演示。

1)引入依赖

在Feign客户端的pom文件中引入Apache的HttpClient依赖:

<!--httpClient的依赖 -->
<dependency><groupId>io.github.openfeign</groupId><artifactId>feign-httpclient</artifactId>
</dependency>

2)配置连接池

在Feign客户端工程的application.yml中添加配置:

feign:client:config:default: # default全局的配置loggerLevel: BASIC # 日志级别,BASIC就是基本的请求和响应信息httpclient:enabled: true # 开启feign对HttpClient的支持max-connections: 200 # 最大的连接数max-connections-per-route: 50 # 每个路径的最大连接数

3)抽取方法

利用Maven分模块开发抽取FeignClient模块

在这里插入图片描述

添加依赖:

<!-- Feign 客户端依赖 -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

在使用该客户端的启动类@EnableFeignClients注解上添加扫描包路径

@EnableFeignClients(basePackages = "com.cwh.feign")

同时引入feign-api公共包

<dependency><groupId>com.cwh</groupId><artifactId>feign-api</artifactId><version>0.0.1-SNAPSHOT</version>
</dependency>

同时引入feign-api公共包

<dependency><groupId>com.cwh</groupId><artifactId>feign-api</artifactId><version>0.0.1-SNAPSHOT</version>
</dependency>

在使用该客户端的地方注入即可。

这篇关于Spring Cloud Alibaba 教程 Fegin 篇的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/302888

相关文章

SpringBoot实现RSA+AES自动接口解密的实战指南

《SpringBoot实现RSA+AES自动接口解密的实战指南》在当今数据泄露频发的网络环境中,接口安全已成为开发者不可忽视的核心议题,RSA+AES混合加密方案因其安全性高、性能优越而被广泛采用,本... 目录一、项目依赖与环境准备1.1 Maven依赖配置1.2 密钥生成与配置二、加密工具类实现2.1

在Java中实现线程之间的数据共享的几种方式总结

《在Java中实现线程之间的数据共享的几种方式总结》在Java中实现线程间数据共享是并发编程的核心需求,但需要谨慎处理同步问题以避免竞态条件,本文通过代码示例给大家介绍了几种主要实现方式及其最佳实践,... 目录1. 共享变量与同步机制2. 轻量级通信机制3. 线程安全容器4. 线程局部变量(ThreadL

python使用Akshare与Streamlit实现股票估值分析教程(图文代码)

《python使用Akshare与Streamlit实现股票估值分析教程(图文代码)》入职测试中的一道题,要求:从Akshare下载某一个股票近十年的财务报表包括,资产负债表,利润表,现金流量表,保存... 目录一、前言二、核心知识点梳理1、Akshare数据获取2、Pandas数据处理3、Matplotl

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Java使用Thumbnailator库实现图片处理与压缩功能

《Java使用Thumbnailator库实现图片处理与压缩功能》Thumbnailator是高性能Java图像处理库,支持缩放、旋转、水印添加、裁剪及格式转换,提供易用API和性能优化,适合Web应... 目录1. 图片处理库Thumbnailator介绍2. 基本和指定大小图片缩放功能2.1 图片缩放的

Python pandas库自学超详细教程

《Pythonpandas库自学超详细教程》文章介绍了Pandas库的基本功能、安装方法及核心操作,涵盖数据导入(CSV/Excel等)、数据结构(Series、DataFrame)、数据清洗、转换... 目录一、什么是Pandas库(1)、Pandas 应用(2)、Pandas 功能(3)、数据结构二、安

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security