Spring Cloud Microservices Practical Tutorial Series (5) - Client Load Balancing through Spring Cloud Ribbon

        Spring Cloud Ribbon is a client-side load balancing tool based on HTTP and TCP. Through the encapsulation of Spring Cloud, it allows us to easily automatically replace service-oriented REST template requests into client-side load balancing service calls. It does not need to be deployed independently like a service registry, configuration center, and API gateway, but is embedded into every Spring Cloud-built microservice and infrastructure. Because of the calls between microservices, the request forwarding of the API gateway is actually implemented through Ribbon.

        Through the encapsulation of Spring Cloud Ribbon, it is very simple for us to use client-side load balancing calls in the microservice architecture, and only need the following two steps:

        1) Service providers need to start multiple service instances and register with one service center or multiple associated service registries;
        2) Service consumers directly implement service-oriented interface calls by calling RestTemplate decorated with @LoadBalanced annotation.

        RestTemplate can implement service calls for GET requests, POST requests, PUT requests, and DELETE requests. Its parameters and calling methods are very similar. Let's take the GET request as an example to develop a simplest example of client load balancing.

        (1) Start the Eureka service registry

java -jar eureka-server-1.0.0.jar

        (2) Start two instances of hello-service and register with the registry together

java -jar hello-service-1.0.0.jar --server.port=8081
java -jar hello-service-1.0.0.jar --server.port=8082

        (3) Create a Spring Boot basic project to implement a service consumer, named ribbon-consumer. Compared with the previous pom.xml, a new Ribbon dependency module spring-cloud-starter-ribbon is added. The code is as follows:

<?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>cn.badtom</groupId>
    <artifactId>ribbon-consumer</artifactId>
    <version>1.0-SNAPSHOT</version>

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

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR1</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>

        (4) Create the main class ConsumerController, and register the application as an Eureka client application through the @EnableDiscoveryClient annotation to obtain service discovery capabilities; enable the client load balancing function through the RestTemplate instance and the @LoadBalanced annotation; create the /ribbon-consumer interface, in In this interface, the /hello interface provided by the HELLO-SERVICE service is invoked through RestTemplate. The service name HELLO-SERVICE is accessed here instead of a specific address, which is a very important feature in the service governance framework.

package cn.badtom.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@EnableAutoConfiguration
@EnableDiscoveryClient
public class ConsumerController {

    @Autowired
    RestTemplate restTemplate;

    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET)
    public String helloConsumer() {
        return restTemplate.getForEntity("http://HELLO-SERVICE/hello",String.class).getBody();
//        return "hello ribbon";
    }

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

        (5) Configure the location of the Eureka service registry in the application.properties configuration file, and set the application port to 9000 to avoid conflicts.

server.port=9000
spring.application.name=ribbon-consumer
eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka
        After starting the ribbon-consumer application, you can see in the Eureka information panel that in addition to HELLO-SERVICE, the RIBBON-CONSUMER service is also registered. By making a GET request to http://localhost:9000/ribbon-consumer, you can see that the console successfully returns the output information of HELLO-SERVICE. Continuously initiate GET requests, check the consoles of the two instances of HELLO-SERVICE, and you can see that they alternately print the accessed information, which proves that Ribbon is polling access to the service.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325685920&siteId=291194637