Spring Cloud Getting Started (3) - REST client Feign

1 Introduction

  In Spring Cloud cluster, communications various roles of REST-based services, when calling the service, the inevitable need to use the REST service client requests end up.

  Spring comes in RestTemplate, RestTemplate using HttpClient transmission request.

  Spring Cloud Feign framework will be integrated into the Spring Cloud's Netflix project, in which the combined Ribbon realizes load balancing, so we usually use the time, you may not feel the presence of the Ribbon.

 

2, commonly used REST client

  1) Apache CXF

  2) Restlet

 

3, used alone Feign 

  1) Create a HelloClient Interface

public interface HelloClient{
    @RequestLine("GET /hello")
    String sayHello();
}

  2) execution request

public  class HelloTest {
     public  static  void Mian (String [] args) {
         // This step actually refers target call JDK dynamic proxy AOP generates a
         // proxy object, when calling the method, performed by a Feign.Client the transmission request, where the request is the AOP "the Advice" 
        the HelloClient Hello = Feign.builder () target (the HelloClient.. class ,
             "HTTP: // localhost: 8080 /" );
         // call the method Hello interface 
        System. Out.println (hello.sayHello 
  ());
} }

 

4, Feign principle resolved
  by the above example, we can see that, Feign actually utilized in the AOP ** Client Interface to generate a proxy object, and then use "Advice" of weaving, transmits the HTTP request

 

5, Spring Cloud integration Feign

  Spring Cloud integrates Feign actually use a "translator" Feign offered to translate the Spring Web annotation to listen to the Feign

  E.g:

    Spring Web in common comment @GetMapping ( "/ hello"), after the Spring Cloud translation, the equivalent of Feign

    @RequestLine("GET /hello")

Guess you like

Origin www.cnblogs.com/lkc9/p/11571357.html