SpringCloud学习笔记(第五章):客户端负载均衡 Ribbon
发布日期:2021-05-10 07:47:14 浏览次数:19 分类:精选文章

本文共 4555 字,大约阅读时间需要 15 分钟。

Ribbon���������������Spring Cloud���������������������������


Ribbon������������������

.Ribbon ������������Netflix ��������������������������������������������������������������������������� HTTP ��� TCP ��������������������������������������������������������������������������������������� Spring Cloud ������Ribbon ��������������������������������������������������������������������� RestTemplate ������������������������������


Ribbon������������������������������������

������������Ribbon ���������������������������(node) ��������������������������������������������������������������������� Nginx������������Ribbon ��������������������������������������� Nginx ������������������������������������������������������������������������������ Ribbon ������������������������������������������������������������������������������������������������ Eureka ��� Zookeeper������������

��� Spring Cloud ������Ribbon ��� RestTemplate ������������������������������������������������������ @LoadBalanced ���������RestTemplate ���������������������������������������������Ribbon ���������������������������������������������������������������


Ribbon������������������������������

��������������������������������������������������������� Ribbon ������������������������������

  • ���������������������������������������������������������
  • ��������������������������������� @LoadBalanced ��������� RestTemplate���
  • ������������������������������������������������������

    @Configuration
    public class BeanConfig {
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
    return new RestTemplate();
    }
    @Bean
    public IRule iRule() {
    return new RetryRule();
    }
    }

    ������������������ RetryRule ���������������������������������������������������������������


    Ribbon������������������

    Ribbon ������������������������������������������������������������

  • RoundRobinRule

    ������������������������������������������������������������������������������

  • RandomRule

    ������������������������������������������������������������������������������

  • AvailabilityFilteringRule

    ���������������������������������������������������������������������������������

  • WeightedResponseTimeRule

    ���������������������������������������������������������������������������������

  • RetryRule

    ���������������������������������������������������������������������������������������

  • BestAvailableRule

    ���������������������������������������������������������

  • ZoneAvoidanceRule

    ������������������������������������������������������������������������


  • Ribbon���������������������������

    ��������������������� IRule ������������������������������������������������������������������������������������������

    public class MyRule implements IRule {
    private final Log log = LoggerFactory.getLogger(MyRule.class);
    @Override
    public List
    > getNodes(String key) {
    // ������������������������������
    }
    @Override
    public int choose(String key, List
    > nodes) {
    // ������������������������������������
    return 1;
    }
    }

    ������������������������������������ MyRule���������������������������������������������


    RestTemplate������������

    RestTemplate ��� Spring ��������������������� Restful ��������������������������� HTTP ���������������������������������������������

    1. GET ������

    getForEntity ������
    ResponseEntity
    responseEntity = restTemplate.getForEntity("http://SERVICE-PROVIDER/hello", String.class);
    System.out.println(responseEntity.getBody());
    System.out.println(responseEntity.getStatusCode());
    uriVariables ���������������
    Map
    uriVariables = new HashMap<>();
    uriVariables.put("id", "1");
    ResponseEntity
    response = restTemplate.getForEntity(
    "http://SERVICE-PROVIDER/hello?id={id}",
    String.class,
    uriVariables
    );

    2. POST ������

    postForEntity ������
    DataTransferObject data = new DataTransferObject();
    restTemplate.postForEntity("http://SERVICE-PROVIDER/info", data, DataTransferObject.class);
    ���������������
    Map
    paramMap = new HashMap<>();
    paramMap.put("name", "������");
    restTemplate.postForEntity(
    "http://SERVICE-PROVIDER/user",
    null,
    "application/json",
    paramMap
    );

    ������

    .Ribbon ��������������������������������������������������������������������������������������������������� RestTemplate ���������������������������������������������������������������������������������������Ribbon ������������������������������������������������������������������������������������������������������������������

    上一篇:SpringCloud学习笔记(第八章):API网关Zuul
    下一篇:SpringCloud学习笔记(第三章):SpringCloud快速开发入门

    发表评论

    最新留言

    表示我来过!
    [***.240.166.169]2025年04月16日 23时48分00秒