Spring Cloud integrates OpenFeign to implement service calls (load balancing)

Reprinted Disclaimer: The source of the article is to carry sacks of teenager


Write at the beginning

  Continued from the previous article: Spring Cloud integrates Ribbon to implement service calls (load balancing) . We already know: the introduction Ribbon components (at the time of the introduction of the latest eureka clients rely on, which by default has helped us integrate the Ribbon, here and no one introduced), in conjunction with RestTemplate + @LoadBalance 注解, can be completed between micro-service 客户端 → 服务端调用 + (集群)服务的负载均衡functions.

  OpenFeign we want to introduce in this article, it is a carry out his 服务调用 + 负载均衡components. Feign is a lightweight Restful HTTP service client in Spring Cloud components. As Feign stops updating, this article will start with OpenFeign.

  Note: Feign mentioned in this article is OpenFeign. OpenFeign is too long to write, haha. Fiegn and OpenFeign are similar. OpenFeign has made some modifications and packages in Feign to support Spring MVC annotations

1. The difference between Feign and OpenFeign

Feign OpenFeign
1. Feign is a lightweight Restful HTTP service client in Spring Cloud components 1. OpenFeign is the encapsulation of Spring Cloud based on Feign (Feign has OpenFeign, and Feign does not have OpenFeign, haha)
2.Feign 内置了Ribbon,用来做客户端负载均衡 , To call the service of the service registry; 2.OpenFeign 支持了 Spring MVC 的注解, Such as @RequesMapping, etc.; cooperating with Spring development, it is even more powerful;
3.Feign的使用方式是: Use Feign's annotation to define the interface, call this interface, you can call the service of the service registry 3. OpenFeign's @FeignClient can parse the interface under SpringMVC's @RequestMapping annotation, and 通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务
4.Feign本身不支持Spring MVC的注解,它有一套自己的注解。(对比OpenFeign支持Spring MVC 注解,开发起来更香) 4.在类名上,@RequesMapping 注解不能与 @FeignClient 注解同时使用
<depedency>
  <groupId>org.springframework.cloud<groupId>
  <artifacaId>spring-cloud-starter-feign</artifacaId>
</dependency>
<depedency>
  <groupId>org.springframework.cloud<groupId>
  <artifacaId>spring-cloud-starter-openfeign</artifacaId>
</dependency>

2. Feign is used in the microservice client

  Feign在消费端使用(官网有这样一句话:Declarative REST Client: Feign 声明式REST客户端:Feign)SpringCloud official website OpenFeign introduction: OpenFeign official website introduction
Insert picture description here

3. With Ribbon, why do we need an OpenFeign

  Ribbon and OpenFeign can 实现服务的调用, 实现负载均衡. Functionally, there is a suspicion of redundancy; but alive has its Feign meaning. And OpenFeign also has built-in Ribbon. After this article, you will know that Feign is more in line with the development process. ! !

  Feign is a declarative web client. Using Feign makes it easier for us to write web clients. (I don’t understand this sentence here. If you continue to understand it, you will understand, easy to understand)它只需要我们创建一个接口,并添加相对应的注解节课完成微服务之间的调用。

3.1 OpenFeign makes development easy

  When Ribbon + RestTemplate is used, RestTemplate is used to encapsulate HTTP requests to form a set of templated invocation methods. Disadvantage: Every time a service is called, a RestTemplate needs to be declared through @Resource/@Autowired.

@RestController
@Slf4j
public class OrderController {
    
    
    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
    @Resource
    private RestTemplate restTemplate;
    @GetMapping("/consumer/payment/create")
    public CommonResult<Payment> create(Payment payment) {
    
    
        return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
    }
}

 
  
  

  Since there may be more than one call to the service dependency 往往一个接口会被多出调用,所以通常都会针对每个微服务自行封装一些客户端类来保证这些依赖服务的调用。(类似封装所谓的工具类等方式,这样明显不能根治,多个消费者的话就需要各持一份,缺点太明显!!!), Feign has made further encapsulation on this basis, and it helps us define and implement the definition of the dependency service interface.

  Under the implementation of Feign, ① 我们只需要创建一个接口   ② 并使用注解的方式来配置它can be completed 对服务提供方的接口绑定, which simplifies the development of automatically encapsulating the service call client when using Spring Cloud Ribbon. Just remember these two steps.

  The vernacular explanation: Client A wants to call service B. Feign creates an exactly the same interface B in A to provide services to the outside world. We call this interface in A to substitute service B. (This is just to meet our Controller → Serviceprocesses; no Feign, we do not need the Service Controller can invoke the service by RestTemplate also need to package RestTemplate, etc., does not meet our general development process)

  Feign's law of true fragrance! ! ! . In particular, OpenFeign supports Spring MVC annotations. It is not too cool to cooperate with Spring for project development. SpringCloud official website OpenFeign introduction: OpenFeign official website introduction

3.2 Breaking up with Ribbon

  Before knowing Feign, call between microservices. We use the Ribbon + RestTemplateway; from now on, I "fell in love" the OpenFeign, and ready to break up the Ribbon.

4. OpenFeign used in Spring Cloud

4.1 Operations on service consumers

  The module name is defined as:, cloud-consumer-feign-order80to act as a service consumer.

Ⅰ. Introduce pom.xml dependency

<!-- 引入 spring-cloud-openfeign 依赖-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

 
  
  

Ⅱ. Application.yml configuration file modification

server:
  port: 80
eureka:
  client:
    # 不将自己注册到 Eureka(注册与否自己决定,随便)
    register-with-eureka: false
    service-url:
      # Eureka注册中心集群地址
      defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka

 
  
  

