【源码品读】IPing机制解析

/**
 * Interface that defines how we "ping" a server to check if its alive
 * @author stonse
 *
 */
public interface IPing {
    
    /**
     * Checks whether the given <code>Server</code> is "alive" i.e. should be
     * considered a candidate while loadbalancing
     * 
     */
    public boolean isAlive(Server server);
}
public class DummyPing extends AbstractLoadBalancerPing {

    public DummyPing() {
    }

    public boolean isAlive(Server server) {
        return true;
    }

    @Override
    public void initWithNiwsConfig(IClientConfig clientConfig) {
    }
}
public class NoOpPing implements IPing {

    @Override
    public boolean isAlive(Server server) {
        return true;
    }

}
public class PingUrl implements IPing {
public String getPingAppendString() {
		return pingAppendString;
}
String pingAppendString = "";
public void setPingAppendString(String pingAppendString) {
		this.pingAppendString = (pingAppendString != null) ? pingAppendString : "";
}
public String getExpectedContent() {
	return expectedContent;
}
String expectedContent = null;
public void setExpectedContent(String expectedContent) {
	this.expectedContent = expectedContent;
}
public boolean isAlive(Server server) {
		String urlStr   = "";
		if (isSecure){
			urlStr = "https://";
		}else{
			urlStr = "http://";
		}
		urlStr += server.getId();
		urlStr += getPingAppendString();

		boolean isAlive = false;

		HttpClient httpClient = new DefaultHttpClient();
		HttpUriRequest getRequest = new HttpGet(urlStr);
		String content=null;
		try {
			HttpResponse response = httpClient.execute(getRequest);
			content = EntityUtils.toString(response.getEntity());
			isAlive = (response.getStatusLine().getStatusCode() == 200);
			if (getExpectedContent()!=null){
				LOGGER.debug("content:" + content);
				if (content == null){
					isAlive = false;
				}else{
					if (content.equals(getExpectedContent())){
						isAlive = true;
					}else{
						isAlive = false;
					}
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			// Release the connection.
			getRequest.abort();
		}

		return isAlive;
}
public static void main(String[] args){
	PingUrl p = new PingUrl(false,"/cs/hostRunning");
	p.setExpectedContent("true");
	Server s = new Server("ec2-75-101-231-85.compute-1.amazonaws.com", 7101);
	
	boolean isAlive = p.isAlive(s);
	System.out.println("isAlive:" + isAlive);
}
public class NIWSDiscoveryPing extends AbstractLoadBalancerPing {
public boolean isAlive(Server server) {
	boolean isAlive = true;
	if (server!=null && server instanceof DiscoveryEnabledServer){
		DiscoveryEnabledServer dServer = (DiscoveryEnabledServer)server;	            
		InstanceInfo instanceInfo = dServer.getInstanceInfo();
		if (instanceInfo!=null){	                
			InstanceStatus status = instanceInfo.getStatus();
			if (status!=null){
				isAlive = status.equals(InstanceStatus.UP);
			}
		}
	}
	return isAlive;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值