Ribbon is a client side IPC library that is battle-tested in cloud. It provides the following features
Load balancing
Fault tolerance
Multiple protocol (HTTP, TCP, UDP) support in an asynchronous and reactive model
Caching and batching
创建ribbon测试项目
项目结构如图
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhang</groupId>
<artifactId>ribbontest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ribbontest</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
属性配置文件application.properties
server.port=7071代表RibbontestApplication暴露在7071端口
spring.application.name=ribbon
server.port=7071
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/servicehello.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
暴露服务调用接口
package com.zhang.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* Created by zhangxiaolong on 17/7/20.
*/
@RestController
public class ConsumerClass {
@Autowired
private RestTemplate restTemplate;
@Autowired
private LoadBalancerClient loadBalancerClient;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String hello(@RequestParam String name) {
this.loadBalancerClient.choose("service-hello");//随机访问策略
return restTemplate.getForEntity("http://service-hello/hello?name="+name, String.class).getBody();
}
}
应用程序运行入口
package com.zhang;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
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 RibbontestApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
@Bean
public IRule ribbonRule() {
return new RandomRule();
}
public static void main(String[] args) {
SpringApplication.run(RibbontestApplication.class, args);
}
}
创建service-hello服务
使用eureka作为注册中心,暴露在端口8761
启动两个service-hello服务,分别暴露在8762和8763端口
可以看到service-hello服务,Availability Zones数量是2,也就是有两个实例在提供服务。
8762提供服务代码
@RequestMapping("hello")
public String hello(@RequestParam String name){
return "hello "+name+",from service provider 1";
}
8763提供服务代码
@RequestMapping("hello")
public String hello(@RequestParam String name){
return "hello "+name+",from service provider 2";
}
测试和验证
1 启动RibbontestApplication中的main函数
2 浏览器中输入
http://172.16.153.1:7071/hello?name=%E5%BC%A0%E5%B0%8F%E9%BE%99
结论
1 ribbon可以作为eureka服务的客户端,可以调通注册在eureka服务器上的服务
2 ribbon天生带着负载均衡的支持,负载策略可配置。
RibbontestApplication中ribbonRule方法返回的RandomRule即是随机策略。
其他负载策略
负载策略 | 备注 |
---|---|
RoundRobinRule | 轮询策略 |
AvailabilityFilteringRule | 可用性过滤策略,可用性越强优先 |
WeightedResponseTimeRule | 响应时间权重策略,响应时间越长,权重越小 |