【SpringCloud基础】Ribbon负载均衡

妖狐艹你老母 2022-06-07 13:41 365阅读 0赞

前言

Github:https://github.com/yihonglei/thinking-in-springcloud

Eureka注册中心:eureka-server

服务提供者(订单服务):eureka-provider-order

ribbon消费(用户服务):eureka-consumer-ribbon

一 Ribbon概要

Ribbon是一个基于HTTP和TCP的客户端负载均衡器,Ribbon与Eureka结合使用时,

Ribbon会从Eureka注册中心获取服务端列表,通过某种轮询方式,达到负载均衡访问服务。

二 启动eureka-server(注册中心)

执行eureka-server项目EurekaServerApplication类的main方法。

三 启动eureka-provider-order(服务提供者)

1、注释掉eureka-provider-order配置文件application.properties中的instance-id配置,

使用默认配置,否则

  1. eureka.instance.instance-id=eureka-provider-order-8001

2、打包eureka-provider-order项目,通过命令行启动两个服务。

java -jar eureka-provider-order-1.0-SNAPSHOT.jar —server.port=8081

java -jar eureka-provider-order-1.0-SNAPSHOT.jar —server.port=8082

启动后查看eureka注册中心,会发现注册了eureka-provider-order的8081,8082端口服务。

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lobF9qeHk_size_16_color_FFFFFF_t_70

四 启动eureka-consumer-ribbon(服务消费者)

1、eureka-consumer-ribbon项目结构

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lobF9qeHk_size_16_color_FFFFFF_t_70 1

2、pom.xml依赖

引入Ribbon模块依赖。

  1. <!-- Eureka Client -->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  5. </dependency>
  6. <!-- Ribbon负载均衡依赖 -->
  7. <dependency>
  8. <groupId>org.springframework.cloud</groupId>
  9. <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
  10. </dependency>

3、application.properties

配置应用名称,服务端口,注册中心。

  1. # 注册到eureka服务端的微服务名称
  2. spring.application.name=eureka-consumer-ribbon
  3. # 服务提供端口
  4. server.port=8004
  5. # 注册到eureka服务端的地址
  6. eureka.client.service-url.defaultZone=http://localhost:9000/eureka/
  7. # 显示指定微服务的名称,默认ip:应用名称:端口(192.168.1.7:eureka-consumer-ribbon:8004)
  8. eureka.instance.instance-id=eureka-consumer-ribbon-8004
  9. eureka.instance.prefer-ip-address=true

4、MainConfig

创建RestTemplate应用实例,通过@LoadBalanced注解开启客户端负载均衡。

  1. package com.jpeony.ribbon.config;
  2. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.web.client.RestTemplate;
  6. /**
  7. * RestTemplate创建
  8. *
  9. * @author yihonglei
  10. */
  11. @Configuration
  12. public class MainConfig {
  13. @LoadBalanced
  14. @Bean
  15. public RestTemplate restTemplate() {
  16. return new RestTemplate();
  17. }
  18. }

5、UserController控制类

通过RestTemplate发起服务访问,而不是具体的地址。

  1. package com.jpeony.ribbon.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.http.ResponseEntity;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestMethod;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import org.springframework.web.client.RestTemplate;
  8. /**
  9. * -- @RestController这个注解等价于spring mvc用法中的@Controller+@ResponseBody
  10. *
  11. * @author yihonglei
  12. */
  13. @RestController
  14. @RequestMapping("/user")
  15. public class UserController {
  16. @Autowired
  17. private RestTemplate restTemplate;
  18. @RequestMapping(value = "/queryUserInfo", method = {RequestMethod.GET, RequestMethod.POST})
  19. public String queryUserInfo() {
  20. ResponseEntity<String> responseEntity =
  21. restTemplate.getForEntity("http://EUREKA-PROVIDER-ORDER/order/queryOrderInfo", String.class);
  22. return responseEntity.getBody();
  23. }
  24. }

6、ConsumerRibbonApplication应用启动

