Microservices have become the mainstream technology architecture pursued by major manufacturers, and the prospect of learning microservices is very promising, and SpringCloud has become the mainstream microservice technology stack. This series of articles will focus on the SpringCloud technology stack, and comprehensively analyze and explain the actual application of the SpringCloud technology stack in the microservice scenario.
Spring Cloud Feign
Knowledge Index
- Introduction to Feign
- Getting Started
- load balancing
- request response compression
- log level
1 Introduction to Feign
In the previous chapters, we call microservices through RestTemplate
completion, but this calling method requires us to write the url, and writing the url requires us to clearly know the code of the called service, and the same is true for the return value, we need to know the returned data. structure and define the interface on the caller to accept the return value. There is no doubt that this approach violates the principles of interface-oriented programming. It is Feign
used to solve this problem.
Feign
The English expression means "pretend, disguise, deform". It is a lightweight framework for http request invocation. It is Java接口注解
invoked in this way Http请求
, rather than directly invoked Java
by encapsulating messages as in . By processing annotations, the request is templated. When the actual call is made, the parameters are passed in, and then applied to the request according to the parameters, and then converted into a real request. This method is more intuitive and conforms to the principle of interface-oriented programming.HTTP请求
Feign
2 Getting Started
Feign
Generally, it is written by the service provider and used by the service consumer.
2.1 Create the feign module
In actual development, the module is located in the service provider project, and is packaged into a maven
private repository after the service is released.
Create a service_provider_feign
module
2.2 Introducing dependencies
<dependencies>
<!--配置feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2.3 Write feign
/**
* Copyright (c) 2022 itmentu.com, All rights reserved.
*
* @Author: yang
*/
@FeignClient(value = "service-provider")
public interface HelloFeign {
@GetMapping("/hello")
String hello();
}
2.4 Consumers depend on this module
depend on service_consumer
this module in
<dependency>
<groupId>com.itmentu</groupId>
<artifactId>service_provider_feign</artifactId>
<version>${version}</version>
</dependency>
2.5 Code modification
2.5.1 Modify the startup class
Add @EnableFeignClients
a note indicating that the feign
function is enabled
/**
* Copyright (c) 2022 itmentu.com, All rights reserved.
*
* @Author: yang
*/
@SpringBootApplication
@EnableFeignClients
public class ServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceConsumerApplication.class, args);
}
@Bean
@LoadBalanced//开启负载均衡
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
2.5.2 Modify and ConsumerController
add new interface
@Autowired
private HelloFeign helloFeign;
@GetMapping("/hello-feign")
public String hello3(){
return helloFeign.hello();
}
2.6 Start the service call interface
You can see that the call has been successful
3 Load Balancing
Spring Cloud Feign
It has been integrated Ribbon
without additional dependencies and configuration, and the load balancing we configured before can still take effect
Since the random strategy was originally configured, the effect at this time is:
Example 1:
Example 2:
4 Request compression
SpringCloudFeign
Supports compression of requests and responses GZIP
to reduce performance loss during communication.
Enable request and response compression by configuring:
feign:
compression:
request:
enabled: true # 开启请求压缩
response:
enabled: true # 开启响应压缩
You can also set the requested data type and the minimum size limit that triggers compression
# Feign配置
feign:
compression:
request:
enabled: true # 开启请求压缩
mime-types: text/html,application/xml,application/json # 设置压缩的数据类型
min-request-size: 2048 # 设置触发压缩的大小下限
#以上数据类型,压缩大小下限均为默认值
5 Configure log levels
5.1 Log Level Description
When sending and receiving requests, Feign
the output of the log is defined and four levels are defined: here we configure the test.
level | illustrate |
---|---|
NONE | no record |
BASIC | Only record the output Http method name, request URL, return status code and execution time |
HEADERS | Record output Http method name, request URL, return status code and execution time and Header information |
FULL | Record the Header, Body and some request metadata of Request and Response |
5.2 Case Demonstration
In service_consumer
the configuration file, set the log level under the com.itmentu package to debug
# com.itmentu 包下的日志级别都为Debug
logging:
level:
com.itmentu: debug
When service_consumer
writing a configuration class, define the log level
@Configuration
public class FeignLogConfig {
//采用full打印日志
@Bean
public Logger.Level configLog(){
return Logger.Level.FULL;
}
}
Specify the configuration class in service_consumer
HelloFeign
/**
* Copyright (c) 2022 itmentu.com, All rights reserved.
*
* @Author: yang
*/
@FeignClient(value = "service-provider",configuration = FeignLogConfig.class)
public interface HelloFeign {
@GetMapping("/hello")
String hello();
}
Restart the project to see the log of each visit