• springCloud初识2-负载均衡Ribbon


    负载均衡Ribbon

    Ribbon是Netflix 发布的负载均衡器,它有助于控制HTTP和TCP客户端的行为。为Ribbon配置服务提供者地址列表后,Ribbon 就可基于某种负载均衡算法,自动地帮助服务泌费者去请求。
    Ribbon 默认为我们提供了很多的负载均衡算法,例如轮询(一个一个轮下去到结尾又从头开始)、随机等。当然,我们也可为Ribbon实现自定义的负载均衡算法。

    基本使用

    1--引入启动器(消费方引入)

    eureka默认集成了ribbon,引入了eureka后可以不用再引入ribbon
    

    2--覆盖默认配置

    可不用覆盖任何配置也可以使用
    

    3--引导类启用组件(消费方开启)

    package com.xiaoai.service;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.client.loadbalancer.LoadBalanced;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;
    
    @SpringBootApplication
    @EnableDiscoveryClient  //启用eureka客户端,@EnableEurekaClient也可以
    public class XiaoaiServiceConsumerApplication {
    
    	@Bean
    	@LoadBalanced  //开启ribbon负载均衡
    	public RestTemplate restTemplate(){
    		return new RestTemplate();
    	}
    
    	public static void main(String[] args) {
    		SpringApplication.run(XiaoaiServiceConsumerApplication.class, args);
    	}
    }
    
    

    4--消费方控制器方法消费

    package com.xiaoai.service.controller;
    
    import com.xiaoai.service.pojo.User;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cloud.client.ServiceInstance;
    import org.springframework.cloud.client.discovery.DiscoveryClient;
    import org.springframework.web.bind.annotation.*;
    import org.springframework.web.client.RestTemplate;
    
    import java.util.List;
    
    @RestController
    @RequestMapping("/consumer/user")
    public class UserController {
    
    //    //--------------------------------------------消费方传统方式调用提供方
    //    @Autowired
    //    private RestTemplate restTemplate;
    
    //    @GetMapping
    //    @ResponseBody
    //    public User queryUserById(@RequestParam("id")Long id){
    //        return this.restTemplate.getForObject("http://localhost:8081/user/"+id,User.class);
    //    }
    
    //    //--------------------------------------------改造消费方,解决地址硬编码问题
    //    @Autowired
    //    private RestTemplate restTemplate;
    //    @Autowired
    //    private DiscoveryClient discoveryClient;  //最早的时候服务发现注册都是通过DiscoveryClient来实现的,随着版本变迁把DiscoveryClient服务注册抽离出来变成了ServiceRegistry抽象,专门负责服务注册,DiscoveryClient专门负责服务发现
    //
    //    @GetMapping
    //    @ResponseBody
    //    public User queryUserById(@RequestParam("id")Long id){
    //        List<ServiceInstance> instances = discoveryClient.getInstances("service-provider"); //通过serviceId获取实例列表
    //        ServiceInstance instance = instances.get(0); //获取一个服务实例,由于这里只运行了一个提供服务,直接获取列表第一个即可
    //        return this.restTemplate.getForObject("http://" + instance.getHost() + ":" + instance.getPort() + "/user/"+id,User.class); //通过实例拼接对应的url
    //    }
    
        //-----------------------------------------------改造消费方,解决地址硬编码问题    启用ribbon负载均衡后
        @Autowired
        private RestTemplate restTemplate;
    
        @GetMapping
        @ResponseBody
        public User queryUserById(@RequestParam("id")Long id){
            return this.restTemplate.getForObject("http://service-provider/user/"+id,User.class);
        }
    
    }
    

    定义负载均衡策略

    可实现 Irule接口自定义策略
    默认已经实现了好几种

    如何设置默认某一种策略?
    可以在消费服务端application.yml配置文件加入配置

    service- provider:  # 这是服务提供方的服务id即之前自己注册服务时定义的名称
      ribbon:
        NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule  
    
  • 相关阅读:
    Kubernetes学习之路(十)之资源清单定义
    Kubernetes学习之路(十一)之Pod状态和生命周期管理
    Kubernetes学习之路(七)之Coredns和Dashboard二进制部署
    Kubernetes学习之路(九)之kubernetes命令式快速创建应用
    Kubernetes学习之路(八)之Kubeadm部署集群
    Ceph学习之路(三)Ceph luminous版本部署
    Kubernetes学习之路(六)之创建K8S应用
    Redis学习之路(二)之Redis入门基础
    Redis学习之路(一)之缓存知识体系
    OpenStack入门篇(二十二)之实现阿里云VPC的SDN网络
  • 原文地址:https://www.cnblogs.com/xiaoaiying/p/13432839.html
Copyright © 2020-2023  润新知