资讯专栏INFORMATION COLUMN

SpringCloud(第 020 篇)Zuul 网关模块添加 listOfServers 属性,达

Dogee / 1539人阅读

摘要:注意注解能注册到服务上,是因为该注解包含了客户端的注解,该是一个复合注解。地址可以查看该微服务网关代理了多少微服务的。

SpringCloud(第 020 篇)Zuul 网关模块添加 listOfServers 属性,达到客户端负载均衡的能力

-

一、大致介绍

</>复制代码

  1. 1、本章节添加另外一个属性 listOfServers 来给 zuul 赋上异样的功能色彩,提供负载均衡的能力;
  2. 2、而其实说到底 zuul 的负载能力还是在于 ribbon,因为 ribbon 才是真正做到让 zuul 达到客户端负载均衡能力的本质;
二、实现步骤 2.1 添加 maven 引用包

</>复制代码

  1. 4.0.0
  2. springms-gateway-zuul-cluster
  3. 1.0-SNAPSHOT
  4. jar
  5. com.springms.cloud
  6. springms-spring-cloud
  7. 1.0-SNAPSHOT
  8. org.springframework.cloud
  9. spring-cloud-starter-zuul
  10. org.springframework.cloud
  11. spring-cloud-starter-eureka
2.2 添加应用配置文件(springms-gateway-zuul-clustersrcmainresourcesapplication.yml)

</>复制代码

  1. spring:
  2. application:
  3. name: springms-gateway-zuul-cluster
  4. server:
  5. port: 8165
  6. eureka:
  7. datacenter: SpringCloud # 修改 http://localhost:8761 地址 Eureka 首页上面 System Status 的 Data center 显示信息
  8. environment: Test # 修改 http://localhost:8761 地址 Eureka 首页上面 System Status 的 Environment 显示信息
  9. client:
  10. service-url:
  11. defaultZone: http://admin:admin@localhost:8761/eureka
  12. healthcheck: # 健康检查
  13. enabled: true
  14. instance:
  15. prefer-ip-address: true
  16. instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
  17. #####################################################################################################
  18. # 测试一,API网关模块发现应用入口(添加 listOfServers 属性,测试 zuul 的负载均衡功能)
  19. zuul:
  20. routes:
  21. hmily:
  22. path: /custom-path/**
  23. serviceId: springms-provider-user
  24. # 注意,这里在运行的时候有个坑,如果以树形展开写法的话,那么就会出错了,所以这个配置还是避免用树形写法
  25. ribbon.eureka.enabled: false
  26. springms-provider-user: # 这里是 ribbon 要请求的微服务的 service-id 值
  27. ribbon:
  28. listOfServers: http://localhost:7900,http://localhost:7899,http://localhost:7898
  29. #####################################################################################################
  30. #####################################################################################################
  31. # 打印日志
  32. logging:
  33. level:
  34. root: INFO
  35. com.springms: DEBUG
  36. #####################################################################################################
  37. #####################################################################################################
  38. ribbon:
  39. ConnectTimeout: 3000
  40. ReadTimeout: 60000
  41. #####################################################################################################
  42. #####################################################################################################
  43. # 解决第一次请求报超时异常的方案,因为 hystrix 的默认超时时间是 1 秒,因此请求超过该时间后,就会出现页面超时显示 :
  44. #
  45. # 这里就介绍大概三种方式来解决超时的问题,解决方案如下:
  46. #
  47. # 第一种方式:将 hystrix 的超时时间设置成 5000 毫秒
  48. hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
  49. #
  50. # 或者:
  51. # 第二种方式:将 hystrix 的超时时间直接禁用掉,这样就没有超时的一说了,因为永远也不会超时了
  52. # hystrix.command.default.execution.timeout.enabled: false
  53. #
  54. # 或者:
  55. # 第三种方式:索性禁用feign的hystrix支持
  56. # feign.hystrix.enabled: false ## 索性禁用feign的hystrix支持
  57. # 超时的issue:https://github.com/spring-cloud/spring-cloud-netflix/issues/768
  58. # 超时的解决方案: http://stackoverflow.com/questions/27375557/hystrix-command-fails-with-timed-out-and-no-fallback-available
  59. # hystrix配置: https://github.com/Netflix/Hystrix/wiki/Configuration#execution.isolation.thread.timeoutInMilliseconds
  60. #####################################################################################################
2.3 添加zuul服务网关微服务启动类(springms-gateway-zuul-clustersrcmainjavacomspringmscloudMsGatewayZuulClusterApplication.java)

</>复制代码

  1. package com.springms.cloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
  5. /**
  6. * Zuul 网关模块添加 listOfServers 属性,达到客户端负载均衡的能力。
  7. *
  8. * 注意 EnableZuulProxy 注解能注册到 eureka 服务上,是因为该注解包含了 eureka 客户端的注解,该 EnableZuulProxy 是一个复合注解。
  9. *
  10. * @EnableZuulProxy --> { @EnableCircuitBreaker、@EnableDiscoveryClient } 包含了 eureka 客户端注解,同时也包含了 Hystrix 断路器模块注解。
  11. *
  12. * http://localhost:8165/routes 地址可以查看该zuul微服务网关代理了多少微服务的serviceId。
  13. *
  14. * @author hmilyylimh
  15. *
  16. * @version 0.0.1
  17. *
  18. * @date 2017/9/24
  19. *
  20. */
  21. @SpringBootApplication
  22. @EnableZuulProxy
  23. public class MsGatewayZuulClusterApplication {
  24. public static void main(String[] args) {
  25. SpringApplication.run(MsGatewayZuulClusterApplication.class, args);
  26. System.out.println("【【【【【【 GatewayZuulCluster微服务 】】】】】】已启动.");
  27. }
  28. }
