Python 官方文档:入门教程 => 点击学习
目录一、Ribbon简介1、什么是Ribbon2、Ribbon能干什么二、使用Ribbon1、客户端导入依赖2、application.yml配置3、Controller配置4、Co
spring cloud Ribbon 是基于Netflix Ribbon 实现的一套客户端负载均衡的工具,它可以很好地控制Http和tcp客户端的行为。
简单的说,Ribbon 是 Netflix 发布的开源项目,主要功能是提供客户端的软件负载均衡算法,将 Netflix 的中间层服务连接在一起。Ribbon 的客户端组件提供一系列完整的配置项,如:连接超时、重试等。简单的说,就是在配置文件中列出 LoadBalancer (简称LB:负载均衡) 后面所有的及其,Ribbon 会自动的帮助你基于某种规则 (如简单轮询,随机连接等等) 去连接这些机器。我们也容易使用 Ribbon 实现自定义的负载均衡算法!
其中lvs是中国技术专家章文嵩发明的
负载均衡简单分类:
即在服务的提供方和消费方之间使用独立的LB设施,如Nginx(反向代理服务器),由该设施负责把访问请求通过某种策略转发至服务的提供方!
将LB逻辑集成到消费方,消费方从服务注册中心获知有哪些地址可用,然后自己再从这些地址中选出一个合适的服务器。 Ribbon就属于进程内LB,它只是一个类库,集成于消费方进程,消费方通过它来获取到服务提供方的地址!
<!--引入Eureka的依赖-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!--引入ribbon-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
</dependencies>
server:
port: 801eureka:
client:
reGISter-with-eureka: false #不向eureka注册自己
service-url:
defaultZone: http://localhost:7001/eureka/ #去哪个地方获取
和前面两节不一样的是,用Ribbon做负载均衡,地址不能写死,也就是不能和前面的一样写成一个具体的值如:localhost:8001,而是这个微服务的名字,也就是这个名字,如下。
package com.you.controller;
import com.you.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.WEB.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
@ResponseBody
public class DeptComsumerController {
@Autowired
RestTemplate restTemplate;
// public static final String REST_URL_PREFIX = "http://localhost:8001";
public static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-DEPT";
@GetMapping("/consumer/dept/getDept/{id}")
public Dept getDeptOfId(@PathVariable("id") Long id)
{
System.out.println(REST_URL_PREFIX+"/dept"+"/aDept/"+id);
return restTemplate.getForObject(REST_URL_PREFIX + "/dept" + "/aDept/"+id, Dept.class);
}
}
在此文件里,增加了@LoadBalanced注解,该注解的作用是让RestTemplate有了负载均衡的能力,而且默认的负载均衡算法是轮询(也就是一个一个的尝试),可以使用系统配备的负载均衡算法,也可以自己写自己的负载均衡算法。
package com.you.config;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ConfigBean {
@Bean
@LoadBalanced //ribbon
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
package com.you;
import com.tan.tanRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "SPRINGCLOUD-PROVIDER-DEPT",configuration = tanRule.class)
public class DeptConsumer_80 {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer_80.class,args);
}
}
为了实现负载均衡,扩充一下服务提供者,将原来的一个服务提供者,改为三个。
访问consumer_80配置的Getmapping地址,然后不断的刷新,会看到依次访问三个数据库,并且一直重复,这就是默认的负载均衡算法:轮询
@RibbonClient()注释的应用,在psvm上面添加该注释,其具体内容为@RibbonClient(name = “SPRINGCLOUD-PROVIDER-DEPT”,configuration = tanRule.class),其中name的值即为微服务的名字,configuration的值对应的就是自己写的路由类
需要注意,自定义的路由类,不可以用启动类放在同一目录,一般要比启动类高一级目录,放在同一目录下,需要配置CompentScan。
package com.tan;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.context.annotation.Bean;
public class tanRule {
@Bean
public IRule myRule()
{
return new tanRandomRule();
}
}
可以模仿系统中的负载均衡算法,撰写自己的负载均衡算法,如下面的例子即为:每个端口的提供者访问5次,然后切换下一个端口,全部访问完成后则重新开始,代码如下:
package com.tan;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public class tanRandomRule extends AbstractLoadBalancerRule {
int total = 0;
int currentIndex = 0;
public tanRandomRule() {
}
@SuppressWarnings({"RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE"})
public Server choose(ILoadBalancer lb, Object key) {
if (lb == null) {
return null;
} else {
Server server = null;
while(server == null) {
if (Thread.interrupted()) {
return null;
}
List<Server> upList = lb.getReachableServers();
List<Server> allList = lb.getAllServers();
if(total<5)
{
server = (Server)upList.get(currentIndex);
total++;
}else{
total = 0;
currentIndex++;
if(currentIndex>2)
{
currentIndex = 0;
}
server = (Server)upList.get(currentIndex);
}
System.out.println("CurrentIndex:"+currentIndex);
System.out.println("Total:"+total);
System.out.println("sever 的值是:"+server);
if (server == null) {
Thread.yield();
} else {
if (server.isAlive()) {
return server;
}
server = null;
Thread.yield();
}
}
return server;
}
}
protected int chooseRandomInt(int serverCount) {
return ThreadLocalRandom.current().nextInt(serverCount);
}
public Server choose(Object key) {
return this.choose(this.getLoadBalancer(), key);
}
public void initWithNiwsConfig(IClientConfig clientConfig) {
}
}
到此这篇关于SpringCloud Netflix Ribbon超详细讲解的文章就介绍到这了,更多相关SpringCloud Netflix Ribbon内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: SpringCloudNetflixRibbon超详细讲解
本文链接: https://www.lsjlt.com/news/170438.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0