1. 程式人生 > ><Spring Cloud>入門三 Ribbon

<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) {
    }

}