Feign
Introduction
Spring Cloud introduces Feign and integrates Ribbon to implement client load balancing calls. Feign is a lightweight rest client that simplifies remote calls and makes remote calls like local methods.
Feign steps
1. Introduce coordinates
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
2. Configure the annotation @EnableFeignClients on the startup class to scan, generate proxy objects, and inject into the IOC container
@SpringBootApplication
@EnableFeignClients
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class);
}
}
3. Add the configuration file as shown below
3. Define an interface and add FeignClient annotations to declare the service name to be called remotely (all service names are written in the public class)
4. Define FeignController to call the test
5. Start each related service test
Service registered to the registry
test results
Now that Feign integrates Ribbon, how to use it?
In fact, after the above steps are completed, the default polling load balancing strategy has been adopted. If you want to change a strategy, how to solve it? (use your brain)
Feign also integrates Hystrix, how to use it?
1. Add the following to the configuration file to enable the Hystrix function
2. Set the callback class on the interface class
3. The MyFallback class is shown below
How to obtain abnormal information of remote service
1. Modify the annotation parameters on the interface class as shown below
2. Define the MyFallbackFactory class as follows
3. Start the service test (omitted)
Precautions
SpringCloud enhanced Feign to be compatible with SpringMVC annotations. We need to pay attention when using SpringMVC annotations:
When the 1feignClient return value is a complex object, its type must have a parameterless constructor.