Spring cloud (Part 011) movie ribbon microservice, use to configure listofservers load balancing scheduling without Eureka

Time:2020-1-17

Spring cloud (Part 011) movie ribbon microservice, separated from Eureka, uses listofservers configuration for client load balancing scheduling

1、 General introduction

1. By trying to break away from the service governance framework and the Eureka ecosystem, the client load balancing scheduling is operated separately;
2. This chapter only uses resttemplate.getforobject to test the client load balancing algorithm;

2、 Implementation steps

2.1 add Maven reference package

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

    <artifactId>springms-consumer-movie-ribbon-properties-without-eureka</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>com.springms.cloud</groupId>
        <artifactId>springms-spring-cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>


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

        <! -- client discovery module -- >
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

        <! -- module to monitor and manage production environment -- >
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>
</project>

2.2 add application configuration file (springms consumer movie ribbon properties without eurekasrcmainresources application. YML)

spring:
  application:
    name: springms-consumer-movie-ribbon-properties-without-eureka
server:
  port: 8040
#When doing load balancing, you do not need this dynamically configured address
#user:
#  userServicePath: http://localhost:7900/simple/
eureka:
  client:
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://admin:[email protected]:8761/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}


ribbon:
  eureka:
    Enabled: false - Disable Eureka
springms-provider-user:
  ribbon:
    The first test
    listOfServers: localhost:7899


#Test two
#    listOfServers: localhost:7898,localhost:7899,localhost:7900

2.3 add entity user class (springms consumer movie ribbon properties without eurekasrcmainjavacomspringmscloudentityuser. Java)

package com.springms.cloud.entity;

import java.math.BigDecimal;

public class User {
  private Long id;

  private String username;

  private String name;

  private Short age;

  private BigDecimal balance;

  public Long getId() {
    return this.id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getUsername() {
    return this.username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getName() {
    return this.name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Short getAge() {
    return this.age;
  }

  public void setAge(Short age) {
    this.age = age;
  }

  public BigDecimal getBalance() {
    return this.balance;
  }

  public void setBalance(BigDecimal balance) {
    this.balance = balance;
  }

}

2.4 add the web access layer controller (springms consumer movie ribbon propertiesrcmainjavacomspringmscloud controllermovieribbonpropertiescontroller. Java)

package com.springms.cloud.controller;

import com.springms.cloud.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class MoviePropertiesWithoutEurekaController {

  @Autowired
  private RestTemplate restTemplate;
  @Autowired
  private LoadBalancerClient loadBalancerClient;

  @GetMapping("/movie/{id}")
  public User findById(@PathVariable Long id) {
      // http://localhost:7900/simple/
      // VIP virtual IP
      // HAProxy Heartbeat

      ServiceInstance serviceInstance = this.loadBalancerClient.choose("springms-provider-user");
      System.out.println(">>>>>" + " " + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort());

      return this.restTemplate.getForObject("http://springms-provider-user/simple/" + id, User.class);
  }
}

2.5 add movie microservice startup class (springms consumer movie ribbon properties without eurekasrcmainjavacomspringmscloudmsconsconsumermovieproperties withouteurekaapplication. Java)

package com.springms.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 *Movie ribbon microservice, separated from Eureka, uses listofservers to perform client load balancing scheduling.
 *
 *Loadbalanced: this load balancing annotation has integrated the ribbon;
 *
 *The default load balancing algorithm of ribbon is: polling;
 *
 *The priority of configuration file is the highest, followed by the configuration set by java code, and the default configuration priority is the lowest;
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 2017/9/18
 *
 */
@SpringBootApplication
@EnableEurekaClient
public class MsConsumerMoviePropertiesWithoutEurekaApplication {

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

    public static void main(String[] args) {
        SpringApplication.run(MsConsumerMoviePropertiesWithoutEurekaApplication.class, args);
        System. Out. Println ("[[[[[[movie microservices - propertieswithouteureka custom ribbon]]]]] has been started.");
    }
}

Three, test

/****************************************************************************************
 1、 Movie ribbon microservice, separated from Eureka, uses listofservers configuration for client load balancing scheduling (normal use service test):

 1. Application.yml configuration: ribbon.eureka.enabled: false
 2. Application.yml configuration: springms provider user.ribbon.listofservers: localhost: 7900
 3. Start the spring MS provider user module service, start three ports (7900, 7899, 7898);
 4. Start the service of springms provider-user2 module, and start two ports (7997, 7996) (directly change the name of spring.application.name to springms provider-user2 and restart);

 5. Start the spring MS consumer movie ribbon properties without Eureka module service;
 6. Enter the address http: // localhost: 8040 / movie / 1 in the browser, refresh it 9 times in a row, and then look at the service printing logs of these ports: springms provider user and springms provider user2. Normally, only 7999 of these ports will have 9 user information logs printed;

 Conclusion: the reason why only 7999 port of user microservice prints logs is that the use of Eureka is disabled. If it is not disabled, all three ports will print logs by reason; secondly, listofservers only selects 7999 port of microservice;
 ****************************************************************************************/

/****************************************************************************************
 2、 Movie ribbon microservice, separated from Eureka, uses listofservers to perform client load balancing scheduling (load balancing scheduling):

 1. Application.yml configuration: ribbon.eureka.enabled: false
 2. Application.yml configuration: springms provider user.ribbon.listofservers: localhost: 7898, localhost: 7899, localhost: 7900
 3. Start the spring MS provider user module service, start three ports (7900, 7899, 7898);
 4. Start the service of springms provider-user2 module, and start two ports (7997, 7996) (directly change the name of spring.application.name to springms provider-user2 and restart);

 5. Start the spring MS consumer movie ribbon properties without Eureka module service;
 6. Enter the address http: // localhost: 8040 / movie / 1 in the browser, refresh it 9 times in a row, and then look at the service printing logs of these ports of spring MS provider user and spring MS provider user2. Normally, the three ports of spring MS provider user will print logs, and they are polling printing;

 Summary: the reason why 7900, 7899 and 7898 ports poll and print is that there is no scheduling algorithm configured, and the default scheduling algorithm is polling, so the three ports will certainly poll and print user information;
 ****************************************************************************************/

4、 Download address

https://gitee.com/ylimhhmily/SpringCloudTutorial.git

Spring cloudtutorial communication QQ group: 235322432

Spring cloudtutorial communication wechat group: wechat communication group QR code image link

Welcome to pay attention, your affirmation is the biggest support to me!!!