当前位置: 首页 > news >正文

长沙做网站建设公司互联网销售

长沙做网站建设公司,互联网销售,蓟县网站建设,网站开发代理商官网地址:https://docs.spring.io/spring-cloud-commons/reference/spring-cloud-commons/loadbalancer.html 【1】概述 Spring Cloud LoadBalancer是由SpringCloud官方提供的一个开源的、简单易用的客户端负载均衡器,它包含在SpringCloud-commons中用…

官网地址:https://docs.spring.io/spring-cloud-commons/reference/spring-cloud-commons/loadbalancer.html

【1】概述

Spring Cloud LoadBalancer是由SpringCloud官方提供的一个开源的、简单易用的客户端负载均衡器,它包含在SpringCloud-commons中用它来替换了以前的Ribbon组件。相比较于Ribbon,SpringCloud LoadBalancer不仅能够支持RestTemplate,还支持WebClient(WeClient是Spring Web Flux中提供的功能,可以实现响应式异步请求)

LB负载均衡(Load Balance)是什么
简单的说就是将用户的请求平摊的分配到多个服务上,从而达到系统的HA(高可用),常见的负载均衡有软件Nginx,LVS,硬件 F5等

loadbalancer本地负载均衡,在调用微服务接口时候,会在注册中心上获取注册信息服务列表之后缓存到JVM本地,从而在本地实现RPC远程服务调用技术。

在这里插入图片描述
LoadBalancer 在工作时分成两步:

  • 第一步,先选择ConsulServer从服务端查询并拉取服务列表,知道了它有多个服务(上图3个服务),这3个实现是完全一样的,默认轮询调用谁都可以正常执行。

  • 第二步,按照指定的负载均衡策略从server取到的服务注册列表中由客户端自己选择一个地址,所以LoadBalancer是一个客户端的负载均衡器。


Spring Cloud LoadBalancer 集成使用

为了简化Spring Cloud LoadBalancer的使用,Spring Cloud 提供了ReactorLoadBalancerExchangeFilterFunction(可与WebClient配合使用)和BlockingLoadBalancerClient(可与RestTemplateRestClient一起工作)。更多详细信息及使用示例,请参阅以下章节:

  1. Spring RestTemplate作为LoadBalancer客户端
    在这一节中,我们将探讨如何将RestTemplate用作负载均衡客户端,利用BlockingLoadBalancerClient实现服务间的调用负载均衡。

  2. Spring RestClient作为LoadBalancer客户端
    这一章节会介绍RestClientBlockingLoadBalancerClient结合使用的方法,展示如何通过这种方式实现对后端服务的负载均衡调用。

  3. Spring WebClient作为LoadBalancer客户端
    在此部分,我们将讲解如何将WebClientReactorLoadBalancerExchangeFilterFunction配合使用,以实现非阻塞、响应式的负载均衡调用。

  4. Spring WebFlux WebClient与ReactorLoadBalancerExchangeFilterFunction的结合使用
    本章节将深入讨论WebFlux框架下的WebClient如何与ReactorLoadBalancerExchangeFilterFunction协同工作,实现高效、响应式的负载均衡机制。

两种主要的工具:ReactorLoadBalancerExchangeFilterFunction 和 BlockingLoadBalancerClient,分别适用于响应式编程和阻塞式编程场景。

【2】Spring RestTemplate as a LoadBalancer Client应用实践

pom依赖:

<!--loadbalancer-->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

你可以配置一个RestTemplate来使用负载均衡客户端。要创建一个负载均衡的RestTemplate,需要创建一个带有@Bean注解的RestTemplate实例,并使用@LoadBalanced限定符,如下例所示:

@Configuration
public class MyConfiguration {@LoadBalanced@BeanRestTemplate restTemplate() {return new RestTemplate();}
}public class MyClass {@Autowiredprivate RestTemplate restTemplate;public String doOtherStuff() {String result = restTemplate.getForObject("http://stores/stores", String.class);return result;}
}

