【SpringCloud】(八):认识Feign及使用

2024-08-26 16:32

本文主要是介绍【SpringCloud】(八):认识Feign及使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  这篇文章我们来认识一下Feign。

  官网链接:http://cloud.spring.io/spring-cloud-static/Camden.SR7/#spring-cloud-feign




1.声明式的客户端web服务,让写web服务客户端更加简单

2.使用的时候创建一个接口,加上注解。

3.可插拔的注解,Feign注解,编码,解码,Spring MVC的支持。

4.Feign整合Ribbon和Eureka,提供负载均衡。


拷贝并修改前面所描述的电影微服务microservice-comsumer-movie,修改为:microservice-comsumer-movie-feign


1.POM.xml加入依赖

		<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId></dependency>

2.在启动类上添加注解:@EnableFeignClients

package com.dynamic.cloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ComsumerMovieFeignApplication {public static void main(String[] args) {SpringApplication.run(ComsumerMovieFeignApplication.class, args);}
}

3.创建一个接口UserFeignClient

在接口上加入了注解@FeignClient。配置了用户为服务的ServicId :microservice-provider-user


package com.dynamic.cloud.feign;import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;import com.dynamic.cloud.entity.User;@FeignClient("microservice-provider-user")
public interface UserFeignClient {@RequestMapping(value="/simple/{id}",method = RequestMethod.GET) //只能是RequestMappingpublic User findById(@PathVariable("id") Long id); //@PathVariable需要设置括号中的名称@RequestMapping(value="/user",method = RequestMethod.POST)public User postUser(@RequestBody User user);//如果参数是对象,请求就不会成功,即使指定了Get方法,feign依然会以Post方式进行发送请求。//解决方案:@RequestParam("id") Long id,@RequestParam("name") Long name@RequestMapping(value="/get-user",method = RequestMethod.GET)public User getUser( @RequestParam("id") Long id,@RequestParam("name") String name); }


Controller

package com.dynamic.cloud.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import com.dynamic.cloud.entity.User;
import com.dynamic.cloud.feign.UserFeignClient;@RestController
public class MovieController {@Autowiredprivate UserFeignClient userFeignClient;@GetMapping("/movie/{id}")public User findById(@PathVariable Long id){return this.userFeignClient.findById(id);}@GetMapping("/test")public User testPost(User user){return this.userFeignClient.postUser(user);}@GetMapping("/get-user")public User getUser(@RequestParam("id") Long id,@RequestParam("name")String name){return this.userFeignClient.getUser(id,name);}
}


用户微服务:microservice-provider-user

在Controller中加入响应调用方式,其它不做修改

	@PostMapping("/user")public User postUser(@RequestBody User user){return user;}@GetMapping("/get-user")public User getUser( User user){return user;}

通过Feign完成调用。



这篇关于【SpringCloud】(八):认识Feign及使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

聊聊springboot中如何自定义消息转换器

《聊聊springboot中如何自定义消息转换器》SpringBoot通过HttpMessageConverter处理HTTP数据转换,支持多种媒体类型,接下来通过本文给大家介绍springboot中... 目录核心接口springboot默认提供的转换器如何自定义消息转换器Spring Boot 中的消息

python之uv使用详解

《python之uv使用详解》文章介绍uv在Ubuntu上用于Python项目管理,涵盖安装、初始化、依赖管理、运行调试及Docker应用,强调CI中使用--locked确保依赖一致性... 目录安装与更新standalonepip 安装创建php以及初始化项目依赖管理uv run直接在命令行运行pytho

Springboot项目构建时各种依赖详细介绍与依赖关系说明详解

《Springboot项目构建时各种依赖详细介绍与依赖关系说明详解》SpringBoot通过spring-boot-dependencies统一依赖版本管理,spring-boot-starter-w... 目录一、spring-boot-dependencies1.简介2. 内容概览3.核心内容结构4.

Spring Boot 整合 SSE(Server-Sent Events)实战案例(全网最全)

《SpringBoot整合SSE(Server-SentEvents)实战案例(全网最全)》本文通过实战案例讲解SpringBoot整合SSE技术,涵盖实现原理、代码配置、异常处理及前端交互,... 目录Spring Boot 整合 SSE(Server-Sent Events)1、简述SSE与其他技术的对

Spring Security 前后端分离场景下的会话并发管理

《SpringSecurity前后端分离场景下的会话并发管理》本文介绍了在前后端分离架构下实现SpringSecurity会话并发管理的问题,传统Web开发中只需简单配置sessionManage... 目录背景分析传统 web 开发中的 sessionManagement 入口ConcurrentSess

Java整合Protocol Buffers实现高效数据序列化实践

《Java整合ProtocolBuffers实现高效数据序列化实践》ProtocolBuffers是Google开发的一种语言中立、平台中立、可扩展的结构化数据序列化机制,类似于XML但更小、更快... 目录一、Protocol Buffers简介1.1 什么是Protocol Buffers1.2 Pro

Java实现本地缓存的四种方法实现与对比

《Java实现本地缓存的四种方法实现与对比》本地缓存的优点就是速度非常快,没有网络消耗,本地缓存比如caffine,guavacache这些都是比较常用的,下面我们来看看这四种缓存的具体实现吧... 目录1、HashMap2、Guava Cache3、Caffeine4、Encache本地缓存比如 caff

C#使用Spire.XLS快速生成多表格Excel文件

《C#使用Spire.XLS快速生成多表格Excel文件》在日常开发中,我们经常需要将业务数据导出为结构清晰的Excel文件,本文将手把手教你使用Spire.XLS这个强大的.NET组件,只需几行C#... 目录一、Spire.XLS核心优势清单1.1 性能碾压:从3秒到0.5秒的质变1.2 批量操作的优雅

MyBatis-Plus 与 Spring Boot 集成原理实战示例

《MyBatis-Plus与SpringBoot集成原理实战示例》MyBatis-Plus通过自动配置与核心组件集成SpringBoot实现零配置,提供分页、逻辑删除等插件化功能,增强MyBa... 目录 一、MyBATis-Plus 简介 二、集成方式(Spring Boot)1. 引入依赖 三、核心机制

Kotlin 枚举类使用举例

《Kotlin枚举类使用举例》枚举类(EnumClasses)是Kotlin中用于定义固定集合值的特殊类,它表示一组命名的常量,每个枚举常量都是该类的单例实例,接下来通过本文给大家介绍Kotl... 目录一、编程枚举类核心概念二、基础语法与特性1. 基本定义2. 带参数的枚举3. 实现接口4. 内置属性三、