首页 > 其他分享 >Spring Cloud构建微服务架构(二)服务消费者

Spring Cloud构建微服务架构(二)服务消费者

时间:2022-12-19 13:37:11浏览次数:76  
标签:load 服务 service Spring server balancing Cloud determine Ribbon

 

Netflix Ribbon is an Inter Process Communication (IPC) cloud library. Ribbon primarily provides client-side load balancing algorithms.

Apart from the client-side load balancing algorithms, Ribbon provides also other features:

Service Discovery Integration – Ribbon load balancers provide service discovery in dynamic environments like a cloud. Integration with Eureka and Netflix service discovery component is included in the ribbon library
Fault Tolerance – the Ribbon API can dynamically determine whether the servers are up and running in a live environment and can detect those servers that are down
Configurable load-balancing rules – Ribbon supports RoundRobinRule, AvailabilityFilteringRule, WeightedResponseTimeRule out of the box and also supports defining custom rules
Ribbon API works based on the concept called “Named Client”. While configuring Ribbon in our application configuration file we provide a name for the list of servers included for the load balancing.

Let’s take it for a spin.

Ribbon Configuration

Ribbon API enables us to configure the following components of the load balancer:

Rule – Logic component which specifies the load balancing rule we are using in our application
Ping – A Component which specifies the mechanism we use to determine the server’s availability in real-time
ServerList – can be dynamic or static. In our case, we are using a static list of servers and hence we are defining them in the application configuration file directly

public class RibbonConfiguration {

@Autowired
IClientConfig ribbonClientConfig;

@Bean
public IPing ribbonPing(IClientConfig config) {
return new PingUrl();
}

@Bean
public IRule ribbonRule(IClientConfig config) {
return new WeightedResponseTimeRule();
}
}

Notice how we used the WeightedResponseTimeRule rule to determine the server and PingUrl mechanism to determine the server’s availability in real-time.

According to this rule, each server is given a weight according to its average response time, lesser the response time gives lesser the weight. This rule randomly selects a server where the possibility is determined by server’s weight.

And the PingUrl will ping every URL to determine the server’s availability.

​http://www.baeldung.com/spring-cloud-rest-client-with-netflix-ribbon​​​


 

在上一篇​​《Spring Cloud构建微服务架构(一)服务注册与发现》​​中,我们已经成功创建了“服务注册中心”,实现并注册了一个“服务提供者:COMPUTE-SERVICE”。那么我们要如何去消费服务提供者的接口内容呢?

Ribbon

Ribbon是一个基于HTTP和TCP客户端的负载均衡器。Feign中也使用Ribbon,后续会介绍Feign的使用。

Ribbon可以在通过客户端中配置的ribbonServerList服务端列表去轮询访问以达到均衡负载的作用。

当Ribbon与Eureka联合使用时,ribbonServerList会被DiscoveryEnabledNIWSServerList重写,扩展成从Eureka注册中心中获取服务端列表。同时它也会用NIWSDiscoveryPing来取代IPing,它将职责委托给Eureka来确定服务端是否已经启动。

下面我们通过实例看看如何使用Ribbon来调用服务,并实现客户端的均衡负载。

准备工作

  • 启动​​Chapter-9-1-1​​中的服务注册中心:eureka-server
  • 启动​​Chapter-9-1-1​​中的服务提供方:compute-service
  • 修改compute-service中的server-port为2223,再启动一个服务提供方:compute-service

此时访问:​​http://localhost:1111/​​

​http://blog.didispace.com/springcloud2/​

 



标签:load,服务,service,Spring,server,balancing,Cloud,determine,Ribbon
From: https://blog.51cto.com/u_15147537/5951925

相关文章

  • Spring CredHub 教程学习
    SpringCredHub提供客户端支持,用于从CloudFoundry​平台中运行的CredHub服务器存储,检索和删除凭据。CredHub提供了一个HTTPAPI,用于安全地存储,生成,检索和删除各种类型的凭......
  • Spring LDAP参考
    SpringLDAP使得构建使用LightweightDirectoryAccess协议的基于Spring的应用程序变得更加容易。本文档的副本可以制作供您自己使用和分发给他人,前提是您不对此类副本......
  • Spring提取@Transactional事务注解的源码解析
    声明:本文是自己在学习spring注解事务处理源代码时所留下的笔记;难免有错误,敬请读者谅解!!!1、事务注解标签<tx:annotation-driven/>2、tx命名空间解析器 事务tx命名空间解析......
  • springboot+postgresql集成anyline试水
    anyline是什么简单讲就是一个工具可以让你抛开常规的机械性建mapper、dao、sql,用通用的语句查询和操作数据库表。目前也在初步探索中,感受还不深。官网文档:http://doc.any......
  • 5. MinIO与springboot的集成
    MinIO与springboot的集成搭建一个springboot的项目,集成MinIO实现文件的管理。一、搭建springboot环境IntelliJIDEAJDK17gradle-7.5.1springboot2.7.6项目地址:g......
  • Spring Cloud 应用 Proxyless Mesh 模式探索与实践
    作者:十眠ServiceMesh简介ServiceMesh早已不是一个新兴的概念,目前已经有许多关于ServiceMesh的探索以及实践。2016年可以说是ServiceMesh的元年,Buoyant公司CEO......
  • Sql Server 服务无法正常启动,126异常
    SQLServer一记异常信息方法/步骤打开【开始菜单】找到软件SQLServer下面的【配置管理器】可以看到数据库主服务是‘停止’状态;![数据库主服务展开【SQL......
  • 游戏服务器的配置如何选择
    1.服务器的CPU大型的游戏要接受大量的访问请求,CPU的快速吞吐量、稳定性、长时间运行都有着严格的要求,因而服务器CPU是衡量服务器性能的首要指标。之前提到根据游戏的类......
  • Spring AMQP项目(四)
    4.1.12.代理事件侦听器启用事件交换插件后,如果将类型的Bean添加到应用程序上下文中,它将选定的代理事件发布为实例,这些实例可以使用普通的Spring或方法使用。事件由代......
  • 【服务器数据恢复】NTFS文件系统服务器raid5数据恢复案例
    服务器数据恢复环境:一台使用NTFS文件系统的服务器;7块硬盘组成了一组raid5磁盘阵列。服务器故障&初检:raid5磁盘阵列磁盘故障离线导致服务器瘫痪。用户在处理掉线磁盘时只......