美文网首页
SpringCloud-2

SpringCloud-2

作者: 我叫土豆但他们喊我猫猫 | 来源:发表于2019-05-12 17:31 被阅读0次

3.服务消费者

通俗一点理解:对于服务消费者,能够调用注册中心的Discovery(发现者)的业务逻辑,同时也可以实现客户端多端口访问起到负载均衡的作用。

这里介绍两种实现负载均衡的两种服务:Ribbon、Feign;

  1. Ribbon:Ribbon是一个基于HTTP和TCP客户端的负载均衡器,可以在通过客户端中配置的ribbonServerList服务端列表去轮询访问以达到均衡负载的作用。

  2. Feign:Feign是一个声明式的Web Service客户端,它使得编写Web Serivce客户端变得更加简单,同时,它也使用了Ribbon。

3.1 Ribbon

  1. 简介:

Ribbon基于Http和TCP客户端的负载均衡器,它起到担当一个重要的较色,当Ribbon与Eureka联合使用时,ribbonServerList会被DiscoveryEnabledNIWSServerList重写,扩展成从Eureka注册中心中获取服务端列表。同时它也会用NIWSDiscoveryPing来取代IPing,它将职责委托给Eureka来确定服务端是否已经启动。

  1. 应用:

①:构建一个基本的Boot项目,在pom.xml中添加相关的依赖:

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="" cid="n116" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); --select-text-font-color: #fff; font-size: 0.9rem; line-height: 1.71429em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(218, 218, 218); position: relative !important; margin-bottom: 3em; margin-left: 2em; padding-left: 0px; padding-right: 1ch; width: inherit; color: rgb(31, 9, 9); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency></pre>

②:在主类中,添加对应的注解,并开启负载均衡能力:

image

其中:在主类上,添加@EnableDiscoveryClient注解,起到发现客户端服务的能力;创建RestTemplate实例,并且添加@LoadBalanced注解来开启负载均衡的能力。

③:创建实体类,来达到消费其它服务的作用:

image

其中:通过注入的方式,来展示负载均衡的使用。

声明方法,添加对应的URL访问路径;

调用RestTemplate中的方法,通过服务发现者的服务名称,进行提供的接口的访问并且调用。

④:在配置文件application.properties中,配置对应的信息:

image

其中:配置一个对应当前的服务名

⑤:启动服务:

a. 依次启动Eureka、Discovery、Discovery1服务,其中Discovery、Discovery1表示创建两个不同端口的服务提供者(两者除了端口不同,其余都相同),这样才能够去展示Ribbon的作用。

image

b. 启动Ribbon服务,并且访问两次Ribbon路径,可以看到在访问的界面、Discovery的两个服务中日志都分别打印出对应的数据,具体展示如下:

Ribbon界面访问:

image

至此,可以通过Ribbon在客户端实现了对服务调用的均衡负载。

3.2 Feign

  1. 简介:

Feign是一个声明式的Web Service客户端,它使得编写Web Serivce客户端变得更加简单。我们只需要使用Feign来创建一个接口并用注解来配置它既可完成。它具备可插拔的注解支持,包括Feign注解和JAX-RS注解。Feign也支持可插拔的编码器和解码器。Spring Cloud为Feign增加了对Spring MVC注解的支持,还整合了Ribbon和Eureka来提供均衡负载的HTTP客户端实现。

  1. 应用

①:创建一个Boot工程,可以使用Ribbon的Pom.xml的依赖,只需要将其中的Ribbon依赖改称为feign的依赖即可:

<pre spellcheck="false" class="md-fences md-end-block md-fences-with-lineno ty-contain-cm modeLoaded" lang="" cid="n145" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); --select-text-font-color: #fff; font-size: 0.9rem; line-height: 1.71429em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(218, 218, 218); position: relative !important; margin-bottom: 3em; margin-left: 2em; padding-left: 0px; padding-right: 1ch; width: inherit; color: rgb(31, 9, 9); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency></pre>

②:在启动类上添加开启Feign的注解(@EnableFeignClients):

image

③:定义一个接口,应用于服务发现者(discovery-service)的接口:

image

其中:

a. 使用@FeignClient("discovery-service ")注解来绑定该接口对应discovery-service服务

b. 通过SpringMvc注解来配置该服务下的方法实现。

注意:定义的url 必须与提供服务的url一致,包括请求方式、参数名

④:定义一个实现类ComputerController:

image

其中:将接口注入到实现类中,然后实现其对应的方法,调用对应的实现方法对其进行参数赋值。

⑤:application.properties中的配置不用改变,改变其端口:

image

⑥:依次启动Eureka、discovery、discovery1,然后再启动当前服务,在浏览器访问

http://localhost:8224/add,连续访问几次,可以看到浏览器界面显示对应的值,在其中的discovery两台服务器上的控制台,日志会按照负载均衡的规律,区分被输出对应的日志信息(共访问11次,其中discovery输出8次,discovery1输出3次)

3.3 小结

Ribbon总结

  1. 先选择 Eureka Server, 它优先选择在同一个Zone且负载较少的Server;

  2. 再根据用户指定的策略,在从Server取到的服务注册列表中选择一个地址。其中Ribbon提供了三种策略:轮询、断路器和根据响应时间加权。

Feign总结:

  1. 在使用Feign提供的注解编写HTTP接口的客户端代码非常简单, 只需要声明一个Java接口加上少量注解即可完成。

  2. Feign会帮我们处理好一切. 根据我们的接口声明, Feign会在Spring容器启动之后, 将生成的代理类注入, 所以我们不需要写HTTP调用的实现代码就能完成REST接口的调用.

  3. Feign服务客户端 定义的请求url必须与服务提供者url一致。

  4. Feign服务客户端中的接口名、返回对象可以任意定义。但对象中的属性类型和属性名必须一致,与两个对象中的属性顺序和数量无关

  5. 启动 Eureka注册中心、服务提供者、Feign服务客户端,然后 Eureka注册中心挂掉时,Feign服务客户端消费服务是不受影响的。

相关文章

  • SpringCloud-2

    3.服务消费者 通俗一点理解:对于服务消费者,能够调用注册中心的Discovery(发现者)的业务逻辑,同时也可以...

  • SpringCloud-2之Eureka注册发现

    本章涉及: Eureka注册中心Eureka Client注册服务官方demo:https://spring.io...

网友评论

      本文标题:SpringCloud-2

      本文链接:https://www.haomeiwen.com/subject/lmocaqtx.html