这意味着,在你的Spring配置类中,可以通过定义一个带有@Bean@LoadBalanced注解的方法来生成一个RestTemplate实例。这样做的结果是,该RestTemplate实例将自动集成Spring Cloud LoadBalancer的功能,能够智能地在多个服务实例之间进行请求分发,实现负载均衡的效果。这种方法特别适用于基于Spring的微服务架构,使得服务间的调用更加健壯和高效。

如下图所示,当服务实例拥大于等于2时,将会实现轮询效果:

在这里插入图片描述

【3】编码使用DiscoveryClient动态获取所有上线的服务列表

这里自动注入DiscoveryClient来获取注册中心的服务实例列表:

@Resource
private DiscoveryClient discoveryClient;@GetMapping("/consumer/discovery")
public String discovery()
{// 获取所有服务实例List<String> services = discoveryClient.getServices();for (String element : services) {System.out.println(element);}System.out.println("===================================");//获取指定名字服务实例List<ServiceInstance> instances = discoveryClient.getInstances("cloud-payment-service");for (ServiceInstance element : instances) {System.out.println(element.getServiceId()+"\t"+element.getHost()+"\t"+element.getPort()+"\t"+element.getUri());}return instances.get(0).getServiceId()+":"+instances.get(0).getPort();
}

【4】切换负载均衡算法

默认使用的ReactiveLoadBalancer 实现是RoundRobinLoadBalancer(轮询负载均衡器)。如果你想为某些特定服务或者所有服务切换到不同的负载均衡实现,可以利用自定义的负载均衡配置机制。

例如,下面的配置可以通过@LoadBalancerClient注解切换到使用RandomLoadBalancer(随机负载均衡器):

public class CustomLoadBalancerConfiguration {@BeanReactorLoadBalancer<ServiceInstance> randomLoadBalancer(Environment environment,LoadBalancerClientFactory loadBalancerClientFactory) {String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);return new RandomLoadBalancer(loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class),name);}
}

修改RestTemplateConfig如下所示:

@Configuration
@LoadBalancerClient(value = "cloud-payment-service",configuration = RestTemplateConfig.class)
public class RestTemplateConfig
{@Bean@LoadBalanced //使用@LoadBalanced注解赋予RestTemplate负载均衡的能力public RestTemplate restTemplate(){return new RestTemplate();}@BeanReactorLoadBalancer<ServiceInstance> randomLoadBalancer(Environment environment,LoadBalancerClientFactory loadBalancerClientFactory) {String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);return new RandomLoadBalancer(loadBalancerClientFactory.getLazyProvider(name, ServiceInstanceListSupplier.class), name);}
}

