This is the seventh article of Spring Cloud column. Understanding the contents of the first six articles helps to better understand this article:
I. what is Feign
Feign is a declarative REST calling client developed by Netflix company. Ribbon load balancing and Hystr "service fusing are very basic components for microservice development in our Spring Cloud. During the use process, we also found that they generally appear at the same time, and the configuration is very similar. Each development has a lot of the same code, so Spring Cloud base Netflix Feign integrates the two components of ribbon and Hystrix, which makes our development work easier. Just like Spring boot is a simplification of Spring + spring MVC, Spring Cloud Feign simplifies ribbon load balancing and Hystr "service fuse", and further encapsulates it. It not only greatly simplifies the development work in configuration, but also provides a declarative type Web service client definition method for. It is similar to Dubbo.
II. Use Feign to realize consumers
1. Create a consumer service named (spring cloud service feign)
2. Add dependency
<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>
3. Add notes on the startup class
@EnableFeignClients
4. Declaration service
Define a HelloService interface, specify the service name through @ FeignClient annotation, and then bind the service provider interface through the annotation provided in Spring MVC, as follows:
//Use feign The client annotation of the binding remote name can be upper case or lower case @FeignClient(value = "springcloud-service-provider") public interface HelloService { //Declare a method that is provided by a remote service provider @RequestMapping("/provider/hello") public String hello(); }
5, use Controller to invoke the service, the code is as follows
@Autowired private HelloService helloService; @RequestMapping("/hello") public String hello(){ //Call declarative interface method,Implement calls to remote services return helloService.hello(); }
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 #Need to register my service with eureka register-with-eureka: true #Retrieval service required fetch-registry: true #Heartbeat detection and renewal time instance: #Tell the server that if I don't send you a heartbeat within 10s, it means I'm out of order. Remove me. The default is 90s #The maximum waiting time of Eureka server after receiving the last heartbeat, in seconds. If it exceeds, it will be rejected (the client tells the server to wait for itself according to this rule) lease-expiration-duration-in-seconds: 10 #Send a heartbeat to the server every 2s to prove that they are still alive. The default is 30s #The time interval between the Eureka client sending heartbeat to the server, in seconds (the client tells the server that it will follow this rule) lease-renewal-interval-in-seconds: 2
7. Start the test at http://localhost:9091/feign/hello
III. features supported by Feign
Load balancing:
Spring Cloud provides the Ribbon to achieve load balancing. Use the Ribbon to directly inject a RestTemplate object. RestTemplate has been configured for load balancing. In Spring Cloud, Feign can also directly achieve load balancing. Define an interface with @ FeignClient annotation, and then use @ requestmap annotation to launch remote REST services Law is also responsible for balanced allocation.
Service fuse:
1. Enable the hystrix function in the application.yml file
#Open the fuse mechanism of hystrix feign: hystrix: enabled: true
2. Specify the fusing callback logic
@FeignClient(value = "springcloud-service-provider", fallback = MyFallback.class)
@Component public class MyFallback implements HelloService { @Override public String hello() { return "Remote service is not available, local logic is temporarily used instead....."; } }
3. Test. Let the service provider time out
If you need to catch the exception thrown by the provider, you can use:
@FeignClient(value = "springcloud-service-provider", fallbackFactory = MyFallbackFactory.class)
@Component public class MyFallbackFactory implements FallbackFactory<HelloService> { @Override public HelloService create(Throwable throwable) { return new HelloService() { @Override public String hello() { return throwable.getMessage(); } }; } }
Refer to the case source code for details: https://gitee.com/coding-farmer/spirngcloud-learn