SpringCloud从入门到精通——本地负载均衡算法

SpringCloud从入门到精通——本地负载均衡算法,第1张

本地负载均衡算法

目录
  • 本地负载均衡算法
    • 1. 轮询算法
    • 2. 随机算法
    • 3. 故障转移算法
    • 4. 权重算法

1. 轮询算法
package com.demo.loadbalance;

import org.springframework.cloud.client.ServiceInstance;

public interface LoadBalance {
    /**
     * 负载均衡算法:给我多个地址,负载均衡会取出一个地址返回使用
     * @param serviceId
     * @return
     */
    ServiceInstance getInstances(String serviceId);
}

package com.demo.loadbalance;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author: JYC
 * @Title: RoundLoadBalance
 * @Description: TODO
 * @Date: 2022/4/22 11:23
 */
@Component
public class RoundLoadBalance implements LoadBalance {
    @Autowired
    private DiscoveryClient discoveryClient;
    private AtomicInteger atomicCount = new AtomicInteger(0);

    @Override
    public ServiceInstance getInstances(String serviceId) {
        // 1. 根据服务的名称,获取该服务集群地址列表
        List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
        // 2. 判断是否为null
        if (instances == null || instances.size() == 0) {
            return  null;
        }
        // 3. 使用负载均衡算法
        int index = atomicCount.incrementAndGet();
        return instances.get(index);
    }
}

package com.demo.service;

import com.demo.loadbalance.RoundLoadBalance;
import com.demo.utils.HttpClientUtils;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: JYC
 * @Title: OrderToMemberService
 * @Description: TODO
 * @Date: 2022/4/21 14:04
 */
@RestController
public class OrderToMemberService {
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private RoundLoadBalance roundLoadBalance;

    /**
     * 订单服务,调用 会员服务接口
     */
    @RequestMapping("/orderToMember")
    public String OrderToMember() {
        // HttpClient 工具类 实现RPC远程调用
//        String memberUrl = "http://192.168.66.1:8080/getMember";
        /**
         * 根据服务名称,从注册中心获取会员的接口地址
         */
//        List instances = discoveryClient.getInstances("demo-member");
//        ServiceInstance serviceInstance = instances.get(0);
        ServiceInstance serviceInstance = roundLoadBalance.getInstances("demo-member");
        // 会员服务的ip和端口
        String memberUrl = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/" + "getMember";
        return "订单服务调用会员服务:" + HttpClientUtils.doGet(memberUrl, null);
    }
}

2. 随机算法
package com.demo.loadbalance;

import org.springframework.cloud.client.ServiceInstance;

public interface LoadBalance {
    /**
     * 负载均衡算法:给我多个地址,负载均衡会取出一个地址返回使用
     * @param serviceId
     * @return
     */
    ServiceInstance getInstances(String serviceId);
}

package com.demo.loadbalance;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Component;

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

/**
 * @Author: JYC
 * @Title: RandomLoadBalance
 * @Description: TODO
 * @Date: 2022/4/22 11:37
 */
@Component
public class RandomLoadBalance implements LoadBalance{
    @Autowired
    private DiscoveryClient discoveryClient;


    @Override
    public ServiceInstance getInstances(String serviceId) {
        // 1. 根据服务的名称,获取该服务集群地址列表
        List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
        // 2. 判断是否为null
        if (instances == null || instances.size() == 0) {
            return  null;
        }
        // 生成随机数范围
        Random random = new Random();
        int index = random.nextInt(instances.size());
        return instances.get(index);
    }
}

package com.demo.service;

import com.demo.loadbalance.RandomLoadBalance;
import com.demo.loadbalance.RoundLoadBalance;
import com.demo.utils.HttpClientUtils;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: JYC
 * @Title: OrderToMemberService
 * @Description: TODO
 * @Date: 2022/4/21 14:04
 */
@RestController
public class OrderToMemberService {
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private RoundLoadBalance roundLoadBalance;
    @Autowired
    private RandomLoadBalance randomLoadBalance;

