1. Introduction of Feign

Feign is a declarative pseudo Http client that makes writing Http clients much easier. With Feign, you just create an interface and annotate it. It has pluggable annotations that use Feign annotations and JAX-RS annotations. Feign supports pluggable encoders and decoders. Feign integrates the Ribbon by default, and when combined with Eureka, implements load balancing by default. In short:

  • Feign uses interface-based annotations;
  • Feign integrates the Ribbon with load balancing capabilities;
  • Integrated with Hystrix, with fuse breaker capability.

2. Create Feign call demo project

2.1 Preparations

Start Eureka Server and Eureka Client Provider services in the Eureka demo project in the previous part, and add an interface to the Controller of Eureka Client Provider for Feign call test

@RestController
@RequestMapping("/demo")
public class DemoController {

    @Value("${server.port:#{null}}")
    private String serverPort;

    @GetMapping("/hello")
    public String hello(a) {
        return "Hello " + serverPort;
    }

    @GetMapping("/feign")
    public String feignTest(@RequestParam(value = "name") String name){
        return "Hello Feign "+ name; }}Copy the code

2.2 Creating a service consumer

Create a New Maven project named spring-Cloud-feign-demo and add the following dependencies to POM.xml

<?xml version="1.0" encoding="UTF-8"? >
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-demo</artifactId>
        <groupId>com.hxmec</groupId>
        <version>1.0 the SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-cloud-feign-demo</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>
</project>
Copy the code

Create a new application.yml and add the following configuration

server:
  port: 9003
spring:
  application:
    name: feign-demo
logging:
  pattern:
    console: '%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n'
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8888/eureka/
Copy the code

Create a startup class FeignDemoApplication and add Feign annotations

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class FeignDemoApplication {

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

Write Feign interface services

@FeignClient(value = "eureka-client-provider")// This name is the service name registered by the consumer provider
public interface FeignService {

    @RequestMapping(value = "/demo/feign",method = RequestMethod.GET)
    String feignTest(@RequestParam(value = "name") String name);
}

Copy the code

Write Controller layer to call Feign interface instance

@RestController
@RequestMapping("/feign")
@AllArgsConstructor
public class FeignClientController {

    private final FeignService feignService;

    @GetMapping(value = "/test1")
    public String feign(@RequestParam String name) {
        returnfeignService.feignTest(name); }}Copy the code

Start the project and open http://localhost:8888 to see that the application has been registered with the Eureka registry

Request to http://localhost:9003/feign/test1? Name =Trazen verifies that the service provider interface was successfully invoked

3. Project Git address

Github.com/ty197287300…

4. Reference

Cloud.spring. IO /spring-clou…