通过注解@EnableDiscoveryClient让应用注册到Eureka客户端,具有获取服务发现的能力。

  1. package com.jpeony.ribbon;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. /**
  6. * -- @SpringBootApplication 启动一个Spring Boot应用程序
  7. * -- @EnableDiscoveryClient 服务发现与注册,当应用启动时,将应用注册到配置的注册中心
  8. *
  9. * @author yihonglei
  10. */
  11. @EnableDiscoveryClient
  12. @SpringBootApplication
  13. public class ConsumerRibbonApplication {
  14. public static void main(String[] args) {
  15. SpringApplication.run(ConsumerRibbonApplication.class, args);
  16. }
  17. }

7、运行RibbonConsumerApplication类启动服务

注册中心多了eureka-consumer-ribbon服务。

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3lobF9qeHk_size_16_color_FFFFFF_t_70 2

五 访问eureka-consumer-ribbon服务,通过ribbon负载,

消费eureka-provider-order服务

http://localhost:8004/user/queryUserInfo

Ribbon从服务端获取eureka-consumer-ribbon服务列表,包含各个实例的位置,

然后Ribbon按照列表信息进行轮询访问,实现基于客户端的负载均衡。

20190622170226443.png

关于eureka-consumer-ribbon访问时控制台打印的关于客户端负载均衡日志。

  1. 2019-06-22 17:02:08.118 INFO 90932 --- [nio-8004-exec-1] c.n.l.DynamicServerListLoadBalancer:
  2. DynamicServerListLoadBalancer for client EUREKA-PROVIDER-ORDER initialized:
  3. -- 服务列表
  4. DynamicServerListLoadBalancer:{NFLoadBalancer:name=EUREKA-PROVIDER-ORDER,
  5. current list of Servers=[192.168.102.186:8081, 192.168.102.186:8082],
  6. -- 负载均衡
  7. Load balancer stats=Zone stats:
  8. {
  9. defaultzone=[
  10. Zone:defaultzone; Instance count:2;
  11. Active connections count: 0;
  12. Circuit breaker tripped count: 0;
  13. Active connections per server: 0.0;
  14. ]
  15. },
  16. -- 服务状态
  17. Server stats: [
  18. [
  19. Server:192.168.102.186:8082;
  20. Zone:defaultZone;
  21. Total Requests:0;
  22. Successive connection failure:0;
  23. Total blackout seconds:0;
  24. Last connection made:Thu Jan 01 08:00:00 CST 1970;
  25. First connection made: Thu Jan 01 08:00:00 CST 1970;
  26. Active Connections:0;
  27. total failure count in last (1000) msecs:0;
  28. average resp time:0.0;
  29. 90 percentile resp time:0.0;
  30. 95 percentile resp time:0.0;
  31. min resp time:0.0;
  32. max resp time:0.0;
  33. stddev resp time:0.0
  34. ],
  35. [
  36. Server:192.168.102.186:8081;
  37. Zone:defaultZone;
  38. Total Requests:0;
  39. Successive connection failure:0;
  40. Total blackout seconds:0;
  41. Last connection made:Thu Jan 01 08:00:00 CST 1970;
  42. First connection made: Thu Jan 01 08:00:00 CST 1970;
  43. Active Connections:0;
  44. total failure count in last (1000) msecs:0;
  45. average resp time:0.0;
  46. 90 percentile resp time:0.0;
  47. 95 percentile resp time:0.0;
  48. min resp time:0.0;
  49. max resp time:0.0;
  50. stddev resp time:0.0
  51. ]
  52. ]}
  53. ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@7abc34b6
  54. 2019-06-22 17:02:09.106 INFO 90932 --- [erListUpdater-0] c.netflix.config.ChainedDynamicProperty:
  55. Flipping property:
  56. EUREKA-PROVIDER-ORDER.ribbon.ActiveConnectionsLimit to use NEXT property:
  57. niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647

从日志中,可以看到Ribbon访问时客户端打印的服务列表,服务状态等等日志信息。

发表评论

表情:
评论列表 (有 0 条评论,365人围观)

还没有评论,来说两句吧...

相关阅读