Spring cloud (4): the first ribbon program

Time:2021-1-28

1. Ribbon

Load balancing framework supports pluggable load balancing rules

Support a variety of protocols, such as HTTP, UDP, etc

Provide load balancing client

1.1 load balancer components

A load balancer, providing at least the following functions:

  • Maintain the IP and other information of each server
  • Select servers according to specific logic

In order to realize the basic load balancing function, the load balancer of ribbon has three sub modules

  • Rule
  • Ping
  • ServerList

1.2 implementation procedure

Create project: Ribbon service

pom.xml:

4.0.0
    org.crazyit.cloud
    ribbon-service
    0.0.1-SNAPSHOT
    
    
        
            org.springframework.boot
            spring-boot-starter-web
            1.5.7.RELEASE

Person:

package org.crazyit.cloud;

public class Person {

    private Integer id;
    private String name;
    private String message;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
    
}

MyController:

package org.crazyit.cloud;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @RequestMapping(value = "/person", method = RequestMethod.GET, 
            produces = MediaType.APPLICATION_JSON_VALUE)
    public Person getPerson(HttpServletRequest request) {
        Person p = new Person();
        p.setId(1);
        p.setName("angus");
        p.setMessage(request.getRequestURL().toString());
        return p;
    }
}

ServiceApp:

package org.crazyit.cloud;

import java.util.Scanner;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class ServiceApp {

    public static void main(String[] args) throws Exception {
        Scanner scan = new Scanner(System.in);
        String port = scan.nextLine();
        new SpringApplicationBuilder(ServiceApp.class).properties(
                "server.port=" + port).run(args);
    }

}

Start two services: console input 8080 and 8081:

 

 

Then create a client ribbon client project:

pom:

4.0.0
    org.crazyit.cloud
    ribbon-client
    0.0.1-SNAPSHOT

    
        
            com.netflix.ribbon
            ribbon-core
            2.2.2
        
        
            com.netflix.ribbon
            ribbon-httpclient
            2.2.2

TestRibbon:

package org.crazyit.cloud;

import com.netflix.client.ClientFactory;
import com.netflix.client.http.HttpRequest;
import com.netflix.client.http.HttpResponse;
import com.netflix.config.ConfigurationManager;
import com.netflix.niws.client.http.RestClient;

public class TestRibbon {

    public static void main(String[] args) throws Exception {
        ConfigurationManager.getConfigInstance().setProperty(
                  "my-client.ribbon.listOfServers", "localhost:8080,localhost:8081");
        RestClient client = (RestClient) ClientFactory.getNamedClient("my-client");
        HttpRequest request = HttpRequest.newBuilder().uri("/person").build();
        for(int i = 0; i < 10; i++) {
            HttpResponse response = client.executeWithLoadBalancer(request);
            String json = response.getEntity(String.class);
            System.out.println(json);
        }
    }

}

Then run:

Load balancing is realized.