SpringCloud Getting Started 3 --- Feign (service call component)

SpringCloud Getting Started 1 Introduction ---

SpringCloud entry 2 --- Eureka (service discovery component)

SpringCloud Getting Started 3 --- Feign (service call component)

SpringCloud entry 4 --- Hystrix (fuse assembly)

SpringCloud entry-5 --- Zuul (Serving Gateway)

Part III, we do the right SpringCloud in Feign (service discovery component) a brief description:

1, Feign Profile

        Feign Web Service client is a declarative. It appears the development of Web Service client becomes very simple. Using Feign only need to create an interface coupled with corresponding annotations, such as: @FeignClient comment. Feign annotated pluggable, including notes and Feign AX-RS annotations. Feign also support encoders and decoders, Spring Cloud Open Feign Feign enhanced support for Spring Mvc annotations, you can use the same as Spring Web HttpMessageConverters and so on.

  Feign is a declarative, template-based HTTP client. Using Feign in Spring Cloud, you can do it using an HTTP request to access a remote service, call the local method as the same, the developers can not fully perceive this remote method invocation is more perception than in access HTTP request. Then tell us about Feign characteristics, as follows:

Pluggable annotation support, including Feign AX-RS annotations and notes.

  • HTTP support pluggable encoder and decoder.
  • Support Hystrix and its Fallback.
  • Ribbon support load balancing.
  • Support compression of HTTP requests and responses. Feign WebService client is a declarative, its aim is to make Web Service calls easier. It integrates Ribbon and Hystrix, thereby eliminating the need for developers to integrate their Feign. Feign also provides a template for HTTP requests, through simple interfaces and writing notes, you can define the parameters of good information HTTP request, format, address and so on. Feign completely Agent HTTP request, in the course we only need to dependency injection Bean, and then call the corresponding method of passing parameters.

 

2, feign call integration 

  (1) adding a dependency module in test_qa

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring‐cloud‐starter‐openfeign</artifactId>
</dependency>

  (2) modify the startup module test_qa class, add annotations

@EnableDiscoveryClient
@EnableFeignClients

  (3) Create com.test.qa.client test_qa module package, the package creates the interface 

@FeignClient("test‐base")
public interface LabelClient {
    @RequestMapping(value="/label/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable("id") String id);
}

@FeignClient annotation is used to specify which calling functions from service, note the name of the service which is called the
name remains the same, and can not contain an underscore.
@RequestMapping comment for micro-called service address mapping. Note @PathVariable injection
solution must specify the parameter name, otherwise an error 

  (4) modified test_qa module ProblemController

@Autowired
private LabelClient labelClient;

@RequestMapping(value = "/label/{labelid}")
    public Result findLabelById(@PathVariable String labelid){
       Result result = labelClient.findById(labelid);
       return result;
}

3, feign load balancing

  Due to feign components integrated by default the ribbon (load balancing component), it can easily achieve load balancing

  Test: open simultaneously two micro-service basis to see whether it is in turn called.

@RequestMapping(value="/{id}", method = RequestMethod.GET)
public Result findById(@PathVariable String id){
   System.out.println("1");
   return new Result(true,StatusCode.OK,"查询成功",labelService.findById(id) );
}
@RequestMapping(value="/{id}", method = RequestMethod.GET)
public Result findById(@PathVariable String id){
   System.out.println("2");
   return new Result(true,StatusCode.OK,"查询成功",labelService.findById(id) );
}

Modify the port and output information, find load balancing.

Above is a brief introduction feign to understand children's shoes Dubbo, Dubbo fact, RPC calls, and load balancing, it is actually the equivalent of roughly Dubbo SpringCloud in feign components.

 

Published 41 original articles · won praise 47 · views 30000 +

Guess you like

Origin blog.csdn.net/u014526891/article/details/87394031