Striveonger

vuePress-theme-reco Mr.Lee    2015 - 2025
Striveonger Striveonger
主页
分类
  • 文章
  • 笔记
  • 工具
标签
时间轴
简历
author-avatar

Mr.Lee

265

Article

134

Tag

主页
分类
  • 文章
  • 笔记
  • 工具
标签
时间轴
简历

SpringCloud 学习笔记-Ribbon负载均衡(三)

vuePress-theme-reco Mr.Lee    2015 - 2025

SpringCloud 学习笔记-Ribbon负载均衡(三)

Mr.Lee 2021-04-25 17:16:23 SpringCloudRibbon

SpringCloud 学习笔记, 第三章 Ribbon 负载均衡

服务发现的任务由 Eureka 的客户端完成, 而服务消费的任务由 Ribbon 完成.

Ribbon 是一个基于HTP和TCP的客户端负载均衡器, 它可以在通过客户端中配置的 RibbonServerList 服务端列表去轮询访问以达到均衡负载的作用. 当 Ribon 与 Eureka 联合使用时, Ribbon 的服务实例清单 RibbonServerList 会被 DiscoveryEnabledNIWSServerlist 重写, 扩展成从 Eureka 注册中心中获取服务端列表.

同时它也会用 NIWSDiscoveryPing 来取代 IPing, 它将职责委托给Eureka来确定服务端是否已经启动.

# 简单使用

# LoadBalancerClient

@Autowired
LoadBalancerClient loadBalancerClient;


/**
 * 调用hello的服务
 * @return
 */
@GetMapping("/load/balancer/message")
public Map<String, String> loadBalancerMessage() {
    Map<String, String> result = Maps.newHashMap();
    // 用负载均衡的客户端, 选择一个服务(ribbon)
    ServiceInstance instance = loadBalancerClient.choose("provider-hello");
    if (Objects.nonNull(instance)) {
        RestTemplate restTemplate = new RestTemplate();
        String url = instance.getUri() + "/hello";
        result.put("message", restTemplate.getForObject(url, String.class));
    } else {
        result.put("message", "empty...");
    }
    return result;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

负载均衡的原始写法, 通过Ribbon的客户端来选择服务实例来实现的

# @LoadBalanced

@Bean(name = "rest")
@LoadBalanced // 开启客户端的负载均衡
public RestTemplate rest() {
    return new RestTemplate();
}

@GetMapping("/rest/load/balancer/message")
public Map<String, String> loadBalancerMessageByRestTemplate() {
    Map<String, String> result = Maps.newHashMap();
    String url = "http://PROVIDER-HELLO/hello";
    result.put("message", rest.getForEntity(url, String.class).getBody());
    return result;
}
1
2
3
4
5
6
7
8
9
10
11
12
13

使用 @LoadBalanced 开启客户端的负载均衡

# 负载均衡策略

package com.striveonger.demo.client.web.config;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
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 Mr.Lee
 * @description:
 * @date 2021-04-25 11:23 AM
 */
@Configuration
public class BeanConfig {

    @Bean(name = "rest")
    @LoadBalanced // 负载均衡(客户端)
    public RestTemplate rest() {
        return new RestTemplate();
    }

    @Bean
    public IRule rule() {
        // 指定负载均衡策略
        // return new RoundRobinRule(); // 默认的策略
        return new RandomRule(); // 随机策略
    }
}
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

自定义的负载均衡策略 DiscoveryClient 取到所有的服务实例后, 选出一个实例来呗~

/**
 * @author Mr.Lee
 * @description:
 * @date 2021-03-31 5:47 PM
 */
@RestController
public class HelloController {

    @Autowired
    RestTemplate rest;

    /**
     * 使用被@LoadBalanced修饰的RestTemplate
     * Ribbon会通过URI中的服务名, 帮我们选择对应的服务实例
     * @return
     */
    @GetMapping("/rest/load/balancer/message")
    public Map<String, String> loadBalancerMessageByRestTemplate() {
        Map<String, String> result = Maps.newHashMap();
        String url = "http://provider-hello/hello";
        result.put("message", rest.getForEntity(url, String.class).getBody());
        return result;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24