笔记:Spring Cloud Ribbon 客户端负载均衡

Spring Cloud Ribbon 是一个基于 HTTP 和 TCP 的客户端负载均衡工具,基于 Netflix Ribbon 实现,通过Spring Cloud 的封装,可以让我们轻松的将面向服务的REST 模板请求自动转换为客户端负载均衡的服务调用。客户端负载均衡在系统架构中是一个非常重要的,并且是不得不去实施的内容,因为负载均衡是对系统的高可用、网络压力的缓解和处理能力扩容的重要手段,客户端负载均衡需要通过心跳去维护服务端清单的健康性,这个需要服务注册中心配合完成,在Spring Cloud 实现的服务治理框架中,默认会创建针对各个服务治理框架的 Ribbon 自动化整合配置,比如 Eureka 中的 org.springframework.cloud.netflix.ribbon.eureka.RibbonEurekaAutoConfiguration 。

通过 Spring Cloud Ribbon 的封装,我们在微服务架构中使用客户端负载均衡调用非常简单,只需要分为两步:

  • 服务提供者只需要启动多个服务实例并注册到一个注册中心或是多个相关联的服务注册中心
  • 服务消费者直接通过调用被 @LoadBalanced 注解修饰过的 RestTemplate 来实现面向服务的接口调用。

调用示例如下:

  • 在 pom.xml 文件中需要增加 spring-cloud-starter-ribbon 和 spring-cloud-starter-eureka 依赖,代码如下:

    <?xml version="1.0" encoding="UTF-8"?>

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

            <modelVersion>4.0.0</modelVersion>

       

            <groupId>org.drsoft.consumer</groupId>

            <artifactId>consumer-helloservice</artifactId>

            <version>0.0.1-SNAPSHOT</version>

            <packaging>jar</packaging>

       

            <name>consumer-helloservice</name>

            <description>Demo project for Spring Boot</description>

       

            <parent>

                    <groupId>org.springframework.boot</groupId>

                    <artifactId>spring-boot-starter-parent</artifactId>

                    <version>1.5.6.RELEASE</version>

                    <relativePath/>

            </parent>

       

            <properties>

                    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

                    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

                    <java.version>1.8</java.version>

                    <spring-cloud.version>Dalston.SR2</spring-cloud.version>

            </properties>

       

            <dependencies>

                    <dependency>

                            <groupId>org.springframework.cloud</groupId>

                            <artifactId>spring-cloud-starter-eureka</artifactId>

                    </dependency>

                    <dependency>

                            <groupId>org.springframework.cloud</groupId>

                            <artifactId>spring-cloud-starter-ribbon</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>${spring-cloud.version}</version>

                                    <type>pom</type>

                                    <scope>import</scope>

                            </dependency>

                    </dependencies>

            </dependencyManagement>

       

            <build>

                    <plugins>

                            <plugin>

                                    <groupId>org.springframework.boot</groupId>

                                    <artifactId>spring-boot-maven-plugin</artifactId>

                            </plugin>

                    </plugins>

            </build>

    </project>

  • 创建应用主类 ConsumerHelloserviceApplication ,通过 @EanbleDiscoveryClient 注解将应用注册为Eureka 客户端应用,增加 @Bean 注解的返回 RestTemplate 类型的方法:

    @EnableDiscoveryClient

    @SpringBootApplication

    public class ConsumerHelloserviceApplication {

       

            public static void main(String[] args) {

                    SpringApplication.run(ConsumerHelloserviceApplication.class, args);

            }

       

            @LoadBalanced

            @Bean

            public RestTemplate createRestTemplate() {

                    return new RestTemplate();

            }

    }

  • 创建 ConsumerController 类,并实现 /consumer 接口,在类中注入 RestTemplate 并通过该类实例来调用服务,在接口中对服务的调用,这里使用的是服务的名而不是一个具体的地址,服务消费Java代码:

    @RestController

    public class ConsumerController {

            @Autowired

            private RestTemplate restTemplate;

              

            @RequestMapping (value = "/ribbonconsumer", method = RequestMethod.GET)

            public String ribbonConsumer() {

       

                    ResponseEntity<String> responseEntity = null;

                    try {

                            responseEntity =

                                            restTemplate.getForEntity("http://ORG.DRSOFT.WEBSERVICE.HELLOSERVICE/hello/get",

    String.class);

       

                            if (responseEntity.getStatusCode() == HttpStatus.OK) {

                                    return responseEntity.getBody();

                            }

       

                            return "response status " + responseEntity.getStatusCodeValue();

                    } catch (UnsupportedEncodingException e) {

                            e.printStackTrace();

                            return "error";

                    }

            }

    }

  • 在 application.properties 中,增加服务注册中的地址配置需要和服务提供者的一致,否则是无法发现服务的,同时设置该消费者的端口号,代码如下:

    server.port=8090

    spring.application.name=consumer-helloservice

       

    eureka.client.service-url.defaultZone=http://eurekaserver1:1111/eureka/,http://eurekaserver2:1112/eureka

  • 启动应用,访问地址 http://localhost:8090/ribbonconsumer ,即可看到请求返回信息

       

       

   

posted @ 2017-09-03 10:50  立3807  阅读(624)  评论(0编辑  收藏  举报