Eureka cluster

Why the Eureka cluster?

In simple terms, Eureka cluster builds multiple registries and shares the services registered in the registries. Each Eureka registry in the cluster has the services of each other. For example, there are three Eureka registries (A, B, C), two services (A, B), one registered to any registry, and two services in A, B, and C, so that when one registry collapses, these services are lost and crash.

Environment set up

  1. First, we need to change the name of the host to be accessed: add the name of the host to be accessed at the end of the hosts file. The default is localhost

  1. Next, we create two more Eureka modules (we already created one eureka module earlier)

  1. Modify the values of the three created modulesapplication.ymlfile

  1. Configure the address at which the service is registered to the registry in the service provider

  1. Turn on the three Eureka modules to test the cluster.

  1. Try starting the service provider as well

The registered services can be seen in all three Eureka registries. This is the Eureka cluster, which can be used through other registries in case one registry collapses

Ribbon

Introduction to the

  1. Spring Cloud Ribbon is a client load balancing tool based on Netflix Ribbon.
  2. To put it simply, The Ribbon is an open source project released by Netflix. Its main function is to provide client-side software load balancing algorithm to connect Netflix’s middle-tier services together. The Ribbon client component provides a complete set of configuration options, such as connection timeout and retry. The Ribbon automatically helps you connect to these machines based on certain rules (e.g. simple polling, random linking, etc.). It’s also easy to implement custom load balancing algorithms using the Ribbon!
  3. Load balancing simply means that users’ requests are evenly distributed among multiple services to achieve high availability (HA) of the system.
  4. SpringCloud’s load balancing algorithm can be customized.
  5. The operation is on the service consumer side.

Simple use of the Ribbon

  1. Import Ribbon dependencies in service consumers
<! -- ribbon -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
    <version>2.2.5. RELEASE</version>
</dependency>
Copy the code
  1. In the service of consumersapplication.ymlConfiguration had been
eureka:
  client:
    register-with-eureka: false # Do not register yourself with Eureka
    service-url: Select one of the three registries at random
      defaultZone: http://eureka7001:7001/eureka/,http://eureka7002:7002/eureka/,http://eureka7003:7003/eureka/
Copy the code
  1. Add to the main startup class of the service consumer@EnableEurekaClientNote, open Eureka
  2. Defining the Spring configuration class:ConfigBean.javaadd@LoadBalancedAnnotation configurationLoad balancingRealize the RestTemplate
  3. Example Change the service access address

  1. Start the service and test

The Ribbon implements load balancing

The principle of

  1. For easy observation, let’s create two more databases db02,db03; And connect

  1. Create two more service provider modules, as modified from the first one
  • Modify the application. Yml –> description of the connected database and configuration
  • Modify theDeptMapper.xmlTo change the location of the query database
  1. After modifying the startup test (according to your own computer to start the service, careful explosion)

===> Polling Queries data in the database

Ribbon custom load balancing algorithm

The Ribbon implements load balancing. The default method is polling. There are other methods, such as randomization. Specific operations are as follows:

  1. Under service consumersjavaPackage to write custom load balancing algorithms

myRule

package myRule;

import com.netflix.loadbalancer.IRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyRule {

    @Bean
    public IRule TestRule(a){
        return new MyRandomRule();// The default is to poll RandomRule and now customize it to your own}}Copy the code

MyRandomRule.java


package myRule;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

public class MyRandomRule extends AbstractLoadBalancerRule {
    public MyRandomRule(a) {}* 

* total=0, default =0, if =5, pointing to the next service node * index=0, default =0, if total=5,index+1 */

private int total = 0;// The number of times it was called private int currentIndex = 0;// Who is currently providing the service //@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE") public Server choose(ILoadBalancer lb, Object key) { if (lb == null) { return null; } Server server = null; while (server == null) { if (Thread.interrupted()) { return null; } List<Server> upList = lb.getReachableServers();// Get the currently alive service List<Server> allList = lb.getAllServers();// Get all the services int serverCount = allList.size(); if (serverCount == 0) { /* * No servers. End regardless of pass, because subsequent passes * only get more restrictive. */ return null; } //int index = chooseRandomInt(serverCount); // Generate interval random number //server = upList.get(index); // Get one randomly from or live services / / = = = = = = = = = = = = = = = = = = = = = custom code = = = = = = = = = = = = = = = = = = = = = = = = = if (total < 5) { server = upList.get(currentIndex); total++; } else { total = 0; currentIndex++; if (currentIndex >= upList.size()) { currentIndex = 0; } server = upList.get(currentIndex);// From the live service, obtain the specified service to operate } / / = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = if (server == null) { /* * The only time this should happen is if the server list were * somehow trimmed. This is a transient condition. Retry after * yielding. */ Thread.yield(); continue; } if (server.isAlive()) { return (server); } // Shouldn't actually happen.. but must be transient or a bug. server = null; Thread.yield(); } return server; } protected int chooseRandomInt(int serverCount) { return ThreadLocalRandom.current().nextInt(serverCount); } public Server choose(Object key) { return this.choose(this.getLoadBalancer(), key); } public void initWithNiwsConfig(IClientConfig clientConfig) {}}Copy the code
  1. Set the algorithm to enable load balancing in the main boot class@RibbonClient(name = "SPRINGCLOUD-PROVIDER",configuration = MyRule.class)
  2. Start the test