一、背景

书接手写MQ框架(二)-服务端实现  ,前面介绍了服务端的实现。但是具体使用框架过程中,用户肯定是以客户端的形式跟服务端打交道的。客户端的好坏直接影响了框架使用的便利性。

虽然框架目前是通过web的形式提供功能的,但是某的目标其实是通过socket实现,所以不仅需要有客户端,还要包装一下,让用户在使用过程中不需要关心服务端是如何实现的。

简单来说,就是客户端使用必须方便。

二、客户端实现

1、HttpUtil

目前客户端的核心功能是HttpUtil这个类,使用httpClient实现的,主要是为了请求服务端。

具体实现如下:

package com.shuimutong.gmq.client.util;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import com.shuimutong.gmq.client.bean.HttpResponseBean;
import com.shuimutong.gutil.common.GUtilCommonUtil; /**
* http请求工具类
* @ClassName: HttpUtil
* @Description:(这里用一句话描述这个类的作用)
* @author: 水木桶
* @date: 2019年10月29日 下午9:43:54
* @Copyright: 2019 [水木桶] All rights reserved.
*/
public class HttpUtil {
private final static Logger log = LoggerFactory.getLogger(HttpUtil.class);
private static CloseableHttpClient HTTP_CLIENT = HttpClients.createMinimal();
static {
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
HTTP_CLIENT.close();
} catch (IOException e) {
log.error("HTTP_CLIENT-closeException", e);
}
}
});
} /**
* get请求
*
* @param url
* @return
* @throws IOException
*/
public static HttpResponseBean get(String url) throws IOException {
HttpResponseBean responseBean = null;
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse res = HTTP_CLIENT.execute(httpGet);
try {
HttpEntity httpEntity = res.getEntity();
String body = EntityUtils.toString(httpEntity);
responseBean = new HttpResponseBean(res.getStatusLine(), body);
EntityUtils.consume(httpEntity);
} finally {
res.close();
}
return responseBean;
} /**
* 带参数的get请求
* @param url
* @param requsetParams
* @return
* @throws IOException
* @throws URISyntaxException
*/
public static HttpResponseBean get(String url, Map<String, String> requsetParams) throws IOException {
HttpResponseBean responseBean = null;
HttpGet httpGet;
try {
URIBuilder uriBuilder = new URIBuilder(url);
if(!GUtilCommonUtil.checkListEmpty(requsetParams)) {
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
requsetParams.forEach((k,v) -> {
nvps.add(new BasicNameValuePair(k, v));
});
uriBuilder.setParameters(nvps);
}
httpGet = new HttpGet(uriBuilder.build());
} catch (Exception e) {
throw new IOException(e);
}
CloseableHttpResponse res = HTTP_CLIENT.execute(httpGet);
try {
HttpEntity httpEntity = res.getEntity();
String body = EntityUtils.toString(httpEntity);
responseBean = new HttpResponseBean(res.getStatusLine(), body);
EntityUtils.consume(httpEntity);
} finally {
res.close();
}
return responseBean;
} /**
* post请求
* @param url
* @param requsetParams
* @return
* @throws IOException
*/
public static HttpResponseBean post(String url, Map<String, String> requsetParams) throws IOException {
HttpResponseBean responseBean = null;
HttpPost httpPost = new HttpPost(url);
if(!GUtilCommonUtil.checkListEmpty(requsetParams)) {
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
requsetParams.forEach((k,v) -> {
nvps.add(new BasicNameValuePair(k, v));
});
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
}
CloseableHttpResponse response = HTTP_CLIENT.execute(httpPost);
try {
HttpEntity httpEntity = response.getEntity();
String body = EntityUtils.toString(httpEntity);
responseBean = new HttpResponseBean(response.getStatusLine(), body);
EntityUtils.consume(httpEntity);
} finally {
response.close();
}
return responseBean;
}
}

封装了get请求和post请求,封装了响应结果。

加了一个钩子,在jvm关闭时能够主动关闭创建的资源。

2、订阅消息、生产消息

这两部分主要就是调用上面的HttpUtil,然后将结果包装一下。

具体代码请参考前文的git。

3、实例管理

为了使得用户不需要关心具体实现,所以建了实例管理类。

package com.shuimutong.gmq.client.util;

import com.shuimutong.gmq.client.cache.CommonObjCache;
import com.shuimutong.gmq.client.cache.impl.CommonObjCacheImpl;
import com.shuimutong.gmq.client.consumer.GmqConsumer;
import com.shuimutong.gmq.client.producer.GmqProducer; public class GmqInstanceManage {
public static GmqProducer getGmqProducer(String gmqServerUrl) {
return new GmqProducer(gmqServerUrl);
} public static GmqConsumer getGmqConsumer(String gmqServerUrl) {
return new GmqConsumer(gmqServerUrl);
} public static CommonObjCache getCommonCache(String serverUrl) {
return new CommonObjCacheImpl(serverUrl);
}
}

主要是为了封装变化。因为之后再迭代的话,实例的具体实现肯定不是目前这么简单,所以要尽量让使用者少关心具体实现。

使用时关心的越多,后续项目迭代肯定越困难。

三、使用示例

1、生产消息

