zoukankan      html  css  js  c++  java
  • spring-cloud-ribbon负载均衡

    Ribbon

    Ribbon是一个基于HTTP和TCP客户端的负载均衡器。Feign中也使用Ribbon,后续会介绍Feign的使用。

    Ribbon可以在通过客户端中配置的ribbonServerList服务端列表去轮询访问以达到均衡负载的作用。

    当Ribbon与Eureka联合使用时,ribbonServerList会被DiscoveryEnabledNIWSServerList重写,扩展成从Eureka注册中心中获取服务端列表。同时它也会用NIWSDiscoveryPing来取代IPing,它将职责委托给Eureka来确定服务端是否已经启动。

    下面我们通过实例看看如何使用Ribbon来调用服务,并实现客户端的均衡负载。

    准备工作

    • 启动Chapter-9-1-1中的服务注册中心:eureka-server
    • 启动Chapter-9-1-1中的服务提供方:compute-service
    • 修改compute-service中的server-port为2223,再启动一个服务提供方:compute-service

    此时访问:http://localhost:1111/

    altalt

    可以看到COMPUTE-SERVICE服务有两个单元正在运行:

    • 192.168.21.101:compute-service:2222
    • 192.168.21.101:compute-service:2223

    使用Ribbon实现客户端负载均衡的消费者

    构建一个基本Spring Boot项目,并在pom.xml中加入如下内容:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
     
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent>
     
    <dependencies>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-ribbon</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</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>Brixton.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
    </dependency>
    </dependencies>
    </dependencyManagement>

    在应用主类中,通过@EnableDiscoveryClient注解来添加发现服务能力。创建RestTemplate实例,并通过@LoadBalanced注解开启均衡负载能力。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
     
    @SpringBootApplication
    @EnableDiscoveryClient
    public class RibbonApplication {
     
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
    return new RestTemplate();
    }
     
    public static void main(String[] args) {
    SpringApplication.run(RibbonApplication.class, args);
    }
     
    }

    创建ConsumerController来消费COMPUTE-SERVICE的add服务。通过直接RestTemplate来调用服务,计算10 + 20的值。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    @RestController
    public class ConsumerController {
     
    @Autowired
    RestTemplate restTemplate;
     
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(@RequestParam("param") String a) {
    return restTemplate.getForEntity("http://COMPUTE-SERVICE/add?a="+a, String.class).getBody();
    }
     
    }

    q将传来的参数a,拼接到链接上,负载均衡到两个COMPUTE-SERVICE上,“COMPUTE-SERVICE”是eureka中的注册结点名

    application.properties中配置eureka服务注册中心

    1
    2
    3
    4
    5
     
    spring.application.name=ribbon-consumer
    server.port=3333
     
    eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

    启动该应用,并访问两次:http://localhost:3333/add?a=111

    然后,打开compute-service的两个服务提供方,分别输出了类似下面的日志内容:

    • 端口为2222服务提供端的日志:
    1
    2016-06-02 11:16:26.787 INFO 90014 --- [io-2222-exec-10] com.didispace.web.ComputeController : /add, host:192.168.21.101, service_id:compute-service, result:30
    • 端口为2223服务提供端的日志:
    1
    2016-06-02 11:19:41.241 INFO 90122 --- [nio-2223-exec-1] com.didispace.web.ComputeController : /add, host:192.168.21.101, service_id:compute-service, result:30

    可以看到,之前启动的两个compute-service服务端分别被调用了一次。到这里,我们已经通过Ribbon在客户端已经实现了对服务调用的均衡负载。

  • 相关阅读:
    正则表达式
    正则表达式-量词
    正则表达式-字符组
    6月学习总结
    利用Magick和gs实现pdf到jpg的转换
    NGS Antenna Calibrations
    VS2017运行旧版本下的C程序工程
    C#.NET SQLite自适应32位/64位系统
    RAID1环境下外挂第三块硬盘
    修复 Fontconfig Error
  • 原文地址:https://www.cnblogs.com/wangjing666/p/6985047.html
Copyright © 2011-2022 走看看