1. What is it?
Feign is a lightweight client framework for HTTP requests. Through the way of interface + annotation HTTP request call, interface oriented programming; Similar to Dubbo, RMI(remote procedure call) is implemented;
2. How to play it?
2.1 Project initialization
Build a Spring-boot project
2.2 introduced the jar
<properties>
<java.version>1.8</java.version>
<spring-cloud-version>2.1.0. RELEASE</spring-cloud-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>${spring-cloud-version}</version>
</dependency>
</dependencies>
Copy the code
2.3 Service Interfaces
/** * eat */
@FeignClient(name = "yiyi-feign-provider", path = "/eat")
public interface EatService {
/** * eat */
@GetMapping("/apple")
String eatApple(a);
/** * eat 🍊 */
@GetMapping("/orange")
String eatOrange(@RequestParam("who") String who);
}
Copy the code
2.4 the junit test
public class FeignTest {
@Test
public void eatApple(a) {
EatService eatService = Feign.builder()
.contract(new SpringMvcContract())
.target(EatService.class, "http://localhost:18081/eat");
String s = eatService.eatApple();
System.out.println(" eatService.eatApple() = "+ s); }}Copy the code
2.5 Running Results
Note: Please start another service before running, when the service provider, ensure the availability of http://localhost:18081/; Available reference to this article source start OK;
3. Principle tracing
3.1 to prepare
Feign-core, as we’ve seen from how it’s played, has to have interfaces; Interface cannot run directly; So, running must be an agent; How does the proxy work? If there is no cglib interface, it must be jdK-proxy.
The core logic of JDK-Proxy is as follows:
Proxy.newProxyInstance(ClassLoader loader, Class<? >[] interfaces, InvocationHandler h);Copy the code
ClassLoader: a ClassLoader, which is usually an application ClassLoader;
Class<? >[] : This is the interface we defined;
InvocationHandler: This general requirements rewrite, analysis principle focus on the implementation of this OK;
3.2 Debug View the flow
\
Purpose: We debug the most important thing to find is where the Proxy object is generated, that is, where the proxy.newProxyInstance method is called; Source personal feeling a little redundant, not paste, directly look at the construction flow chart;
\
The core objects FeignInvocationHandler, HardCodedTarget, MethodHandler and Method have the following relationships;
Note: look at the source code can not see every line, to have their own understanding of the framework, must learn debug!!
4. Common usage questions
A bean with that name has already been defined and overriding is disabled.
Cause: There is more than one Feign interface pointing to the same microservice solution:
Add the following configuration to application.properties
spring.main.allow-bean-definition-overriding=true
Copy the code
see
Now that OpenFeign is used, A bean with that name has already been defined and Ancient is disabled.
This article source address – Github
Talk about how Fein works
Spring Cloud integrates Feign’s principles