如何停止向到达负载极限的edge服务器重定向?
转载请注明:文章来自www.wowza.cn
注意: 要访问最新的技术资料,请访问http://www.ttstream.com/wowza/
这个模块安装在一个edge服务器上,当这台edge服务器到达负载极限时,www.wowza.cn,它会停止向Load Balancer listener发送自己的状态信息。这样新的客户连接请求就不会重定向到这台edge服务器上。

这个edge服务器不会限制客户端对它的直接访问,只是不会再有重定向到它的连接请求。

注意: 这个服务器必须按照>如何获得Wowza的动态负载均衡模块中的指南,被配置为Load Balancer Listener edge服务器。

Code:
package com.wowza.wms.plugin.collection.module;
// com.wowza.wms.plugin.collection.module.ModuleLoadBalancerEdgeLimitConnections
import com.wowza.wms.amf.AMFDataList;
import com.wowza.wms.application.IApplicationInstance;
import com.wowza.wms.client.ConnectionCounter;
import com.wowza.wms.client.IClient;
import com.wowza.wms.httpstreamer.cupertinostreaming.httpstreamer.HTTPStreamerSessionCupertino;
import com.wowza.wms.httpstreamer.sanjosestreaming.httpstreamer.HTTPStreamerSessionSanJose;
import com.wowza.wms.httpstreamer.smoothstreaming.httpstreamer.HTTPStreamerSessionSmoothStreamer;
import com.wowza.wms.logging.WMSLoggerIDs;
import com.wowza.wms.module.ModuleBase;
import com.wowza.wms.plugin.loadbalancer.LoadBalancerSender;
import com.wowza.wms.plugin.loadbalancer.ServerListenerLoadBalancerSender;
import com.wowza.wms.request.RequestFunction;
import com.wowza.wms.rtp.model.RTPSession;
import com.wowza.wms.server.Server;

public class ModuleLoadBalancerEdgeLimitConnections extends ModuleBase
{
	static final public int MAXCONNECTIONS = 200;
	
	private ConnectionCounter counter;
	private int maxEdgeConnections = MAXCONNECTIONS;
	
	private LoadBalancerSender loadBalancerSender;
	
	public void onAppStart(IApplicationInstance appInstance)
	{
		this.counter = appInstance.getConnectionCounter();
		this.maxEdgeConnections = appInstance.getProperties().getPropertyInt("maxEdgeConnections", maxEdgeConnections);
		loadBalancerSender = (LoadBalancerSender)Server.getInstance().getProperties().get(ServerListenerLoadBalancerSender.PROP_LOADBALANCERSENDER);
		getLogger().info("ModuleLoadBalancerEdgeLimitConnections maxEdgeConnections: " + maxEdgeConnections);
	}
	
	public void changeLimitEdge(IClient client, RequestFunction function,
			AMFDataList params) {
		Integer newLimit = params.getInt(PARAM1);
		this.maxEdgeConnections = newLimit;
		getLogger().info("ModuleLoadBalancerEdgeLimitConnections New Limit: " + newLimit);
	}
	
	private void addConnection() {
		
		loadBalancerSender = (LoadBalancerSender)Server.getInstance().getProperties().get(ServerListenerLoadBalancerSender.PROP_LOADBALANCERSENDER);
		
		if (loadBalancerSender == null)
		{
			getLogger().info("ModuleLoadBalancerEdgeLimitConnections Error (addConnection): Load Balancer Sender is not installed", WMSLoggerIDs.CAT_application, WMSLoggerIDs.EVT_comment);
			return;
		}
		
		long count = counter.getCurrent(); 
		
		if ((count+1) > this.maxEdgeConnections)
		{
			loadBalancerSender.pause();
			getLogger().info("ModuleLoadBalancerEdgeLimitConnections Pause", WMSLoggerIDs.CAT_application, WMSLoggerIDs.EVT_comment);
		}
		getLogger().info("ModuleLoadBalancerEdgeLimitConnections Count: " + count, WMSLoggerIDs.CAT_application, WMSLoggerIDs.EVT_comment);
	}
	
	private void removeConnection() {
		loadBalancerSender = (LoadBalancerSender)Server.getInstance().getProperties().get(ServerListenerLoadBalancerSender.PROP_LOADBALANCERSENDER);
		if (loadBalancerSender == null)
		{
			getLogger().info("ModuleLoadBalancerEdgeLimitConnections Error (removeConnection): Load Balancer Sender is not installed", WMSLoggerIDs.CAT_application, WMSLoggerIDs.EVT_comment);
			return;
		}
		
		long count = counter.getCurrent(); 
		
		if ((count-1) < this.maxEdgeConnections)
		{
			loadBalancerSender.unpause();
			getLogger().info("ModuleLoadBalancerEdgeLimitConnections UnPause", WMSLoggerIDs.CAT_application, WMSLoggerIDs.EVT_comment);
		}
		getLogger().info("ModuleLoadBalancerEdgeLimitConnections Count: " + count, WMSLoggerIDs.CAT_application, WMSLoggerIDs.EVT_comment);
	}
	
	public void onConnect(IClient client, RequestFunction function, AMFDataList params)
	{		
		addConnection();
	}
	
	public void onDisconnect(IClient client) {
		removeConnection();
	}
	
	public void onHTTPSmoothStreamingSessionCreate(HTTPStreamerSessionSmoothStreamer httpSmoothStreamingSession)
	{
		addConnection();		
	}
	
	public void onHTTPSmoothStreamingSessionDestroy(HTTPStreamerSessionSmoothStreamer httpSmoothStreamingSession)
	{
		removeConnection();		
	}
	
	public void onHTTPCupertinoStreamingSessionCreate(HTTPStreamerSessionCupertino httpCupertinoStreamingSession)
	{
		addConnection();		
	}

	public void onHTTPCupertinoStreamingSessionDestroy(HTTPStreamerSessionCupertino httpCupertinoStreamingSession)
	{
		removeConnection();		
	}
	
	public void onHTTPSanjoseStreamingSessionCreate(HTTPStreamerSessionSanJose httpSanJoseStreamingSession)
	{
		addConnection();
	}
	
	public void onHTTPSanjoseStreamingSessionDestroy(HTTPStreamerSessionSanJose httpSanJoseStreamingSession)
	{
		removeConnection();
	}

	public void onRTPSessionCreate(RTPSession rtpSession)
	{
		addConnection();	
	}

	public void onRTPSessionDestroy(RTPSession rtpSession)
	{
		removeConnection();
	}
}
这个模块编译后的版本已经被包含www.wowza.cn,在Wowza Modules Collection。 下载并解压缩后,将/lib/wms-plugin-collection.jar 拷贝到Wowza安装目录下的 /lib 文件夹下。然后重启Wowza。


在/conf/[app-name]/Application.xml文件的模块列表的最后添加下面的模块。

Code:
<Module> 
<Name>ModuleLoadBalancerEdgeLimitConnections</Name> 
<Description>Limit connects to an edge</Description> 
<Class>com.wowza.wms.plugin.collection.module.ModuleLoadBalancerEdgeLimitConnections</Class> 
</Module>
将下面的属性参数添加到属性列表中(在模块列表的下面)

Code:
<Property>
<Name>maxEdgeConnections</Name>
<Value>200</Value>
</Property>