This is the seventh article in the Spring Cloud column, and knowing the first six will help you understand it better:

  1. Spring Cloud first article | Spring Cloud preface introduces an overview and its commonly used components

  2. The second Spring Cloud | use and understand Eureka registry

  3. Spring Cloud third | build a high availability Eureka registry

  4. Spring Cloud fourth article | client side load balancing Ribbon

  5. The fifth canto of Spring Cloud fusing Hystrix | service

  6. Spring Cloud article 6 | Hystrix Dashboard monitoring Hystrix Dashboard

What is Feign

Feign is a declarative REST invocation client developed by Netflix. Ribbon load balancing and Hystrⅸ service fuse are very basic components for micro service development in Spring Cloud. In the process of using them, we also found that they generally appear at the same time, and their configurations are very similar. There are a lot of the same code in each development, so Spring Cloud integrates Ribbon and Hystrix based on Netflix Feign, which makes our development work easier. Just like Spring Boot is a simplification of Spring+ SpringMVC, Spring Cloud Feign simplifies Ribbon load balancing and Hystrⅸ service fusing, and further encapsulates them. It not only simplifies development in configuration, but also provides a declarative way to define Web service clients. The usage is similar to that of Dubbo.

Second, using Feign to achieve consumers

1, create a consumer service named (SpringCloud-service-feign)

2. Add dependencies

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>Copy the code

3. Add annotations to the startup class

@EnableFeignClientsCopy the code

4. Declare services

Define a HelloService interface, bind the service with the @FeignClient annotation specifying the service name, and then bind the service provider’s interface with the annotation provided in Spring MVC as follows:

// Use Feign's client annotation to bind the remote name, which can be uppercase or lowercase @feignClient (value =)"springcloud-service-provider"Public interface HelloService {// Declare a method that is provided by the remote service provider @requestMapping ("/provider/hello")
    public String hello();   
}Copy the code

5. Use the Controller to call the service

    @Autowired
    private HelloService helloService;

    @RequestMapping("/hello")
    public String hello(){// Call the declarative interface method to implement the call to the remote servicereturn helloService.hello();
    }Copy the code

6. Application. yml is configured as follows

spring:
  application:
    name: springcloud-service-feign
server:
  port: 9091
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8700/eureka
    The client updates the service information from Eureka service every 30 seconds
    registry-fetch-interval-seconds: 30
    I need to register my service with Eureka
    register-with-eureka: true
    # Need a retrieval service
    fetch-registry: true
  Heartbeat detection detection and renewal time
  instance:
    # tell the server that if I do not send you a heartbeat within 10 seconds, it means THAT I am faulty. Delete me
    #Eureka Specifies the maximum time the server must wait after receiving the last heartbeat, in seconds.
    lease-expiration-duration-in-seconds: 10
    Send a heartbeat to the server every 2 seconds to prove that the server is still alive. Default is 30 seconds
    #Eureka Interval in seconds for the client to send the heartbeat to the server (the client tells the server it will follow this rule)
    lease-renewal-interval-in-seconds: 2Copy the code

7, launch the test, visit the address http://localhost:9091/feign/hello

Use Feign supported features

Load balancing:

Spring Cloud provides Ribbon for load balancing. Use Ribbo to directly inject a RestTemplate object. RestTemplate is configured for load balancing under Spring Cloud Feign can also implement load balancing directly by defining an interface annotated with @FeignClient and then using @RequestMappin annotations to map remote REST services to methods that are also configured to be responsible for balancing.

Service fuse:

1. Enable hystrix in the application.yml file

# Enable hystrix circuit breaker
feign:
  hystrix:
    enabled: trueCopy the code

2. Specify circuit breaker callback logic

@FeignClient(value = "springcloud-service-provider", fallback = MyFallback.class)Copy the code

@Component
public class MyFallback implements HelloService {
    @Override
    public String hello() {
        return "Remote service unavailable, local logic is used instead..."; }}Copy the code

3. Test by telling the service provider to time out

If you need to catch an exception thrown by a provider, you can use:

@FeignClient(value = "springcloud-service-provider", fallbackFactory = MyFallbackFactory.class)Copy the code

@Component
public class MyFallbackFactory implements FallbackFactory<HelloService> {
    @Override
    public HelloService create(Throwable throwable) {

        return new HelloService() {
            @Override
            public String hello() {
                returnthrowable.getMessage(); }}; }}Copy the code

Detailed reference case source: gitee.com/coding-farm…