• LoadBalancer


    一、手写随机负载均衡

        1、引入依赖

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-web</artifactId>
    4. </dependency>
    5. <!--引入nacos discovery-->
    6. <dependency>
    7. <groupId>com.alibaba.cloud</groupId>
    8. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    9. </dependency>

        2、controller定义

    1. @Resource
    2. private RestTemplate restTemplate;
    3. @Resource
    4. private DiscoveryClient discoveryClient;
    5. @RequestMapping("/restLoadbalance")
    6. public String manualLoadBlance(){
    7. //1、获取order服务urls
    8. List<ServiceInstance> instances = this.discoveryClient.getInstances("order");
    9. List<String> urls = instances.stream().map(instance->instance.getUri().toString()+"/order/query").collect(Collectors.toList());
    10. //2、随机负载请求
    11. int index = ThreadLocalRandom.current().nextInt(urls.size());
    12. return this.restTemplate.getForObject(urls.get(index),String.class);
    13. }

    二、LoadBalancer

          

        2.1、API

            (1)、引入依赖

    1. <dependency>
    2. <groupId>org.springframework.cloud</groupId>
    3. <artifactId>spring-cloud-starter-loadbalancer</artifactId>
    4. </dependency>

            (2)、代码

    1. //2、loadbalancer api
    2. @Resource
    3. private LoadBalancerClient loadBalancerClient;
    4. @RequestMapping("/loadbanlancerapi")
    5. public String loadBanlancerApi(){
    6. ServiceInstance serviceInstance = this.loadBalancerClient.choose("order");
    7. return this.restTemplate.getForObject(serviceInstance.getUri().toString()+"/order/query",String.class);
    8. }

        2.2、注解

            (1)、引入依赖   

    1. <dependency>
    2. <groupId>org.springframework.cloud</groupId>
    3. <artifactId>spring-cloud-starter-loadbalancer</artifactId>
    4. </dependency>

           (2)、自定义@LoadBanlancer注解得RestTemplate

    1. @Bean
    2. @LoadBalanced // 相当于为restTemplate整合了lb的能力
    3. public RestTemplate restTemplateLoadBalancer(){
    4. return new RestTemplate();
    5. }

            (3)、controller代码

    1. @Resource
    2. private RestTemplate restTemplateLoadBalancer;
    3. @RequestMapping("/loadbanlanceraop")
    4. public String loadBanlancerAop(){
    5. return this.restTemplateLoadBalancer.getForObject("http://order/order/query",String.class);
    6. }

    三、默认负载算法

        默认轮询负载:RoundRobinLoadBalancer

        更改默认负载算法:Cloud Native Applications

        (1)、自定义负载算法

    1. public class CustomLoadBalancerConfiguration {
    2. @Bean
    3. ReactorLoadBalancer<ServiceInstance> randomLoadBalancer(Environment environment,
    4. LoadBalancerClientFactory loadBalancerClientFactory) {
    5. String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
    6. return new RandomLoadBalancer(loadBalancerClientFactory
    7. .getLazyProvider(name, ServiceInstanceListSupplier.class),
    8. name);
    9. }
    10. }

        (2)、启动类增加注解

    1. @SpringBootApplication
    2. @LoadBalancerClients(defaultConfiguration = CustomLoadBalancerConfiguration.class)
    3. public class UserApplicaiton implements ApplicationRunner {
    4. }

    四、源码分析

        (1)、RestTemplate流程

        (2)、LoadBalancerClient#choose(String serviceId)

  • 相关阅读:
    LVS+NAT 负载均衡群集,NAT模式部署
    布线前,布局这步极为关键!分享一些PCB设计布线注意要点
    C#中HashMap和HashTable有什么区别
    Django——ORM增删改查
    解决ConfigurationBuilder未包含“SetBasePath”的定义
    【Rust】操作日期与时间
    meta-learning(元学习)是什么?
    未来社区的人车房隐私数据权属确认方法
    Python获取本机IP地址的几种方式~转
    2022一带一路暨金砖国家技能发展与技术创新大赛小程序应用开发国内赛-二等奖经验分享
  • 原文地址:https://blog.csdn.net/u011627218/article/details/139058719