@Test
public void produceMsg() {
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、消费消息

主要思路是:消费消息之前,先查询当前已经消费到了哪条消息。消息消费之后,将消费的编号存入缓存。

典型的主动拉消息,消息是否消费由自己负责的模式。

实现如下:

@Test
public void comsumerMsgByCache() {
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<MqContent> 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版设计、开发中……

下一篇:手写MQ框架(四)-使用netty改造梳理

手写MQ框架(三)-客户端实现的更多相关文章

  1. 手写MQ框架(一)-准备启程

    一.背景 很久以前写了DAO框架和MVC框架,前段时间又重写了DAO框架-GDAO(手写DAO框架(一)-从“1”开始,源码:https://github.com/shuimutong/gdao.gi ...

  2. 手写MQ框架(二)-服务端实现

    一.起航 书接上文->手写MQ框架(一)-准备启程 本着从无到有,从有到优的原则,所以计划先通过web实现功能,然后再优化改写为socket的形式. 1.关于技术选型 web框架使用了之前写的g ...

  3. 手写MQ框架(四)-使用netty改造梳理

    一.背景 书接上文手写MQ框架(三)-客户端实现,前面通过web的形式实现了mq的服务端和客户端,现在计划使用netty来改造一下.前段时间学习了一下netty的使用(https://www.w3cs ...

  4. 手写DAO框架(三)-数据库连接

    -------前篇:手写DAO框架(二)-开发前的最后准备--------- 前言 上一篇主要是温习了一下基础知识,然后将整个项目按照模块进行了划分.因为是个人项目,一个人开发,本人采用了自底向上的开 ...

  5. 手写SpringMVC框架(三)-------具体方法的实现

    续接前文 手写SpringMVC框架(二)结构开发设计 本节我们来开始具体方法的代码实现. doLoadConfig()方法的开发 思路:我们需要将contextConfigLocation路径读取过 ...

  6. 手写SpringMVC 框架

    手写SpringMVC框架 细嗅蔷薇 心有猛虎 背景:Spring 想必大家都听说过,可能现在更多流行的是Spring Boot 和Spring Cloud 框架:但是SpringMVC 作为一款实现 ...

  7. 手写DAO框架(二)-开发前的最后准备

    -------前篇:手写DAO框架(一)-从“1”开始 --------- 前言:前篇主要介绍了写此框架的动机,把主要功能点大致介绍了一下.此篇文章主要介绍开发前最后的一些准备.主要包括一些基础知识点 ...

  8. 手写DAO框架(四)-SQL执行

    -------前篇:手写DAO框架(三)-数据库连接--------- 前言 通过上一篇写的方法,可以灵活的获取.释放数据库连接,拿到连接之后,我们就可以执行sql了!所以,本篇介绍的就是SQL执行器 ...

  9. (二)springMvc原理和手写springMvc框架

    我们从两个方面了解springmvc执行原理,首先我们去熟悉springmvc执行的过程,然后知道原理后通过手写springmvc去深入了解代码中执行过程. (一)SpringMVC流程图 (二)Sp ...

随机推荐

  1. mybatis自定义插件(拦截器)开发详解

    mybatis插件(准确的说应该是around拦截器,因为接口名是interceptor,而且invocation.proceed要自己调用,配置中叫插件)功能非常强大,可以让我们无侵入式的对SQL的 ...

  2. XML 中 5 个预定义的实体引用

    < < 小于 > > 大于 & & 和号 &apos; ' 省略号 " " 引号

  3. Windows上的那些效率神器,让你快到飞起

    转自:https://zhuanlan.zhihu.com/p/41771626 本文为Windows上有哪些让你效率倍增的软件和小技巧系列第二篇,上一篇请点击蓝字查看:打造高逼格PC,让你的电脑好用 ...

  4. 理解 uptime 的:“平均负载”? 如何模拟测试

    每次发现系统变慢时,我们通常做的第一件事,就是执行 top 或者 uptime 命令,来了解系统的负载情况.比如像下面这样,我在命令行里输入了 uptime 命令,系统也随即给出了结果. [root@ ...

  5. C# 嵌入第三方EXE界面到panel中

    C#可以通过windows API,将第三方程序嵌入到panel中,并且可以隐藏程序边框.问题:焦点在内部程序时,主窗口失去焦点:与内部EXE如何通讯? 代码如下: public partial cl ...

  6. Qt编写安防视频监控系统15-远程回放

    一.前言 远程回放有两种处理方式,一种是采用NVR厂家提供的SDK开发包来登录到NVR上,然后根据SDK的函数接口指定的视频文件,当然也有接口查询视频文件列表等:一种是采用视频监控行业的国标GB281 ...

  7. CentOS7 ping: unknown host www.baidu.com

    原文链接:https://blog.csdn.net/zz657114506/article/details/53871470

  8. [ ceph ] 基本介绍及硬件配置

    1. Ceph简介 所有的 Ceph 存储集群的部署都始于一个个 Ceph节点.网络和 Ceph存储集群.Ceph 存储集群至少需要一个 Ceph Monitor.一个 Manager和一个Ceph ...

  9. 【Spring Boot学习之十一】整合mongoDB

    环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2 参考: SpringBoot+Mongodb的使用

  10. elasticsearch-head

    elasticsearch-head 是用于监控 Elasticsearch 状态的客户端插件,包括数据可视化.执行增删改查操作等 安装前先安装nodejs 1.下载  地址 2.安装 npm ins ...