当前位置:网站首页>【云原生】微服务之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
文章到这里就结束了,如果有什么疑问的地方请指出
再次感谢各位小伙伴儿们的支持
边栏推荐
- 什麼是定義?什麼是聲明?它們有何區別?
- C#课程设计之员工信息管理系统
- About the problem that the editor and the white screen of the login interface cannot be found after the location of unityhub is changed
- Clip Related Script
- 十六进制编码简介
- Viz artist advanced script video tutorial -- stringmap use and vertex operation
- P1896 [SCOI2005] 互不侵犯(状压dp)
- Ilruntime learning - start from scratch
- Base64 and base64url
- C language - Introduction - essence Edition - take you into programming (I)
猜你喜欢
[end of 2021] National Meteorological Short Video (Kwai, Tiktok) influence list in December
Base64和Base64URL
How to configure GDAL under idea
C language - Introduction - essence Edition - take you into programming (I)
Are you still watching the weather forecast on TV?
A tunnel to all ports of the server
Worldview satellite remote sensing image data / meter resolution remote sensing image
IP production stream is so close to me
C#课程设计之员工信息管理系统
MAE
随机推荐
Use filechannel to copy files
[step on the pit series] MySQL failed to modify the root password
璞华PLM为全场景产品生命周期管理赋能,助力产品主线的企业数字化转型
Transplantation of freetype Library
【cocos creator】点击按钮切换界面
idea取消引用顯示效果
haproxy+keepalived集群搭建02
Generate video using clipout in viz engine
Uniapp learning records
How to configure GDAL under idea
Multi traveling salesman problem -- overview of formula and solution process
The difference between hdmi2.1 and hdmi2.0 and the conversion of PD signals.
Install cross compiler arm none liunx gnueabihf
One dimensional array two dimensional array (sort Max insert sort)
My touch screen production "brief history" 1
jupyter远程服务器配置以及服务器开机自启
Golang中删除字符串的最后一个字符
Unity performance optimization
Conversion between golang JSON format and structure
Transplantation of tslib Library