3、Spring Cloud 服务消费者-初探Ribbon

一、服务发现与消费
通过上面的内容与实践,我们已经搭建起微服务架构中的核心组件——服务注册中心(包括单点模式和高可用模式)。并且成功的将服务注册到了注册中心上,现在我们已经有了服务注册中心和服务提供者,下面我们会构建一个 服务消费者,他主要有两个功能:发现服务,消费服务。 其中,服务发现的任务是由Eureka的客户端完成的,服务消费的任务是由Ribbon完成。Ribbon是一个基于HTTP和TCP的客户端负载均衡器,他可以通过配置在客户端中的ribbonServerList服务端列表去轮询访问达到负载均衡的作用。当ribbon和Eureka联合使用时,ribbon的服务实例清单RibbonServerList会被DiscoveryEnabledNIWSServerList重写,扩展成从Eureka注册中心获取服务列表。同时他也会用NIWSDiscoveryPing来取代IPing,他将责任委托给Eureka来确定服务端是否启动!

二、将eureka-service-hello服务注册两个实例


打包启动eureka-service-hello:
java -jar eureka-service-hello.jar --spring.profiles.active=hello1
java -jar eureka-service-hello.jar --spring.profiles.active=hello2

查看注册中心:

我们可以看见有两个注册中心,两个eureka-service-hello。


三、构建服务消费者
创建一个新的工程,名字叫做:ribbon-consumer-hello,pom.xml配置

<? xml version ="1.0" encoding ="UTF-8" ?>
< modelVersion >4.0.0 </ modelVersion >

< groupId >com.wl.test </ groupId >
< artifactId >ribbon-consumer-hello </ artifactId >
< version >0.0.1-SNAPSHOT </ version >
< packaging >jar </ packaging >

< name >ribbon-consumer-hello </ name >
< description >服务消费者,消费hello服务 </ description >

< parent >
< groupId >org.springframework.boot </ groupId >
< artifactId >spring-boot-starter-parent </ artifactId >
< version >1.5.10.RELEASE </ version >
< relativePath /> <!-- lookup parent from repository -->
</ parent >

< properties >
< project.build.sourceEncoding >UTF-8 </ project.build.sourceEncoding >
< project.reporting.outputEncoding >UTF-8 </ project.reporting.outputEncoding >
< java.version >1.8 </ java.version >
< spring-cloud.version >Dalston.SR5 </ spring-cloud.version >
</ properties >

< dependencies >
< dependency >
< groupId >org.springframework.boot </ groupId >
< artifactId >spring-boot-starter-web </ artifactId >
</ dependency >
< dependency >
< groupId >org.springframework.cloud </ groupId >
< artifactId >spring-cloud-starter-eureka </ artifactId >
</ dependency >
< dependency >
< groupId >org.springframework.cloud </ groupId >
< artifactId >spring-cloud-starter-ribbon </ artifactId >
</ dependency >

< dependency >
< groupId >org.springframework.boot </ groupId >
< artifactId >spring-boot-starter-test </ artifactId >
< scope >test </ scope >
</ dependency >
</ dependencies >

< dependencyManagement >
< dependencies >
< dependency >
< groupId >org.springframework.cloud </ groupId >
< artifactId >spring-cloud-dependencies </ artifactId >
< version >${spring-cloud.version} </ version >
< type >pom </ type >
< scope >import </ scope >
</ dependency >
</ dependencies >
</ dependencyManagement >

< build >
< plugins >
< plugin >
< groupId >org.springframework.boot </ groupId >
< artifactId >spring-boot-maven-plugin </ artifactId >
</ plugin >
</ plugins >
</ build >
</ project >
新增了一个spring-cloud-starter-ribbon依赖。

配置application.yml


修改启动类:

通过 @LoadBalanced 开启客户端负载均衡!

新增controller:这里是根据你配置的 spring.application.name 来进行查找服务。
注意:这个 spring.application.name 是服务名,不是instance-id,一个服务可能有很多实例,所有一个 spring.application.name  可以对应很多的instance-id。


测试服务消费者:


访问  http://localhost:1111/consumer-hello  测试服务:
第一次访问:


第二次访问:


可以看见,我们进行了客户端的负载均衡!nginx的反向代理和LVS属于服务端的负载均衡。
什么是客户端的负载均衡:就是客户(在这里是eureka的服务消费者)知道自己调用的是哪个服务实例,决定权在客户手中
什么是服务端的负载均衡:比如nginx的反向代理就是典型的服务端负载均衡,用户只需要访问nginx服务器,完全不了解具体是哪台服务器响应的自己,决定权在服务端手里!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值