1. 程式人生 > 其它 >Kubernetes中使用ClusterDNS進行服務發現

Kubernetes中使用ClusterDNS進行服務發現

Http請求工具類

1、HTTP請求工具類程式碼

package test;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.Closeable;
import java.io.IOException;
import java.nio.charset.Charset;

public class HttpClient {
	
    private static final Log LOG = LogFactory.getLog(HttpClient.class);
    
    /** 
     * 傳送 get請求 返回Json
     */  
    public static String getJson(String url,String token) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        String responseMessange = "";
        try {
            HttpGet httpget = new HttpGet(uri);
            if(!"".equals(token))
                httpget.setHeader("x-wlk-Authorization",token);

            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                	responseMessange = EntityUtils.toString(entity);
                }
            } finally {  
                response.close();  
            }  
        } catch (Exception e) {
            LOG.error("get請求失敗",e);
        } finally {  
            // 關閉連線,釋放資源    
            try {  
                httpclient.close();  
            } catch (IOException e) {
                LOG.error("關閉連線失敗",e);
            }  
        }  
        return responseMessange;
    }
    
    /** 
     * 傳送post請求 返回Json
     */ 
    public String postJson(String url,String token,String body){
    	HttpPost post = null;
    	String responseMessange = "";
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            post = new HttpPost(url);
            // 構造訊息頭
            post.setHeader("Content-type",
                           "application/x-www.form-urlencoded; 
                           charset=utf-8");
            post.setHeader("Connection", "keep-alive");
            if(!"".equals(token))
                post.setHeader("x-wlk-Authorization",token);
            // 構建訊息實體
            StringEntity entity = new StringEntity(body, Charset.forName("UTF-8"));
            entity.setContentEncoding("UTF-8");
            // 傳送Json格式的資料請求
            entity.setContentType("application/x-www.form-urlencoded");
            post.setEntity(entity);
            HttpResponse response = httpClient.execute(post);
            try {  
                // 獲取響應實體    
                HttpEntity httpEntity = response.getEntity();
                if (entity != null) {
                	responseMessange = EntityUtils.toString(httpEntity);
                }
            } finally {  
                ((Closeable) response).close();  
            }  
        } catch (Exception e) {
            LOG.error("post請求失敗",e);
        }finally{
            if(post != null){
                try {
                    post.releaseConnection();
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    LOG.error("關閉連線失敗",e);
                }
            }
        }
        return responseMessange;
    }
}

2、請求url獲取token示例:

//url是舉得一個例子,具體地址以實際情況為主
String url="http://ip/api/getToken"+"user="+user+"password="+password;
String Response= HttpClient.getJson(url,"");
  if(Strings.isBlank(Response)){
	throw new Exception("獲取Response:"+Response);
  }else {
	JSONObject ResponseJson = JSONObject.parseObject(Response);
	String data = ResponseJson.getString("data");
	if(StringUtils.isBlank(data))
		throw new Exception("獲取Response失敗:"+Response);
	JSONObject dataJson = JSONObject.parseObject(data);
       //具體引數名稱以實際情況為主
	token = dataJson.getString("token");
  }