二、SpringCloud之Ribbon负载均衡

本文介绍了Ribbon作为Netflix的客户端负载均衡器,用于在服务消费者端选择服务提供者。首先解释了Ribbon的基本概念和工作原理,接着通过步骤详细展示了如何搭建Eureka服务端和生产者服务,以及创建消费者服务并使用Ribbon实现轮询负载均衡。最后,讨论了如何修改负载均衡策略,例如切换到随机策略,并提供了相应的配置方法。
摘要由CSDN通过智能技术生成

一、Ribbon介绍

Ribbon是Netflix发布的开源项⽬,主要功能是提供客户端的软件负载均衡算 法,将Netflix的中间层服务连接在⼀起。

Ribbon客户端组件提供⼀系列完善的配置项如连接超时,重试等。简单的说,就是在配置⽂件中列出Load Balancer后⾯所有的机器,Ribbon会⾃动的帮助你基于某种规则(如简单轮 询,随机连接等)去连接这些机器。

我们也很容易使⽤Ribbon实现⾃定义的负载均衡算法。简单地说,Ribbon是⼀个客户端负载均衡器。

Ribbon⼯作时分为两步:

第⼀步先选择 Eureka Server, 它优先选择在同⼀个 Zone且负载较少的Server;

第⼆步再根据⽤户指定的策略,在从Server取到的服务注册列表中选择⼀个地址。其中Ribbon提供了多种策略,例如轮询、 随机、根据响应时间加权等。

二、Ribbon搭建

2.1、搭建Eureka服务端

新建springboot项目neil-eureka-server。

在pom.xml里面添加Eureka的依赖

	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
	</dependency>

配置文件application.yml

server:
  port: 8989

spring:
  application:
    name: neil-eureka-server

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8989/eureka/
    fetch-registry: true
    register-with-eureka: true
  instance:
    prefer-ip-address: true

springboot启动类里面添加@EnableEurekaServer注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class NeilEurekaServerApplication {

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

启动neil-eureka-server

在浏览器地址栏输入机器ip加端口访问Eureka页面
http://127.0.0.1:8989/

请添加图片描述

2.2、创建生产者服务

新建springboot项目neil-producer-server作为服务提供者。

在pom.xml里面添加Eureka client和web的依赖

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

配置文件我们创建2个,用来启动2个生产者服务,端口设置不一样

application.yml

server:
  port: 8991
  servlet:
    context-path: /
spring:
  application:
    name: neil-producer-server
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8989/eureka/ #Eureka Server地址
  instance:
    prefer-ip-address: true  #Hostname使用主机ip

application-dev.yml

server:
  port: 8992
  servlet:
    context-path: /
spring:
  application:
    name: neil-producer-server
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8989/eureka/ #Eureka Server地址
  instance:
    prefer-ip-address: true  #Hostname使用主机ip

springboot启动类里面添加@EnableEurekaServer注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@SpringBootApplication
public class ProducerApplication {

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

}

创建一个controller类

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProducerController {

    @RequestMapping(value = "/hello")
    @ResponseBody
    public String hello(){
        System.out.println("producer服务调用成功");
        return "producer调用";
    }
}

在idea里面添加一个启动配置,用来启动application-dev.yml配置的服务
请添加图片描述

添加配置,Main class就是你的启动类,直接复制上面那个ProductApplication配置里的就行了。Active Profilesdev,跟配置文件相对应就行application-dev.yml

请添加图片描述

然后分别启动2个服务

请添加图片描述
请添加图片描述

再访问Eureka页面,能看到2个服务已经注册上了
http://127.0.0.1:8989/

请添加图片描述

2.3、消费者创建

上面服务起好后,我们正式开始ribbon的实现,来调用2个生产者服务,实现负载均衡

创建springboot项目neil-producer-server

在pom.xml里面添加Eureka client和web的依赖

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

配置文件application.yml

server:
  port: 8993
  servlet:
    context-path: /
spring:
  application:
    name: neil-ribbon-server
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8989/eureka/ #Eureka Server地址
  instance:
    prefer-ip-address: true  #Hostname使用主机ip

springboot启动类里面添加@EnableEurekaServer注解,并且添加RestTemplate的Bean开启ribbon

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;

@EnableEurekaClient
@SpringBootApplication
public class NeilRibbonServerApplication {
	
	@Bean
	@LoadBalanced //开启负载均衡
	public RestTemplate getRestTemplate(){
		return new RestTemplate();
	}
	
	public static void main(String[] args) {
		SpringApplication.run(NeilRibbonServerApplication.class, args);
	}
}

创建一个controller来调用生产者服务;因为服务都注册到了Eureka上,我们调用服务的时候只需填写服务名加接口就行了http://neil-producer-server/get

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class RibbonController {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("/ribbon")
    @ResponseBody
    public String test(){
        return restTemplate.getForObject("http://neil-producer-server/get",String.class);
    }

}

启动neil-ribbon-server,并注册到了Eureka上

请添加图片描述

2.4、测试

我们直接在浏览器调用neil-ribbon-server上的接口,通过neil-ribbon-server服务去轮询调用2个生产者服务neil-producer-server。

浏览器访问ribbon服务接口:
http://127.0.0.1:8993/ribbon

请添加图片描述

显示调用成功了。我们多调用几次,然后查看producer服务的日志。

请添加图片描述
请添加图片描述

2个producer服务都有被调用,这个调用是轮询的。

2.5、修改负载均衡策略

如果想要修改负载均衡策略,Ribbon有7种策略可供选择,也支持自定义负载策略。

策略备注
RoundRobinRule轮询策略
RandomRule随机策略
AvailabilityFilteringRule可用过滤策略
RetryRule重试策略
BestAvailableRule最低并发策略
ResponseTimeWeightedRule响应时间加权重策略
ZoneAvoidanceRule区域权重策略

下面我们使用下随机策略:

在neil-ribbon-server的pom.xml中添加loadbalancer依赖

		<dependency>
			<groupId>com.netflix.ribbon</groupId>
			<artifactId>ribbon-loadbalancer</artifactId>
			<version>2.3.0</version>
		</dependency>

在neil-ribbon-server的启动类NeilRibbonServerApplication创建IRule的Bean

	@Bean
	public IRule iRule(){
		return new RandomRule();
	}

请添加图片描述

然后再启动neil-ribbon-server进行测试,上面7种策略可自行测试。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值