1. The background

2. Knowledge

Feign is a framework for declarative REST client network requests, similar to RestTemplate. It integrates Ribbon with Hystrix and provides a declarative way to define Web service clients. That is, with client load balancers and circuit breakers.

Compared to RestTemplate, it has these characteristics:

  • Declarative calls are clearer
  • Better packaging: load balancer and circuit breaker
  • Convenient configuration

Feign uses the JDK’s native URLConnection to send HTTP requests by default and can be configured to change.

Using Feign To use Feign, create an interface and annotate it.

Example 3.

1) Import dependency packages

dependencies { implementation 'org.springframework.boot:spring-boot-starter' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' implementation 'org. Springframework. Cloud: spring - the cloud - starter - openfeign' / / to make fuse work, here also to refer to netflix - hystrix 'implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-hystrix', version: '2.2.8. RELEASE' testImplementation 'org. Springframework. The boot: spring - the boot - starter - test'}Copy the code

Note: To see the circuit breaker hystrix effect, be sure to add the Spring-cloud-starter-Netflix-Hystrix dependency. The reason is that while Feign can attach circuit breakers to implement Fallback processing, it lacks the Hystrix package and I’m stuck here for a long time. Be sure to add

2) import

Write a Feign client invocation class that invokes the remote service as an annotation declaration.

@FeignClient(value = "HELLO-SERVICE-1", fallbackFactory = TestFallbackFactory.class)
public interface HelloClient {

    @RequestMapping(method = RequestMethod.GET, value = "/hello")
    String hello();

}
Copy the code

Hello-service-1 is the name of the remote SERVICE. /hello points to the URL to be accessed. The main parameters should also be consistent.

3) Implementation of fallback circuit breaker

Service degradation occurs when a timeout occurs, the remote service becomes unavailable, and so on. We add a fallback method. Annotations so write fallbackFactory = TestFallbackFactory. Class

The code is as follows:

@Component public class TestFallbackFactory implements FallbackFactory<HelloClient> { private static final Logger logger  = LoggerFactory.getLogger(DemoApplication.class); @Override public HelloClient create(Throwable cause) { logger.info("**********"); cause.printStackTrace(); return new MyFallback(); } } public class MyFallback implements HelloClient { @Override public String hello() { //throw new NoFallbackAvailableException("Boom!" , new RuntimeException()); Return B. }}Copy the code

4) Enable the circuit breaker during configuration

The key is to write in a configuration file “feign. Circuitbreaker. Enabled = true”, will open the circuit breaker.

server.port=9001
spring.application.name=consumer
eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/

feign.circuitbreaker.enabled=true

#hystrix.command.default.execution.isolation.thread.timeoutinMilliseconds=2OOO
Copy the code

The load balancer client above will want to discover the physical address of the “Stores” service

4. The extension

Example of my code: github.com/vir56k/demo…

5. Reference:

The Spring Cloud micro service field “zhuanlan.zhihu.com/p/149693905… Spring. IO/projects/sp…