<Spring Cloud>入门三 Ribbon

1.Ribbon

  客户端软负载均衡组件

1.1配置

  搭建了三个消费者供客户端调用:

    

1.修改yml

eureka:
  client:
    service-url:
      defaultZone: http://eureka-server01:8761/eureka/,http://eureka-server02:8762/eureka/
    register-with-eureka: false

2.修改配置类

  @LoadBalanced ,默认采用RoundRobin

@Configuration
public class ConfigBean {

    @Bean
    @LoadBalanced //开启负载均衡 Ribbon
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }

}

3.启动类上标注 eurekaclient

@SpringBootApplication
@EnableEurekaClient
public class App_Consumer_Dept_80 {

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

1.2 修改负载均衡算法

在配置类中注入需要算法的Bean

 

可选算法

1.3 自定义负载均衡算法

@RibbonClient

  name:服务提供方的application.name

  configuration = 自定义配置类的名字

注意自定义配置类不能在spring boot 启动类的同包或子包下,或者使@ComponentScan不扫描该类

package org.rule.define;

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

/**
 * @author mapleins
 * @Date 2019-01-12 21:45
 * @Desc 自定义ribbon 的rule不能再@ComponentScan的包或子包下
 **/
@Configuration
public class MySelfRule {

    /**
     * 需求:轮询访问,每个服务访问3次
     */
    @Bean
    public IRule myRule(){
        return new RoundRobin_Maple();
    }
}

编写自己的算法,轮询访问,每台服务器访问3次

复制了随机算法的源代码,进行修改

package org.rule.define;

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.Random;

/**
 * @author mapleins
 * @Date 2019-01-12 22:05
 * @Desc 需求:轮询访问,每个服务访问3次
 **/
public class RoundRobin_Maple extends AbstractLoadBalancerRule {

    /**
     * 当前该服务器被访问的次数
     */
    private int total = 0 ;

    /**
     * 当前是哪台服务器
     */
    private int currentIndex = 0 ;

    public RoundRobin_Maple() {
    }

    public Server choose(ILoadBalancer lb, Object key) {
        if (lb == null) {
            return null;
        } else {
            Server server = null;

            while(server == null) {
                if (Thread.interrupted()) {
                    return null;
                }

                List<Server> upList = lb.getReachableServers();
                List<Server> allList = lb.getAllServers();
                int serverCount = allList.size();
                if (serverCount == 0) {
                    return null;
                }

                if(currentIndex < upList.size()){ //当前服务器的index<节点数
                    if(total < 3){
                        total++;
                    }else {
                        currentIndex++;
                        total = 0;
                        continue;
                    }
                }else {
                    currentIndex = 0;
                    total = 0;
                    continue;
                }

                server = (Server)upList.get(currentIndex);
                if (server == null) {
                    Thread.yield();
                } else {
                    if (server.isAlive()) {
                        return server;
                    }

                    server = null;
                    Thread.yield();
                }
            }

            return server;
        }
    }

    public Server choose(Object key) {
        return this.choose(this.getLoadBalancer(), key);
    }

    public void initWithNiwsConfig(IClientConfig clientConfig) {
    }

}

猜你喜欢

转载自www.cnblogs.com/mapleins/p/10261348.html