Ⅲ. Main startup class, add @EnableFeignClients annotation support

@SpringBootApplication
@EnableFeignClients //该注解的作用是扫描标记了@FeignClient的接口并创建实例bean,默认扫描并创建所在工程下的包
public class OrderFeignMain80 {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(OrderFeignMain80.class,args);
    }
}

 
  
  

Ⅳ. Create a new interface on the consumer side and add the annotation @FeignClient

@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE") //添加@FeignClient注解,指定服务提供方服务名称
public interface PaymentFeignService {
    
    
    //OpenFeign支持Spring MVC注解,此处通过@GetMapping去调用 CLOUD-PAYMENT-SERVICE 该服务指定的接口
    @GetMapping(value = "/payment/get/{id}")  
    public CommonResult getPaymentById(@PathVariable("id") Long id);//该接口声明必须和服务提供方一致
}

 
  
  

Ⅴ. Downgrade the service for Fegin (the way to go)

  Write a class that implements the PaymentFeignService interface and rewrite the methods inside. The content of the method is the bottom method.

public class PaymentFallbackFeignService implements PaymentFeignService {
    
    
    @Override
    public CommonResult getPaymentById(Long id) {
    
    
        return new CommonResult(404, "OpenFeign兜底方法", new Payment(id, "errorSerial"));
    }
}

 
  
  

Ⅵ. Control layer controller

@RestController
public class OrderFeignController {
    
    
    //此处就可以通过调用Service来完成 Controller → Service 
    //具体Service具体业务怎么调用,具体业务实现就由它随便实现吧
    //这就是 OpenFeign 相比 RestTemplate 的好处
    @Resource
    private PaymentFeignService paymentFeignService;
    @GetMapping(value = "/consumer/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
    
    
        return paymentFeignService.getPaymentById(id);
    }
}

 
  
  

4.2 Service provider (the service provider does not need to operate, this is only used to display relevant content)

Ⅰ. Service name display

Insert picture description here

Ⅱ. Controller interface service display

Insert picture description here

5.OpenFeign test

  After integrating OpenFeign, we use URL on the consumer side to localhost/consumer/payment/get/31send requests, use OpenFeign to implement service calls, and achieve load balancing. The test animation is as follows:

  OpenFeign 服务调用 + 负载均衡relatively RestTemplate + Ribbonspeaking, OpenFeign used 服务调用 + 负载均衡, in line with our daily development Controller → Serviceinvocation; at the same time eliminates the need for RestTemplate 的封装, and so on.

  The final result: OpenFeign is so fragrant! ! ! In 服务调用 + 负载均衡the choice of recommended OpenFeign.Insert picture description here

6. OpenFeign timeout control

  When a service consumer makes a service call, due to network and query efficiency issues, the consumer cannot obtain the returned data in time. This is Feign's timeout control.

  默认 Feign 客户端只等待 1 秒钟, But the server processing takes more than 1 second, which causes the Feign client to stop waiting 直接以超时报错的方式返回. In order to avoid this kind of situation, we need to set the timeout control of Feign client.

6.1 Human overtime intervention service return

  With the same interface, we sleep for 5s at this time, and the service consumer calls the service again, because the server processing takes more than 1 second, which causes the Feign client to directly return with an error.
Insert picture description here

6.2 Set Feign timeout

  At this point need 客户端timeout time Feign configuration, we only need to configure the application.yml ribbon.ReadTimeoutand ribbon.ConnectTimeouttwo properties and set the timeout to allow.

server:
  port: 80
eureka:
  client:
    # 不将自己注册到 Eureka(注册与否自己决定,随便)
    register-with-eureka: false
    service-url:
      # Eureka注册中心集群地址
      defaultZone: http://eureka7001.com:7001/eureka, http://eureka7002.com:7002/eureka
#设置feign客户端超时时间(OpenFeign集成了Ribbon,超时由ribbon控制)
ribbon:
  #指的是建立连接后从服务器读取到可用资源所用的时间
  ReadTimeout:  5000
  #指的是建立连接所用的时间,适用于网络正常的情况下,两端连接所用的时间
  ConnectTimeout: 5000

 
  
  

6.3 Feign timeout effective test

  Set the Feign timeout time to 5s, at this time the server sleep is modified to 3s. When sending the request again, we can observe that the browser will wait for 3s. When the sleep is over, the result can be returned normally within the timeout range allowed by Feign.
Insert picture description here

7. OpenFeign log printing function

  Using Feign call between services, we can 使用 Feign 为我们提供的日志打印功能, through 配置日志级别, to see details Feign in Http request. It means to call the situation Feign interface for monitoring and output

7.1 Feign log level

Log level Description
NONE By default, no logs are displayed
BASIC Only record request method, URL, response status code, execution time
HEADERS In addition to the information defined in BASIC, there are also request and response header information
FULL In addition to the information defined in HEADERS, there are also the body and metadata of the request and response

7.2 Set Feign log level

1. Define Feign configuration class
Insert picture description here

import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FeignConfig {
    
    
    @Bean
    Logger.Level feignLoggerLevel(){
    
    
        return Logger.Level.FULL;
    }
}

 
  
  

2. Application.yml configures the log output level

#配置日志输出级别
logging:
  level:
    com.study.springcloud.service.PaymentFeignService: debug

 
  
  

3. Feign log output
Insert picture description here

Download the code for this article: Spring Cloud integrates OpenFeign to implement service calls (extract code: mnh4)
 
Next article: Spring Cloud integrates Hystrix to implement service degradation, service fuse, and service current limit

Guess you like

Origin blog.csdn.net/m0_37989980/article/details/108482101