SpringCloud Feign first experience of Fallback

SpringCloud Feign first experience of Fallback

In the micro SpringCloud service framework, Feign is one of very important and common components. Feign is declarative, template-based HTTP client that can help us be more convenient to call HTTP API. In this paper, a brief introduction for the fusing mechanism Fallback Feign. Fallback is mainly used to solve the dependent services are not available or call the service fails or a timeout, use the default return value.

1. The introduction of Feign

  • pom dependencies

    <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-feign</artifactId>
                <version>1.4.7.RELEASE</version>
    </dependency>
  • Application notes increased startup class

    @SpringBootApplication(scanBasePackages = {"com.xiaoqiang.feigncomsumer"})
    @EnableFeignClients(basePackages = {"com.xiaoqiang.feigncomsumer"})
    @EnableEurekaClient
    public class FeigncomsumerApplication {
        public static void main(String[] args) {
            SpringApplication.run(FeigncomsumerApplication.class, args);
        }
    
    }
  • Interface class configuration

    @FeignClient(name = "${feign.provider}",path = "/feignprovider")
    @Component
    public interface StudentClient {
    
         @GetMapping("/stud/getStudentList")
         List<Student> getStudentList(@RequestParam(required = false,name = "name") String name);
    }

2.Fallback Configuration

  • FallBack class

One thing to note here is that @RequestMapping in value, that is, url, not like the interface class in the url. Because a URL can not be mapped to the two methods.

@Component
@RequestMapping("fallback/")
public class  FallBackStudentClient implements StudentOtherClient {

    @Override
    public Student getStudent(String name) {
        Student student = new Student();
        student.setAge(0);
        student.setName("fall back test");
        return student;
    }
}
  • Interface classes

    Fallback class specified in @FeignClient annotation parameters, and requires @Component comment.

@Component
@FeignClient(name = "${feign.provider}",path = "/feignprovider"
        ,fallback = FallBackStudentClient.class)
public interface StudentOtherClient {
    @GetMapping("/stud/getStudent")
    Student getStudent(@RequestParam(required = false, name = "name") String name);
}
  • Open Hystrix fuse function

    Increase Hystrix configuration bootstrap.yml in. Which Hystrix the default time-out period is 1s.

    feign:
      name: MFRAMEWORK-PROVIDER
      provider: feignprovider
    ##开启Hystrix断路器
      hystrix:
        enabled: true

That's all Figen's first experience of the Fallback.

demo Institute Add: https: //github.com/lanxuan826/sample-library/tree/master/feigndemo-fallback

ps: in the testing process encountered a problem, the error contents shown.

The reason: @FeignClient ( "same service name") on multiple interfaces will complain, overriding is disabled.

solve:

In application.yml configure:

spring
    main:  allow-bean-definition-overriding: true

Guess you like

Origin www.cnblogs.com/lanxuan826/p/11488555.html