三、测试

</>复制代码

  1. /****************************************************************************************
  2. 一、Zuul 网关模块添加 listOfServers 属性,达到客户端负载均衡的能力:
  3. 1、编写 application.yml 文件,添加应用程序的注解 EnableZuulProxy 配置;
  4. # 测试一,API网关模块发现应用入口(添加 listOfServers 属性,测试 zuul 的负载均衡功能)
  5. zuul:
  6. routes:
  7. hmily:
  8. path: /user-serviceId/**
  9. serviceId: springms-provider-user
  10. # 注意,这里在运行的时候有个坑,如果以树形展开写法的话,那么就会出错了,所以这个配置还是避免用树形写法
  11. ribbon.eureka.enabled: false
  12. springms-provider-user: # 这里是 ribbon 要请求的微服务的 service-id 值
  13. ribbon:
  14. listOfServers: http://localhost:7900,http://localhost:7899,http://localhost:7898
  15. 2、启动 springms-discovery-eureka 模块服务,启动1个端口;
  16. 3、启动 springms-provider-user 模块服务,启动1个端口(application.yml 文件中的 appname 属性不去掉的话,测试一是无法测试通过的);
  17. 4、启动 springms-gateway-zuul-cluster 模块服务;
  18. 5、新起网页页签,输入 http://localhost:7900/simple/5 正常情况下是能看到 ID != 0 一堆用户信息被打印出来;
  19. 总结一:第5步正常,说明 springms-provider-user 服务目前正常;
  20. 6、新起网页页签,然后输入 http://localhost:8165/springms-provider-user/simple/6,正常情况下是能看到 ID != 0 一堆用户信息被打印出来;
  21. 总结二:第6步也能正常打印用户信息,说明 API 网关已经生效了,可以通过API服务器地址链接各个微服务的 http://localhost:8150/serviceId/path 这样的路径来访问了;
  22. 7、新起网页页签,然后输入 http://localhost:8165/user-serviceId/simple/1,正常情况下是能看到 ID != 0 一堆用户信息被打印出来;
  23. 总结三:path、serviceId 设置的反向代理路径也通了;
  24. 8、清除 springms-provider-user 模块控制台的所有的日志,然后刷新 9 次该地址 http://localhost:8165/user-serviceId/simple/1 的网页,然后会发现 3 个用户服务都各打印了3次,再多刷新几次,会发现该负载均衡的调度的算法是轮论调,依次轮询调用每个用户服务微服务;
  25. 总结四:listOfServers 属性也生效了,从而说明添加 listOfServers 也可以达到 zuul 负载均衡的能力;
  26. ****************************************************************************************/
