注意:安装zookeeper,启动zookeeper服务,我只起了当前这一台虚拟机,所以之前配置的集群起不来,需要修改conf文件夹下的zoo.cfg,将集群节点注释掉,记得用完恢复。
安装之前需要先安装好zookeeper 和 kafka
zookeeper配置文件
Kafka 的server.properties 配置
上面两个安装完成,并测试可以启动,开始安装nginx和ngx_kafka_module
确认安装以下jar包
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel
编译librdkafka
#cd librdkafka目录
make & make install
#注意这个需要git,编译之前要先 git init一下
安装nginx
Yum install -y nginx 下载nginx ng版本 1.18.0
也可以下载tar包,解压,重要的是配置文件,配置nginx.conf如下:server_name是后台的服务器地址。
下载ngx_kafka_module nginx
#在解压的nginx文件夹下执行下面的命令,将nginx和module结合起来。
./configure --add-module=/opt/ngx_kafka_module
make & make install
#之后启动nginx,首先到下面这个目录,他的可执行程序在下面这个目录
cd /usr/local/nginx/sbin
#初次启动
./nginx -c /customSoft/nginx/nginx-1.18.0/conf/nginx.conf
#后面启动
./nginx -c /customSoft/nginx/nginx-1.18.0/conf/nginx.conf -s reload
#启动异常
error while loading shared libraries: librdkafka.so.1: cannot open shared object file: No such file or directory
#解决方案
加载so库
#开机加载/usr/local/lib下面的库
echo "/usr/local/lib" >> /etc/ld.so.conf
#手动加载
ldconfig
测试:
[root@localhost customSoft]# curl http://192.168.31.11:80/kafka/log -d "kafka testing"
[root@localhost customSoft]# curl http://192.168.31.11:80/kafka/log -d "i am liuchang , tested kafka is used"
#消费消息
./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic user_info_topic --from-beginning
效果
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AguWbplb-1642518221885)(/Users/liuchang/Library/Application Support/typora-user-images/image-20220118003806669.png)]
编写前台代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>我的大数据作业</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
</head>
<body>
<input type="button" onclick="clickInout('click')" value="点击我">
</body>
<script>
function clickInout (action){
var user = {
"user_id":0001,
"user_name":"liuchang",
"class":"big data",
"action":action
}
$.ajax({
url: "http://192.168.31.11:80/kafka/log",
type: "POST",
crossDomain: true,
data: JSON.stringify(user),
// 下面这句话允许跨域的cookie访问
xhrFields: {
withCredentials: true
},
success:function (data, status, xhr) {
// console.log("操作成功:'" + action)
},
error:function (err) {
// console.log(err.responseText);
}
});
};
</script>
</html>