负载均衡框架 ribbon 一
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 一的更多相关文章
- 负载均衡框架 ribbon 二
Ribbon 负载均衡机制 官方文档地址:https://github.com/Netflix/ribbon/wiki/Working-with-load-balancers 1. Ribbon 内置 ...
- API网关spring cloud gateway和负载均衡框架ribbon实战
通常我们如果有一个服务,会部署到多台服务器上,这些微服务如果都暴露给客户,是非常难以管理的,我们系统需要有一个唯一的出口,API网关是一个服务,是系统的唯一出口.API网关封装了系统内部的微服务,为客 ...
- 负载均衡框架 ribbon 三
Ribbon 在 SpringCloud 中的使用 1.构建 Eureka 注册中心 smart-platform-eureka1 (1)导入jar包 <properties> <p ...
- 客户端负载均衡框架:Spring Cloud Ribbon
最近在学习Spring Cloud的知识,现将客户端负载均衡框架 Spring Cloud Ribbon 的相关知识笔记整理如下.[采用 oneNote格式排版]
- Spring Cloud官方文档中文版-客户端负载均衡:Ribbon
官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_spring_cloud_netflix 文中例子我做了一些测试在:h ...
- 【Spring Cloud】客户端负载均衡组件——Ribbon(三)
一.负载均衡 负载均衡技术是提高系统可用性.缓解网络压力和处理能力扩容的重要手段之一. 负载均衡可以分为服务器负载均衡和客户端负载均衡,服务器负载均衡由服务器实现,客户端只需正常访问:客户端负载均衡技 ...
- SpringCloud系列之客户端负载均衡Netflix Ribbon
1. 什么是负载均衡? 负载均衡是一种基础的网络服务,它的核心原理是按照指定的负载均衡算法,将请求分配到后端服务集群上,从而为系统提供并行处理和高可用的能力.提到负载均衡,你可能想到nginx.对于负 ...
- Spring Cloud之负载均衡组件Ribbon原理分析
目录 前言 一个问题引发的思考 Ribbon的简单使用 Ribbon 原理分析 @LoadBalanced 注解 @Qualifier注解 LoadBalancerAutoConfiguration ...
- SpringCloud 客户端负载均衡:Ribbon
目录 Ribbon 介绍 开启客户端负载均衡,简化 RestTemplate 调用 负载均衡策略 Ribbon 介绍 Ribbon 是 Netflix 提供的一个基于 Http 和 TCP 的客户端负 ...
随机推荐
- Normally Distributed|
6.1Introducing Normally Distributed Variables Why the word “normal”? Because, in the last half of th ...
- chkconfig原理
ll /etc/rc.d 里面有运行级别对应的脚本 chkconfig --list sshd ll /etc/rc.d/rc3.d/ | grep sshd (查看3启动 里面 ...
- Angular开发者指南(七)依赖注入
依赖注入 依赖注入(DI)是一种软件设计模式,处理组件如何获取其依赖关系. AngularJS注入器子系统负责创建组件,解析它们的依赖关系,并根据请求将它们提供给其他组件. 使用依赖注入 DI遍布An ...
- python模块之shelve,xml,hashlib,configpaser
shelve shelve模块也是一种可以将数据序列化的模块 使用方法 1. open 2. 读写 3. close 特点:使用方法比较简单 提供一个文件名字就可以开始读写,读写的方法和字典一致;跨平 ...
- SpringMVC静态资源拦截的问题
通常在web.xml中的核心控制器的DispatcherServlet中的url-pattern属性配置成类似“/”的拦截路径,但是会出现静态资源找不到的问题,比如js脚本.图片.css等无法加载,那 ...
- .net core && python
最近.net core的发展,确实值得激动,强力推荐传教文章<.NET:持续进化的统一开发平台>http://www.cnblogs.com/wer-ltm/p/8776846.html ...
- Java POI导出Excel不弹框选择下载路径(下载文件不选择下载路径,默认) Chrome
在Chrome浏览器中,Java导出Excel文件时,浏览器弹出提示框,需要选择下载路径 在Chrome中的高级设置中,把“下载前询问每个文件的保存位置”去掉就解决了 DEEPLOVE(LC)
- com.mysql.jdbc.exceptions.jdbc4.MySQLDataException: '2.34435678977654336E17' in column '3' is outside valid range for the datatype INTEGER.
### Error querying database. Cause: java.lang.reflect.UndeclaredThrowableException### The error may ...
- python ATM项目
1.需求: 指定最大透支额度 可取款 定期还款(每月指定日期还款,如15号) 可存款 定期出账单 支持多用户登陆,用户间转帐 支持多用户 管理员可添加账户.指定用户额度.冻结用户等 购物车: 商品信息 ...
- SIM卡的消失会让运营商们恐慌吗?
中国移动.联通.电信三大运营商原本高高在上,每天乐滋滋地数钱数到手抽筋,但近年来移动互联网的快速普及,让运营商的制霸状态不复存在.成为众多互联网公司的"流量通道",语音.短信等业 ...