SpringCloud Distributed Microservice Cloud Architecture Part 3: Service Consumer (Finchley Version)

Posted by bassdog65 on Thu, 12 Dec 2019 19:27:09 +0100

In the previous article, we talked about how to consume services through RestTemplate+Ribbon. This article mainly talks about how to consume services through Feign.

1. Introduction to Feign
Feign is a declarative pseudo-Http client that makes it easier to write Http clients.With Feign, you only need to create an interface and annotate it.It has pluggable annotation features and can use Feign and JAX-RS annotations.Understanding the springcloud architecture requires additional support: 35362247259 Feign supports pluggable encoders and decoders.Feign integrates Ribbon by default and works with Eureka to achieve load balancing by default.

In short:

Feign uses interface-based annotations
Feign integrates ribbon with load balancing capabilities
Hystrix integrated with the ability to fuse
2. Preparations
Continue with the previous section of the project, starting eureka-server with port 8761 and service-hi twice with ports 8762 and 8773, respectively.

3. Create a feign service
Create a new spring-boot project, named serice-feign, in which Feign is introduced into the pom file starting with spring-cloud-starter-feign, Eureka starting with spring-cloud-starter-netflix-eureka-client, and Web starting with spring-boot-starter-web, coded as follows:

<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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.forezp</groupId>
    <artifactId>service-feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>service-feign</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.forezp</groupId>
        <artifactId>sc-f-chapter3</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <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>

In the project configuration file application.yml file, specify the program name service-feign, port number 8765, service registration address http://localhost:8761/eureka/, code as follows:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8765
spring:
  application:
    name: service-feign

Open Feign in the program's startup class ServiceFeignApplication with the @EnableFeignClients annotation:

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

    public static void main(String[] args) {
        SpringApplication.run( ServiceFeignApplication.class, args );
    }
}

Define a feign interface that specifies which service to call through the @ FeignClient ("service name").For example, the'/hi'interface of the service-hi service is called in the code as follows:

@FeignClient(value = "service-hi")
public interface SchedualServiceHi {
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

At the controller s layer of the Web layer, expose an API interface of'/hi'to consume services through the Feign client SchedualServiceHi defined above.The code is as follows:

@RestController
public class HiController {

    //Compiler error ignored.The error occurred because the Bean was injected at program startup and was not perceived by the compiler.
    @Autowired
    SchedualServiceHi schedualServiceHi;

    @GetMapping(value = "/hi")
    public String sayHi(@RequestParam String name) {
        return schedualServiceHi.sayHiFromClientOne( name );
    }
}

Start the program, visit http://localhost:8765/hi?name=forezp multiple times, browser alternating display:

hi forezp,i am from port:8762
hi forezp,i am from port:8763

Topics: Programming Spring Maven Apache snapshot