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. 多版本firefox共存

    https://blog.csdn.net/gavinxlf/article/details/53173034 https://blog.csdn.net/weixin_33979745/articl ...

  2. tomcat部署项目方式

    三大部署方式1.   Context描述文件部署通过独立的Context文件描述清楚项目的访问路径和地址,tomcat在启动的时候会解析这个Context文件,创建一个Context对象. Conte ...

  3. vue-cli多页面应用常遇到的问题

    1.TypeError: webpack.optimize.OccurenceOrderPlugin is not a constructor 此问题出现在webpack 3中,解决办法很简单,将oc ...

  4. JS数组与字符串相互转化

    <script type="text/javascript"> var obj="new1abcdefg".replace(/(.)(?=[^$]) ...

  5. documentFragment深入理解

    documentFragment是一个保存多个element的容器对象(保存在内存)当更新其中的一个或者多个element时,页面不会更新.只有当documentFragment容器中保存的所有ele ...

  6. js Array 中的 map, filter 和 reduce

    原文中部分源码来源于:JS Array.reduce 实现 Array.map 和 Array.filter Array 中的高阶函数 ---- map, filter, reduce map() - ...

  7. Docker学习笔记_04 Rancher的部署安装(编排选用K8S)

    原文地址:http://dbase.cc/2018/01/12/docker/04_rancher的部署安装/ 为什么要使用Rancher Rancher是一个开源的企业级容器管理平台.通过Ranch ...

  8. 吴裕雄--天生自然 python语言数据分析:开普勒系外行星搜索结果分析

    import pandas as pd pd.DataFrame({'Yes': [50, 21], 'No': [131, 2]}) pd.DataFrame({'Bob': ['I liked i ...

  9. centos7上Jenkins通过rpm包方式直接安装及使用war包方式升级

    一.通过rpm包方式直接安装jenkins 1.官网下载rpm安装包(前提是安装jdk) wget https://pkg.jenkins.io/redhat-stable/jenkins-2.121 ...

  10. Hexo和github搭建个人博客 - 朱晨

    GitHub账号 mac/pc 环境 12 node.jsgit 创建GitHub仓库 登陆GitHub,创建一个新的Respository Repository name叫做{username}.g ...