Ribbon开源地址:https://github.com/Netflix/ribbon/wiki/Getting-Started

1.Ribbon简介

 负载均衡框架,支持可插拔式的负载均衡规则
 支持多种协议,如HTTP, UDP等
 提供负载均衡客户端

 2.Ribbon 负载均衡器组件

 一个负载均衡器,至少要提供一下功能:
    要维护各个服务器的IP等信息
    根据特定的逻辑选取服务器

 为了实现基本的负载均衡功能,Ribbon的负载均衡器有三大子模块
  1.Rule
  2.Ping
  3.ServerList

3. 编写ribbon 接口服务 ribbon-server-test

(1)导入jar包

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies> <build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

(2)编写测试接口

public class Person {

    private Integer id;

    private String name;

    private String message;

    public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
}
}
@RestController
public class TestController { @GetMapping(value = "/person")
public Person getPerson (HttpServletRequest request) {
Person person = new Person();
person.setId(1);
person.setName("delan");
person.setMessage(request.getRequestURL().toString());
return person;
}
}

(3)编写启动类(由于要做负载均衡,此处通过动态输入端口,来开启两个服务)

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder; import java.util.Scanner; @SpringBootApplication
public class Application {
public static void main( String[] args ) {
Scanner scan = new Scanner(System.in);
System.out.println("请输入端口号:");
String port = scan.nextLine();
new SpringApplicationBuilder(Application.class).properties("server.port="+port).run(args);
}
}

4. 编写ribbon 客户端 ribbon-client-test

(1)导入jar包

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon-core</artifactId>
<version>2.3.0</version>
</dependency> <dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon-httpclient</artifactId>
<version>2.3.0</version>
</dependency> <!--官方文档中 未导入一下包 测试发现编写代码找不到 com.netflix.config.ConfigurationManage-->
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.10</version>
</dependency> <dependency>
<groupId>com.netflix.ribbon</groupId>
<artifactId>ribbon-loadbalancer</artifactId>
<version>2.3.0</version>
</dependency> <dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency> <dependency>
<groupId>com.netflix.archaius</groupId>
<artifactId>archaius-core</artifactId>
<version>0.7.6</version>
</dependency>
</dependencies> <build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration> <!--热启动配置-->
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

(2)编写ribbon客户端测试类

import com.netflix.client.ClientFactory;
import com.netflix.client.http.HttpRequest;
import com.netflix.client.http.HttpResponse;
import com.netflix.config.ConfigurationManager;
import com.netflix.niws.client.http.RestClient; import java.net.URI; public class TestRibbon {
public static void main(String[] args) throws Exception{
//通过properties配置文件导入配置信息
ConfigurationManager.loadPropertiesFromResources("my-ribbon.properties"); //通过java代码导入配置信息
// ConfigurationManager.getConfigInstance().setProperty(
// "my-client.ribbon.listOfServers", "localhost:8080, localhost:8081"); RestClient client = (RestClient) ClientFactory.getNamedClient("my-client");
HttpRequest request = HttpRequest.newBuilder().uri(new URI("/person")).build(); //
for (int i = 0; i < 10; i++) {
HttpResponse response = client.executeWithLoadBalancer(request); //
System.out.println("返回结果为 Status code for " + response.getRequestedURI() + " :" + response.getStatus()+"内容:"+response.getEntity(String.class));
}
}
}

(3)my-ribbon.properties

#指定my-client 客户端的服务地址,如果不写my-client则对所有的ribbon客户端生效
my-client.ribbon.listOfServers = localhost:8080, localhost:8081

