In the past few years, I have written a lot of interfaces, mainly Http interfaces, which are quite common. If there are some unusual interfaces, the INTERFACES encapsulated in SDK may be different, but when I see the interface defined by Sping Cloud Feign, it is really different.
Let’s talk about Sping Cloud Feign
advantages
Templated Http interface, declarative interface is as simple as calling a method
2 Integrated load balancer, fuse service degraded
These two points are expounded respectively
Eruka is a declarative interface. We first create a Hello-service that provides the interface method and registers the service with Eruka
@EnableHystrix @EnableDiscoveryClient @SpringBootApplication public class HelloApplication { public static void main(String[] args) { SpringApplication.run(HelloApplication.class, args); }}Copy the code
Take a look at the interface methods in this service
@RestController
public class HelloController {
private final Logger logger = Logger.getLogger(getClass());
@Autowired
private DiscoveryClient client;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello() throws Exception {
ServiceInstance instance = client.getLocalServiceInstance();
logger.info("/hello, host:" + instance.getHost() + ", service_id:" + instance.getServiceId());
return "Hello World";
}
Copy the code
When we see the /hello method, we start the Feign-Consumer project
@EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); }}Copy the code
configuration
spring.application.name=feign-consumer
server.port=9001
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
Copy the code
Then we start writing the method that calls /hello in the Hello-server service, which uses feign
@FeignClient(name="HELLO-SERVICE", fallback = HelloServiceFallback.class)
public interface HelloService {
@RequestMapping("/hello")
String hello();
}
Copy the code
@feignClient (name=” hello-service “), which specifies the SERVICE on which the method is to be called
@RequestMapping("/hello")
String hello();
Copy the code
Call the /hello method of the service
Then we’ll write a call class to test @restController Public Class ConsumerController {
@Autowired
HelloService helloService;
@Autowired
RefactorHelloService refactorHelloService;
@RequestMapping(value = "/feign-consumer", method = RequestMethod.GET)
public String helloConsumer() {
return helloService.hello();
}
Copy the code
By this time visit to feign service http://localhost:9001/feign-consumer
return
This is done through the call to Feign
For the second point, load balancing and circuit breakers were already mentioned in Zuul’s talk, and feign’s configuration is the same.
Let’s talk about the fallback of service
@FeignClient(name="HELLO-SERVICE", fallback = HelloServiceFallback.class)
Copy the code
This is before the class notes, this annotation specifies the HelloServiceFallback down-cycled class. The class, he is like this
@Component public class HelloServiceFallback implements HelloService { @Override public String hello() { return "error"; }Copy the code
When a fuse is triggered, the degraded method is used and error is returned. Note also that ribbon load balancing and Hystrix fuse are both set for a timeout period. The timeout period must be shorter than the fuse period. Otherwise, the ribbon will retry