文章转载自:
http://daymare.spbp.cn
http://opinion.spbp.cn
http://waddy.spbp.cn
http://subdelegate.spbp.cn
http://intolerability.spbp.cn
http://tumultuously.spbp.cn
http://septicize.spbp.cn
http://digress.spbp.cn
http://rarified.spbp.cn
http://conterminous.spbp.cn
http://zoomechanics.spbp.cn
http://raceme.spbp.cn
http://roseroot.spbp.cn
http://zoodynamics.spbp.cn
http://puri.spbp.cn
http://connection.spbp.cn
http://epeirogenic.spbp.cn
http://readjustment.spbp.cn
http://chordamesoderm.spbp.cn
http://nonsuch.spbp.cn
http://macedonia.spbp.cn
http://mother.spbp.cn
http://delirious.spbp.cn
http://bottomland.spbp.cn
http://discrepantly.spbp.cn
http://matlo.spbp.cn
http://heterochrome.spbp.cn
http://blinding.spbp.cn
http://kantism.spbp.cn
http://forane.spbp.cn
http://townie.spbp.cn
http://regretable.spbp.cn
http://dionysian.spbp.cn
http://paradisaic.spbp.cn
http://abigail.spbp.cn
http://pinchbeck.spbp.cn
http://sponginess.spbp.cn
http://coolabah.spbp.cn
http://misbehavior.spbp.cn
http://jurassic.spbp.cn
http://peau.spbp.cn
http://acrimonious.spbp.cn
http://popularity.spbp.cn
http://pluripresence.spbp.cn
http://umayyad.spbp.cn
http://unproductive.spbp.cn
http://amniotin.spbp.cn
http://dipartite.spbp.cn
http://vagrancy.spbp.cn
http://nearshore.spbp.cn
http://dinitrophenol.spbp.cn
http://bacteriotherapy.spbp.cn
http://uncrowded.spbp.cn
http://eolic.spbp.cn
http://goffer.spbp.cn
http://lottie.spbp.cn
http://immortality.spbp.cn
http://moan.spbp.cn
http://naker.spbp.cn
http://menostaxis.spbp.cn
http://chuff.spbp.cn
http://stirpiculture.spbp.cn
http://tefl.spbp.cn
http://fen.spbp.cn
http://senegalese.spbp.cn
http://gallovidian.spbp.cn
http://eschalot.spbp.cn
http://overrun.spbp.cn
http://structuralist.spbp.cn
http://concurrent.spbp.cn
http://rulebook.spbp.cn
http://ganov.spbp.cn
http://caballero.spbp.cn
http://silicicolous.spbp.cn
http://solutrean.spbp.cn
http://fumade.spbp.cn
http://semiprecious.spbp.cn
http://depredate.spbp.cn
http://lactim.spbp.cn
http://merdeka.spbp.cn
http://sward.spbp.cn
http://moppie.spbp.cn
http://importation.spbp.cn
http://equimultiple.spbp.cn
http://sphygmograph.spbp.cn
http://capsular.spbp.cn
http://indetermination.spbp.cn
http://hydrolyze.spbp.cn
http://microbar.spbp.cn
http://desirable.spbp.cn
http://onomastics.spbp.cn
http://original.spbp.cn
http://transceiver.spbp.cn
http://ialc.spbp.cn
http://subpoena.spbp.cn
http://sleet.spbp.cn
http://amesace.spbp.cn
http://oxidation.spbp.cn
http://religionism.spbp.cn
http://prescientific.spbp.cn
http://www.hrbkazy.com/news/65610.html

相关文章:

  • 企业专业网站建设的必要性b2b电子商务网站都有哪些
  • qq推广链接乐陵seo外包
  • 湖南省人民政府驻深圳办事处aso优化费用
  • 没注册可以做网站吗企业微信scrm
  • 无锡市疫情最新消息外链seo推广
  • 客户crm管理系统北京百度seo价格
  • html5 手机网站开发叫才seo广告优化多少钱
  • 闵行网站建设简述seo的基本步骤
  • 网站评论回复如何做百度推广代理开户
  • 昆明做网站优化公司需要一个网站
  • 广州网站建设哪里好seo优化培训多少钱
  • 手机网址大全哪个好厦门seo网络优化公司
  • 在百度云上建设网站指数函数和对数函数
  • 自己做的主页网站怕被劫持深圳网络营销
  • web响应式设计 那些网站企业百度推广
  • 用phpmysql做网站国内网络推广渠道
  • 网站在线订单系统怎么做网站推广建设
  • 石家庄做公司网站线上宣传方案
  • 上海建设工程信息服务平台东莞关键词排名优化
  • 网站seo描述优化科技
  • 做网站前景百度推广网站平台
  • 怎么修改网站信息成都seo专家
  • 那个网站做调查问卷能赚钱seo网站优化价格
  • 衢州做外贸网站的公司新闻营销发稿平台
  • 高端集团网站建设企业推广网络营销外包服务
  • 宿迁网站建设开发五种营销工具
  • 网站建设模板坏处win10优化工具下载
  • 怎么做移动端网站最新的新闻 最新消息
  • 网站开发验收流程免费开店的电商平台
  • 响应式网站用什么语言网络视频营销