directory

Introduction to Spring Cloud Alibaba (I

Spring Cloud Alibaba (II) Environment construction and NACOS registry configuration center

(3) Build API gateway gateway dynamic routing

Spring Cloud Alibaba (IV) Project stratification

What is Feign

Feign is a declarative, template-based HTTP Client (used only in the Application Client) (call Feign: declarative service invocation). Declarative invocation means that a remote method is invoked as if it were a local method, without being aware of manipulating the remote HTTP request.

What is declarative service invocation

Declarative invocation invokes remote methods as if they were local methods; No HTTP request is sent.

The declarative invocation of Spring Cloud makes it possible to request a remote service using HTTP just like invoking a local method, without the developer being aware that it is a remote method, much less an HTTP request. Feign’s Application makes Spring Cloud microservice invocation like Dubbo, where the Application Client calls the Application Service directly through the interface method, rather than having to parse the returned data through the normal RestTemplate construct request. It solves the problem of letting developers call remote interfaces as if they were calling local methods, without having to focus on the details of interaction with the remote, and without having to focus on distributed environment development.

Integrate OpenFeign

  1. Introduction of depend on
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> < version > 2.2.6. RELEASE < / version > < / dependency >Copy the code
  1. Add annotations to the start class
@EnableFeignClients
Copy the code
  1. Adding Configuration Information
Feign: client: config: default: #feign Client connection timeout connect-timeout: 2000 # Feign client read resource timeout read-timeout: Hystrix: Enabled: true #Hystrix Configuration Hystrix: Command: HystrixCommandKey: Execution: Isolation: TimeoutInMilliseconds: 4000 Default: circuitBreaker: Error percentage threshold. When this threshold is reached, the short circuit starts. # the default errorThresholdPercentage: 50% and 50%, in short, request with a total of 20 failure within 10 s, circuit breaker to open. When this number of failures is reached in the configured time window, a short circuit is performed. Default requestVolumeThreshold: 20 # 20 how long short circuit after started trying to restore, the default 5 s sleepWindowInMilliseconds: 5 execution: isolation: thread: # # configuration Hystrix timeout timeout close hystrix.com mand. Default. Execution. A timeout. # timeout enabled = false (default 1000 ms) in the configuration of the caller, the caller's all timeout is the value of TimeoutInMilliseconds: 3000 fallback: Isolation: semaphore: # the calling thread to allow request HystrixCommand. GetFallback () the maximum number of 10 by default. MaxConcurrentRequests: 50000 ThreadPool: default: 10 # Maximum queue length. The default value -1 is reset to change from -1 to another value. This value cannot be adjusted dynamically. To do this, use the following configuration: maxQueueSize 100 # queuing thread count threshold, the default is 5, achieve the rejection, if configured with this option, the size of the queue is the queue queueSizeRejectionThreshold: 5Copy the code
  1. Create a few new classes in zhc-system-API
@Data
public class TestData {
    private Long id;

    private String name;

}
Copy the code
@feignClient (name = "zhc-system-service ", Fallback = FeignSystemClientCallback. Class) public interface FeignTestClient {/ * * * * * @ return * / test interface @RequestMapping(value = "/test", method = RequestMethod.POST) String test(@RequestBody TestData testData); }Copy the code
@Slf4j @Component public class FeignSystemClientCallback implements FeignTestClient { @Override public String Test (TestData TestData) {return "Server busy! Please try again later!!" ; }}Copy the code

Create a new test method in zhc-system-web

@RequestMapping(value = "/test", method = RequestMethod.POST)
public String test(@RequestBody TestData testData){
    log.info(String.valueOf(testData));
    return testDataService.select().toString();
}
Copy the code
  1. call

Introduce the zhC-System-API module dependency in zhc-business-service

< the dependency > < groupId > com. ZHC. Cloud < / groupId > < artifactId > ZHC - system - API < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > </dependency>Copy the code

Then write and call the /test method in a test methodThe test method is then called with successThe integration with OpenFeign is successful

GitHub, Gitee (I was the first to build the project, and then make up the documentation. So the project and the document may be different, please ask me if you have any questions.)