Hadoop生态圈-Kafka的API之生产者-消费者

                    Hadoop生态圈-Kafka的API之生产者-消费者

                                            作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.旧API实现生产者-消费者

1>.开启kafka集群

[yinzhengjie@s101 ~]$ more `which xkafka.sh`
#!/bin/bash
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie
#EMAIL:[email protected]

#判断用户是否传参
if [ $# -ne 1 ];then
    echo "无效参数,用法为: $0  {start|stop}"
    exit
fi

#获取用户输入的命令
cmd=$1



for (( i=102 ; i<=104 ; i++ )) ; do
    tput setaf 2
    echo ========== s$i  $cmd ================
    tput setaf 9
    case $cmd in
        start) 
            ssh s$i  "source /etc/profile ; kafka-server-start.sh -daemon /soft/kafka/config/server.properties" 
            echo  s$i  "服务已启动"
            ;;
        stop) 
            ssh s$i  "source /etc/profile ; kafka-server-stop.sh" 
            echo s$i  "服务已停止"
            ;;
            *) 
            echo "无效参数,用法为: $0  {start|stop}"
            exit 
            ;;
     esac
done

[yinzhengjie@s101 ~]$ 
[yinzhengjie@s101 ~]$ more `which xkafka.sh`
[yinzhengjie@s101 ~]$ xkafka.sh start
========== s102 start ================
s102 服务已启动
========== s103 start ================
s103 服务已启动
========== s104 start ================
s104 服务已启动
[yinzhengjie@s101 ~]$ 
开启kafka集群([yinzhengjie@s101 ~]$ xkafka.sh start)
[yinzhengjie@s101 ~]$ xcall.sh jps
============= s101 jps ============
15173 Jps
命令执行成功
============= s102 jps ============
2841 QuorumPeerMain
6745 Kafka
6842 Jps
命令执行成功
============= s103 jps ============
6657 Jps
2821 QuorumPeerMain
6590 Kafka
命令执行成功
============= s104 jps ============
7920 Jps
2823 QuorumPeerMain
7853 Kafka
命令执行成功
============= s105 jps ============
12896 Jps
命令执行成功
[yinzhengjie@s101 ~]$ 
[yinzhengjie@s101 ~]$ more `which xcall.sh`
#!/bin/bash
#@author :yinzhengjie
#blog:http://www.cnblogs.com/yinzhengjie
#EMAIL:[email protected]


#判断用户是否传参
if [ $# -lt 1 ];then
        echo "请输入参数"
        exit
fi

#获取用户输入的命令
cmd=$@

for (( i=101;i<=105;i++ ))
do
        #使终端变绿色 
        tput setaf 2
        echo ============= s$i $cmd ============
        #使终端变回原来的颜色,即白灰色
        tput setaf 7
        #远程执行命令
        ssh s$i $cmd
        #判断命令是否执行成功
        if [ $? == 0 ];then
                echo "命令执行成功"
        fi
done
[yinzhengjie@s101 ~]$ 
检查kafka是否正常启动([yinzhengjie@s101 ~]$ xcall.sh jps)

2>.编写生产者端代码

3>.编写消费者端代码

二.

猜你喜欢

转载自www.cnblogs.com/yinzhengjie/p/9210502.html
今日推荐