您的位置:首页 > 运维架构

RabbitMQ监控(3)——核心监控类实现

2016-03-23 13:45 801 查看
在监控时,我们的思路大概是这样的,使用rabbitmq的java客户端开发检测端口,发现不符合要求的性能参数,予以警告,给负责人发送短信或邮件,并提供前台可视化的监控界面,大家先看整体设计:

流程设计:



数据设计

根据这个过程,我们看看数据库设计图:

请参看文章底部《附录:数据库设计》

代码实现

其中我们用到一个比较关键的方法,在官方api中如此介绍:



RabbitMQ监控类:

@Service("rabbitMQService")
public class RabbitMQServiceImpl implements IRabbitMQService, IDebugService {

@Override
public Boolean checkOneBusinessMQ(Business business) throws Exception {
if(business == null ){
return false;
}
boolean isFinish=false;
Channel channel = null;
Connection conn = null;
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername(business.getMqUsername());
factory.setPassword(business.getMqPassword());
if(!(business.getMqVirtualhost()==null || business.getMqVirtualhost().trim().isEmpty() || business.getMqVirtualhost().equals("no"))){
factory.setVirtualHost(business.getMqVirtualhost());
}

factory.setHost(business.getMqHost());
factory.setPort(Integer.valueOf(business.getMqPort()));
try {
conn = factory.newConnection();
channel = conn.createChannel();
long count = channel.messageCount(business.getMqQueueName());
HashMap<String,String> hashMap = JSON.parseObject(business.getWarningconfig(),HashMap.class);
Integer max=Integer.valueOf(hashMap.get(RabbitMQ.WarningConfig_Max.getValue()));
try {
FileSaveMq.addOneBusinessDate(business.getId().toString(), count + "");
}catch (Exception e){
log.error("=checkOneBusinessMQ=>error write file"+business.getBusinessName(),e);
}
if(count <=max){
isFinish=true;
}else{
isFinish=false;
isFinish = sendWarningOnebusiness(business);
}
}catch(Exception e){
log.error("=checkOneBusinessMQ=>error business name:"+business.getBusinessName(),e);
sendWarningOnebusiness(business);
}finally {
if(channel !=null){
channel.close();
if(conn != null){
conn.close();
}
}
}
return isFinish;
}

@Override
public Boolean checkOneService(ControlService controlService) throws Exception {
if(controlService == null ){
return false;
}
boolean isFinish=false;
if(URLUtils.isConnect(controlService.getServiceUrl())){
isFinish=true;
}else{
isFinish = sendWarningOneservice(controlService);
isFinish=false;
}
return isFinish;
}

}


总结:

有了核心代码,其他的都是辅助其实现的工具,在这个基础之上,我们就可以扩展我们的思路,想想如何做页面展示,如何发送短信,邮件等,看似高大上的技术,其实也是简单技术的积累加上人的大脑创新!现在看看,“世上无难事,只怕有心人”这句话,真的是至理名言!

附录

File帮助类:

/**
* Created by Administrator on 2016/3/22.
*/
public class FileSaveMq {

public static void addOneBusinessDate(String key,String value){
String json = getDate();
HashMap<String,String> hashMap = null;
List<String> listvalue=null;
if(json==null||json.trim().isEmpty()){
hashMap=new  HashMap<String,String>();
}else{
hashMap = JSON.parseObject(json,HashMap.class);
}

if(hashMap.containsKey(key)){
String OneValueJson = hashMap.get(key);
if(OneValueJson==null ||OneValueJson.trim().isEmpty()){
listvalue = new ArrayList<String>();
listvalue.add(value);
}else{
listvalue = JSON.parseObject(OneValueJson, List.class);
listvalue.add(value);
if(listvalue.size()>100){
listvalue.remove(listvalue.size()-1);
}
}
}else{
listvalue = new ArrayList<String>();
listvalue.add(value);
}

hashMap.put(key,JSON.toJSONString(listvalue));
addDate(JSON.toJSONString(hashMap));

}

public static void addDate(String oneValue){
File file = new File("D://export/date/mq.log");
if(!file.canWrite()){
try {
file.createNewFile();
}catch (Exception e){
e.printStackTrace();
}
}

try {
FileUtils.write(file, oneValue);
}catch (Exception e1){
e1.printStackTrace();
}

}

public static String getDate(){
File file = new File("D://export/date/mq.log");
if(!file.canRead()){
try {
file.createNewFile();
}catch (Exception e){
e.printStackTrace();
}
}
String result = "";
try {
result  =FileUtils.readFileToString(file);
}catch (Exception e){
e.printStackTrace();
}
return result;
}
}


数据库设计(部分字段已经改动):

d_responsible_user(责任人)
字段名称

类型

必填

备注

id
integer

y

id

username
string

y

用户名(未来登录用)

password

string

y

密码(未来登录用)

chinesename

string

y

中文姓名

mobile

integer

y

手机号

email

string

y

邮箱

remark

string

y

备注

created

timestamp

n

创建时间

modified

timestamp

n

修改时间

createusername
string

n

创建人

d_service(需要监控的服务)
字段名称

类型

必填

备注

id
integer

y

id

servicename
string

y

服务名称

serviceurl

string

y

服务监控地址

warningmsg

string

y

报警信息

remark

string

n

备注

created

timestamp

n

创建时间

modified

timestamp

n

修改时间

createusername
string

n

创建人

d_service_user(服务与负责人关系)
字段名称

类型

必填

备注

id
integer

y

id

serviceid
integer

y

服务id

servicename

string

y

服务名称

uid

integer

y

用户id

chinesename

string

y

中文名称

isemail

integer

y

报警是否发邮件

ismessage

integer

y

报警是否发短信

remark

string

n

备注

created

timestamp

n

创建时间

modified

timestamp

n

修改时间

createusername
string

n

创建人

d_ business(需要监控的业务)
字段名称

类型

必填

备注

id
integer

y

id

businessname

string

y

业务名称

mqhost

string

y

mqhost

mqport

string

y

mq端口

mqusername

string

y

mq用户名

mqpassword

string

y

mq密码

mqvirtualhost

string

y

mq虚拟host

mqconnectiontimeout

string

y

mq链接超时

warningconfig

string

y

警告策略(阻塞值等)

remark

string

n

备注

created

timestamp

n

创建时间

modified

timestamp

n

修改时间

createusername
string

n

创建人

d_ business_user(监控的业务与用户关系)
字段名称

类型

必填

备注

id
integer

y

id

businessid

integer

y

业务id

businessname

string

y

业务名称

uid

integer

y

用户id

isemail

integer

y

报警是否发邮件

ismessage

integer

y

报警是否发短信

remark

string

n

备注

created

timestamp

n

创建时间

modified

timestamp

n

修改时间

createusername
string

n

创建人

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: