Spring Cloud构建微服务架构:服务消费(Ribbon)【Dalston版】
来源:互联网 发布:人工智能家庭生活 编辑:程序博客网 时间:2024/06/11 09:32
Spring Cloud Ribbon
Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具。它是一个基于HTTP和TCP的客户端负载均衡器。它可以通过在客户端中配置ribbonServerList来设置服务端列表去轮询访问以达到均衡负载的作用。
当Ribbon与Eureka联合使用时,ribbonServerList会被DiscoveryEnabledNIWSServerList重写,扩展成从Eureka注册中心中获取服务实例列表。同时它也会用NIWSDiscoveryPing来取代IPing,它将职责委托给Eureka来确定服务端是否已经启动。
而当Ribbon与Consul联合使用时,ribbonServerList会被ConsulServerList来扩展成从Consul获取服务实例列表。同时由ConsulPing来作为IPing接口的实现。
我们在使用Spring Cloud Ribbon的时候,不论是与Eureka还是Consul结合,都会在引入Spring Cloud Eureka或Spring Cloud Consul依赖的时候通过自动化配置来加载上述所说的配置内容,所以我们可以快速在Spring Cloud中实现服务间调用的负载均衡。
下面我们通过具体的例子来看看如何使用Spring Cloud Ribbon来实现服务的调用以及客户端均衡负载。
动手试一试
下面的例子,我们将利用之前构建的eureka-server
作为服务注册中心、eureka-client
作为服务提供者作为基础。而基于Spring Cloud Ribbon实现的消费者,我们可以根据eureka-consumer
实现的内容进行简单改在就能完成,具体步骤如下:
- 根据
eureka-consumer
复制一个服务消费者工程,命名为:eureka-consumer-ribbon
。在pom.xml
中增加下面的依赖:
1234567
<dependencies>...<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-ribbon</artifactId></dependency></dependencies>
- 修改应用主类。为
RestTemplate
增加@LoadBalanced
注解:
1234567891011121314
public class Application {public RestTemplate restTemplate() {return new RestTemplate();}public static void main(String[] args) {new SpringApplicationBuilder(Application.class).web(true).run(args);}}
- 修改Controller。去掉原来通过
LoadBalancerClient
选取实例和拼接URL的步骤,直接通过RestTemplate发起请求。
123456789101112
public class DcController {RestTemplate restTemplate;"/consumer")(public String dc() {return restTemplate.getForObject("http://eureka-client/dc", String.class);}}
可以看到这里,我们除了去掉了原来与LoadBalancerClient
相关的逻辑之外,对于RestTemplate
的使用,我们的第一个url参数有一些特别。这里请求的host位置并没有使用一个具体的IP地址和端口的形式,而是采用了服务名的方式组成。那么这样的请求为什么可以调用成功呢?因为Spring Cloud Ribbon有一个拦截器,它能够在这里进行实际调用的时候,自动的去选取服务实例,并将实际要请求的IP地址和端口替换这里的服务名,从而完成服务接口的调用。
在完成了上面你的代码编写之后,读者可以将eureka-server、eureka-client、eureka-consumer-ribbon都启动起来,然后访问http://localhost:2101/consumer ,来跟踪观察eureka-consumer-ribbon服务是如何消费eureka-client服务的/dc
接口的,并且也可以通过启动多个eureka-client服务来观察其负载均衡的效果。
- Spring Cloud构建微服务架构:服务消费(Ribbon)【Dalston版】
- Spring Cloud构建微服务架构:服务消费(Ribbon)【Dalston版】
- Spring Cloud构建微服务架构:服务消费(Ribbon)
- Spring Cloud构建微服务架构-服务消费(Ribbon)
- Spring Cloud构建微服务架构:服务消费(基础)【Dalston版】
- Spring Cloud构建微服务架构:服务消费(Feign)【Dalston版】
- Spring Cloud构建微服务架构:服务消费(基础)【Dalston版】
- Spring Cloud构建微服务架构:服务消费(Feign)【Dalston版】
- Spring Cloud构建微服务架构—服务消费Ribbon
- Spring Cloud构建微服务架构服务消费Ribbon
- Spring Cloud构建微服务架构—服务消费(Ribbon)
- 3.Spring Cloud:服务消费(Ribbon)【Dalston版】
- Spring Cloud构建微服务架构:服务容错保护(Hystrix服务降级)【Dalston版】
- Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)[Dalston版]
- Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)【Dalston版】
- Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)【Dalston版】
- Spring Cloud构建微服务架构:服务容错保护(Hystrix依赖隔离)【Dalston版】
- Spring Cloud构建微服务架构:服务容错保护(Hystrix断路器)【Dalston版】
- 登陆3次 不成功则无法继续
- LeetCode-1. Two Sum
- leetcode 132 Pattern
- 面向对象的几个问题
- Array Partition I
- Spring Cloud构建微服务架构:服务消费(Ribbon)【Dalston版】
- MAMP PRO的mySQL启动失败
- [leetcode]517. Super Washing Machines
- 1049. 数列的片段和(20)
- 冒泡事件、默认事件、事件委托、事件监听和事件捕获
- sqlserver、Mysql、Oracle三种数据库的优缺点总结
- ELF文件格式及其他
- Activity的启动模式(一)
- javascript 获取元素在父节点中的下标