当前位置:网站首页>Microservice - remote invocation (feign component)
Microservice - remote invocation (feign component)
2022-07-25 05:26:00 【Java world from scratch】
List of articles
1. Feign brief introduction
a RestTemplate comparison ,Feign Provide more elegant sending Http Requested function .
public Order queryOrderById(Long orderId) {
// 1. According to the order id Query order
Order order = orderMapper.findById(orderId);
//2. According to the user id Query user information
String url = "http://userserivce/user/" + order.getUserId(); // Will change , Without elegance .
// The remote invocation
User user = restTemplate.getForObject(url,User.class).var;
return order;
}
2. Use Feign Instead of RestTemplate
- Introduce dependency coordinates
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
- Open the annotation
@SpringCloudApplication
@EnableFeignClients // Turn on OpenFeign annotation
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
- Create an interface 、 Declare the path of the remote call
@FeignClient("userservice") //service id
public interface UserClient {
@GetMapping("/user/{id}")
User findById(@PathVariable("id") Long id);
}
- Use FeignClient The method defined in replaces RestTemplate
User user = userClient.findById(order.getUserId());
3. Feign To configure
| type | effect | explain |
|---|---|---|
| feign.Logger.Level | Modify log level | Contains four different levels :NONE、BASIC、HEADERS、FULL |
| feign.codec.Decoder | The parser that responds to the result | http Analyze the result of remote call , For example, parsing json String is java object |
| feign.codec.Encoder | Request parameter encoding | Encode the request parameters , Easy to pass http Request to send |
| feign. Contract | Supported annotation formats | The default is SpringMVC Annotations |
| feign. Retryer | Failure retry mechanism | Retry mechanism for request failure , The default is No , However, it will be used Ribbon Retry |
In general , The default value can meet our use , If you want to customize , Just create a custom @Bean Override default Bean that will do .
There are four levels of logs :
- NONE: No log information is recorded , This is the default .
- BASIC: Record only the requested method ,URL And the response status code and execution time
- HEADERS: stay BASIC On the basis of , Additionally, the request and response header information is recorded
- FULL: Record details of all requests and responses , Including header information 、 Request body 、 Metadata .
Mode one : The configuration file
Modify based on the configuration file feign Log level of :
feign:
client:
config:
userservice: # Configuration for a microservice
loggerLevel: FULL # The level of logging
feign:
client:
config:
default: # Here we use default Global configuration , If you write the service name , It is the configuration for a micro service
loggerLevel: FULL # The level of logging
4. Feign Use optimization
Feign Bottom layer initiation http request , Rely on other frameworks . Its underlying client implementation includes :
URLConnection: Default implementation , Connection pooling is not supported
Apache HttpClient : Support connection pool
OKHttp: Support connection pool
So improve Feign The main means of performance is to use Connection pool Instead of default URLConnection.
Use Apache HttpClient
- Introduce dependencies
<!--Apache HttpClient Dependence -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
- Configure connection pool
feign:
client:
config:
userservice: # Configuration for a microservice
loggerLevel: BASIC # The level of logging
httpclient: # Configure connection pool
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
take Feign The interface is drawn into a module
- Create a new module , Special preservation Feign Interface , Introduce dependencies
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>

2. Open annotation when using , Add scan path
@SpringCloudApplication
@EnableFeignClients(basePackages = {
"cn.xin.feign.clients"})
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
边栏推荐
- Typera+picgo+ Alibaba cloud OSS setup and error reporting solution [reprint]
- AirServer 7.3.0中文版手机设备无线传送电脑屏幕工具
- Pikachu vulnerability platform exercise
- 剑指offer专项突击版第9天
- Teach you three ways to optimize the performance from 20s to 500ms
- 1310_一个printf的实现分析
- nacos中哪边有这个列的sql脚本啊?
- After Oracle user a deletes a table under user B's name, can user B recover the deleted table through the recycle bin?
- How do novices open accounts for stock speculation? Is it safe for securities companies to open accounts online?
- Project management tool - Introduction and practice of Alibaba cloud projex
猜你喜欢
随机推荐
rhce第一天
Necessary skills for mobile terminal test: ADB command and packet capturing
Openfegin remote call lost request header problem
Add click event to unity 3D object
Render Optimization: repaint and reflow
Arm PWN basic tutorial
微服务 - 远程调用(Feign组件)
STL notes (VIII): container - List
LCP插件创建对等VLAN接口
Sword finger offer II 012. the sum of the left and right subarrays is equal
I have seven schemes to realize web real-time message push, seven!
Teach you three ways to optimize the performance from 20s to 500ms
Go language function
Terminate 5g chip cooperation! The official response of Intel and zhanrui came
LeetCode第302场周赛
What about reinstalling win11 system?
使用getifaddrs获取本机网口IP地址
剑指offer专项突击版第9天
epoll的实现原理
SystemVerilog中$write与$display区别









