图灵社区 : 图书 : 一步步搭建物联网系统
设计物联网系统是件有意思的事情,它需要考虑到软件、硬件、通讯等多个不同方面。通过探索不同的语言,不同的框架,从而形成不同的解决方案。在这里,我们将对设计物联网系统有一个简单的介绍,并探讨如何设计一个最小的物联网系统。
关于内容的选择,这是一个有意思的话题,因为我们很难判断不同的开发者用的是怎样的语言,用的是怎样的框架。
于是我们便自作主张地选择了那些适合于理论学习的语言、框架、硬件,去除掉其他一些我们不需要考虑的因素,如语法,复杂度等等。当然,这些语言、框架、硬件也是最流行的。
Arduino: 如果你从头开始学过硬件的话,那么你会爱上它的。
Raspberry PI: 如果你从头编译过GNU/Linux的话,我想你会爱上她的。
Python: 简单地来说,你可以方便地使用一些扩展,同时代码就表达了你的想法。
PHP : 这是一门容易部署的语言,我想你只需要在你的Ubuntu机器上,执行一下脚本就能完成安装了。而且,如果你是一个硬件开发者的话,你会更容易地找到其他开发者。
Javascript : 考虑到javascript这门语言已经无处不在了,而且会更加流行。所以,在这里CoAP、MQTT等版本是基于Nodejs的。
HTML、CSS : 这是必须的,同样,他们仍然无处不在。
GNU/Linux: 作为部署到服务器的一部分——你需要掌握他。当然如果你要用WAMP也是可以的。
CoAP: 用NodeJS构建IOT CoAP物联网
本文档对一些概念(如)只做了一些基本介绍,以方便读者理解。如果您想进一步了解这些知识,会列出一些推荐书目,以供参考。
作者黄峰达,1991年出生于闽南,双子座,毕业于西安文理学院。
农村长大,热爱大自然,热爱生活。和哈比特人一样喜欢土地里长出来的东西。
是一个文艺控。喜欢读书,喜欢纸的质感,喜欢用笔铭刻记忆和思想。
是一个设计控,一个DIY爱好者。喜欢自行设计一些小东西,相比理论而言更喜欢动手实践,比如拆装自己的手机、电脑等。
最喜欢折腾与计算机相关的东西:SEO、前端开发、硬件相关(如:Raspberry Pi,51,Arduino,OpenWRT,uCOS...)。
在编程的路上已经走了较长一段,但始终相信前路更长更加广阔。
Nginx负载均衡服务器实现会话粘贴的几种方式
1、使用Nginx 的ip_hash作为负载均衡服务并支持Session sticky
2、使用nginx sticky第三方模块实现基于cookie的负载均衡
3、使用nginx的map指令根据cookie分流:
map $COOKIE_abcdexpid $group {
~*1$ apache001;
~*2$ apache002;
default root;
}
upstream apache001 {
server 192.168.1.1:8080 weight=1 max_fails=1 fail_timeout=30s;
}
upstream apache002 {
server 192.168.1.2:8080 weight=1 max_fails=1 fail_timeout=30s;
}
upstream root {
server 192.168.1.0:8080 weight=1 max_fails=1 fail_timeout=30s;
}
server {
listen 8080;
server_name neoremind.net;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" "group=$group"'
'"$http_user_agent" $gzip_ratio $request_time "$http_x_forwarded_for"';
access_log logs/access_log main;
error_log logs/error_log;
location / {
proxy_pass http://$group;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
4、利用set和if…else… 根据cookie分流
upstream apache001 {
server 192.168.1.1:8080 weight=1 max_fails=1 fail_timeout=30s;
}
upstream apache002 {
server 192.168.1.2:8080 weight=1 max_fails=1 fail_timeout=30s;
}
upstream root {
server 192.168.1.0:8080 weight=1 max_fails=1 fail_timeout=30s;
}
server {
listen 8080;
server_name beidoutest.baidu.com;
#match cookie
set $group "root";
if ($http_cookie ~* "abcdexpid=([^;]+)(1$)"){
set $group apache001;
}
if ($http_cookie ~* "abcdexpid=([^;]+)(2$)"){
set $group apache002;
}
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" "group=$group"'
'"$http_user_agent" $gzip_ratio $request_time "$http_x_forwarded_for"';
access_log logs/access_log main;
error_log logs/error_log;
location / {
proxy_pass http://$group;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
5、nginx1.7.2版本后提供的hash方法:
# http context
upstream backend_hosts {
hash $cookie_JSESSIONID consistent;
server host1.example.com;
server host2.example.com;
server host3.example.com;
}
提升tomcat服务器性能的七条经验
1. 服务器资源
2. 利用缓存和压缩
企业nginx的gzip,客户端资源文件缓存,服务器端页面缓存,数据库缓存等等
3. 采用集群
Nginx负载均衡,加上Tomcat的session共享或复制集群
4. 优化tomcat参数
- <Connector port="8080"
- protocol="org.apache.coyote.http11.Http11NioProtocol"
- connectionTimeout="20000"
- redirectPort="8443"
- maxThreads="500"
- minSpareThreads="20"
- acceptCount="100"
- disableUploadTimeout="true"
- enableLookups="false"
- URIEncoding="UTF-8" />
5. 改用APR库
- Java Blocking Connector Java Nio Blocking Connector APR/native Connector
- BIO NIO APR
- Classname AjpProtocol AjpNioProtocol AjpAprProtocol
- Tomcat Version 3.x onwards 7.x onwards 5.5.x onwards
- Support Polling NO YES YES
- Polling Size N/A maxConnections maxConnections
- Read Request Headers Blocking Sim Blocking Blocking
- Read Request Body Blocking Sim Blocking Blocking
- Write Response Blocking Sim Blocking Blocking
- Wait for next Request Blocking Non Blocking Non Blocking
- Max Connections maxConnections maxConnections maxConnections
6. 优化网络
- 1. 修改/etc/sysctl.cnf文件,在最后追加如下内容:
- net.core.netdev_max_backlog = 32768
- net.core.somaxconn = 32768
- net.core.wmem_default = 8388608
- net.core.rmem_default = 8388608
- net.core.rmem_max = 16777216
- net.core.wmem_max = 16777216
- net.ipv4.ip_local_port_range = 1024 65000
- net.ipv4.route.gc_timeout = 100
- net.ipv4.tcp_fin_timeout = 30
- net.ipv4.tcp_keepalive_time = 1200
- net.ipv4.tcp_timestamps = 0
- net.ipv4.tcp_synack_retries = 2
- net.ipv4.tcp_syn_retries = 2
- net.ipv4.tcp_tw_recycle = 1
- net.ipv4.tcp_tw_reuse = 1
- net.ipv4.tcp_mem = 94500000 915000000 927000000
- net.ipv4.tcp_max_orphans = 3276800
- net.ipv4.tcp_max_syn_backlog = 65536
- 2. 保存退出,执行sysctl -p生效
