1.准备4台VPS, 系统是64位ubuntu,创建时开启私有网络(private networking)。使用私有网络的好处是数据库多台机器之间复制数据时如果数据传输是通过私有网络,产生的数据流量将不会计入用户的流量。私有网络只能在同一机房里的机器上设置。目前很多VPS提供商都提供了该功能。
然后按照mongodb官网ubuntu下安装步骤安装mongodb完整版(包括mongo, mongod, mongos等)。
2.在3台VPS上执行以下命令
mkdir -p /data/db
mongod –bind_ip 0.0.0.0 –replSet spock –fork –logpath /var/log/mongod.log
意思是创建存放数据库的目录,并且设置好replica set的名称(这里叫做spock),并且在后台运行。
3.在刚才3台VPS其中1台上执行以下命令
首先登录本地mongodb:mongo –nodb
然后执行以下命令:
config = {“_id”:”spock”, “members”:[{“_id”:0,”host”:”server1:27017″},{“_id”:1,”host”:”server2:27017″},{“_id”:2,”host”:”server3:27017″}]}
db = (new Mongo(“server1:27017”)).getDB(“test”)
rs.initiate(config)
以上server1, server2, server3换成对应3台VPS的private networking ip即可。
注意:我们只需要在其中1台上执行以上命令,mongodb会把以上replica set的配置发给其余2台。
4.验证replica set创建成功
首先我们要知道哪台是primary(只有1台),哪些是secondary。
紧接上一步,在mongodb命令行里输入:rs.status()
我们会看到这个replica set的当前状态,其中members会列出这个replica set里所有服务器的信息(host,端口等),再下一级的stateStr字段会显示每台服务器是PRIMARY或SECONDARY。于是就可以知道哪台是primary,哪台是secondary。
在primary服务器上执行以下:
首先使用命令行进入本地mongodb:mongo –nodb
然后执行:
use test
db.myCollection.insert({“name”:”xiaoming”})
在secondary上执行:
mongo –nodb
use test
db.getMongo().setSlaveOk()
db.myCollection.find()
这里使用setSlaveOK()的目的是让secondary服务器接受读操作,因为默认情况下是不允许读secondary的。
正常情况下,在secondary可以看见刚刚在primary上插入的数据,说明replica set配置成功。
完毕后把测试数据删掉。
返回前面primary的mongodb命令行,执行:
db.myCollection.drop()
secondary上的数据会同步drop掉,所以就不用管了。
5.配置config server
在3台VPS上执行以下命令:
mkdir -p /data/configdb
mongod –configsvr –fork –logpath /var/log/mongo_config.log
意思是创建配置文件的目录,然后启动config server,在后台运行。
config server不会耗费太多的CPU,内存和硬盘等资源,可以和其它应用部署在同一台机器上。这里,它和mongod放在同一台vps上。
6.配置mongos
在第4台上执行以下命令:
mongos –configdb server1:27019,server2:27019,server3:27019 –fork –logpath /var/log/mongos.log
意思是启动mongos服务。这里的server1, server2, server3是3台config server的地址。
然后使用命令行进入本地mongos:mongo –nodb
然后执行以下命令:
sh.addShard(“spock/server1:27017,server2:27017,server3:27017”)
use test
db.record.createIndex({“uid”:1,”time”:1})
sh.enableSharding(“test”)
sh.shardCollection(“test.record”,{“uid”:1,”time”:1})
server1, server2, server3用3台mongod的ip替换掉。
意思是添加shard,然后在collection上创建索引,在数据库级别设置sharding,然后在collection级别设置sharding,指定sharding key(这里用了2个字段,应该根据个人实际情况处理)。
sharding key必须在之前已经建立了索引。
7.创建数据
在mongos服务器上连接本地mongos:mongo
执行以下代码:
for(i=0;i<100000;i++){ db.record.insert({“uid”:Math.ceil(Math.random()*100),”long”:Math.random()*360-180,”lat”:Math.random()*180-90,”time”:new Date(Math.round(Math.random()*new Date().getTime()))}) }
8.添加shard
目前只有一个shard,以后如果遇到数据量增大,读写频率增大,我们可以添加新的replica set作为shard,添加步骤就按照前面的几步。需要注意的是新的replica set的名称不能和已经存在的重复。并且新的replica set里不能包含已经存在的相同名称的collection。
9.停止服务
上面的mongod和mongos服务都可以按照以下相同的方式停止。
使用命令行进入本地mongod或mongos:mongo
然后执行:
use admin
db.shutdownServer()
至于停止config server,可以使用linux kill命令。
关闭顺序:先停止mongos,然后停止mongod和config server。
使用Mosquitto-Auth-Plugin对mqtt客户端进行验证
1.安装需要的包
sudo apt-get install libc-ares-dev libcurl4-openssl-dev libmysqlclient-dev
2.下载mosquitto源码并编译安装
从http://mosquitto.org/download/ 下载源码并解压到某个目录,进入该目录编译安装
make mosquitto
sudo make install
3.下载mosquitto-auth-plug源码
git clone https://github.com/jpmens/mosquitto-auth-plug.git
并使用copy命令生成config.mk文件:cp config.mk.in config.mk
编辑config.mk:根据需要选择合适的后台。我这里只使用mysql数据库验证用户,BACKEND_MYSQL这一行是yes,其余行都是no。在MOSQUITTO_SRC一行输入mosquitto的源码路径,比如MOSQUITTO_SRC =/root/mosquitto-1.4.4/
。在OPENSSLDIR一行输入openssl的路径,比如OPENSSLDIR = /usr/lib/ssl。
可以使用以下命令得到openssl的路径:openssl version -a
4.进入mosquitto-auth-plug所在目录,使用make命令生成so文件。
5.移动so文件到mosquitto目录(不是源码目录,而是安装后的目录)
mv auth-plug.so /etc/mosquitto/
6.编辑mosquitto配置文件
mosquitto-auth-plug的源码里已经有多种范例配置文件,可以使用这些文件。比如,我用mysql做验证,可以使用 examples目录下的mosquitto-mysql.conf。
mv mosquitto.conf mosquitto.conf.origin(保留原始配置文件,供以后使用)
mv mosquitto-auth-plug目录/examples/mosquitto-mysql.conf mosquitto目录/mosquitto.conf
根据实际情况,编辑文件里的auth_plugin,backend,数据库host,端口,数据库名,用户,密码,sql语句等。
7.运行mosquitto并观察输出
mosquitto -c mosquitto-conf -v
问题:
1. 输出显示Unable to load auth plugin “…/auth-plug.so”
解决:
编辑Mosquitto源码目录下的config.mk:WITH_SRV:=no
然后重新编译:
make clean
make
生成新的so文件。
adobe illustrator常用操作
1.矩形剪切掉一条边
使用矩形工具画1个矩形,选择剪刀工具,鼠标分别点击要删除的边的2个顶点。这时候已经剪断了,只是肉眼看不到。然后鼠标切换成选择工具,框住剩余3条边(不要包含刚才的2个顶点),使用键盘,按住远离那条边的方向键。这时候我们能看到2部分已经分开了。
同理适用于椭圆。使用剪刀工具,点击圆周上的2点,使用键盘分开2部分即可。
2.画梯形
使用矩形工具先画个矩形,选择直接选择工具,使用鼠标框住一个顶点,使用键盘,按住能使该顶点变成梯形顶点的方向键,直到该顶点移动到合适的位置变成理想的梯形顶点位置。然后对另一个顶点执行同理操作。
3.多个图形组合成一个图形
鼠标框住想要组合的多个图形,然后菜单里选择“对象—路径—连接”。
4.切割图形的一部分
先画一个图形,然后画另一个图形,使2者重叠后,后者能够覆盖掉想第一个图形要保留的的部分。然后选中第一个图形,选择“对象—路径—分割下方对象”,然后鼠标只框住第2个图形,使用键盘的方向键将2部分分离,剩余第一个图形想要保留的那部分。
5.复制图形快捷键
选中图形,然后按住alt键,然后拖动鼠标。一个新的图形出现了。
6.旋转快捷键
选中图形,鼠标双击旋转工具,输入角度。
memcached命令行常用操作
启动:
memcached -d -u user -p 11211
默认内存64M,如果想指定内存大小,使用-m,后面加上要分配的内存大小(单位M)
停止:
service memcached stop
清空缓存:
telnet 127.0.0.1 11211
flush_all 回车
quit
查看状态:
telnet 127.0.0.1 11211
stats
其中,STAT bytes 一行显示的是当前占用内存的大小,
STAT limit_maxbytes 一行显示的是可用的最大内存。
android使用paho进行MQTT协议消息推送Demo
package com.example.myapplication;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
public class MainActivity extends ActionBarActivity implements MqttCallback{
private MqttClient mqttClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void clickButton(View button){
switch (button.getId()){
case R.id.register:
new Thread(new Runnable() {
@Override
public void run() {
int qos = 2;
String broker = "tcp://iot.eclipse.org:1883";
EditText editTextID = (EditText)findViewById(R.id.id);
String clientId = editTextID.getText().toString();
MemoryPersistence persistence = new MemoryPersistence();
try{
mqttClient = new MqttClient(broker, clientId, persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
mqttClient.connect(connOpts);
mqttClient.subscribe(clientId);
mqttClient.setCallback(MainActivity.this);
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), mqttClient.isConnected() ? "连接成功" : "连接失败", Toast.LENGTH_LONG).show();
}
});
}
catch (Exception e){
e.printStackTrace();
}
}
}).start();
break;
case R.id.send:
EditText editTextTargetID =(EditText)findViewById(R.id.targetID);
final String targetID = editTextTargetID.getText().toString();
String content = ((EditText)findViewById(R.id.content)).getText().toString();
final MqttMessage message = new MqttMessage(content.getBytes());
if(mqttClient==null || !mqttClient.isConnected()){
Toast.makeText(getApplicationContext(),"MQTT未连接",Toast.LENGTH_LONG).show();
return;
}
new Thread(new Runnable() {
@Override
public void run() {
try{
mqttClient.publish(targetID, message);
}
catch (Exception e){
e.printStackTrace();
}
}
}).start();
break;
}
}
@Override
public void connectionLost(final Throwable cause) {
this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "失去连接:" + cause.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
@Override
public void messageArrived(String topic, MqttMessage message)
throws Exception {
final String msg = new String(message.getPayload());
this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "收到:"+msg, Toast.LENGTH_LONG).show();
}
});
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
try {
final String msg = new String(token.getMessage().getPayload());
this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "发送完毕:" + msg, Toast.LENGTH_LONG).show();
}
});
}
catch (Exception e){
e.printStackTrace();
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:layout_width="50dp"
android:layout_height="fill_parent"
android:gravity="center"
android:text="ID:"/>
<EditText
android:id="@+id/id"
android:layout_width="100dp"
android:layout_height="fill_parent" />
<Button
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="register"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="clickButton" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:layout_width="80dp"
android:layout_height="fill_parent"
android:gravity="center"
android:text="接收方ID:"/>
<EditText
android:id="@+id/targetID"
android:layout_width="50dp"
android:layout_height="fill_parent" />
<TextView
android:layout_width="40dp"
android:layout_height="fill_parent"
android:gravity="center"
android:text="内容:"/>
<EditText
android:id="@+id/content"
android:layout_width="100dp"
android:layout_height="fill_parent" />
<Button
android:text="Send"
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="clickButton" />
</LinearLayout>
</LinearLayout>