四、下载地址

https://gitee.com/ylimhhmily/SpringCloudTutorial.git

SpringCloudTutorial交流QQ群: 235322432

SpringCloudTutorial交流微信群: 微信沟通群二维码图片链接

欢迎关注,您的肯定是对我最大的支持!!!

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/70553.html

相关文章

  • SpringCloud 022 Zuul 网关微服务的regexmapper属性测试, 类似

    摘要:地址可以查看该微服务网关代理了多少微服务的。微服务已启动使用提供和之间的绑定它使用正则表达式组来从提取变量然后注入到路由表达式中。 SpringCloud(第 022 篇)Zuul 网关微服务的 regexmapper 属性测试, 类似测试 zuul 的自定义路径规则一样 - 一、大致介绍 1、本章节将 Zuul 的 regexmapper 属性单独拿出来,主要是这种配置规则,可以在一...

    cyqian 评论0 收藏0
  • SpringCloud 018 Zuul 服务 API 网关微服务之代理与反向代理

    摘要:注意注解能注册到服务上,是因为该注解包含了客户端的注解,该是一个复合注解。地址可以查看该微服务网关代理了多少微服务的。 SpringCloud(第 018 篇)Zuul 服务 API 网关微服务之代理与反向代理 - 一、大致介绍 1、API 服务网关顾名思义就是统一入口,类似 nginx、F5 等功能一样,统一代理控制请求入口,弱化各个微服务被客户端记忆功能; 2、本章节主要讲解了使用...

    YancyYe 评论0 收藏0
  • SpringCloud 019 Zuul 网关微服务的一些属性应用测试

    SpringCloud(第 019 篇)Zuul 网关微服务的一些属性应用测试 - 一、大致介绍 1、本章节根据官网资料,尝试了一些其它属性的设置,比如 path、serviceId、prefix、strip-prefix 等应用; 2、这些组合试用的场景大多数在一些地址方面需要重新映射或者针对特殊地址做特殊处理等,至于其它一些深层次的应用大家做过知道的话也可以告尽情回帖让大家都来学习学习。 二、...

    imtianx 评论0 收藏0
  • springCloud学习4(Zuul服务路由)

    摘要:但是如果将负载均衡器置于所有服务前便不是一个好主意,会造成瓶颈。服务超时使用的和库来进行请求。支持以下四种过滤器前置过滤器在将请求发送到目的地之前被调用。通常用于记录从目标服务返回的响应处理错误或审核敏感信息。 showImg(https://segmentfault.com/img/remote/1460000019531578); springcloud 总集:https://ww...

    wuaiqiu 评论0 收藏0
  • SpringCloud 021 Zuul 的过滤器 ZuulFilter 的使用

    摘要:第篇的过滤器的使用一大致介绍我们在学的时候,就有过滤器和拦截器的使用,而同样也有过滤器的使用,本章节我们指在如何简单使用。是否执行该过滤器。说明需要过滤说明不要过滤过滤器的具体逻辑。请求的添加服务网关微服务启动类的过滤器的使用。 SpringCloud(第 021 篇)Zuul 的过滤器 ZuulFilter 的使用 - 一、大致介绍 1、我们在学 Spring 的时候,就有过滤器和拦...

    kumfo 评论0 收藏0

发表评论

0条评论

Dogee

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<