动态策略

 

 

 

 

从spring 配置文件中 设置 每台服务器的 权重, 或者 在 .properties 中设置 ,进行热部署,,,,

public class WeightAllot implements Allot
{
	protected static Logger logger = LoggerFactory.getLogger(WeightAllot.class);
	@Resource
	private AoigwService aoigwService;

	// 该字符串为逗号分割的数字 格式如:1,2,3
	private String weights;
	/** 数据库中的AOG列表 */
	List<AoiGw> aoigwlist = null;
	/** 每个AOG的权值 */
	int[] aoiws;
	/**
	 * 总权值
	 */
	static int num = 0;

	@Override
	public void afterPropertiesSet() throws Exception
	{
		aoigwlist = aoigwService.findAll();
		String[] ws = weights.replaceAll(",", ",").replaceAll(" ", "").trim().split(",");

		aoiws = new int[aoigwlist.size()];
		for (int i = 0; i < aoiws.length; i++)
		{
			aoiws[i] = 1;
			if (i < ws.length)
			{
				try
				{
					aoiws[i] = Integer.parseInt(ws[i]);
				}
				catch (Exception e)
				{
				}
			}
			num += aoiws[i];
		}
		logger.info("aogdns Allot: aogNode = " + aoigwlist.size() + ", weightStr = " + weights + ", sum = " + num);
	}

	public AoiGw getAoiGw(String lid)
	{
		// 先取LID后8位转成INT值,然后与权值和取模
		long ai = 1;
		try
		{
			String s = lid.substring(lid.length() - 8, lid.length());
			ai = Long.parseLong(s, 16);
		}
		catch (Exception e)
		{
			logger.warn("", e);
		}
		int mod = (int) (ai % num);

		int temp = 0;
		AoiGw result = aoigwlist.get(0);
		for (int i = 0; i < aoiws.length; i++)
		{
			temp += aoiws[i];
			if (mod < temp)
			{
				result = aoigwlist.get(i);
				break;
			}
		}
		if (logger.isDebugEnabled())
			logger.debug("lid:" + lid + ", mod: " + mod + ", serverId: " + result.getAoiId());
		return result;
	}

	public String getWeights()
	{
		return weights;
	}

	public void setWeights(String weights)
	{
		this.weights = weights;
	}

	public AoigwService getAoigwService()
	{
		return aoigwService;
	}

	public void setAoigwService(AoigwService aoigwService)
	{
		this.aoigwService = aoigwService;
	}

	public static void main(String[] args)
	{
		try
		{
			Configuration.init("applicationContext.xml");
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		Allot allot = Configuration.getInstance().getAllot();
		
		for (int i = 0; i < 50; i++)
		{
			int tmp = new Random().nextInt(Integer.MAX_VALUE);
			System.out.println(" num: " + num + ", people: " + tmp + ", id: "
					+ allot.getAoiGw(MseqUtil.toHex((int) tmp)).getAoiId());
		}
		System.exit(1);
	}
}

 

 

 

 

package Java2014.java20141102;

import java.util.ArrayList;
import java.util.List;

public class OtherCategary {
	
	static int  num =6 ;
	static int[] aoiws = {2,0,3,1};
	/**
	 * <pre>
	 *                    1             0   1
	 *                        1         2   3
	 *                          1       3   4
	 *                    1             0   1 
	 *                        1         2   3 
	 *                        1         2   3 
	 *   </pre>                      
	 *   标记 flag  第一次 flag = 0 ;
	 *   注册一次  用户后 flag 偏移一位 ,flag++ ;  flag = (flag >= aoiws.length?0:flag);
	 *                      if(reaoiws[flag]<aoiws[flag]) {}  else { continue;} 
	 *      for (i< max )  
	 *        
	 *       j  if(reaoiws[flag] < aoiws[flag]) reaoiws[flag]++; flag++;flag = (flag >= aoiws.length?0:flag);
	 *        
	 *                     
	 */
	static int[] reaoiws = null;
	static List aoigwlist = null;
	static int currIndex = 0;
	static {
		aoigwlist = new ArrayList<Integer>();
		aoigwlist.add(1);
		aoigwlist.add(2);
		aoigwlist.add(3);
		aoigwlist.add(4);
		reaoiws = new int[aoigwlist.size()];
	}
	
	public static Integer getAoiGw(Integer ai) {
		Integer result = (Integer) aoigwlist.get(0);
		if(ai % num == 0) {currIndex = 0;reaoiws = null;reaoiws = new int[aoigwlist.size()];}
		for (int i = 0; i < aoiws.length; i++) {
		 if(reaoiws[currIndex] < aoiws[currIndex]){
			  reaoiws[currIndex]++; 
			  result = (Integer) aoigwlist.get(currIndex);
			  currIndex = (currIndex >= aoiws.length-1 ? 0 : currIndex+1);
			 break;
		 }else{
			 currIndex = (currIndex >= aoiws.length-1 ? 0 : currIndex+1);
		 }
		}
		return result;
	}
	
	public static void main(String[] args) {
      for (int i = 0; i < 40; i++) {
    	  if(i%6==0) System.out.println ( );
    	 // try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }
    	  System.out.print ( getAoiGw(i) +" ");
    	 
      }
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值