mirror of https://github.com/ZhongFuCheng3y/austin
parent
ca4d91a8c2
commit
0c7fbc79b3
@ -0,0 +1,41 @@
|
||||
package com.java3y.austin.handler.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.ToString;
|
||||
|
||||
/**
|
||||
* @Author Gavin
|
||||
* @Date 2024/9/14
|
||||
*/
|
||||
@Getter
|
||||
@ToString
|
||||
@AllArgsConstructor
|
||||
public class LoadBalancerStrategy {
|
||||
|
||||
/**
|
||||
* 随机算法
|
||||
*/
|
||||
public static final String SERVICE_LOAD_BALANCER_RANDOM = "random";
|
||||
|
||||
/**
|
||||
* 加权随机算法
|
||||
*/
|
||||
public static final String SERVICE_LOAD_BALANCER_RANDOM_WEIGHT_ENHANCED = "random_weight";
|
||||
|
||||
/**
|
||||
* 哈希算法
|
||||
*/
|
||||
public static final String SERVICE_LOAD_BALANCER_HASH = "hash";
|
||||
|
||||
/**
|
||||
* 轮询算法
|
||||
*/
|
||||
public static final String SERVICE_LOAD_BALANCER_ROBIN = "robin";
|
||||
|
||||
/**
|
||||
* 加权轮询算法
|
||||
*/
|
||||
public static final String SERVICE_LOAD_BALANCER_ROBIN_WEIGHT = "robin_weight";
|
||||
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.java3y.austin.handler.loadbalance;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author Gavin
|
||||
* @Date 2024/9/14
|
||||
*/
|
||||
public interface ServiceLoadBalancer<T> {
|
||||
|
||||
/**
|
||||
* 以负载均衡的方式选取一个服务节点
|
||||
* @param servers 服务列表
|
||||
* @return 可用的服务节点
|
||||
*/
|
||||
T selectOne(List<T> servers);
|
||||
|
||||
/**
|
||||
* 以负载均衡的方式选取一个服务节点和一个备选服务节点
|
||||
* @param servers 服务列表
|
||||
* @return 可用的服务节点
|
||||
*/
|
||||
List<T> select(List<T> servers);
|
||||
}
|
||||
|
@ -0,0 +1,55 @@
|
||||
package com.java3y.austin.handler.loadbalance;
|
||||
|
||||
import com.java3y.austin.handler.loadbalance.annotations.LoadBalancer;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.aop.support.AopUtils;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* @Author Gavin
|
||||
* @Date 2024/9/14
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class ServiceLoadBalancerFactory<T> implements ApplicationContextAware {
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
private final Map<String, ServiceLoadBalancer<T>> serviceLoadBalancerMap = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
public List<T> selectService(List<T> servers, String loadbalancerStrategy) {
|
||||
ServiceLoadBalancer<T> serviceLoadBalancer = serviceLoadBalancerMap.get(loadbalancerStrategy);
|
||||
if (Objects.isNull(serviceLoadBalancer)) {
|
||||
log.error("没有找到对应的负载均衡策略");
|
||||
return servers;
|
||||
}
|
||||
return serviceLoadBalancer.select(servers);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
Map<String, Object> serviceMap = this.applicationContext.getBeansWithAnnotation(LoadBalancer.class);
|
||||
serviceMap.forEach((name, service) -> {
|
||||
if (service instanceof ServiceLoadBalancer) {
|
||||
LoadBalancer LoadBalancer = AopUtils.getTargetClass(service).getAnnotation(LoadBalancer.class);
|
||||
String loadbalancerStrategy = LoadBalancer.loadbalancer();
|
||||
//通常情况下 实现的负载均衡service与loadBalanceStrategy一一对应
|
||||
serviceLoadBalancerMap.put(loadbalancerStrategy, (ServiceLoadBalancer) service);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.java3y.austin.handler.loadbalance.annotations;
|
||||
|
||||
import com.java3y.austin.handler.enums.LoadBalancerStrategy;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 单机限流注解
|
||||
* Created by TOM
|
||||
* On 2022/7/21 17:03
|
||||
*
|
||||
* @author TOM
|
||||
*/
|
||||
@Target({ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Service
|
||||
public @interface LoadBalancer {
|
||||
|
||||
String loadbalancer() default LoadBalancerStrategy.SERVICE_LOAD_BALANCER_RANDOM_WEIGHT_ENHANCED;
|
||||
}
|
Loading…
Reference in new issue