4.Spring Cloud Initial Acquaintance - --- Feign Load Balancing

Keywords: Java Spring Maven Programming

Preface:

In the last section, we learned about the use of ribbon.

We learned that ribbon is a client load balancing mechanism.

And what we're going to talk about today is Feign, which is also a client load balancing mechanism.

In other words, Feign encapsulates ribbon's load balancing and implements Interface-oriented call service programming to eliminate service-oriented programming.

ribbon service-oriented programming:

@GetMapping("/hello")
public List<String> sayHello() {
    List<String> list = new ArrayList<>();
    for(int i=0;i<30;i++) {
        list.add(restTemplate.getForObject("http://CL-HELLO-PRODUCER/hello", String.class));
    }
    return list;
}

feign Interface-oriented programming:

@FeignClient(value="CL-HELLO-PRODUCER")
public interface HelloService {
    
    @GetMapping("/hello")
    public String sayHello();

}

Create a new service consumer (cl_hello_consumer_feign):

1. Adding dependencies

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

    <groupId>com.xm.cloud</groupId>
    <artifactId>cl_hello_consumer_feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>cl_hello_consumer_feign</name>
    <description>This is a Web about springcloud</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

2. Modify configuration

eureka.client.service-url.defaultZone=http://127.0.0.1:7001/eureka/

eureka.client.register-with-eureka=false

3. Open Notes

package com.xm.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;

@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class ClHelloConsumerFeignApplication {

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

4. Adding Service

package com.xm.cloud.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(value="CL-HELLO-PRODUCER")
public interface HelloService {
    
    @GetMapping("/hello")
    public String sayHello();

}

5. Add Controller

package com.xm.cloud.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xm.cloud.service.HelloService;

@RestController
public class HelloController {
    @Autowired
    private HelloService helloService;
    
    @GetMapping("/hello")
    public List<String> sayHello() {
        List<String> list = new ArrayList<String>();
        for(int i=0;i<10;i++) {
            list.add(helloService.sayHello());
        }
        return list;
    }

}

6. test

Visit: localhost:8080/helo

0 "Hello Spring Cloud! Machine 001"
1 "Hello Spring Cloud! 003 Machine"
2 "Hello Spring Cloud! Machine 002"
3 "Hello Spring Cloud! Machine 001"
4 "Hello Spring Cloud! 003 Machine"
5 "Hello Spring Cloud! Machine 002"
6 "Hello Spring Cloud! Machine 001"
7 "Hello Spring Cloud! 003 Machine"
8 "Hello Spring Cloud! Machine 002"
9 "Hello Spring Cloud! Machine 001"

Posted by Randomizer on Thu, 24 Jan 2019 21:09:15 -0800