Nginx
摩拜单车整体流程
简介
百度百科
Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等
安装
下载
安装
1.上传nginx安装包
2.解压nginx
tar -zxvf nginx-1.18.1.tar.gz -C /usr/local/src/
3.进入到nginx的源码目录
cd /usr/local/src/nginx-1.18.1/
4.预编译
./configure
5.安静gcc编译器
yum -y install gcc pcre-devel openssl openssl-devel
6.然后再执行
./configure
7.编译安装nginx,nginx被安装到/usr/local/nginx目录;/usr/local/src/nginx是源码,编译安装后可以删除。
make && make install
8.启动nginx
sbin/nginx
9.查看nginx进程
ps -ef | grep nginx
netstat -anpt | grep nginx
安装目录
配置负载均衡
将服务发布到多台机器
将springboot程序部署在多台服务器上,然后启动springboot
启动:java -jar niubike-0.0.1-SNAPSHOT.war >> ./logs 2>&1 &
修改nginx的配置文件,让nginx实现负载均衡功能
vi /usr/local/nginx/conf/nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#响应数据的来源
upstream tomcats {
server n1:8888 weight=1;
server n2:8888 weight=1;
server n3:8888 weight=1;
}
server {
listen 80;
# 服务的名字随意
server_name n1;
location ~ .* {
proxy_pass http://tomcats;
}
}
安装kafka插件
老的方案:通过flume将nignx中的日志文件采集到kafka中。
目的:将nginx中产生的日志直接存储到kafka。
安装nginx-kafka插件
1.安装git
yum install -y git
2.切换到/usr/local/src目录,然后将kafka的c客户端源码clone到本地
cd /usr/local/src
git clone https://github.com/edenhill/librdkafka
3.进入到librdkafka,然后进行编译
cd librdkafka
#克隆kafka的c,c++的客户端源码包到本地
yum install -y gcc gcc-c++ pcre-devel zlib-devel
./configure
make && make install
4.安装nginx整合kafka的插件,进入到/usr/local/src,clone nginx整合kafka的源码
cd /usr/local/src
#克隆kafka插件源码包到本地
git clone https://github.com/brg-liuwei/ngx_kafka_module
5.进入到nginx的源码包目录下 (编译nginx,然后将将插件同时编译)
cd /usr/local/src/nginx-1.12.2
#configure的本质就是预编译,检测是否缺少插件。
./configure --add-module=/usr/local/src/ngx_kafka_module/
make && make install
6.修改nginx的配置文件,详情请查看当前目录的nginx.conf
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#gzip on;
kafka;
kafka_broker_list n1:9092 n2:9092 n3:9092;
server {
listen 80;
server_name n2;
#charset koi8-r;
#access_log logs/host.access.log main;
location = /kafka/track {
kafka_topic track;
}
location = /kafka/user {
kafka_topic user;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
7.启动zk和kafka集群(创建topic)
/appdata/zookeeper/bin/zkServer_all.sh start
启动:/appdata/kafka/bin/kafka-server-start.sh -daemon /appdata/kafka/config/server.properties
创建topic:/appdata/kafka/bin/kafka-topics.sh --create --zookeeper n1:2181,n2:2181,n3:2181 --replication-factor 3 --partitions 3 --topic user
/appdata/kafka/bin/kafka-topics.sh --create --zookeeper n1:2181,n2:2181,n3:2181 --replication-factor 3 --partitions 3 --topic track
查看topic:/appdata/kafka/bin/kafka-topics.sh --describe --zookeeper n1:2181,n2:2181,n3:2181 --topic user
8.启动nginx,报错,找不到kafka.so.1的文件
error while loading shared libraries: librdkafka.so.1: cannot open shared object file: No such file or directory
在/usr/local/nginx启动,sbin/nginx -h
9.加载so库
echo “/usr/local/lib” >> /etc/ld.so.conf
ldconfig
10.测试,向nginx中写入数据,然后观察kafka的消费者能不能消费到数据
curl localhost/kafka/track -d “message send to kafka topic”
curl localhost/kafka/track -d “老赵666”
11.查看数据
启动Kafka客户端(0.8版本启动方式):
/appdata/kafka/bin/kafka-console-consumer.sh --zookeeper n1:2181,n2:2181,n3:2181 --topic track --from-beginning
微信小程序对接nginx-kafka
在一个kafka客户端中,观看日志数据采集的情况。
index.js
//index.js
Page({
/**
* 页面的初始数据
*/
data: {
//该页面中的两个变量
latitude: 0,
longitude: 0,
//控件的变量,数组类型
controls: [],
//显示的单车
markers: []
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
//将当前对象赋给that,就是获取当前信息的拷贝。
var that = this;
//获取当前的位置信息
wx.getLocation({
//如果获取成功,会调用success
success: function (res) {
var lat = res.latitude;
var log = res.longitude;
//查找附近的单车
wx.request({
url: "http://localhost:8888/bikes",
method: 'GET',
success: function (res) {
const bikes = res.data.map((item) => {
return {
id: item.id,
iconPath: "/image/bike.png",
width: 35,
height: 40,
latitude: item.latitude,
longitude: item.longitude
};
});
// 修改data里面的markers
that.setData({
markers: bikes,
latitude: lat,
longitude: log
});
}
})
//console.log("纬度" + lat + "经度" + log)
}
});
//创建一个map上下文,如果想要调用地图相关的方发
that.mapCtx = wx.createMapContext('map');
//在地图页中加入按钮(归位、扫描、充值)
//获取当前设备的信息,获取屏幕的宽高
wx.getSystemInfo({
success: function (res) {
//屏幕高
var height = res.windowHeight;
//屏幕宽
var width = res.windowWidth;
//往页面中设置数据
that.setData({
controls: [{
//中心点位置
id: 1,
iconPath: '/image/location.png',
position: {
width: 20,
height: 35,
left: width / 2 - 10,
top: height / 2 - 35.
},
//是否可点击
clickable: true
},{
//定位按钮安置
id: 2,
iconPath: '/image/img1.png',
position: {
width: 40,
height: 40,
left: 20,
top: height - 60.
},
//是否可点击
clickable: true
}, {
//扫码按钮
id: 3,
iconPath: '/image/qrcode.png',
position: {
width: 100,
height: 40,
left: width / 2 - 50,
top: height - 60.
},
//是否可点击
clickable: true
}, {
//充值按钮
id: 4,
iconPath: '/image/pay.png',
position: {
width: 40,
height: 40,
left: width - 45,
top: height - 60.
},
//是否可点击
clickable: true
}, { //手动添加一辆单车的按钮
id: 5,
iconPath: "/image/bike.png",
position: {
width: 35,
height: 40,
},
//是否可点击
clickable: true
}]
})
},
})
},
//在地图中绑定的事件
contap(e) {
//console.log(e)
var that = this;
if (e.controlId == 2) {
//点击定位当前位置
that.mapCtx.moveToLocation();
}
if (e.controlId == 3) {
//点击扫描按钮
wx.scanCode({
success: function (r) {
//扫描成功获取二维码的信息
var code = r.result;
//向后台发送请求
wx.request({
//method: 'POST',
url: 'http://localhost:8888/bike', //仅为示例,并非真实的接口地址
data: {
qrCode: code,
status: 0,
latitude: that.data.latitude,
longitude: that.data.longitude
},
header: {
'content-type': 'application/json' // 默认值
},
success: function (res) {
console.log(res.data)
}
})
}
})
}
if (e.controlId == 5) {
//添加车辆的按钮
that.mapCtx.getCenterLocation({
success: function (res) {
var lat = res.latitude;
var log = res.longitude;
wx.request({
url: "http://localhost:8888/bike",
method: 'POST',
data: {
latitude: lat,
longitude: log
},
success: function () {
//向后台发送请求,将单车查找出来
wx.request({
url: "http://localhost:8888/bikes",
method: 'GET',
success: function (res) {
const bikes = res.data.map((item) => {
return {
id: item.id,
iconPath: "/image/bike.png",
width: 35,
height: 40,
latitude: item.latitude,
longitude: item.longitude
};
});
// 修改data里面的markers
that.setData({
markers: bikes
});
}
})
}
})
}
})
}
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
//在这个事件中,记录用户的行为,然后发送到后台的服务器
//获取当前位置
wx.getLocation({
success: function (res) {
//纬度
var lat = res.latitude;
//经度
var log = res.longitude;
//从本地存储中取出唯一身份标识
var openid = wx.getStorageSync('openid')
//发送request向mongo中添加数据(添加一条文档(json))
wx.request({
//用POST方式请求es可以只指定index和type,不用指定id
url: "http://192.168.145.202/kafka/user",
data: {
time: new Date(),
openid: openid,
lat: lat,
log: log
},
method: "POST"
})
},
})
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})
index.wxml
<!--index.wxml
显示地图,其实是一般map标签
40.138000,116.264110
controls:地图中的控件按钮(图片)
markers:后台请求的数据
bindcontroltap:绑定事件
bindregionchange: 视野发生变化时触发
-->
<map id="map"
latitude="{{latitude}}"
longitude="{{longitude}}"
controls="{{controls}}"
markers="{{markers}}"
scale="17"
show-location
style="width: 100%; height:100%"
bindcontroltap="contap"
bindregiοnchange="regionchange"
></map>