负载均衡框架 ribbon 一的更多相关文章

  1. 负载均衡框架 ribbon 二

    Ribbon 负载均衡机制 官方文档地址:https://github.com/Netflix/ribbon/wiki/Working-with-load-balancers 1. Ribbon 内置 ...

  2. API网关spring cloud gateway和负载均衡框架ribbon实战

    通常我们如果有一个服务,会部署到多台服务器上,这些微服务如果都暴露给客户,是非常难以管理的,我们系统需要有一个唯一的出口,API网关是一个服务,是系统的唯一出口.API网关封装了系统内部的微服务,为客 ...

  3. 负载均衡框架 ribbon 三

    Ribbon 在 SpringCloud 中的使用 1.构建 Eureka 注册中心 smart-platform-eureka1 (1)导入jar包 <properties> <p ...

  4. 客户端负载均衡框架:Spring Cloud Ribbon

    最近在学习Spring Cloud的知识,现将客户端负载均衡框架 Spring Cloud Ribbon 的相关知识笔记整理如下.[采用 oneNote格式排版]

  5. Spring Cloud官方文档中文版-客户端负载均衡:Ribbon

    官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_spring_cloud_netflix 文中例子我做了一些测试在:h ...

  6. 【Spring Cloud】客户端负载均衡组件——Ribbon(三)

    一.负载均衡 负载均衡技术是提高系统可用性.缓解网络压力和处理能力扩容的重要手段之一. 负载均衡可以分为服务器负载均衡和客户端负载均衡,服务器负载均衡由服务器实现,客户端只需正常访问:客户端负载均衡技 ...

  7. SpringCloud系列之客户端负载均衡Netflix Ribbon

    1. 什么是负载均衡? 负载均衡是一种基础的网络服务,它的核心原理是按照指定的负载均衡算法,将请求分配到后端服务集群上,从而为系统提供并行处理和高可用的能力.提到负载均衡,你可能想到nginx.对于负 ...

  8. Spring Cloud之负载均衡组件Ribbon原理分析

    目录 前言 一个问题引发的思考 Ribbon的简单使用 Ribbon 原理分析 @LoadBalanced 注解 @Qualifier注解 LoadBalancerAutoConfiguration ...

  9. SpringCloud 客户端负载均衡:Ribbon

    目录 Ribbon 介绍 开启客户端负载均衡,简化 RestTemplate 调用 负载均衡策略 Ribbon 介绍 Ribbon 是 Netflix 提供的一个基于 Http 和 TCP 的客户端负 ...

随机推荐

  1. 堆排Heap Sort

    1. #define LeftChild(i) (2*(i)+1) void PercDown(vector<int>&num, int i, int n) { int child ...

  2. pycharm2018后版本执行Flask app.run()深坑

    在2018年以前的版本,以上配置在app.run()里面的内置方法

  3. K3CLOUD表关联

    销售订单关联发货通知单 销售订单表 T_SAL_ORDER A T_SAL_ORDERENTRY B T_SAL_ORDERENTRY_LK C 发货通知单表 T_SAL_DELIVERYNOTICE ...

  4. 百度AI技术

    利用百度提供接口,实现智能语音 语音合成 -- TTS(text to speech) 注册 在 ai.baidu.com 页面中点击 控制台 ,弹出登陆 / 注册页面 创建应用 登陆成功后,点击左侧 ...

  5. VSAN磁盘扩容与收缩

    删除闪存盘后,整个磁盘组都会被删除 假如一个磁盘组里面只有一块HDD盘,删除此HDD盘,磁盘组也只接被删除 可以整体删除整个磁盘组

  6. Qt QString类及常用函数功能详解

    QString 是 Qt 编程中常用的类,除了用作数字量的输入输出之外,QString 还有很多其他功能,熟悉这些常见的功能,有助于灵活地实现字符串处理功能. QString 存储字符串釆用的是 Un ...

  7. java String、StringBuilder

    Java中的String和StringBuilder类: 1.String对象是不可变的.每一个看起来修改了String值的方法,实际上都是创建了全新的String对象.代码示例如下: String ...

  8. 吴裕雄--天生自然HTML学习笔记:HTML 图像

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. [洛谷P3403] 跳楼机

    题目传送门 套路题,同余最短路. 先只考虑y.z进行连边,再在mod x的意义下进行计算. 这里的“距离”dis[i]指的是,在所有满足a mod x=i的a里,能到达的最小的a是多少. 显然只要能到 ...

  10. Ionic3学习笔记(九)关于 Android 端软键盘弹出后界面被压缩的问题

    本文为原创文章,转载请标明出处 今天做了一个如下图所示的页面. iOS 端毫无 bug,Android 端却出现了问题.当软键盘弹出后,Android 端的 tabs 移到了软键盘的上面,再仔细一看, ...