当前位置:网站首页>Research on the best implementation scheme of feign
Research on the best implementation scheme of feign
2022-07-20 15:22:00 【Liu Chu, Ge Nian】
List of articles
Feign Research on the best scheme
First of all, let's take a look Feign Connection pool configuration
Feign Connection pool usage optimization
Feign By default URLConnection Client connection pool .
Feign Bottom layer initiation http request , Rely on other frameworks . Its underlying client implementation includes :
client | explain |
---|---|
URLConnection | Default implementation , Connection pooling is not supported |
Apache HttpClient | Support connection pool |
OKHttp | Support connection |
So improve Feign The main means of performance is to use Other connection pools Instead of default URLConnection.
Switch to Apache HttpClient client
We use it Apache Of HttpClient Let's operate it .
1) Introduce dependencies
stay order-service Of pom Introduce in the file Apache Of HttpClient rely on :
<!--httpClient Dependence -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
2) Configure connection pool
stay order-service Of application.yml Add configuration in :
feign:
client:
config:
default: # default Global configuration
loggerLevel: BASIC # The level of logging ,BASIC Is the basic request and response information
httpclient:
enabled: true # Turn on feign Yes HttpClient Support for
max-connections: 200 # Maximum number of connections
max-connections-per-route: 50 # Maximum number of connections per path
Next , stay FeignClientFactoryBean Medium loadBalance Break in the method :
Debug Mode start order-service service , You can see... Here client, The bottom is Apache HttpClient:
OK 了 , The rest of the calls are the same as the original , His bottom layer calls by default Apache HttpClient The connection pool of the client
Feign Optimization summary of :
1. Try to use the log level basic
2. Use HttpClient or OKHttp Instead of URLConnection:
① introduce feign-httpClient rely on
② Profile on httpClient function , Set connection pool parameters
Feign Best practices
Problem introduction
The so-called recent practice , Is the experience summarized in the process of use , The best way to use .
Self study observation can find ,Feign Client and service provider controller The code is very similar :
feign client :
UserController:
Is there a way to simplify this repetitive coding ?
We thought of two ways to solve this problem :
- First, it can be inherited , The same code can be shared through inheritance , Reduce the writing of repetitive code
- The second method is to Feign Of Client Extracted as a separate module , And the interface related POJO、 default Feign The configuration is put into this module , Available to all consumers , Achieve the architecture of module separation
Now let's take a detailed look at which of these two methods is the best :
Inheritance mode
The same code can be shared through inheritance :
1) Define a API Interface , Use the definition method , And based on SpringMVC Comment make a statement .
2)Feign Client and Controller All integrated interfaces
Such writing makes the code very simple and realizes code sharing
But this will create new problems :
service provider 、 Service consumers are tightly coupled
Annotation mappings in the parameter list do not inherit , therefore Controller Method must be declared again in 、 parameter list 、 annotation
Extraction method
take Feign Of Client Extracted as a separate module , And the interface related POJO、 default Feign The configuration is put into this module , Available to all consumers .
for example , take UserClient、User、Feign The default configuration is extracted into a feign-api In bag , All microservices reference the dependent package , It can be used directly .
This way will be better than the first on the whole , Achieve low coupling between modules
Implement extraction based best practices
The practical steps are as follows :
1) extract
First create a module, Name it feign-api:
Project structure :
stay feign-api And then introduce feign Of starter rely on
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
then ,order-service Writing in the UserClient、User、DefaultFeignConfiguration All copied to feign-api In the project
UserClient
package com.yyl.feign.clients;
import com.yyl.feign.pojo.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(value = "userservice")
public interface UserClient {
@GetMapping("/user/{id}")
User findById(@PathVariable("id") Long id);
}
DefaultFeignConfiguration
package com.yyl.feign.config;
import feign.Logger;
import org.springframework.context.annotation.Bean;
public class DefaultFeignConfiguration {
@Bean
public Logger.Level logLevel(){
return Logger.Level.BASIC;
}
}
User
package com.yyl.feign.pojo;
import lombok.Data;
@Data
public class User {
private Long id;
private String username;
private String address;
}
2) stay order-service Use in feign-api
First , Delete order-service Medium UserClient、User、DefaultFeignConfiguration And other classes or interfaces .
stay order-service Of pom Introduce... Into the file feign-api Dependence :
<dependency>
<groupId>com.yyl.demo</groupId>
<artifactId>feign-api</artifactId>
<version>1.0</version>
</dependency>
modify order-service All the guide package parts related to the above three components , Change to import feign-api In the package
3) Restart test
After restart , Found that the service reported an error :
This is because UserClient Now in com.yyl.feign.clients It's a bag ,
and order-service Of @EnableFeignClients The note is in com.yyl.order It's a bag , Not in the same bag , Can't scan UserClient.
4) Solve the problem of scanning package
Mode one :
Appoint Feign Packages that should be scanned :
@EnableFeignClients(basePackages = "cn.itcast.feign.clients")
Mode two :
Specify the Client Interface :
@EnableFeignClients(clients = {
UserClient.class})
Restart the access , No problem
边栏推荐
- (0711-0717)本周开源软件安全大事记
- Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 5
- Freeotp installation and use process [easy to understand]
- npm warn config global `--global`, `--local` are deprecated. use `--location 解决方法
- Leetcode | 滑动窗口
- Leetcode424.替换后的最长重复字符
- Zhiyuan admitted plagiarizing the paper, and the relevant responsible person has resigned!
- mos管的输入输出特性曲线及gm/id仿真曲线(cadence IC617)
- [question brushing diary] longest palindrome substring
- Three moves from 20s to 500ms
猜你喜欢
组合数学学习笔记(未完待续
[special topic of golang database 5] golang language operation redis for addition, deletion, modification and query
clickhouse 20.x 与prometheus + grafana的集成(三)
Excrt/ extended Chinese remainder theorem learning notes
npm 的语义版本控制、解决控制台Prop being mutated: “placement“报错
Preparation of SILVACO diode, triode and CMOS
25、继承与派生
集合之Arraylist
Later discussion on the products (containers) suitable for Zhongke Fangde system
unet和mask rcnn
随机推荐
APICloud AVM框架打造数字滚动组件
Decorator solves typeerror: 'nonetype' object is not callable
Import word document pictures kernel synchronization and mutual exclusion
Gearman 任务调度程序「建议收藏」
【Mindspore】【读取图数据】无法读取Mindrecord格式图数据
婚恋交友网站开发社交聊天平台代码分享(三)
和Leo一起做爱数学的好孩子之容斥 【SCOI2010】幸运数字
reduce函数构建炫耀字体,字体动画。
组合数学学习笔记(未完待续
Use of & & operators
Use masonry to realize width adaptation and height adaptation of controls (including uitableview) according to content
Analysis of e-commerce user behavior data
中科方德系统适配产品(容器类)后谈
[mindspore] [read graph data] cannot read graph data in mindrecord format
Analysis scaffold
SVN比较本地相对于上一版本的修改
Three moves from 20s to 500ms
LP双币质押流动性挖矿dapp系统开发逻辑分析
IDEA中如何安装插件和宝贝插件的推荐
[mindspore] [warning reason] give a warning when training the model