博客
关于我
Springcloud微服务-组件三Ribbon
阅读量:521 次
发布时间:2019-03-07

本文共 1073 字,大约阅读时间需要 3 分钟。

继上两篇接着说我们将服务注册到Eureka注册中心后,那么服务之间的调用是怎么完成的?

我们的消费者想要去注册中心上消费我们的服务这里就需要使用到springcloud 的组件ribbon 。

一、Ribbon是什么?

Ribbon是一个基于HTTP和TCP的客户端负载均衡器。当使用Ribbon对服务进行访问的时候,它会实现从Eureka注册中心中获取服务端列表,并通过Eureka客户端来确定服务端已经启动。Ribbon在Eureka客户端服务发现的基础上,实现了对服务实例的选择策略,因此Ribbon也是实现负载均衡的策略的。

二、Ribbon在项目中怎么使用?

1、起步依赖

org.springframework.cloud
spring-cloud-starter-netflix-eureka-client

2、激活 Eureka 中的EnableEurekaClient 功能:

在 Spring Boot 的入口函数处,通过添加@EnableEurekaClient 注解来表明自己是一个 eureka 客户端,让我的服务消费者可以使用 eureka 注册中心;

3、配置服务的名称和注册中心的地址:

spring.application.name=03-springcloud-web-consumereureka.client.service-url.defaultZone=http://localhost:8761/eureka

4、Ribbon来实现服务的调用

@loadBalance 该注册就是Ribbon的核心,Springcloud对Ribbon实现的封装,让我们开发变得非常的方便,我们在项目中一般会见一个配置类,例如:

@LoadBalanced@Beanpublic RestTemplate restTemplate () { return new RestTemplate();}

那么我们可以说我们新建了一个RestTemplate模板对象,交由spring来管理,我们需要调用微服务的时候就可以使用该模板对象来调用了。例如

需要注意的时:加入了Ribbon的支持,在调用服务的过程中,就是使用服务的名称来调用。

restTemplate.getForEntity("http://01-SPRINGCLOUD-SERVICE-PROVIDER/cloud/hello", String.class).getBody();

转载地址:http://wnunz.baihongyu.com/

你可能感兴趣的文章
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>