【SpringCloud】【Hoxton】Ribbon 快来打我* 2023-07-14 10:43 2阅读 0赞 [01 基础环境准备][01] [02 一文读懂Eureka][02 _Eureka] [03 Zookeeper注册中心][03 Zookeeper] [04 Consule注册中心][04 Consule] [05 Ribbon][] [06 OpenFegin][] [07 Hystrix全面解析][07 Hystrix] [08 Gateway全面解析][08 Gateway] [09 Config配置中心][09 Config] [10 Bus消息总线][10 Bus] # 1 Ribbon介绍 # Spring Cloud Ribbon是基于 Netflix Ribbon实现的一套客户端负载均衡的工具。简单的说, Ribbon是Netflix发布的开源项目,主要功能是提供客户端的软件负載均衡算法和服务调用。 Ribbon客户端组件提供一系列完善的配置项如连接超时,重试等。简单的说,就是在配置文件中列出 Load Balancer(简称LB)后面所有的机器, Ribbon会自动的帮助你基于某种规则(如简单轮询,随机连接等)去连接这些机器。我们很容易使用 Ribbon实现自定义的负载均衡算法。 # 2 LB负载均衡( Load Balance)是什么 # 简单的说就是将用户的请求平雄的分配到多个服务上,从而达到系统的HA(高可用)。常见的负载均衡有软件 Nginx,LVS,硬件F5等。 # 3 Ribbon本地负载均衡客户端 VS Nginx服务端负载均衡区别 # Nginx是服务器负载均衡,客户端所有请求都会交给 nginx,然后由 nginx实现转发请求。即负载均衡是由服务端实现的。Ribbon本地负载均衡,在调用微服务接口时候,会在注册中心上获取注册信息服务列表之后存到JVM本地,从而在本地实现RPC远程服务调用技术。 # 4 引入包 # <!--eureka 客户端--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MTI1OTk5_size_16_color_FFFFFF_t_70] # 5 演示 # ## 5.1 修改eureka-server ## (1) 修改application.yml server: port: 7000 eureka: instance: #服务名称 hostname: eureka-server-master client: serviceUrl: #eureka注册中心地址 defaultZone: http://localhost:7000/eureka/ #配置URL #不注册自己 register-with-eureka: false #服务发现,false表示自己不从Eureka服务中获取注册信息 fetch-registry: false (2) 启动Eureka-server ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MTI1OTk5_size_16_color_FFFFFF_t_70 1] ## 5.2 修改provider-payment服务 ## (1) application.yml server: port: 9000 spring: application: name: provider-payment-service datasource: url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=UTC username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource eureka: client: #是否将自己注册到EurekaServer register-with-eureka: true #是否从EurekaServer获取已有的注册服务 fetch-registry: true #注册地址 service-url: defaultZone: http://localhost:7000/eureka/ instance: instance-id: provider-payment prefer-ip-address: true (2) 新建RibbonServerController @RestController @RequestMapping("/ribbon") public class RibbonServerController { @Value("${server.port}") private String port; @GetMapping("/server") public String server(){ return "ribbon server port is "+port; } } (3) 启动服务 ![在这里插入图片描述][20200314140327118.png] ## 5.3 修改provider-payment-slave服务 ## (1) 修改application.yml server: port: 9001 spring: application: name: provider-payment-service datasource: url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=UTC username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource eureka: client: #是否将自己注册到EurekaServer register-with-eureka: true #是否从EurekaServer获取已有的注册服务 fetch-registry: true #注册地址 service-url: defaultZone: http://localhost:7000/eureka/ instance: instance-id: provider-payment-slave prefer-ip-address: true (2) 新建RibbonServerController @RestController @RequestMapping("/ribbon") public class RibbonServerController { @Value("${server.port}") private String port; @GetMapping("/server") public String server(){ return "ribbon server port is "+port; } } (3) 启动服务 ![在这里插入图片描述][20200314140359176.png] ## 5.4 修改customer-order服务 ## (1) application.yml server: port: 8000 spring: application: name: customer-order-service eureka: client: #是否将自己注册到EurekaServer register-with-eureka: true #是否从EurekaServer获取已有的注册服务 fetch-registry: true #注册地址 service-url: defaultZone: http://localhost:7000/eureka/ instance: instance-id: order-service prefer-ip-address: true (2) RibbonClientController @RestController @RequestMapping("/ribbon") public class RibbonClientController { @Autowired private RestTemplate restTemplate; private static final String RIBBON_SERVER_URL = "http://provider-payment-service/ribbon/server"; @GetMapping("/client") public String client() { ResponseEntity<String> response = restTemplate.getForEntity(RIBBON_SERVER_URL, String.class); return response.getBody(); } } (3) 启动主类 ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MTI1OTk5_size_16_color_FFFFFF_t_70 2] ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MTI1OTk5_size_16_color_FFFFFF_t_70 3] (4) 测试 ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MTI1OTk5_size_16_color_FFFFFF_t_70 4] ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MTI1OTk5_size_16_color_FFFFFF_t_70 5] # 6 负载均衡算法 # ## 6.1 算法介绍 ## RoundRobinRule(轮询算法) RandomRule(随机算法) AvailabilityFilteringRule():会先过滤由于多次访问故障而处于断路器跳闸状态的服务,还有并发的连接数量超过阈值的服务,然后对剩余的服务列表按照轮询策略进行访问 WeightedResponseTimeRule():根据平均响应的时间计算所有服务的权重,响应时间越快服务权重越大被选中的概率越高,刚启动时如果统计信息不足,则使用RoundRobinRule策略,等统计信息足够会切换到WeightedResponseTimeRule RetryRule():先按照RoundRobinRule的策略获取服务,如果获取失败则在制定时间内进行重试,获取可用的服务。 BestAviableRule():会先过滤掉由于多次访问故障而处于断路器跳闸状态的服务,然后选择一个并发量最小的服务 ZoneAvoidanceRule():默认规则,符合判断server所在区域的性能和server的可用性选择服务器 ## 6.2 随机算法,修改配置文件 ## @Configuration public class ApplicationConfig { @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } @Bean public IRule rule(){ return new RandomRule(); } } ## 6.3 测试略 ## # GITHUB # #分支Ribbon-enviroment-release-v1.0 https://github.com/zhurongsheng666/spring-cloud-hoxton [01]: https://blog.csdn.net/qq_34125999/article/details/104732409 [02 _Eureka]: https://blog.csdn.net/qq_34125999/article/details/104744751 [03 Zookeeper]: https://blog.csdn.net/qq_34125999/article/details/104788216 [04 Consule]: https://blog.csdn.net/qq_34125999/article/details/104809738 [05 Ribbon]: https://blog.csdn.net/qq_34125999/article/details/104859515 [06 OpenFegin]: https://blog.csdn.net/qq_34125999/article/details/104865809 [07 Hystrix]: https://blog.csdn.net/qq_34125999/article/details/104872389 [08 Gateway]: https://blog.csdn.net/qq_34125999/article/details/104878977 [09 Config]: https://blog.csdn.net/qq_34125999/article/details/104887587 [10 Bus]: https://blog.csdn.net/qq_34125999/article/details/104890652 [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MTI1OTk5_size_16_color_FFFFFF_t_70]: /images/20230529/6616793a9e3a47fb9ec7113c7970789f.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MTI1OTk5_size_16_color_FFFFFF_t_70 1]: /images/20230529/ffc28925c18d47b3a7f6bd06ade1de41.png [20200314140327118.png]: /images/20230529/125c498feafa45d786b88d4141e88022.png [20200314140359176.png]: /images/20230529/e767ba4e43c94f8bb2ff1a5109007a4d.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MTI1OTk5_size_16_color_FFFFFF_t_70 2]: /images/20230529/ed7f77a5a3bc4f7cac5d661a86117a67.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MTI1OTk5_size_16_color_FFFFFF_t_70 3]: /images/20230529/6ae97caf43bf4530bef521ba1f520278.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MTI1OTk5_size_16_color_FFFFFF_t_70 4]: /images/20230529/35dfdf340ab24f45b7a2599b7fe16f4f.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM0MTI1OTk5_size_16_color_FFFFFF_t_70 5]: /images/20230529/07cd86116de34304ae44737f62d472d7.png
还没有评论,来说两句吧...