Spring Cloud (7): Feign declarative interface call

1. What is Feign

Feign is also to achieve load balancing, but its use is more simplified than Ribbon. It is actually encapsulated based on Ribbon, so that we can achieve load balancing by calling the interface. Both Feign and Ribbon are provided by Netflix. Feign is a declarative, templated Web Service client, which simplifies the operation of developers writing Web Service clients. Developers can call HTTP API through simple interfaces and annotations. Make development more simplified and faster. Spring Cloud Feign is also a secondary development based on Netflix Feign. It integrates Ribbon and Hystrix, and has a series of convenient functions such as pluggable, annotation-based, load balancing, and service fusing. That is to say, we can use Feign in actual development to replace Ribbon.
Compared with Ribbon+RestTemplate, Feign greatly simplifies code development. Feign supports multiple annotations, including Feign annotations, JAX-RS annotations, Spring MVC annotations, etc. Spring Cloud optimizes Feign and integrates Ribbon and Eureka , so that Feign is more convenient to use.

2. The difference between Ribbon and Feign

Ribbon is a common HTTP client tool, and Feign is implemented based on Ribbon.

3. Advantages of Feign

1. Feign is a declarative Web Service client.

2. Support Feign annotations, Spring MVC annotations, JAX-RS annotations

3. Feign is implemented based on Ribbon, which is more convenient to use

4. Feign integrates Hystrix and has the function of service fusing

Four, actual combat!

  1. Create a Module and configure pom.xml as follows:
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
	<version>2.0.2.RELEASE</version>
</dependency>

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-openfeign</artifactId>
	<version>2.0.2.RELEASE</version>
</dependency>
  1. Create a configuration file application.yml, the configuration is as follows:
server:
  port: 8050
spring:
  application:
    name: feign
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
  instance:
    prefer-ip-address: true
  1. Create a startup class with the following code:
package com.zing;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

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

}
注解说明:

    * @EnableFeignClients:声明其为 Feign 客户端
  1. Create a declarative interface, the code is as follows:
package com.zing.feign;

import java.util.Collection;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

import com.zing.entity.Student;

@FeignClient(value = "provider")
public interface IFeignService {
    
    
	
	@GetMapping("/student/findAll")
	public Collection<Student> findAll();
	
	@GetMapping("/student/index")
	public String index();
}

5. The Handler code is as follows:

package com.zing.controller;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zing.entity.Student;
import com.zing.feign.IFeignService;

@RestController
@RequestMapping("/feign")
public class FeignHandler {
    
    

	@Autowired
	private IFeignService feignservice;
	
	@GetMapping("/findAll")
	public Collection<Student> findAll(){
    
    
		return feignservice.findAll();
	}
	
	@GetMapping("/index")
	public String index() {
    
    
		return feignservice.index();
	}
	
}
  1. Test the load balancing function of feign
    (1) start the registration center separately, two service providers with different ports, feign, the registration center page is as follows:
    insert image description here
    (2) visit http://localhost:8050/feign/index multiple times, we can Seeing that the two port addresses appear alternately proves that Feign has achieved load balancing. As shown in the figure below:
    insert image description here
    insert image description here
    7. Test the fuse mechanism of Feign
    (1) We stop all service providers first, and only keep the services of the registration center and Feign, and open the registration center as shown in the figure below: (2
    insert image description here
    ) Visit http://localhost:8050 again /feign/index You can see the content in the following figure:
    insert image description here
    (3) In order not to directly expose the error message, we need to add a service fuse mechanism, modify application.yml as follows:
server:
  port: 8050
spring:
  application:
    name: feign
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
  instance:
    prefer-ip-address: true
feign:
  hystrix:
    enabled: true

Configuration instructions:

  • feign.hystrix.enable: Whether to enable the fuse mechanism, the default is false.

(4) Create the implementation class FeignServiceImpl of IFeignService, define the fault-tolerant processing mechanism in it, and inject the FeignServiceImpl instance into the IOC container through the @Component annotation. The code is as follows:

package com.zing.feign.impl;

import java.util.Collection;

import org.springframework.stereotype.Component;

import com.zing.entity.Student;
import com.zing.feign.IFeignService;

@Component
public class FeignServiceImpl implements IFeignService{
    
    

	@Override
	public Collection<Student> findAll() {
    
    
		return null;
	}

	@Override
	public String index() {
    
    
		return "服务器维护中。。。";
	}

}

(5) Define the fallback attribute of @FeignClient at the interface definition of IFeignService for downgrade processing, set the mapping, and map it to FeignServiceImpl. After modifying IFeignService, the code is as follows:

package com.zing.feign;

import java.util.Collection;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

import com.zing.entity.Student;
import com.zing.feign.impl.FeignServiceImpl;

@FeignClient(value = "provider",fallback = FeignServiceImpl.class)
public interface IFeignService {
    
    
	
	@GetMapping("/student/findAll")
	public Collection<Student> findAll();
	
	@GetMapping("/student/index")
	public String index();
}

(6) Repeat steps (1) and (2), and the following interface appears, which proves that the service fuse mechanism works. As shown in the picture:
insert image description here

V. Summary

In this code, we use Feign to achieve load balancing and service fusing. After understanding the configuration of Feign and the basic principles of its use, let's study the fault tolerance mechanism of Hystix carefully. Let us look forward to the next article Spring Cloud (8): Hystix Fault Tolerance Mechanism .

One development engineer is also in the continuous learning stage, and the usual small experiences are shared from time to time. I hope that those who read the text I wrote can avoid detours and wish you success in work and study.
Bloggers have limited experience, if there are any shortcomings, welcome to communicate and improve together~ I hope to make progress together with you who are also in CSDN.

Author | Sweet Little Sweet Potato
Produced | Little Sweet Potato

Guess you like

Origin blog.csdn.net/qq_36836370/article/details/130901115
Recommended