【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

相关文章

Pydantic中Optional 和Union类型的使用

《Pydantic中Optional和Union类型的使用》本文主要介绍了Pydantic中Optional和Union类型的使用,这两者在处理可选字段和多类型字段时尤为重要,文中通过示例代码介绍的... 目录简介Optional 类型Union 类型Optional 和 Union 的组合总结简介Pyd

Java Predicate接口定义详解

《JavaPredicate接口定义详解》Predicate是Java中的一个函数式接口,它代表一个判断逻辑,接收一个输入参数,返回一个布尔值,:本文主要介绍JavaPredicate接口的定义... 目录Java Predicate接口Java lamda表达式 Predicate<T>、BiFuncti

Vue3使用router,params传参为空问题

《Vue3使用router,params传参为空问题》:本文主要介绍Vue3使用router,params传参为空问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录vue3使用China编程router,params传参为空1.使用query方式传参2.使用 Histo

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

使用Python自建轻量级的HTTP调试工具

《使用Python自建轻量级的HTTP调试工具》这篇文章主要为大家详细介绍了如何使用Python自建一个轻量级的HTTP调试工具,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下... 目录一、为什么需要自建工具二、核心功能设计三、技术选型四、分步实现五、进阶优化技巧六、使用示例七、性能对比八、扩展方向建

Spring Security方法级安全控制@PreAuthorize注解的灵活运用小结

《SpringSecurity方法级安全控制@PreAuthorize注解的灵活运用小结》本文将带着大家讲解@PreAuthorize注解的核心原理、SpEL表达式机制,并通过的示例代码演示如... 目录1. 前言2. @PreAuthorize 注解简介3. @PreAuthorize 核心原理解析拦截与

一文详解JavaScript中的fetch方法

《一文详解JavaScript中的fetch方法》fetch函数是一个用于在JavaScript中执行HTTP请求的现代API,它提供了一种更简洁、更强大的方式来处理网络请求,:本文主要介绍Jav... 目录前言什么是 fetch 方法基本语法简单的 GET 请求示例代码解释发送 POST 请求示例代码解释

Java图片压缩三种高效压缩方案详细解析

《Java图片压缩三种高效压缩方案详细解析》图片压缩通常涉及减少图片的尺寸缩放、调整图片的质量(针对JPEG、PNG等)、使用特定的算法来减少图片的数据量等,:本文主要介绍Java图片压缩三种高效... 目录一、基于OpenCV的智能尺寸压缩技术亮点:适用场景:二、JPEG质量参数压缩关键技术:压缩效果对比

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

Feign Client超时时间设置不生效的解决方法

《FeignClient超时时间设置不生效的解决方法》这篇文章主要为大家详细介绍了FeignClient超时时间设置不生效的原因与解决方法,具有一定的的参考价值,希望对大家有一定的帮助... 在使用Feign Client时,可以通过两种方式来设置超时时间:1.针对整个Feign Client设置超时时间