当前位置:网站首页>【云原生】微服务之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
文章到这里就结束了,如果有什么疑问的地方请指出
再次感谢各位小伙伴儿们的支持
边栏推荐
猜你喜欢

How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03

Abstract classes and interfaces

Multi traveling salesman problem -- overview of formula and solution process

animation

LwIP learning socket (application)

A tunnel to all ports of the server

【cocos creator】点击按钮切换界面

Xlua task list youyou

一条通往服务器所有端口的隧道

Viz artist advanced script video tutorial -- stringmap use and vertex operation
随机推荐
regular expression
C language - Introduction - essence Edition - take you into programming (I)
Client server model
Uniapp learning records
Wpf: solve the problem that materialdesign:dialoghost cannot be closed
Classes and objects
PIP uses image website to solve the problem of slow network speed
Generate video using clipout in viz engine
Easy touch plug-in
E: 无法定位软件包 ros-melodic-desktop-full
Unity2019_ Natural ambient light_ Sky box
L'installateur a été installé avec une erreur inattendue
JSON与Object之间转换
Yolo series --- xml2txt script
2020-12-12
Worldview satellite remote sensing image data / meter resolution remote sensing image
璞华PLM为全场景产品生命周期管理赋能,助力产品主线的企业数字化转型
Retail philosophy retail psychological warfare after reading -- 7-11 is a good product!
Abstract classes and interfaces
Lua hot update basic grammar