本地负载均衡算法
1. 轮询算法
package com.demo.loadbalance;
import org.springframework.cloud.client.ServiceInstance;
public interface LoadBalance {
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;
@Component
public class RoundLoadBalance implements LoadBalance {
@Autowired
private DiscoveryClient discoveryClient;
private AtomicInteger atomicCount = new AtomicInteger(0);
@Override
public ServiceInstance getInstances(String serviceId) {
List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
if (instances == null || instances.size() == 0) {
return null;
}
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;
@RestController
public class OrderToMemberService {
@Autowired
private DiscoveryClient discoveryClient;
@Autowired
private RoundLoadBalance roundLoadBalance;
@RequestMapping("/orderToMember")
public String OrderToMember() {
ServiceInstance serviceInstance = roundLoadBalance.getInstances("demo-member");
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 {
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;
@Component
public class RandomLoadBalance implements LoadBalance{
@Autowired
private DiscoveryClient discoveryClient;
@Override
public ServiceInstance getInstances(String serviceId) {
List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
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;
@RestController
public class OrderToMemberService {
@Autowired
private DiscoveryClient discoveryClient;
@Autowired
private RoundLoadBalance roundLoadBalance;
@Autowired
private RandomLoadBalance randomLoadBalance;
@RequestMapping("/orderToMember")
public String OrderToMember() {
ServiceInstance serviceInstance = randomLoadBalance.getInstances("demo-member");
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;
@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);
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;
@Component
public class WeightLoadBalance implements LoadBalance {
@Autowired
private DiscoveryClient discoveryClient;
private AtomicInteger countAtomicInteger = new AtomicInteger(0);
@Override
public ServiceInstance getInstances(String serviceId) {
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;
@RestController
@Slf4j
public class OrderToMemberService {
@Autowired
private DiscoveryClient discoveryClient;
@Autowired
private WeightLoadBalance weightLoadBalance;
@RequestMapping("/orderToMember")
public String OrderToMember() {
ServiceInstance serviceInstance = weightLoadBalance.getInstances("demo-member");
String memberUrl = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/" + "getMember";
return "订单服务调用会员服务:" + HttpClientUtils.doGet(memberUrl, null);
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9daeb53dc58f5e2d704e27bf148568c1.png)