当前位置:网站首页>[cloud native] introduction and use of feign of microservices
[cloud native] introduction and use of feign of microservices
2022-07-03 08:17:00 【Xiao Wei wants to learn from Zhu Lao】
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 
List of articles
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")
@SpringBootApplication
public class OrderApplication
public 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:
@Autowired
private 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 0rder
order.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
This is the end of the article , If you have any questions, please point out
Thank you again for your support 
边栏推荐
- idea取消引用顯示效果
- Mall management system of database application technology course design
- oracle 插入单引号
- Haproxy+kept cluster setup 02
- [global product discovery 2] the first pure cloud augmented reality (AR) platform - Israel
- Transplantation of tslib Library
- 【云原生】微服务之Feign的介绍与使用
- The general trend of data news releases the power of visual reporting ----- essays after reading
- Zohocrm deluge function application time verification
- Lua framwrok framework starts
猜你喜欢

C course design employee information management system

Display terrain database on osgearth ball

Kwai 20200412 recruitment

Abstract classes and interfaces

Unity performance optimization

Zohocrm deluge function application time verification

Redis的数据结构

KunlunBase MeetUP 等您来!

haproxy+keepalived集群搭建02

JS common basic case sorting (continuous update)
随机推荐
Map的实现类的顺序性
haproxy+keepalived集群搭建02
Open the influence list of "National Meteorological Short Videos (Kwai, Tiktok) in November" in an interactive way“
Unity2019_ Natural ambient light_ Sky box
2021-10-19
Idea unreference Display Effect
数据库应用技术课程设计之商城管理系统
tslib库的移植
Easy touch plug-in
[set theory] order relation (the relation between elements of partial order set | comparable | strictly less than | covering | Haas diagram)
Golang string segmentation, substitution and interception
Un système de gestion de centre commercial pour la conception de cours de technologie d'application de base de données
About the problem that the editor and the white screen of the login interface cannot be found after the location of unityhub is changed
Exe file running window embedding QT window
Shader foundation 01
Cesium service deployment, and import and display local 3dfiles data
Transplantation of freetype Library
Basic operation and process control
Some understandings of 3dfiles
Compilation error: "not in executable format: file format not recognized"“