SpringCloudOpenFeign practical selection

Author: Zen and the Art of Computer Programming

1 Introduction

  Spring Cloud OpenFeign is a declarative HTTP client implemented based on Ribbon. It makes it easier to write web service clients, and remote services can be called through annotations. OpenFeign also supports pluggable encoders and decoders, allowing you to flexibly configure the encoding/decoding methods of request parameters, and develop highly customized applications. This article will elaborate on the following aspects: 1.1 Why use OpenFeign? First of all, you need to understand what RESTful is. REST (Representational State Transfer) is a design style used to create interoperable Web services between distributed systems. It provides a standard set of methods for defining resources on the network, defining their state transitions, and communicating between them. Therefore, we can use these methods to define and access the service interface. Now suppose there is a calculator service, which provides some calculation functions, such as addition, subtraction, multiplication and division. Now I want the client to call this calculator service, and pass in two numbers as parameters, and get the calculation result. Normally, the client may directly call the API interface of the calculator service, but this requires the client to master the details of calling these interfaces and process the response data. In addition, when the calculator service changes, the client also needs to modify the corresponding code. After using OpenFeign, you only need to create an interface, and use annotations to mark the parameters and return value types of the method, and then you can use OpenFeign to call remote services. For example:

      ```java
      @FeignClient(name = "calculator") // 指定服务名
      public interface CalculatorService {
          @RequestMapping("/add/{a}/{b}") // 指定路径࿰

Guess you like

Origin blog.csdn.net/universsky2015/article/details/132002410