手写MQ框架(三)-客户端实现,
一、背景
书接手写MQ框架(二)-服务端实现 ,前面介绍了服务端的实现。但是具体使用框架过程中,用户肯定是以客户端的形式跟服务端打交道的。客户端的好坏直接影响了框架使用的便利性。
虽然框架目前是通过web的形式提供功能的,但是某的目标其实是通过socket实现,所以不仅需要有客户端,还要包装一下,让用户在使用过程中不需要关心服务端是如何实现的。
简单来说,就是客户端使用必须方便。
二、客户端实现
1、HttpUtil
目前客户端的核心功能是HttpUtil这个类,使用httpClient实现的,主要是为了请求服务端。
具体实现如下:
packagecom.shuimutong.gmq.client.util;importjava.io.IOException;importjava.net.URISyntaxException;importjava.util.ArrayList;importjava.util.List;importjava.util.Map;importorg.apache.http.HttpEntity;importorg.apache.http.NameValuePair;importorg.apache.http.client.entity.UrlEncodedFormEntity;importorg.apache.http.client.methods.CloseableHttpResponse;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.client.utils.URIBuilder;importorg.apache.http.impl.client.CloseableHttpClient;importorg.apache.http.impl.client.HttpClients;importorg.apache.http.message.BasicNameValuePair;importorg.apache.http.util.EntityUtils;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importcom.shuimutong.gmq.client.bean.HttpResponseBean;importcom.shuimutong.gutil.common.GUtilCommonUtil;/*** http请求工具类
* @ClassName: HttpUtil
* @Description:(这里用一句话描述这个类的作用)
*@author: 水木桶
* @date: 2019年10月29日 下午9:43:54
* @Copyright: 2019 [水木桶] All rights reserved.*/
public classHttpUtil {private final static Logger log = LoggerFactory.getLogger(HttpUtil.class);private static CloseableHttpClient HTTP_CLIENT =HttpClients.createMinimal();static{
Runtime.getRuntime().addShutdownHook(newThread() {
@Overridepublic voidrun() {try{
HTTP_CLIENT.close();
}catch(IOException e) {
log.error("HTTP_CLIENT-closeException", e);
}
}
});
}/*** get请求
*
*@paramurl
*@return*@throwsIOException*/
public static HttpResponseBean get(String url) throwsIOException {
HttpResponseBean responseBean= null;
HttpGet httpGet= newHttpGet(url);
CloseableHttpResponse res=HTTP_CLIENT.execute(httpGet);try{
HttpEntity httpEntity=res.getEntity();
String body=EntityUtils.toString(httpEntity);
responseBean= newHttpResponseBean(res.getStatusLine(), body);
EntityUtils.consume(httpEntity);
}finally{
res.close();
}returnresponseBean;
}/*** 带参数的get请求
*@paramurl
*@paramrequsetParams
*@return*@throwsIOException
*@throwsURISyntaxException*/
public static HttpResponseBean get(String url, Map requsetParams) throwsIOException {
HttpResponseBean responseBean= null;
HttpGet httpGet;try{
URIBuilder uriBuilder= newURIBuilder(url);if(!GUtilCommonUtil.checkListEmpty(requsetParams)) {
List nvps = new ArrayList();
requsetParams.forEach((k,v)->{
nvps.add(newBasicNameValuePair(k, v));
});
uriBuilder.setParameters(nvps);
}
httpGet= newHttpGet(uriBuilder.build());
}catch(Exception e) {throw newIOException(e);
}
CloseableHttpResponse res=HTTP_CLIENT.execute(httpGet);try{
HttpEntity httpEntity=res.getEntity();
String body=EntityUtils.toString(httpEntity);
responseBean= newHttpResponseBean(res.getStatusLine(), body);
EntityUtils.consume(httpEntity);
}finally{
res.close();
}returnresponseBean;
}/*** post请求
*@paramurl
*@paramrequsetParams
*@return*@throwsIOException*/
public static HttpResponseBean post(String url, Map requsetParams) throwsIOException {
HttpResponseBean responseBean= null;
HttpPost httpPost= newHttpPost(url);if(!GUtilCommonUtil.checkListEmpty(requsetParams)) {
List nvps = new ArrayList();
requsetParams.forEach((k,v)->{
nvps.add(newBasicNameValuePair(k, v));
});
httpPost.setEntity(newUrlEncodedFormEntity(nvps));
}
CloseableHttpResponse response=HTTP_CLIENT.execute(httpPost);try{
HttpEntity httpEntity=response.getEntity();
String body=EntityUtils.toString(httpEntity);
responseBean= newHttpResponseBean(response.getStatusLine(), body);
EntityUtils.consume(httpEntity);
}finally{
response.close();
}returnresponseBean;
}
}
封装了get请求和post请求,封装了响应结果。
加了一个钩子,在jvm关闭时能够主动关闭创建的资源。
2、订阅消息、生产消息
这两部分主要就是调用上面的HttpUtil,然后将结果包装一下。
具体代码请参考前文的git。
3、实例管理
为了使得用户不需要关心具体实现,所以建了实例管理类。
packagecom.shuimutong.gmq.client.util;importcom.shuimutong.gmq.client.cache.CommonObjCache;importcom.shuimutong.gmq.client.cache.impl.CommonObjCacheImpl;importcom.shuimutong.gmq.client.consumer.GmqConsumer;importcom.shuimutong.gmq.client.producer.GmqProducer;public classGmqInstanceManage {public staticGmqProducer getGmqProducer(String gmqServerUrl) {return newGmqProducer(gmqServerUrl);
}public staticGmqConsumer getGmqConsumer(String gmqServerUrl) {return newGmqConsumer(gmqServerUrl);
}public staticCommonObjCache getCommonCache(String serverUrl) {return newCommonObjCacheImpl(serverUrl);
}
}
主要是为了封装变化。因为之后再迭代的话,实例的具体实现肯定不是目前这么简单,所以要尽量让使用者少关心具体实现。
使用时关心的越多,后续项目迭代肯定越困难。
三、使用示例
1、生产消息
@Testpublic voidproduceMsg() {
GmqProducer producer=GmqInstanceManage.getGmqProducer(gmqServerUrl);for(int i=0; i<5; i++) {
String message= "message:" +i;try{
SendMqResult res=producer.sendMq(topic, message);
System.out.println(res.getRes());
}catch(SendMqException e) {
e.printStackTrace();
}
}
}
2、消费消息
主要思路是:消费消息之前,先查询当前已经消费到了哪条消息。消息消费之后,将消费的编号存入缓存。
典型的主动拉消息,消息是否消费由自己负责的模式。
实现如下:
@Testpublic voidcomsumerMsgByCache() {
GmqConsumer comsumer=GmqInstanceManage.getGmqConsumer(gmqServerUrl);
CommonObjCache commonCache=GmqInstanceManage.getCommonCache(gmqServerUrl);
String gmqSign= "gmq_consumer_id";long consumerId = 0;int size = 2;for(int i=0; i<5; i++) {try{
CacheObj cacheId=commonCache.getById(gmqSign);if(cacheId != null) {
consumerId=Long.parseLong(cacheId.getContent());
}
List res =comsumer.getMq(topic, consumerId, size);for(MqContent mq : res) {
System.out.println(JSONObject.toJSONString(mq));if(mq.getId() >consumerId) {
consumerId=mq.getId();
}
}
commonCache.save(gmqSign, String.valueOf(consumerId));
System.out.println("保存consumerId:" +consumerId);
}catch(Exception e) {
e.printStackTrace();
}
}
}
四、总结
gmq的初版至今已经完成,当然这只是开始。
后续计划先将gmvc框架替换掉,直接使用netty进行通信。
然后把消息存到数据库改为存到磁盘上。
然后就是服务的高可用改造。
届时欢迎指导。
第2版设计、开发中……
http://www.dengb.com/Javabc/1381861.htmlwww.dengb.comtruehttp://www.dengb.com/Javabc/1381861.htmlTechArticle手写MQ框架(三)-客户端实现, 一、背景 书接手写MQ框架(二)-服务端实现 ,前面介绍了服务端的实现。但是具体使用框架过程中,用户...