当前位置:网站首页>[cloud native] introduction and use of feign of microservices
[cloud native] introduction and use of feign of microservices
2022-07-26 05:35:00 【Hua Weiyun】
Preface :
Recently, I am learning about micro Services , Read the relevant courses of dark horse , Will relate Feign The knowledge of has summed up some , I hope I can help you guys and deepen your impression
If there is anything to be improved in the article, please give me more advice
Xiao Wei first thanked everyone for their support 
The following text begins
@[toc]
Feign An introduction to the
Feign Is a declarative artifact RPC Of REST client , It uses interface based annotation , Very convenient client configuration ,Spring Cloud to Feign Added support Spring MVC annotation , And integrate Ribbon And Eureka Support load balancing .
Feign yes ⼀ individual HTTP Lightweight client framework for requests . By connecting to ⼝ Oral and annotated ⽅ Style initiation HTTP Ask for a tune ⽤, Interface oriented programming , It's not like Java Through encapsulation HTTP Ask for the newspaper ⽂ Of ⽅ Direct adjustment ⽤.
Service consumption ⽅ Get the service ⽅ Of ⼝, And then it's like ⽤ Local connection ⼝⽅ Law ⼀ I'm going to adjust it ⽤, It's actually a remote request . Let's make it more convenient and elegant ⽤ be based on HTTP Of API, By ⼴ Pan Ying ⽤ stay Spring Cloud The solution of the ⽅ In the case .
Feign The advantages of
Before we used RestTemplate The code that initiates the remote call :
String url = "http://userservice/user/" + order.getUserld();User user = restTemplate.getForObject(url, User.class); The problems above are code :
Poor readability , Inconsistent programming experience ;
Complex parameters URL Difficult to maintain
Feign It's a declarative one http client , Official address :Feign The official link
Its function is to help us achieve http Send request , Solve the problems mentioned above .
Feign How to use
Take our previous project as an example
stay order-service Service pom Introduce in the file feign rely on :
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId></dependency>stay order-service Add comments to the startup class to open Feign The function of :
@EnableFeignClients@MapperScan("cn.itcast.order,mapper")@SpringBootApplicationpublic class OrderApplicationpublic static void main(String[] args) [SpringApplication.run(OrderApplication.class,args)stay order-service Create a new interface in , The contents are as follows :
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); }This client is mainly based on SpringMVC To declare the information of the remote call , such as :
- The service name :userservice
- Request mode :GET
- Request path :/user/{id}
- Request parameters :Long id
- return type :User
such ,Feign Can help us send http request , No need to use RestTemplate To send .
We modify order-service Medium OrderService Class queryOrderById Method , Use Feign Client instead of RestTemplate:
@Autowiredprivate UserClient userClient;public Order query0rderById(Long orderId) //1. Query order Order order = orderMapper.findById(orderId);//2. utilize Feign launch http request Query the user Useruser = userClient.findById(order.getUserId());//3. encapsulation user To 0rderorder.setUser(user);// 4. return return order;Feign Summary of the use of
Use Feign Steps for :
The first ① Step Introduce dependencies
The first ② Step add to @EnableFeignClients annotation
The first ③ Step To write FeignClient Interface
The first ④ Step Use FeignClient The method defined in replaces RestTemplate

边栏推荐
- MongoDB 常用命令
- Circular structure practice
- A trick to teach you to easily understand Potter's map
- Recommended reading: how can testers get familiar with new businesses quickly?
- TZC 1283: simple sort - Comparative sort
- 攻防世界--easy_web
- 如何从内存解析的角度理解“数组名实质是一个地址”?
- Hack the box - Introduction to networking module detailed Chinese tutorial
- 提升命令行效率的 Bash 快捷键 [完整版]
- 520 for what? DIY is a high-value RGB clock that girls want to watch
猜你喜欢
C语言详解系列——函数的认识(4)函数的声明与定义,简单练习题

LNMP架构

Shell process control (emphasis), if judgment, case statement, let usage, for ((initial value; loop control condition; variable change)) and for variable in value 1 value 2 value 3..., while loop

代码审计之百家cms

如何从内存解析的角度理解“数组名实质是一个地址”?

10. Regular expression matching

A trick to teach you to easily understand Potter's map

ES Cluster in Red status: what about write & delete operations?

Real scientific weight loss

攻防世界-FlatScience
随机推荐
MySQL optimization
Hack The Box - Web Requests Module详细讲解中文教程
LAMP架构
[STM32 series summary] blogger's way to quickly advance STM32 in actual combat (continuous update)
芯片的翻新和造假,人被坑麻了
攻防世界-FlatScience
LNMP架构
代码审计之百家cms
Okaleido上线聚变Mining模式,OKA通证当下产出的唯一方式
Bash shortcut key to improve command line efficiency [Full Version]
Do you really understand fiddler, a necessary tool for testing?
Recommended reading: how can testers get familiar with new businesses quickly?
How students apply for free idea
Basic methods of realizing licensing function in C language
元宇宙为服装设计展示提供数字化社交平台
Hack The Box -SQL Injection Fundamentals Module详细讲解中文教程
87. Disturb string
Lesson 2 getting to know slam for the first time
nn.Moudle模块-创建神经网络结构需要注意的细节
Lamp architecture