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

}

原文地址:https://www.cnblogs.com/mapleins/p/10261348.html

时间: 2024-07-31 13:27:21

<Spring Cloud>入门三 Ribbon的相关文章

spring cloud: zuul(三): ribbon负载均衡配置

zuul的routes配置下path/url组合不支持负载均衡 下面介绍zuul的routes配置下的path/serviceId负载均衡配置 spring-boot-user微服务开启了:7901,7902两个服务 zuul服务 eureka服务 看配置: zuul: routes: myroute1: #配置的路由名 path: /user/** #访问路径 serviceId: spring-boot-user #配置路由的微服务名 ribbon: eureka: enable: fals

Spring Cloud 入门教程(五): Ribbon实现客户端的负载均衡

接上节,假如我们的Hello world服务的访问量剧增,用一个服务已经无法承载, 我们可以把Hello World服务做成一个集群. 很简单,我们只需要复制Hello world服务,同时将原来的端口8762修改为8763.然后启动这两个Spring Boot应用, 就可以得到两个Hello World服务.这两个Hello world都注册到了eureka服务中心.这时候再访问http://localhost:8761, 可以看到两个hello world服务已经注册.(服务与注册参见Spr

Spring Cloud 入门教程(三): 配置自动刷新

之前讲的配置管理, 只有在应用启动时会读取到GIT的内容, 之后只要应用不重启,GIT中文件的修改,应用无法感知, 即使重启Config Server也不行. 比如上一单元(Spring Cloud 入门教程(二): 配置管理)中的Hello World 应用,手动更新GIT中配置文件config-client-dev.properties的内容(别忘了用GIT push到服务器) hello=Hello World from GIT version 1 刷新 http://locahost/8

Spring Cloud 入门教程(六): 用声明式REST客户端Feign调用远端HTTP服务

首先简单解释一下什么是声明式实现? 要做一件事, 需要知道三个要素,where, what, how.即在哪里( where)用什么办法(how)做什么(what).什么时候做(when)我们纳入how的范畴. 1)编程式实现: 每一个要素(where,what,how)都需要用具体代码实现来表示.传统的方式一般都是编程式实现,业务开发者需要关心每一处逻辑 2)声明式实现: 只需要声明在哪里(where )做什么(what),而无需关心如何实现(how).Spring的AOP就是一种声明式实现,

Spring Cloud 入门教程(八): 断路器指标数据监控Hystrix Dashboard

1. Hystrix Dashboard (断路器:hystrix 仪表盘)  Hystrix一个很重要的功能是,可以通过HystrixCommand收集相关数据指标. Hystrix Dashboard可以很高效的现实每个断路器的健康状况. 1). 在Ribbon服务g和Feign服务的Maven工程的pom.xml中都加入依赖 1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <art

Spring Cloud 入门教程(二): 配置管理

使用Config Server,您可以在所有环境中管理应用程序的外部属性.客户端和服务器上的概念映射与Spring Environment和PropertySource抽象相同,因此它们与Spring应用程序非常契合,但可以与任何以任何语言运行的应用程序一起使用.随着应用程序通过从开发人员到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用程序具有迁移时需要运行的一切.服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置环境,以及可以访问用于管理内容的各种工具.很容易添加

Spring Cloud 入门教程(四): 分布式环境下自动发现配置服务

前一章, 我们的Hello world应用服务,通过配置服务器Config Server获取到了我们配置的hello信息"hello world". 但自己的配置文件中必须配置config server的URL(http://localhost:8888), 如果把config server搬到另外一个独立IP上, 那么作为一个client的hello world应用必须修改自己的bootstrap.yml中的config server的URL地址.这明显是不够方便的. 既然confi

Spring Cloud 入门 之 Config 篇(六)

原文地址:Spring Cloud 入门 之 Config 篇(六) 博客地址:http://www.extlight.com 一.前言 随着业务的扩展,为了方便开发和维护项目,我们通常会将大项目拆分成多个小项目做成微服务,每个微服务都会有各自配置文件,管理和修改文件起来也会变得繁琐.而且,当我们需要修改正在运行的项目的配置时,通常需要重启项目后配置才能生效. 上述的问题将是本篇需要解决的问题. 二.介绍 2.1 简单介绍 Spring Cloud Config 用于为分布式系统中的基础设施和微

Spring Cloud:使用Ribbon实现负载均衡详解(下)

在上一篇文章(Spring Cloud:使用Ribbon实现负载均衡详解(上))中,我对 Ribbon 做了一个介绍,Ribbon 可以实现直接通过服务名称对服务进行访问.这一篇文章我详细分析一下如何使用 Ribbon 实现客户端的负载均衡. 1. 使用 Ribbon 实现负载均衡 要实现负载均衡,首先要有多个订单服务提供者,目前我们就一个 microservice-order-provider01,端口号 8001,我们可以仿照这个服务,再创建两个子模块,也是订单服务提供者,取名为 micro