Detailed Feign Call Remote Service Guide for Spring Cloud2.0

  • 2021-06-28 12:45:47
  • OfStack

What is Feign

Feign is a tool to simplify Java HTTP client development (java-to-httpclient-binder), inspired by Retrofit, JAXRS-2.0, and WebSocket.Feign was originally designed to reduce the complexity of system 1 binding Denominator to HTTP API, regardless of whether it is restful or not.

Why use Feign

Developers can easily write java client using tools such as Jersey and CXF to provide REST or SOAP services.Developers can also write their own java http client based on http transport toolkits such as Apache HC.Feign's focus, however, is on simplifying the complexity of the toolkit for developers to write code with minimal code to provide an java http client.With custom decoders and exception handling, developers can write a textual HTTP API at will.

Okay, it's official, if I were to say what Feign is.

1 sentence to explain Feign

Feign is understood as a super-convenient framework/tool for calling Spring-Cloud remote services, helping developers make remote service calls in a faster and more compatible way with less coupling and less code.

configuration file

When invoked, both sides of application.yml1 must add two configurations, since both servers need to discover each other and get a list of services.

Otherwise an error will prompt "feign Load balancer does not have available server for client: xxxx"


eureka:
 client:
  # Whether to register yourself eureka Service Registry, default is true
  register-with-eureka: true
  # Whether to get the list of available services from the service registry, default is true
  fetch-registry: true
  serviceUrl:
   defaultZone: http://192.168.114.152:9091/eureka/

Then there's pom.xml with the latest feign package


    <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-feign -->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-feign</artifactId>
      <version>1.4.4.RELEASE</version>
    </dependency>

Service Caller

Starter 1 must add @EnableFeignClients to represent an Feign call, Feign will pull the list of services to Eureka for invocation.


@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients
public class CheckCollectSysApplication {
  public static void main(String[] args) {
    SpringApplication.run(CheckCollectSysApplication.class,args);
  }
}

Caller business aspect.It is recommended that you write an interface class to encapsulate the methods for other Controller calls.

Note here

1. @FeignClient inside 1 must specify name, a long time ago version specified serviceId, which is obsolete.

2. If a placeholder is used, @PathVariable also needs to specify value, which is a mandatory requirement or an error will occur, prompting "Feign PathVariable annotation was empty on param 0."

3. RequestParam also needs to specify value, otherwise it will prompt "RequestParam.value() was empty on parameter 1"

In addition, what other people say about GetMapping or PutMapping is not possible, in fact, it won't. I can try it all.


@FeignClient(name = "demo-checksys")
public interface ChecysysRemoteClient {
  /**
   *  Get basic information 
   * ( PutMapping Of URL= Remote project Path+ControllerPath+MethodPath
   *  Simply put, you are direct postman Accessible project paths)  
   */
  @PutMapping("/checksys/register/info/{checkNum}")
  public ApiReturnObject updateCheckRegisterByBodyCheck(@PathVariable(value="checkNum") String checkNum,@RequestParam(value="bodyJson") String bodyJson);
}

name of FeignClient here refers to ApplicationName of the callee registered with Eureka, which is the name of your application and can be configured in application.yml


spring:
  application:
    name: demo-checksys

URL for PutMapping = Path+ControllerPath+MethodPath for a remote project, in short, the project path that you can access directly from postman.

For example, the project is http://127.0.0.1:8080/demo/user/getUser/{userId},

So what's written here is "/demo/user/getUser/{userId}"

Callee

As for the callee, as in the past, code, there is no difference, after all, it is called by someone else, which is very comfortable.


  /**
   *  Get basic information 
   */
  @PutMapping("/info/{checkNum}")
  public ApiReturnObject updateCheckRegisterByBodyCheck(@PathVariable String checkNum,String bodyJson) {
    // You can print here 1 Some logs 
    outSystemService.updateCheckRegisterByBodyCheck(checkNum, bodyJson);
    return ApiReturnUtil.success(" Call succeeded ");
  }

Related articles: