当前位置:网站首页>【云原生】微服务之Feign的介绍与使用
【云原生】微服务之Feign的介绍与使用
2022-07-03 08:13:00 【小威要向诸佬学习呀】
前言:
最近在学习微服务相关的知识,看了黑马的相关课程,将关于Feign的知识又总结了一些,希望能帮到各位小伙儿们以及加深下自己的印象
如果文章有什么需要改进的地方还请大佬多多指教
小威先感谢大家的支持了
Feign的简介
Feign 是一个声明式的伪RPC的REST客户端,它用了基于接口的注解方式,很方便的客户端配置,Spring Cloud 给 Feign 添加了支持Spring MVC注解,并整合Ribbon及Eureka进行支持负载均衡。
Feign 是⼀个 HTTP 请求的轻量级客户端框架。通过接⼝口和注解的⽅式发起 HTTP 请求调⽤,面向接口编程,并不是像 Java 中通过封装 HTTP 请求报⽂的⽅式直接调⽤。
服务消费⽅拿到服务提供⽅的接⼝,然后像调⽤本地接⼝⽅法⼀样去调⽤,实际发出的是远程的请求。让我们更加便捷和优雅的去调⽤基于 HTTP 的 API,被⼴泛应⽤在 Spring Cloud 的解决⽅案中。
Feign的优点
之前我们利用RestTemplate发起远程调用的代码:
String url = "http://userservice/user/" + order.getUserld();
User user = restTemplate.getForObject(url, User.class);
上面的存在的问题有代码:
可读性差,编程体验不统一;
参数复杂URL难以维护
Feign是一个声明式的http客户端,官方地址:Feign官方链接
其作用就是帮助我们优雅的实现http请求的发送,解决上面提到的问题。
Feign如何使用
还是以我们之前的项目为例
在order-service服务的pom文件中引入feign依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
在order-service的启动类添加注解开启Feign的功能:
@EnableFeignClients
@MapperScan("cn.itcast.order,mapper")
@SpringBootApplication
public class OrderApplication
public static void main(String[] args) [
SpringApplication.run(OrderApplication.class,args)
在order-service中新建一个接口,内容如下:
package cn.itcast.order.client;
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")
public interface UserClient {
@GetMapping("/user/{id}")
User findById(@PathVariable("id") Long id);
}
这个客户端主要是基于SpringMVC的注解来声明远程调用的信息,比如:
- 服务名称:userservice
- 请求方式:GET
- 请求路径:/user/{id}
- 请求参数:Long id
- 返回值类型:User
这样,Feign就可以帮助我们发送http请求,无需自己使用RestTemplate来发送了。
我们修改order-service中的OrderService类中的queryOrderById方法,使用Feign客户端代替RestTemplate:
@Autowired
private UserClient userClient;
public Order query0rderById(Long orderId)
//1.查询订单
Order order = orderMapper.findById(orderId);
//2. 利用Feign发起http请求 查询用户
Useruser = userClient.findById(order.getUserId());
//3.封装user到0rder
order.setUser(user);
// 4.返回
return order;
Feign的使用总结
使用Feign的步骤:
第①步 引入依赖
第②步 添加@EnableFeignClients注解
第③步 编写FeignClient接口
第④步 使用FeignClient中定义的方法代替RestTemplate
文章到这里就结束了,如果有什么疑问的地方请指出
再次感谢各位小伙伴儿们的支持
边栏推荐
- [set theory] order relation (hastu example | divisive relation hastu | inclusive relation hastu | refinement relation hastu)
- Are you still watching the weather forecast on TV?
- Free use until 2015 -- viz artist multi touch plug-in package
- Lua hot update basic grammar
- 方正锐利重磅升级到12.0版本,包装印前处理更加便捷、高效!
- 十六进制编码简介
- YOLO系列 --- xml2txt脚本
- regular expression
- the installer has encountered an unexpected error installing this package
- P2704 [noi2001] artillery position (shape pressure DP)
猜你喜欢

Open the influence list of "National Meteorological Short Videos (Kwai, Tiktok) in November" in an interactive way“

animation

A tunnel to all ports of the server

Wpf: solve the problem that materialdesign:dialoghost cannot be closed

超限黑客认知

Free use until 2015 -- viz artist multi touch plug-in package

VMware virtual machine configuration static IP

Easy touch plug-in

An intern's journey to cnosdb

CLion-Toolchains are not configured Configure Disable profile问题解决
随机推荐
freetype库的移植
animation
Base64 and base64url
Unity performance optimization
使用 FileChannel 进行文件的复制拷贝
idea取消引用顯示效果
C language - Introduction - essence Edition - take you into programming (I)
JSON与Object之间转换
How to establish rectangular coordinate system in space
Transfinite hacker cognition
IP production stream is so close to me
MaxCompute字符串分割函数-SPLIT_PART
[usaco12mar]cows in a skyscraper g (state compression DP)
haproxy+keepalived集群搭建02
十六进制编码简介
vcs import src < ros2. Repos failed
go 解析身份证
Transplantation of tslib Library
Iterm2 setting
[set theory] order relation (the relation between elements of partial order set | comparable | strictly less than | covering | Haas diagram)