负载均衡Ribbon

一.  什么是负载均衡?

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

 

二. 自定义实现负载均衡

2.1 创建服务提供者

  •  创建两个服务提供者工程:

  • 配置文件 application.yml:
ribbon_provider_1的配置文件
server:
  port: 9091
spring:
 cloud:
   nacos:
     discovery:
       server-addr: 192.168.128.132:8848 #nacos服务的地址
 application:
   name: ribbon-provider #向注册中心注册的名字


ribbon_provider_2的配置文件
server:
  port: 9092
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.128.132:8848 #nacos服务的地址
  application:
    name: ribbon-provider #向注册中心注册的名字
  • app

package com.bjpowernode;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient//向注册中心注册该服务,并可以获取其他服务的调用地址
public class RibbonProvider1App {
    public static void main(String[] args) {
        SpringApplication.run(RibbonProvider1App.class,args);
    }
}
package com.bjpowernode;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;


@SpringBootApplication
@EnableDiscoveryClient//向注册中心注册该服务,并可以获取其他服务的调用地址
public class RibbonProvider2App {
    public static void main(String[] args) {
        SpringApplication.run(RibbonProvider2App.class,args);
    }
}

2.2 创建服务消费者

  •  配置文件 application.yml:
server:
  port: 81
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.128.132:8848 #nacos服务的地址
  application:
    name: ribbon-consumer #向注册中心注册的名字

controller

package com.bjpowernode.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.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 currentIndex;

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

		//随机方式获得服务
		//int currentIndex = new Random().nextInt(serviceList.size());
		//轮询方式获得服务
		currentIndex = (currentIndex + 1) % 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 测试

浏览器访问http://192.168.128.132:8848/nacos 查看服务列表

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

 三. Ribbon介绍

3.1 什么是Ribbon

  • Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具

 

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

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

 3.2 基于ribbon实现负载均衡

 3.2.1 修改ribbon_consumer

  • ConfigBean

    package com.bjpowernode.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;
    
    
    @Configuration
    public class ConfigBean {
        @Bean
        @LoadBalanced //开启负载均衡,默认是轮询策略
        /**
         * 添加了@LoadBalanced注解之后,Ribbon会给restTemplate请求添加了一个拦截器,在拦截器中获取
         * 注册中心的所有可用服务,通过获取到的服务信息(ip,port) 替换 serviceId实现负载请求
         */
        public RestTemplate restTemplate(){
            return new RestTemplate();
        }
        随机策略
        @Bean
        public IRule iRule(){
            return new RandomRule();
        }
    }
    
  • controller
package com.bjpowernode.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.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 {
    //访问Rest服务的客户端
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;

    private int currentIndex;

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

 3.2.2 测试

        启动提供方和消费方的服务,分别测试随机和轮询的效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值