package com.lanyuanxiaoyao.service.sync.utils;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.lanyuanxiaoyao.service.sync.configuration.GlobalConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 动态平衡获取指标 url
 *
 * @author ZhangJiacheng
 * @date 2022-07-05
 */
public class LoadBalance {
    private static final Logger logger = LoggerFactory.getLogger(LoadBalance.class);
    private static String[] urls = null;
    private static int length = 0;

    public static String getCustomPublishUrl(GlobalConfiguration globalConfiguration) {
        if (ObjectUtil.isNull(urls)) {
            urls = globalConfiguration.getMetricsPublishCustomUrl().split(",");
            length = urls.length;
        }
        String url = urls[RandomUtil.randomInt(length)];
        logger.info("Random url: {}", url);
        return url;
    }
}