    /**
     * 订单服务,调用 会员服务接口
     */
    @RequestMapping("/orderToMember")
    public String OrderToMember() {
        // HttpClient 工具类 实现RPC远程调用
//        String memberUrl = "http://192.168.66.1:8080/getMember";
        /**
         * 根据服务名称,从注册中心获取会员的接口地址
         */
//        List instances = discoveryClient.getInstances("demo-member");
//        ServiceInstance serviceInstance = instances.get(0);
//        ServiceInstance serviceInstance = roundLoadBalance.getInstances("demo-member");
        ServiceInstance serviceInstance = randomLoadBalance.getInstances("demo-member");
        // 会员服务的ip和端口
        String memberUrl = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/" + "getMember";
        return "订单服务调用会员服务:" + HttpClientUtils.doGet(memberUrl, null);
    }
}

3. 故障转移算法
package com.demo.service;

import com.demo.loadbalance.RandomLoadBalance;
import com.demo.loadbalance.RoundLoadBalance;
import com.demo.utils.HttpClientUtils;
import lombok.extern.slf4j.Slf4j;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: JYC
 * @Title: OrderToMemberService
 * @Description: TODO
 * @Date: 2022/4/21 14:04
 */
@RestController
@Slf4j
public class OrderToMemberService {
    @Autowired
    private DiscoveryClient discoveryClient;

    /**
     * 订单服务,调用 会员服务接口
     */
    @RequestMapping("/orderToMember")
    public String OrderToMember() {
        /**
         * 根据服务名称,从注册中心获取会员的接口地址
         */
        List<ServiceInstance> instances = discoveryClient.getInstances("demo-member");
        for (int i = 0; i < instances.size(); i++) {
            try {
                ServiceInstance serviceInstance = instances.get(i);
                // 会员服务的ip和端口
                String memberUrl = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/" + "getMember";
            }catch (Exception e) {
                log.error("[rpc远程调用发生了故障,开始故障转移,切换下一个地址调用 e:{}]", e);
            }
        }
        return "fail";
    }
}

4. 权重算法
package com.demo;

import com.demo.loadbalance.LoadBalance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author: JYC
 * @Title: WeightLoadBalance
 * @Description: TODO
 * @Date: 2022/4/22 14:38
 */
@Component
public class WeightLoadBalance implements LoadBalance {
    @Autowired
    private DiscoveryClient discoveryClient;
    private AtomicInteger countAtomicInteger = new AtomicInteger(0);

    @Override
    public ServiceInstance getInstances(String serviceId) {
        // 1.根据服务Id名称,获取该接口多个实例
        List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
        if (instances == null) {
            return null;
        }

        List<ServiceInstance> newInstances = new ArrayList<>();
        // 循环遍历该服务名称,对应的多个实例
        instances.forEach((service) -> {
            // 获取该服务实例对应的权重比例
            Double weight = Double.parseDouble(service.getMetadata().get("nacos.weight"));
            for (int i = 0; i < weight; i++) {
                newInstances.add(service);
            }
        });
        // 线程安全
        return newInstances.get(countAtomicInteger.incrementAndGet() % newInstances.size());
    }
}

package com.demo.service;

import com.demo.WeightLoadBalance;
import com.demo.loadbalance.RandomLoadBalance;
import com.demo.loadbalance.RoundLoadBalance;
import com.demo.utils.HttpClientUtils;
import lombok.extern.slf4j.Slf4j;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: JYC
 * @Title: OrderToMemberService
 * @Description: TODO
 * @Date: 2022/4/21 14:04
 */
@RestController
@Slf4j
public class OrderToMemberService {
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private WeightLoadBalance weightLoadBalance;

    /**
     * 订单服务,调用 会员服务接口
     */
    @RequestMapping("/orderToMember")
    public String OrderToMember() {
        /**
         * 根据服务名称,从注册中心获取会员的接口地址
         */
//        List instances = discoveryClient.getInstances("demo-member");
//        ServiceInstance serviceInstance = instances.get(0);
        ServiceInstance serviceInstance = weightLoadBalance.getInstances("demo-member");
        // 会员服务的ip和端口
        String memberUrl = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/" + "getMember";
        return "订单服务调用会员服务:" + HttpClientUtils.doGet(memberUrl, null);
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/720891.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存