当前位置:网站首页>【云原生】微服务之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
文章到这里就结束了,如果有什么疑问的地方请指出
再次感谢各位小伙伴儿们的支持
边栏推荐
- Register keyword
- 2020-12-12
- Golang url的编码和解码
- One dimensional array two dimensional array (sort Max insert sort)
- [step on the pit series] MySQL failed to modify the root password
- Puhua PLM empowers the whole scene product lifecycle management and helps the enterprise digital transformation of the main line of products
- Unity performance optimization
- go 解析身份证
- Compilation error: "not in executable format: file format not recognized"“
- 什麼是定義?什麼是聲明?它們有何區別?
猜你喜欢

jupyter远程服务器配置以及服务器开机自启

Zohocrm deluge function application time verification

Base64编码简介

Base64和Base64URL

Abstract classes and interfaces

Base64 and base64url

数据的存储
![[cocos creator] Click the button to switch the interface](/img/b8/f0fd54a2a197cbfd788990e2806b52.png)
[cocos creator] Click the button to switch the interface

Docker installs MySQL and successfully uses Navicat connection

Unity change default editor
随机推荐
Initial unity
I want to do large screen data visualization application feature analysis
haproxy+keepalived搭建01
超限黑客认知
Classes and objects
CLion-Toolchains are not configured Configure Disable profile问题解决
[cocos creator] Click the button to switch the interface
haproxy+keepalived集群搭建02
Golang json格式和结构体相互转换
十六进制编码简介
Generate video using clipout in viz engine
Demonstration of plug-in use of ventuz basic series
Xlua task list youyou
Un système de gestion de centre commercial pour la conception de cours de technologie d'application de base de données
Free use until 2015 -- viz artist multi touch plug-in package
animation
Easy touch plug-in
Multi traveling salesman problem -- overview of formula and solution process
P2622 light off problem II (state compression search)
E: Unable to locate package ROS melody desktop full