spring cloud-ribbon 实现负载均衡

一.概述

Spring Cloud Ribbon 是一个基于Http和TCP的客服端负载均衡工具,它是基于Netflix Ribbon实现的。它不像服务注册中心、配置中心、API网关那样独立部署,但是它几乎存在于每个微服务的基础设施中。包括前面的提供的声明式服务调用也是基于该Ribbon实现的。理解Ribbon对于我们使用Spring Cloud来讲非常的重要,因为负载均衡是对系统的高可用、网络压力的缓解和处理能力扩容的重要手段之一。我们采用了声明式的方式来实现负载均衡。实际上,内部调用维护了一个RestTemplate对象,该对象会使用Ribbon的自动化配置,同时通过@LoadBalanced开启客户端负载均衡。其实RestTemplate是Spring自己提供的对象,不是新的内容。读者不知道RestTemplate可以查看相关的文档。

通过ribbon实现负载均衡,默认按照轮询方式

所谓轮询方式是:A,B两个服务,对请求的处理方式是AB,AB,AB......。

第一个请求 A处理;第二个请求B处理,总共A,B两个服务,到此第一轮询完成。

第三个请求A处理,第四个请求B处理,到此第二轮询完成。

以此类推......

https://www.cnblogs.com/liferecord/p/6893786.html

二.案例

2.1 ms-ribbon-conumer的操作

1.pom文件:

<!-- 注册eureka -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- 打印日志 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--  web-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

resources文件:

server:
  port: 8001
spring:
  application:
    name: ms-ribbon-consumer
eureka:
  client:
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://ljf:123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
test: nihao

controller类:

package com.ljf.weifuwu.springcloud.ribbon.controller;

import com.ljf.weifuwu.springcloud.ribbon.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class RibbonUserController {
    @Autowired
    private RestTemplate restTemplate;
    @GetMapping("/consumer-ribbon/{id}")
    public User findById(@PathVariable Long id) {
        // return this.restTemplate.getForObject("http://localhost:7900/eureka-provider/" + id, User.class);
        return this.restTemplate.getForObject("http://ms-eureka-provider/eureka-provider/" + id, User.class);
    }
}

model:

package com.ljf.weifuwu.springcloud.ribbon.model;

import java.math.BigDecimal;

public class User {
    private Long id;

    private String username;

    private String name;

    private Short age;

    private BigDecimal balance;

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Short getAge() {
        return this.age;
    }

    public void setAge(Short age) {
        this.age = age;
    }

    public BigDecimal getBalance() {
        return this.balance;
    }

    public void setBalance(BigDecimal balance) {
        this.balance = balance;
    }
}

启动类:

package com.ljf.weifuwu.springcloud.ribbon;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 * Hello world!
 *
 */
@SpringBootApplication
@EnableEurekaClient
public class RibbonConsumerApp
{
    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
    public static void main( String[] args )
    {    //ngix是服务端的负载均衡;ribbon是客户端的负载均衡器
        SpringApplication.run(RibbonConsumerApp.class, args);
        System.out.println( "ribbon consumer启动起来了!" );
    }
}

主要是在启动类上添加注解@loadbalanced注解。

启动eureka-sever,启动ms-eureka-approvider(7901),ms-eureka-approvider(7900),ms-eureka-approvider(7903),ms-ribbon-consumer(8001) 分别启动起来,如图:

 

访问:http://localhost:8001/consumer-ribbon/2 分别进行6次的访问。

ms-eureka-approvider(7901):

ms-eureka-approvider(7903):

两个服务提供者通过ribbon的负载均衡后,按照轮询法,分别都执行3次。(共访问了6次),ok大功告成!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值