一、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 Profiles
填dev
,跟配置文件相对应就行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种策略可自行测试。