• Spring Cloud Ribbon


    Spring Cloud Ribbon:负载均衡的服务调用


    前言

    • 什么是Ribbon?

    Spring Cloud Ribbon是一套实现客户端负载均衡的工具,注意是客户端,当然也有服务端的负载均衡工具,如Ngnix,可以认为Ribbon就是一个负载均衡(Load Balancer)。负载均衡就是将用户的请求平摊的分配到多个服务器,从而达到系统的高可用。

    简单来说,Ribbon的主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起

    • 在微服务架构中,很多服务都会部署多个,其他服务去调用该服务的时候,如何保证负载均衡是个不得不去考虑的问题。负载均衡可以增加系统的可用性和扩展性,当我们使用RestTemplate来调用其他服务时,Ribbon可以很方便的实现负载均衡功能。

    RestTemplate的使用

    RestTemplate是一个HTTP客户端,使用它我们可以方便的调用HTTP接口,支持GET、POST、PUT、DELETE等方法。

    GET请求方法
    <T> T getForObject(String url, Class<T> responseType, Object... uriVariables);
    
    <T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables);
    
    <T> T getForObject(URI url, Class<T> responseType);
    
    <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables);
    
    <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables);
    
    <T> ResponseEntity<T> getForEntity(URI var1, Class<T> responseType);
    
    
    • getForObject方法

    返回对象为响应体中数据转化成的对象,如下:

    @GetMapping("/findByName/{userName}")
    public CommonResult getByUserName(@PathVariable String userName) {
        return restTemplate.getForObject(userServiceUrl + "/user/findByName/{1}", CommonResult.class, userName);
    }
    
    • getForEntity方法

    返回对象为ResponseEntity对象,包含了响应中的一些重要信息,比如响应头、响应状态码、响应体等,如下:

    @GetMapping("/getEntityByUsername")
    public CommonResult getEntityByUsername(@RequestParam String username) {
        ResponseEntity<CommonResult> entity = restTemplate.getForEntity(userServiceUrl + "/user/getByUsername?username={1}", CommonResult.class, username);
        if (entity.getStatusCode().is2xxSuccessful()) {
            return entity.getBody();
        } else {
            return new CommonResult("操作失败", 500);
        }
    }
    
    POST请求方法
    <T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables);
    
    <T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Map<String, ?> uriVariables);
    
    <T> T postForObject(URI url, @Nullable Object request, Class<T> responseType);
    
    <T> ResponseEntity<T> postForEntity(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables);
    
    <T> ResponseEntity<T> postForEntity(String url, @Nullable Object request, Class<T> responseType, Map<String, ?> uriVariables);
    
    <T> ResponseEntity<T> postForEntity(URI url, @Nullable Object request, Class<T> responseType);
    
    • postForObject示例
    @PostMapping("/create")
    public CommonResult create(@RequestBody User user) {
        restTemplate.postForObject(userServiceUrl + "/user/create", user, CommonResult.class);
        return new CommonResult(200, "操作成功");
    }
    
    • postForEntity
    @PostMapping("/create")
    public CommonResult create(@RequestBody User user) {
        return restTemplate.postForEntity(userServiceUrl + "/user/create", user, CommonResult.class).getBody();
    }
    
    PUT请求方法
    void put(String url, @Nullable Object request, Object... uriVariables);
    
    void put(String url, @Nullable Object request, Map<String, ?> uriVariables);
    
    void put(URI url, @Nullable Object request);
    

    示例

    @PutMapping("/update")
    public CommonResult update(@RequestBody User user) {
        restTemplate.put(userServiceUrl + "/user/update", user);
        return new CommonResult("操作成功",200);
    }
    
    DELETE请求方法
    void delete(String url, Object... uriVariables);
    
    void delete(String url, Map<String, ?> uriVariables);
    
    void delete(URI url);
    

    示例

    @DeleteMapping("/delete/{id}")
    public CommonResult delete(@PathVariable Long id) {
       restTemplate.delete(userServiceUrl + "/user/delete/{1}", null, id);
       return new CommonResult("操作成功",200);
    }
    

    正是开始Spring Cloud Ribbon的实战代码学习

    user-service

    首先创建一个user-service模块提供服务,用于给Ribbon调用

    创建模块user-service,并在pom.xml中添加依赖
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
    
    创建application.yml文件进行配置

    作为Ribbon调用的服务端,主要配置了服务名、端口和注册中心地址

    server:
      port: 8001    #端口
    
    spring:
      application:
        name: user-service    #应用名,后面Ribbon调用可直接通过应用名调用
    eureka:
      instance:
        hostname: localhost
      client:
        fetch-registry: true    #指定是否从注册中心获取服务列表
        register-with-eureka: true    #指定是否要注册到注册中心
        service-url:
          defaultZone: http://jamie:123456@localhost:8761/eureka/     #注册中心地址
    
    
    创建UserController用于提供调用接口

    UserController控制层类定义了对User对象常见的CRUD接口。

    package com.jamieLove.controller;
    
    import com.jamieLove.domain.CommonResult;
    import com.jamieLove.domain.User;
    import com.jamieLove.service.UserService;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    import java.util.List;
    
    /**
     * @Author:JamieLove
     * @Date:2019-12-10 13:42
     * @Description:
     */
    @RestController
    @RequestMapping("/user")
    public class UserController {
        private static Logger LOGGER = LoggerFactory.getLogger(UserController.class);
    
        @Autowired
        private UserService userService;
    
        @PostMapping("/create")
        public CommonResult create(@RequestBody User user) {
            userService.create(user);
            LOGGER.info("新增用户:{}", user);
            return new CommonResult(200, "新增成功");
        }
    
        @GetMapping("/all")
        public CommonResult getAllUserList() {
            List<User> userLists = userService.getAllUserList();
            LOGGER.info("查询所有 的用户列表:{}", userLists);
            return new CommonResult(200, "查询成功", userLists);
        }
    
        @GetMapping("/getUser/{id}")
        public CommonResult getUser(@PathVariable Long id) {
            User user = userService.getUser(id);
            LOGGER.info("通过id:{}查询用户信息:", id);
            return new CommonResult(200, "操作成功", user);
        }
    
        @PostMapping("/update")
        public CommonResult update(@RequestBody User user) {
            userService.update(user);
            LOGGER.info("更新用户信息:{}", user);
            return new CommonResult(200, "更新成功");
        }
    
        @GetMapping("/delete/{id}")
        public CommonResult delete(@PathVariable Long id) {
            userService.delete(id);
            LOGGER.info("根据id:{}删除用户信息", id);
            return new CommonResult(200, "删除成功");
        }
    
        @GetMapping("/findByName/{userName}")
        public CommonResult findByName(@PathVariable String userName) {
            User user = userService.getUserByName(userName);
            LOGGER.info("根据用户名:{}查询用户信息", userName);
            return new CommonResult(200, "查询成功", user);
        }
    
        @PostMapping("/getUserList")
        public CommonResult getUserList(@RequestBody List<Long> ids) {
            List<User> userList = userService.getUserList(ids);
            LOGGER.info("根据id集合:{}查询用户列表:{}", ids, userList);
            return new CommonResult(200, "查询成功", userList);
        }
    }
    
    
    创建User实体类和结果返回对象封装类
    package com.jamieLove.domain;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    import java.io.Serializable;
    
    /**
     * @Author:JamieLove
     * @Date:2019-12-10 12:48
     * @Description:
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class User implements Serializable {
        private static final long serialVersionUID = 9085447891383824313L;
    
        private Long id;
    
        private String userName;
    
        private String passWord;
    }
    
    package com.jamieLove.domain;
    
    import lombok.Data;
    
    /**
     * @Author:JamieLove
     * @Date:2019-12-10 12:51
     * @Description:
     */
    @Data
    public class CommonResult<T> {
    
        private Integer code;
    
        private String message;
    
        private T data;
    
        public CommonResult() {
        }
    
        public CommonResult(Integer code, String message, T data) {
            this.code = code;
            this.message = message;
            this.data = data;
        }
    
        public CommonResult(Integer code, String message) {
            this.code = code;
            this.message = message;
        }
    
        public CommonResult(T data) {
            this.data = data;
        }
    }
    
    创建User对象CRUD的service类

    接口类:

    package com.jamieLove.service;
    
    import com.jamieLove.domain.User;
    
    import java.util.List;
    
    /**
     * @Author:JamieLove
     * @Date:2019-12-10 12:55
     * @Description:
     */
    public interface UserService {
    
        void create(User user);
    
        User getUser(Long id);
    
        void update(User user);
    
        void delete(Long id);
    
        User getUserByName(String userName);
    
        List<User> getUserList(List<Long> ids);
    
        List<User> getAllUserList();
    }
    

    接口实现类:

    package com.jamieLove.service.impl;
    
    import com.jamieLove.domain.User;
    import com.jamieLove.service.UserService;
    import org.springframework.stereotype.Service;
    import org.springframework.util.CollectionUtils;
    
    import javax.annotation.PostConstruct;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.stream.Collectors;
    
    /**
     * @Author:JamieLove
     * @Date:2019-12-10 12:58
     * @Description:
     */
    @Service
    public class UserServiceImpl implements UserService {
    
        private List<User> userList;
    
        @Override
        public void create(User user) {
            userList.add(user);
        }
    
        @Override
        public User getUser(Long id) {
            List<User> findUserList = userList.stream().filter(userList -> userList.getId().equals(id)).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(findUserList)) {
                return findUserList.get(0);
            }
            return null;
        }
    
        @Override
        public void update(User user) {
            userList.stream().filter(userItem -> userItem.getId().equals(user.getId())).forEach(userItem -> {
                userItem.setUserName(user.getUserName());
                userItem.setPassWord(user.getPassWord());
            });
        }
    
        @Override
        public void delete(Long id) {
            User user = getUser(id);
            if (user != null) {
                userList.remove(user);
            }
        }
    
        @Override
        public User getUserByName(String userName) {
            List<User> findList = userList.stream().filter(userItem -> userItem.getUserName().equals(userName)).collect(Collectors.toList());
            if (!CollectionUtils.isEmpty(findList)) {
                return findList.get(0);
            }
            return null;
        }
    
        @Override
        public List<User> getUserList(List<Long> ids) {
            List<User> findUserList = userList.stream().filter(userItem -> ids.contains(userItem.getId())).collect(Collectors.toList());
            return findUserList;
        }
    
        @Override
        public List<User> getAllUserList() {
            return userList;
        }
    
        @PostConstruct
        public void initUser() {
            userList = new ArrayList<>();
            userList.add(new User(1L, "张三", "123456"));
            userList.add(new User(2L, "李四", "223456"));
            userList.add(new User(3L, "王五", "323456"));
        }
    }
    

    ribbon-service

    创建ribbon-service模块来调用user-service模块实现负载均衡的服务调用。

    创建模块ribbon-service,并在pom.xml中添加相关依赖
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
        </dependency>
    </dependencies>
    
    创建application.yml配置文件进行配置

    这里主要配置端口、应用名、注册中心地址、user-service的调用路径(通过应用名调用)以及ribbon相关的配置。

    server:
      port: 8003
    spring:
      application:
        name: ribbon-service
    eureka:
      instance:
        hostname: localhost
      client:
        fetch-registry: true
        register-with-eureka: true
        service-url:
          defaultZone: http://jamie:123456@localhost:8761/eureka/
    
    serice-url:
      user-service: http://user-service
    
    user-service:
      ribbon:
        ConnectTimeout: 1000 #服务请求连接超时时间(毫秒)
        ReadTimeout: 3000 #服务请求处理超时时间(毫秒)
        OkToRetryOnAllOperations: true #对超时请求启用重试机制
        MaxAutoRetriesNextServer: 1 #切换重试实例的最大个数
        MaxAutoRetries: 1 # 切换实例后重试最大次数
        NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule
    #    com.netflix.loadbalancer.RandomRule #修改负载均衡算法
    
    使用@LoadBalanced注解可以赋予RestTemplate负载均衡的能力
    package com.jamieLove.config;
    
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.client.RestTemplate;
    /**
     * @Author:JamieLove
     * @Date:2019-12-10 18:34
     * @Description:
     */
    @Configuration
    public class RibbonConfig {
    
        @Bean
        @LoadBalanced   //赋予了restTemplate具有负载均衡的能力
        public RestTemplate restTemplate() {
            return new RestTemplate();
        }
    }
    
    创建ribbon-service的控制层类
    package com.jamieLove.controller;
    
    import com.jamieLove.domain.CommonResult;
    import com.jamieLove.domain.User;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.*;
    import org.springframework.web.client.RestTemplate;
    
    /**
     * @Author:JamieLove
     * @Date:2019-12-10 18:36
     * @Description:
     */
    @RestController
    @RequestMapping("/user")
    public class RibbonServiceController {
    
        @Autowired
        private RestTemplate restTemplate;
    
        @Value("${serice-url.user-service}")
        private String userServiceUrl;
    
        @RequestMapping("/all")
        public CommonResult getAllUserList() {
            return restTemplate.getForObject(userServiceUrl + "/user/all", CommonResult.class);
        }
    
        @PostMapping("/create")
        public CommonResult create(@RequestBody User user) {
            restTemplate.postForObject(userServiceUrl + "/user/create", user, CommonResult.class);
            return new CommonResult(200, "操作成功");
        }
    
        @GetMapping("/getUser/{id}")
        public CommonResult getUser(@PathVariable Long id) {
            return restTemplate.getForObject(userServiceUrl + "/user/getUser/{1}", CommonResult.class, id);
        }
    
        @GetMapping("/findByName/{userName}")
        public CommonResult getByUserName(@PathVariable String userName) {
            return restTemplate.getForObject(userServiceUrl + "/user/findByName/{1}", CommonResult.class, userName);
        }
    }
    
    启动类
    package com.jamieLove;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    /**
     * @Author:JamieLove
     * @Date:2019-12-10 18:30
     *
     * @Description:
     */
    @SpringBootApplication
    @EnableDiscoveryClient
    public class RibbonServiceApplication {
        public static void main(String[] args) {
            SpringApplication.run(RibbonServiceApplication.class, args);
        }
    }
    
    负载均衡功能演示
    • 启动eureka-server:8761
    • 启动user-service:8001
    • 启动user-service:8002,可以通过修改IDEA中SpringBoot的启动配置实现:

    • 启动ribbon-service:8003

    注:以上服务启动成功,可在http://localhost:8761/eureka/有如下显示:

    可以发现运行在8001和8002的user-service控制台交替打印如下信息:

    可以通过修改ribbon的配置,改变负载均衡策略选择实例。

    user-service:
      ribbon:
        ConnectTimeout: 1000 #服务请求连接超时时间(毫秒)
        ReadTimeout: 3000 #服务请求处理超时时间(毫秒)
        OkToRetryOnAllOperations: true #对超时请求启用重试机制
        MaxAutoRetriesNextServer: 1 #切换重试实例的最大个数
        MaxAutoRetries: 1 # 切换实例后重试最大次数
        NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule
    #    com.netflix.loadbalancer.RandomRule #修改负载均衡算法
    

    与全局配置的区别就是ribbon节点挂在服务名称下面,如上就是对ribbon-service调用user-service时的单独配置

    Ribbon的负载均衡策略

    所谓负载均衡策略,就是当A服务调用B服务时,此时B服务有多个实例,这时A服务以何种方式来选择调用B服务的实例,Ribbon可以选择以下几种负载均衡策略。

    1. com.netflix.loadbalancer.RandomRule:从提供服务的实例中以随机的方式;
    2. com.netflix.loadbalancer.RoundRobinRule:以线性轮询的方式,就是维护一个计数器,从提供服务的实例中按顺序选取,第一次选第一个,第二次选第二个,以此类推,到最后一个以后再从头来过;
    3. com.netflix.loadbalancer.RetryRule:在RoundRobinRule的基础上添加重试机制,即在指定的重试时间内,反复使用线性轮询策略来选择可用实例;
    4. com.netflix.loadbalancer.WeightedResponseTimeRule:对RoundRobinRule的扩展,响应速度越快的实例选择权重越大,越容易被选择;
    5. com.netflix.loadbalancer.BestAvailableRule:选择并发较小的实例;
    6. com.netflix.loadbalancer.AvailabilityFilteringRule:先过滤掉故障实例,再选择并发较小的实例;
    7. com.netflix.loadbalancer.ZoneAwareLoadBalancer:采用双重过滤,同时过滤不是同一区域的实例和故障实例,选择并发较小的实例。
  • 相关阅读:
    洛谷1525关押罪犯——二分
    洛谷P1525关押罪犯——二分做法
    poj2411铺砖——状压DP
    1 理解Linux系统的“平均负载”
    3.2-3 tac、more
    3.20 tr:替换或删除字符
    3.14-19 wc、iconv、dos2unix、diff、vimdiff、rev
    3.21-22 od、tee
    指针和引用的区别
    new与malloc区别
  • 原文地址:https://www.cnblogs.com/MrAshin/p/12019869.html
Copyright © 2020-2023  润新知