负载均衡框架 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 的客户端负 ...
随机推荐
- linux下java调用C
linux下java调用C 分类: linux2012-05-22 09:12 1529人阅读 评论(0) 收藏 举报 javalinuxmakefilegccclasscommand 下面是在ubu ...
- 03-kubeadm初始化Kubernetes集群
请求地址https://pc-shop.xiaoe-tech.com/appc7XFLu4K9234/video_details?id=v_5b615b9e432f5_DXDGopmB
- Java IO: 其他字符流(下)
作者: Jakob Jenkov 译者: 李璟(jlee381344197@gmail.com) 本小节会简要概括Java IO中的PushbackReader,LineNumberReader,St ...
- try中定义的变量在finally中找不到
凡是代码块中的变量,作用域都只在代码块中 https://blog.csdn.net/qq_20936333/article/details/81062966 问题: 解决:
- Spring第一课:IOC控制反转,什么是反转,什么又是控制?
前言 学习Spring第一课,就是认识IOC控制反转,要了解它还真得花一些功夫.今天主要理解透彻它的真谛,而不仅限于表面. 上道小菜 public class BusinessService { pr ...
- shell-变量学习-01
1.变量 1.1 变量赋值 > variable_zhou="hello world!" #等号两边不能有空格 1.2 使用变量 > echo $variabl ...
- 吴裕雄--python学习笔记:爬虫包的更换
python 3.x报错:No module named 'cookielib'或No module named 'urllib2' 1. ModuleNotFoundError: No module ...
- 吴裕雄--天生自然 R语言开发学习:高级数据管理(续三)
#-----------------------------------# # R in Action (2nd ed): Chapter 5 # # Advanced data management ...
- 吴裕雄--天生自然 R语言开发学习:功效分析
#----------------------------------------# # R in Action (2nd ed): Chapter 10 # # Power analysis # # ...
- MicrosoftOfficeProfessionalPlus2013傻瓜式激活工具
用微软的office系列,总是提示需要输入秘钥,直接找个破解软件破解算了. 破解软件地址:http://www.3322.cc/soft/10037.html 1.下载解压: 2.点击office系列 ...