1. Introduction

This paper mainly introduces the basic use of feign-Core and Spring-boot integration, and analyzes how to integrate; Feign-core – Basic Use; feign-core- Basic Use;

Use cases

2.1 Registry

This article uses Eurake, source code has the corresponding service, directly run main method can;

2.2 Interface Provision

This article is provided as a separate JAR project for the convenience of service consumers; See the source code

The interfaces are as follows:

/** * 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.3 Service Provider

Provide an HTTP service, you can directly use this article’s Spring-boot project, see github- source code

2.4 Service consumer

The consumer is the focus of this paper, focusing on the following:

Pom configuration

Introduced interface, Eureka client, feign-core

<dependency>
     <groupId>com.husky</groupId>
     <artifactId>yiyi-feign-intf</artifactId>
     <version>0.0.1 - the SNAPSHOT</version>
 </dependency>

 <! -- =========================== spring-boot start =========================== -->
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
 </dependency>

 <! -- =========================== spring-boot end =========================== -->
 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
     <version>${spring-cloud-version}</version>
 </dependency>
Copy the code

The configuration properties

app.id=yiyi-feign-consumer
spring.application.name=yiyi-feign-consumer
logging.level.org.apache.http=trace
server.port=18084
spring.main.allow-bean-definition-overriding=true
ribbon.eureka.enabled=true
eureka.client.service-url.defaultZone=http://yiyi.cn:8761/eureka/
Copy the code

Startup Class Configuration

@EnableDiscoveryClient
@EnableFeignClients(basePackages = {"com.husky.intf"})
@SpringBootApplication
public class HuskyFeignConsumerApplication {

    public static void main(String[] args) { SpringApplication.run(HuskyFeignConsumerApplication.class, args); }}Copy the code

The Controller calls

@RestController
@RequestMapping("/eat")
public class EatController {

    @Autowired
    private EatService eatService;

    /** * eat  */
    @GetMapping("/apple")
    public String eatApple(a) {
        return "provider: " + eatService.eatApple();
    }

    @GetMapping("/orange")
    public String eatOrange(String who) {
        returneatService.eatOrange(who); }}Copy the code

2.5 start

Start the registry, service provider and service consumer in turn;

Postman access: localhost: 18084 / eat/apple

Provider: I ate apple on 18081

3. Integration principles

If you are not familiar with the feign-core principle, you are advised to take a look at the previous article: Feign-core – Basic Use. This article focuses on how feign-core is integrated with the spring-boot principle.

3.1 Feign-core principle review

The process for generating proxy objects is as follows:

3.2 Envision

Before understanding the principle, consider the following questions 🤔?

1) What do we want?

A proxy object for an interface is required

2) How do most spring-boot integration frameworks work?

You typically annotate the startup class with @enablexxx

He did not take any medicine

As expected in 3.2, we analyze the loading logic of the proxy class with the @enableFeignClient annotation on the startup class as the entry point.

As shown in the figure below: Our analysis process is as follows:

Note: At the last step: registerFeignClients(), we get a FactoryBean that generates the interface proxy object;

So when will the proxy object be generated?

That’s when @autowired is assigned

@Autowired

private EatService eatService;

At this point, the proxy class can provide the service

see

This article source address – Github

Talk about how Fein works

Spring Cloud integrates Feign’s principles