Ribbon负载均衡

1.什么是负载均衡?

通俗的讲,负载均衡就是将负载(工作任务,访问请求)进行分摊到多个操作单元(服务器,组件)上进行执行

项目结构

2.自定义实现负载均衡

2.1.创建服务提供者

.2.1.1.拷贝nacos_provider           并改名(ribbon_provider)

分别设置俩个端口号:服务名需要一样

server:
  port: 9090
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.209.129:8848
  application:
    name: ribbon-provider

 server:
  port: 9091
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.209.129:8848
  application:
    name: ribbon-provider

2.2.创建服务消费者

2.2.1.创建工程

  拷贝nacos_consumer:并改名(ribbon_consumer)

修改配置文件

server:
  port: 80
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.209.129:8848
  application:
    name: ribbon-consumer

2.2.3.controller

package peng.controller;

import com.peng.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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Random;

@RestController
@RequestMapping(value = "/consumer")
public class ConsumerController {
    
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    private int index;

    @RequestMapping(value="/getUserById/{id}")
    public User getUserById(@PathVariable Integer id){
        List<ServiceInstance> serviceList = 
                            discoveryClient.getInstances("ribbon-provider");

        //随机方式获得服务
        //int currentIndex = new Random().nextInt(serviceList.size());
        //轮询方式获得服务
        index=index + 1;
        int currentIndex = (index) % serviceList.size();
        
        ServiceInstance instance = serviceList.get(currentIndex);
        String serviceUrl = instance.getHost() + ":" + instance.getPort();
        System.out.println("serviceUrl:"+serviceUrl);
        String url = "http://"+serviceUrl+"/provider/getUserById/"+id;
        return restTemplate.getForObject(url, User.class);
    }
}

2.3.测试

启动ribbon-consumer01访问测试

3.Ribbon介绍

3.1.什么是Ribbon

我们不需要去引入ribbon的依赖,因为在nacos里面已经集成了ribbon的依赖

Ribbon默认提供 很多种负载均衡算法,例如轮询、随机 等等。

3.2.负载均衡策略

负载均衡接口:com.netflix.loadbalancer.IRule

3.2.1.随机策略

 com.netflix.loadbalancer.RandomRule:该策略实现了从服务清单中随机选择一个服务实例的功能

3.2.2.轮询策略

 

4.基于ribbon实现负载均衡

4.1.修改ribbon_consumer

4.1.1.ConfigBean

package com.peng.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ConfigBean {

    @Bean
    /**
     * 添加了@LoadBalanced注解之后,Ribbon会给restTemplate请求添加一个拦截器,在拦截器中获取
     * 注册中心的服务列表,并使用Ribbon内置的负载均衡算法从服务列表里选中一个服务,通过获取到的服务信息        * (ip,port)替换 serviceId 实现负载请求。
     */
    @LoadBalanced //开启负载均衡
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }

    //随机策略
    @Bean
    public IRule iRule() {
        return new RandomRule();
    }
}

4.1.2.controller

package com.peng.controller;

import com.bjpowernode.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.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Random;

@RestController
@RequestMapping(value = "/consumer")
public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/getUserById/{id}")
    public User getUserById(@PathVariable Integer id) {
        //不使用ribbon:ip:port
        //String serviceUrl = "127.0.0.1:9090";
        //使用ribbon:不再使用ip:port的方式,而是改成了serviceId(即Nacos中注册的服务名称)
        String serviceUrl = "ribbon-provider";
        return restTemplate.getForObject("http://" + serviceUrl + 
                                         "/provider/getUserById/" + id, User.class);
    }
}

4.2.测试

  1. 分别使用轮询和随机策略调用服务提供者

  2. 目前负载均衡存在的问题,死亡的服务不会自动剔除。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值