MM"咱们选什么连接池组件?"
我“这个组件要满足连接池和HTTP协议,那就选HttpClient吧”
~~~~~~~安装的一些东西
版本: 4.3.3
编写代码时,从eclipse里导入相关jar包。
#elasticsearch
conf.elasticsearch.ip_port="192.168.56.200:9200,192.168.56.66:9200"
libraryDependencies ++= Seq(
javaJdbc,
javaEbean,
cache,
"mysql" % "mysql-connector-java" % "5.1.19",
"org.mongodb" % "mongo-java-driver" % "2.12.2",
"org.apache.httpcomponents" % "httpclient" % "4.3.3"
)
一些类
package controllers;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.CodingErrorAction;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.config.SocketConfig;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import play.Play;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import controllers.project1.v1.Remote;
public class ElasticSearchConnectionPool {
private static PoolingHttpClientConnectionManager cm;
private static CloseableHttpClient httpClient;
private static List<Remote> remoteAddress = new ArrayList<Remote>();
private static HashMap<Long,Integer> hashMap = new HashMap<Long,Integer>();
private ElasticSearchConnectionPool() {
// single instance...
}
public static void setup() {
cm = new PoolingHttpClientConnectionManager();
int maxTotalConnection = Play.application().configuration()
.getInt("conf.elasticsearch.maxTotalConnections");
cm.setMaxTotal(maxTotalConnection);
System.out.println("maxTotalConnection---"+maxTotalConnection);
cm.setDefaultMaxPerRoute(10);
SocketConfig defaultSocketConfig = SocketConfig.custom()
.setTcpNoDelay(true).setSoKeepAlive(true)
.setSoReuseAddress(true).build();
cm.setDefaultSocketConfig(defaultSocketConfig);
ConnectionConfig defaultConnectionConfig = ConnectionConfig.custom()
.setMalformedInputAction(CodingErrorAction.IGNORE)
.setUnmappableInputAction(CodingErrorAction.IGNORE)
.setCharset(Consts.UTF_8).build();
cm.setDefaultConnectionConfig(defaultConnectionConfig);
// 取配置文件中的IP:PORT
String ip_ports = Play.application().configuration()
.getString("conf.elasticsearch.ip_port");
String[] array = ip_ports.split(",");
int index = 0;
for (index = 0; index < array.length; index++) {
String str = array[index];
String[] ip_port = str.split(":");
System.out.println("ip-" + ip_port[0] + " port-" + ip_port[1]);
// 设置远程连接的个数
cm.setMaxPerRoute(
new HttpRoute(new HttpHost(ip_port[0], Integer
.parseInt(ip_port[1]))), maxTotalConnection
/ array.length);
System.out.println("maxTotalConnection/array.length---"+(maxTotalConnection/array.length));
Remote r = new Remote(ip_port[0], Integer.parseInt(ip_port[1]));
remoteAddress.add(r);
}
// 创建httpClient
httpClient = HttpClients.custom().setConnectionManager(cm).build();
}
public static CloseableHttpClient getConnection() {
return httpClient;
}
public static JsonNode post(String urlPrefix, JsonNode postData) {
long begin = System.currentTimeMillis();
// 根据负载均衡原则...循环取下一个...
JsonNode result = null;
long threadId = Thread.currentThread().getId();
System.out.println("current play thread:" + threadId);
int next;
Object obj = hashMap.get(threadId);
if (null == obj) {
next = 0;
} else {
next = (int) obj;
}
System.out.println("begin next:"+next);
int count = remoteAddress.size();
while (count-- > 0) {
Remote remote = (Remote) remoteAddress.get(next);
result = post1("http://" + remote.getIP() + ":" + remote.getPort()
+ urlPrefix, postData);
if (null != result) {
next = (next + 1) % remoteAddress.size();
break;
}
next = (next + 1) % remoteAddress.size();
}
hashMap.put(threadId, next);
long end = System.currentTimeMillis();
System.out.println("cost time:"+(end-begin)+" ms");
return result;
}
public static JsonNode post1(String fullUrl, JsonNode postData) {
System.out.println("full-url:"+fullUrl);
JsonNode result = null;
CloseableHttpClient client = null;
HttpPost httppost;
HttpResponse response;
HttpEntity entity = null;
InputStream is = null;
byte[] responseStr;
try {
client = getConnection();
httppost = new HttpPost(fullUrl);
httppost.addHeader("Content-Type", "application/json");
httppost.addHeader("User-Agent", "dewmobile");
httppost.setEntity(new StringEntity(postData.toString()));
response = client.execute(httppost);
entity = response.getEntity();
responseStr = new byte[(int) (entity.getContentLength())];
is = entity.getContent();
is.read(responseStr);
String str = new String(responseStr);
ObjectMapper mapper = new ObjectMapper();
result=mapper.readValue(str,JsonNode.class);
return result;
} catch (Exception e) {
System.out.println(e.toString());
return null;
} finally {
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != entity)
try {
entity.consumeContent();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (null != client) {
// 这个方法可以释放连接吗?网上是这么写的!
//System.out.println("invoke method ... client.getConnectionManager().shutdown();");
//client.getConnectionManager().shutdown();
}
}
}
}
package controllers.project1.v1;
public class Remote {
String ip;
int port;
public Remote(String ip, int port) {
this.ip = ip;
this.port = port;
}
public String getIP() {
return ip;
}
public int getPort() {
return port;
}
}
package controllers.project1.v1;
import org.elasticsearch.index.analysis.IkAnalyzerProvider;
import com.fasterxml.jackson.databind.JsonNode;
import play.mvc.Controller;
import play.mvc.Result;
import controllers.ElasticSearchConnectionPool;
import controllers.LoginSecurity;
public class Blog extends Controller {
public static Result search() {
// 校验登陆
boolean login = LoginSecurity.verify();
if (login == false) {
return badRequest("{\"result\":\"fail\",\"desc\":\"please login firstly!\"}");
}
//begin to search blog
System.out.println("-------------------------------------------------");
String urlPrefix = "/test_ik/blog_ik/_search";
JsonNode postData = request().body().asJson();
JsonNode obj = ElasticSearchConnectionPool.post(urlPrefix, postData);
if(null!=obj){
//System.out.println(obj.toString());
response().setContentType("application/json");
return ok(obj.toString());
}else{
return badRequest("{\"result\":\"fail\",\"desc\":\"search fail\"}");
}
}
}
运行:
结果: