zoukankan      html  css  js  c++  java
  • spring boot整合mqtt存储到mysql数据库

      1 package com.mqtt.util;
      2  
      3 import javax.annotation.PostConstruct;
      4 import javax.annotation.Resource;
      5 
      6 import org.eclipse.paho.client.mqttv3.MqttClient;
      7 import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
      8 import org.eclipse.paho.client.mqttv3.MqttException;
      9 import org.eclipse.paho.client.mqttv3.MqttTopic;
     10 import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
     11 import org.springframework.beans.BeansException;
     12 import org.springframework.beans.factory.annotation.Autowired;
     13 import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
     14 import org.springframework.context.ApplicationContext;
     15 import org.springframework.context.ApplicationContextAware;
     16 import org.springframework.stereotype.Component;
     17 import org.springframework.stereotype.Service;
     18 
     19 import com.mqtt.service.IMqttTopicService;
     20  
     21 /**
     22  * MQTT工具类操作
     23  *
     24  */
     25 @Component
     26 public class MQTTConnect{
     27         
     28                 //从数据库获取top主题和qos服务质量----可以从数据库存在多个主题
     29         @Resource
     30         private IMqttTopicService imqtttopicservice;
     31         
     32         private static MQTTConnect test;
     33         
     34          @PostConstruct 
     35             public void init() {
     36                 test = this;
     37                 test.imqtttopicservice = this.imqtttopicservice;
     38             }
     39     
     40         //MQTT安装的服务器地址和端口号(本机的ip)
     41         public static final String HOST = "tcp://ip地址:1883";
     42         //定义一个主题
     43 //        public static final String TOPIC = "1102_pub";
     44         public static String TOPIC;
     45         //定义MQTT的ID,可以在MQTT服务配置中指定
     46 //        private static final String clientid = "client-1102";
     47         private static final String clientid = "client-1102";
     48         private MqttClient client;
     49         private MqttConnectOptions options;
     50         private String userName = "zhny";
     51         private String passWord = "zhny2020";
     52         
     53         
     54 //        private ScheduledExecutorService scheduler;
     55         
     56         @PostConstruct
     57         public void start() {
     58             try {
     59                 // host为主机名,clientid即连接MQTT的客户端ID,一般以唯一标识符表示,MemoryPersistence设置clientid的保存形式,默认为以内存保存
     60                 client = new MqttClient(HOST, clientid, new MemoryPersistence());
     61                 // MQTT的连接设置
     62                 options = new MqttConnectOptions();
     63                 // 设置是否清空session,这里如果设置为false表示服务器会保留客户端的连接记录,这里设置为true表示每次连接到服务器都以新的身份连接-----false是开启持久化
     64                 options.setCleanSession(false);
     65                 // 设置连接的用户名
     66                 options.setUserName(userName);
     67                 // 设置连接的密码
     68                 options.setPassword(passWord.toCharArray());
     69                 // 设置超时时间 单位为秒
     70                 options.setConnectionTimeout(10);
     71                 // 设置会话心跳时间 单位为秒 服务器会每隔1.5*20秒的时间向客户端发送个消息判断客户端是否在线,但这个方法并没有重连的机制
     72                 options.setKeepAliveInterval(30);
     73 
     74                 // 设置回调,client.setCallback就可以调用PushCallback类中的messageArrived()方法
     75                 client.setCallback(new PushCallback());
     76 //                MqttTopic topic3 = client.getTopic(TOPIC);
     77                 
     78                 //setWill方法,如果项目中需要知道客户端是否掉线可以调用该方法。设置最终端口的通知消息
     79 //                options.setWill(topic3, "This is yizhu...".getBytes(), 2, true);
     80                 
     81                 client.connect(options);
     82                 //订阅消息
     83 //                int qos = 2;
     84 //                int[] Qos  = {qos};
     85 //                String[] topic1 = {TOPIC};
     86                 int[] Qos = test.imqtttopicservice.selectqos();
     87                 
     88                 String[] topic = test.imqtttopicservice.selecttopic();
     89                 client.subscribe(topic, Qos);
     90 
     91             } catch (Exception e) {
     92                 e.printStackTrace();
     93             } 
     94         }
     95 
     96     }
     97 
     98     
     99     
    100     
    101     
    102     
    103     
    104     
    105 
    106     
    MQTTConnect

    上面---mqtt订阅端--工具类

      1 package com.mqtt.util;
      2 
      3 
      4 import java.text.SimpleDateFormat;
      5 import java.util.Date;
      6 import java.util.regex.Matcher;
      7 import java.util.regex.Pattern;
      8 
      9 import javax.annotation.PostConstruct;
     10 import javax.annotation.Resource;
     11 
     12 import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
     13 import org.eclipse.paho.client.mqttv3.MqttCallback;
     14 import org.eclipse.paho.client.mqttv3.MqttClient;
     15 import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
     16 import org.eclipse.paho.client.mqttv3.MqttException;
     17 import org.eclipse.paho.client.mqttv3.MqttMessage;
     18 import org.jboss.logging.Logger;
     19 import org.springframework.beans.factory.annotation.Autowired;
     20 import org.springframework.stereotype.Component;
     21 
     22 import com.mqtt.model.GreenhouseAlarm;
     23 import com.mqtt.model.GreenhouseSensor;
     24 import com.mqtt.model.GreenhouseData;
     25 import com.mqtt.model.GreenhouseDevice;
     26 import com.mqtt.service.AlarmService;
     27 import com.mqtt.service.IGreenhouseDataService;
     28 import com.mqtt.service.IMqttTopicService;
     29 import com.mqtt.service.QueryService;
     30 
     31 
     32 
     33 
     34 /**
     35  * 必须实现MqttCallback的接口并实现对应的相关接口方法CallBack 类将实现 MqttCallBack。 
     36  * 每个客户机标识都需要一个回调实例。在此示例中,构造函数传递客户机标识以另存为实例数据。 
     37  * 在回调中,将它用来标识已经启动了该回调的哪个实例。 
     38  * 必须在回调类中实现三个方法: 
     39  *  public void messageArrived(MqttTopic topic, MqttMessage message)接收已经预订的发布。 
     40  *  public void connectionLost(Throwable cause)在断开连接时调用。 
     41  *  public void deliveryComplete(MqttDeliveryToken token)) 
     42  *  接收到已经发布的 QoS 1 或 QoS 2 消息的传递令牌时调用。 
     43  *  由 MqttClient.connect 激活此回调。
     44  */
     45 @Component
     46 public class PushCallback implements MqttCallback{
     47     
     48     @Resource
     49     private IMqttTopicService imqtttopicservice;
     50     
     51     @Resource
     52     public IGreenhouseDataService igreenhousedataservice;
     53     
     54     public final Logger log = Logger.getLogger(this.getClass());
     55     
     56     @Resource
     57     private ServerMQTT servermqtt;
     58     
     59     @Resource
     60     public QueryService queryService;
     61     
     62     @Resource
     63     public AlarmService alarmService;
     64     
     65     private static PushCallback pu;
     66     
     67     
     68      private MqttClient client;
     69      private MqttConnectOptions options;
     70     
     71      @PostConstruct 
     72         public void init() {
     73          pu = this;
     74         }
     75 
     76    public void connectionLost(Throwable cause) {
     77        // 连接丢失后,一般在这里面进行重连
     78        log.info("连接断开……(可以做重连)");
     79        
     80        //失败重连逻辑
     81        while (true){
     82            try {
     83                log.info("连接失败重连");
     84                client.connect(options);
     85                //发布相关的订阅
     86                int[] qos = pu.imqtttopicservice.selectqos();
     87                 
     88                 String[] topic = pu.imqtttopicservice.selecttopic();
     89                client.subscribe(topic, qos);
     90                log.info("连接失败重连成功");
     91                break;
     92            } catch (MqttException e) {
     93                e.printStackTrace();
     94                log.info("连接失败重连失败");
     95                log.info(e);
     96            }
     97        }
     98 
     99        
    100        
    101    }
    102    
    103    
    104 
    105    public void deliveryComplete(IMqttDeliveryToken token) {
    106        log.info("deliveryComplete---------" + token.isComplete());
    107    }
    108    
    109    
    110         public void messageArrived(String topic, MqttMessage message) {
    111             // subscribe后得到的消息会执行到这里面
    112             log.info("接收消息主题:" + topic + "  接收消息Qos:" + message.getQos() + "接收消息内容:" + new String(message.getPayload()));
    113             try {
    114                   String rgex = "网关:(.*?)PM2.5:";
    115 
    116                     String rgex1 = "PM2.5:(.*?)温度:";
    117                     
    118                     String rgex2 = "温度:(.*?)湿度:";
    119                     
    120                     String rgex3 = "湿度:(.*?)CO2:";
    121                     
    122                     String rgex4 = "CO2:(.*?)甲醛:";
    123                     
    124                     String rgex5= "甲醛:(.*?)液位:";
    125                     
    126                     String rgex6= "液位:(.*?)光照:";
    127                     
    128                     String rgex7 = "光照:(.*)";
    129                 
    130                     String sb = (new PushCallback()).getSubUtilSimple(new String(message.getPayload()), rgex);
    131                     
    132                     String sb1 = (new PushCallback()).getSubUtilSimple(new String(message.getPayload()), rgex1);
    133                     
    134                     String sb2 = (new PushCallback()).getSubUtilSimple(new String(message.getPayload()), rgex2);
    135                     
    136                     String sb3 = (new PushCallback()).getSubUtilSimple(new String(message.getPayload()), rgex3);
    137                     
    138                     String sb4 = (new PushCallback()).getSubUtilSimple(new String(message.getPayload()), rgex4);
    139                     
    140                     String sb5 = (new PushCallback()).getSubUtilSimple(new String(message.getPayload()), rgex5);
    141                     
    142                     String sb6 = (new PushCallback()).getSubUtilSimple(new String(message.getPayload()), rgex6);
    143                     
    144                     String sb7 = (new PushCallback()).getSubUtilSimple(new String(message.getPayload()), rgex7);
    145                     
    146                     //插入
    147                     GreenhouseData greenhousedata=new GreenhouseData();
    148 //                    greenhousedata.setId(0);
    149 //                    greenhousedata.setLiquidTemperature(0.0);
    150 //                    greenhousedata.setReserve1("0");
    151 //                    greenhousedata.setReserve2("0");
    152                     
    153                     greenhousedata.setTag(sb);
    154                     greenhousedata.setPm(Double.parseDouble(sb1));
    155                     greenhousedata.setTemperature(Double.parseDouble(sb2));
    156                     greenhousedata.setHumidity(Double.parseDouble(sb3));
    157                     greenhousedata.setCo2(Double.parseDouble(sb4));
    158                     greenhousedata.setFormaldehyde(Double.parseDouble(sb5));
    159                     greenhousedata.setIllumination(Double.parseDouble(sb7));
    160                     greenhousedata.setLiquidLevel(Double.parseDouble(sb6));
    161                     
    162                     SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    163                     
    164                     greenhousedata.setDataTime(df.format(new Date()));
    165                     
    166                     pu.igreenhousedataservice.insert(greenhousedata);
    167                     
    168                     
    169                     //查询温度传感器信息
    170                     GreenhouseSensor sensor= pu.queryService.querySensorInfoByWgAndModel(sb,"1301");
    171                     //得到白天设定值
    172                     Double TmSetPoint = sensor.getSetPoint();
    173                     //得到晚上设定值
    174                     Double TmReserve1 = Double.valueOf(sensor.getReserve1());
    175                     
    176                     
    177                     
    178                     
    179                     //24小时标准化
    180                     SimpleDateFormat ft = new SimpleDateFormat("HH:mm:ss");
    181                     //当前时间
    182                      Date now = ft.parse(ft.format(new Date()));
    183                      long nowTime = now.getTime();
    184                     //起始时间
    185                      Date start = ft.parse("06:00:00");
    186                      long startTime = start.getTime();
    187                     //结束时间
    188                     Date end = ft.parse("18:00:00");
    189                     long endTime = end.getTime();
    190                     
    191                     
    192                     //白天
    193                     if (nowTime>=startTime && nowTime<=endTime) {
    194                         //查询降温设备
    195                         GreenhouseDevice jwDevice = pu.queryService.queryDeviceInfoByWgAndModel(sb,"1208");
    196                         //当检测值大于设定值+1并且状态是0未开启状态时进行降温
    197                         if (Double.parseDouble(sb2)>TmSetPoint+1 && jwDevice.getDeviceStatus()==0) {
    198                             log.info("白天温度异常"+"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
    199                             //异常时报警记录里插入温度报警
    200                             GreenhouseAlarm greenhouseAlarm = new GreenhouseAlarm();
    201                             greenhouseAlarm.setGatewayId(Integer.valueOf(sb));
    202                             greenhouseAlarm.setSensorId(Integer.valueOf("1301"));
    203                             SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    204                             greenhouseAlarm.setAlarmTime(String.valueOf(simpleDateFormat.format(new Date())));
    205                             greenhouseAlarm.setName(sensor.getName());
    206                             greenhouseAlarm.setValue(Double.parseDouble(sb2));
    207                             pu.alarmService.insertAlarmInfo(greenhouseAlarm);
    208                             
    209                             log.info("打开降温设备!"+"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"+"白天");
    210                             //发送打开降温设备指令
    211                             pu.servermqtt.mqtt("81", sb);
    212                             //修改设备状态
    213                             GreenhouseDevice greenhouseDevice = new GreenhouseDevice();
    214                             greenhouseDevice.setGatewayId(sb);
    215                             greenhouseDevice.setModel("1208");
    216                             SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    217                             greenhouseDevice.setUpdateTtime(simpleDateFormat1.format(new Date()));
    218                             greenhouseDevice.setDeviceStatus(1);
    219                             pu.queryService.update(greenhouseDevice);
    220                         }    
    221                         if (Double.parseDouble(sb2)<TmSetPoint-1 && jwDevice.getDeviceStatus()==1){
    222                             log.info("关闭降温设备!"+"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"+"白天");
    223                             pu.servermqtt.mqtt("80", sb);
    224                             //修改设备状态
    225                             GreenhouseDevice greenhouseDevice = new GreenhouseDevice();
    226                             greenhouseDevice.setGatewayId(sb);
    227                             greenhouseDevice.setModel("1208");
    228                             SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    229                             greenhouseDevice.setUpdateTtime(simpleDateFormat1.format(new Date()));
    230                             greenhouseDevice.setDeviceStatus(0);
    231                             pu.queryService.update(greenhouseDevice);
    232                         }
    233                         
    234                     }else {
    235                         //查询降温设备
    236                         GreenhouseDevice jwDevice = pu.queryService.queryDeviceInfoByWgAndModel(sb,"1208");
    237                         //当检测值大于设定值+1并且状态是0未开启状态时进行降温
    238                         if (Double.parseDouble(sb2)>TmReserve1+1 && jwDevice.getDeviceStatus()==0) {
    239                             log.info("晚上温度异常"+"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
    240                             
    241                             //异常时报警记录里插入温度报警
    242                             GreenhouseAlarm greenhouseAlarm = new GreenhouseAlarm();
    243                             greenhouseAlarm.setGatewayId(Integer.valueOf(sb));
    244                             greenhouseAlarm.setSensorId(Integer.valueOf("1301"));
    245                             SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    246                             greenhouseAlarm.setAlarmTime(String.valueOf(simpleDateFormat.format(new Date())));
    247                             greenhouseAlarm.setName(sensor.getName());
    248                             greenhouseAlarm.setValue(Double.parseDouble(sb2));
    249                             pu.alarmService.insertAlarmInfo(greenhouseAlarm);
    250                             
    251                             
    252                             log.info("打开降温设备!"+"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"+"晚上");
    253                             //发送打开降温设备指令
    254                             pu.servermqtt.mqtt("81", sb);
    255                             //修改设备状态
    256                             GreenhouseDevice greenhouseDevice = new GreenhouseDevice();
    257                             greenhouseDevice.setGatewayId(sb);
    258                             greenhouseDevice.setModel("1208");
    259                             SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    260                             greenhouseDevice.setUpdateTtime(simpleDateFormat1.format(new Date()));
    261                             greenhouseDevice.setDeviceStatus(1);
    262                             pu.queryService.update(greenhouseDevice);
    263                         }    
    264                         if (Double.parseDouble(sb2)<TmReserve1-1 && jwDevice.getDeviceStatus()==1){
    265                             log.info("关闭降温设备!"+"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"+"晚上");
    266                             pu.servermqtt.mqtt("80", sb);
    267                             //修改设备状态
    268                             GreenhouseDevice greenhouseDevice = new GreenhouseDevice();
    269                             greenhouseDevice.setGatewayId(sb);
    270                             greenhouseDevice.setModel("1208");
    271                             SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    272                             greenhouseDevice.setUpdateTtime(simpleDateFormat1.format(new Date()));
    273                             greenhouseDevice.setDeviceStatus(0);
    274                             pu.queryService.update(greenhouseDevice);
    275                         }
    276                     }
    277                     
    278                     //报警记录添加液位异常
    279                     GreenhouseSensor sensor1= pu.queryService.querySensorInfoByWgAndModel(sb,"1308");    
    280                     if (1==Double.valueOf(sb6)) {
    281                         log.info("液位异常"+"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
    282                         
    283                         //异常时报警记录里插入温度报警
    284                         GreenhouseAlarm greenhouseAlarm = new GreenhouseAlarm();
    285                         greenhouseAlarm.setGatewayId(Integer.valueOf(sb));
    286                         greenhouseAlarm.setSensorId(Integer.valueOf("1308"));
    287                         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    288                         greenhouseAlarm.setAlarmTime(String.valueOf(simpleDateFormat.format(new Date())));
    289                         greenhouseAlarm.setName(sensor1.getName());
    290                         greenhouseAlarm.setValue(Double.valueOf(sb6));
    291                         pu.alarmService.insertAlarmInfo(greenhouseAlarm);
    292                     }
    293                     
    294                     
    295 //                    MySQLDemo mysqldemo=new MySQLDemo();
    296 //                    mysqldemo.add(sb, sb2, sb3, sb4, sb1, sb5, sb6 , sb7);
    297                     
    298             } catch (Exception e) {
    299                 e.printStackTrace();
    300                 log.info(e);
    301             }
    302         }
    303         
    304         
    305         
    306         
    307         /** 
    308          * 返回单个字符串,若匹配到多个的话就返回第一个,方法与getSubUtil一样 
    309          * @param soap 
    310          * @param rgex 
    311          * @return 
    312          */  
    313         
    314          public String getSubUtilSimple(String soap,String rgex){  
    315                 Pattern pattern = Pattern.compile(rgex);// 匹配的模式  
    316                 Matcher m = pattern.matcher(soap);  
    317                 while(m.find()){  
    318                     return m.group(1);  
    319                 }  
    320                 return "";  
    321             }  
    322 }
    323 
    324     
    View Code

    上面回调函数

    ------------------------------

    附赠下mqtt服务器端配置文件

      1 # Config file for mosquitto
      2 #
      3 # See mosquitto.conf(5) for more information.
      4 #
      5 # Default values are shown, uncomment to change.
      6 #
      7 # Use the # character to indicate a comment, but only if it is the
      8 # very first character on the line.
      9 
     10 # =================================================================
     11 # General configuration
     12 # =================================================================
     13 
     14 # Use per listener security settings.
     15 #
     16 # It is recommended this option be set before any other options.
     17 #
     18 # If this option is set to true, then all authentication and access control
     19 # options are controlled on a per listener basis. The following options are
     20 # affected:
     21 #
     22 # password_file acl_file psk_file auth_plugin auth_opt_* allow_anonymous
     23 # auto_id_prefix allow_zero_length_clientid
     24 #
     25 # Note that if set to true, then a durable client (i.e. with clean session set
     26 # to false) that has disconnected will use the ACL settings defined for the
     27 # listener that it was most recently connected to.
     28 #
     29 # The default behaviour is for this to be set to false, which maintains the
     30 # setting behaviour from previous versions of mosquitto.
     31 #per_listener_settings false
     32 
     33 
     34 # If a client is subscribed to multiple subscriptions that overlap, e.g. foo/#
     35 # and foo/+/baz , then MQTT expects that when the broker receives a message on
     36 # a topic that matches both subscriptions, such as foo/bar/baz, then the client
     37 # should only receive the message once.
     38 # Mosquitto keeps track of which clients a message has been sent to in order to
     39 # meet this requirement. The allow_duplicate_messages option allows this
     40 # behaviour to be disabled, which may be useful if you have a large number of
     41 # clients subscribed to the same set of topics and are very concerned about
     42 # minimising memory usage.
     43 # It can be safely set to true if you know in advance that your clients will
     44 # never have overlapping subscriptions, otherwise your clients must be able to
     45 # correctly deal with duplicate messages even when then have QoS=2.
     46 #allow_duplicate_messages false
     47 
     48 # This option controls whether a client is allowed to connect with a zero
     49 # length client id or not. This option only affects clients using MQTT v3.1.1
     50 # and later. If set to false, clients connecting with a zero length client id
     51 # are disconnected. If set to true, clients will be allocated a client id by
     52 # the broker. This means it is only useful for clients with clean session set
     53 # to true.
     54 #allow_zero_length_clientid true
     55 
     56 # If allow_zero_length_clientid is true, this option allows you to set a prefix
     57 # to automatically generated client ids to aid visibility in logs.
     58 # Defaults to 'auto-'
     59 #auto_id_prefix auto-
     60 
     61 # This option affects the scenario when a client subscribes to a topic that has
     62 # retained messages. It is possible that the client that published the retained
     63 # message to the topic had access at the time they published, but that access
     64 # has been subsequently removed. If check_retain_source is set to true, the
     65 # default, the source of a retained message will be checked for access rights
     66 # before it is republished. When set to false, no check will be made and the
     67 # retained message will always be published. This affects all listeners.
     68 #check_retain_source true
     69 
     70 # QoS 1 and 2 messages will be allowed inflight per client until this limit
     71 # is exceeded.  Defaults to 0. (No maximum)
     72 # See also max_inflight_messages
     73 #max_inflight_bytes 0
     74 
     75 # The maximum number of QoS 1 and 2 messages currently inflight per
     76 # client.
     77 # This includes messages that are partway through handshakes and
     78 # those that are being retried. Defaults to 20. Set to 0 for no
     79 # maximum. Setting to 1 will guarantee in-order delivery of QoS 1
     80 # and 2 messages.
     81 #max_inflight_messages 20
     82 
     83 # For MQTT v5 clients, it is possible to have the server send a "server
     84 # keepalive" value that will override the keepalive value set by the client.
     85 # This is intended to be used as a mechanism to say that the server will
     86 # disconnect the client earlier than it anticipated, and that the client should
     87 # use the new keepalive value. The max_keepalive option allows you to specify
     88 # that clients may only connect with keepalive less than or equal to this
     89 # value, otherwise they will be sent a server keepalive telling them to use
     90 # max_keepalive. This only applies to MQTT v5 clients. The maximum value
     91 # allowable is 65535. Do not set below 10.
     92 #max_keepalive 65535
     93 
     94 # For MQTT v5 clients, it is possible to have the server send a "maximum packet
     95 # size" value that will instruct the client it will not accept MQTT packets
     96 # with size greater than max_packet_size bytes. This applies to the full MQTT
     97 # packet, not just the payload. Setting this option to a positive value will
     98 # set the maximum packet size to that number of bytes. If a client sends a
     99 # packet which is larger than this value, it will be disconnected. This applies
    100 # to all clients regardless of the protocol version they are using, but v3.1.1
    101 # and earlier clients will of course not have received the maximum packet size
    102 # information. Defaults to no limit. Setting below 20 bytes is forbidden
    103 # because it is likely to interfere with ordinary client operation, even with
    104 # very small payloads.
    105 #max_packet_size 0
    106 
    107 # QoS 1 and 2 messages above those currently in-flight will be queued per
    108 # client until this limit is exceeded.  Defaults to 0. (No maximum)
    109 # See also max_queued_messages.
    110 # If both max_queued_messages and max_queued_bytes are specified, packets will
    111 # be queued until the first limit is reached.
    112 #max_queued_bytes 0
    113 
    114 # The maximum number of QoS 1 and 2 messages to hold in a queue per client
    115 # above those that are currently in-flight.  Defaults to 100. Set
    116 # to 0 for no maximum (not recommended).
    117 # See also queue_qos0_messages.
    118 # See also max_queued_bytes.
    119 #max_queued_messages 100
    120 #
    121 # This option sets the maximum number of heap memory bytes that the broker will
    122 # allocate, and hence sets a hard limit on memory use by the broker.  Memory
    123 # requests that exceed this value will be denied. The effect will vary
    124 # depending on what has been denied. If an incoming message is being processed,
    125 # then the message will be dropped and the publishing client will be
    126 # disconnected. If an outgoing message is being sent, then the individual
    127 # message will be dropped and the receiving client will be disconnected.
    128 # Defaults to no limit.
    129 #memory_limit 0
    130 
    131 # This option sets the maximum publish payload size that the broker will allow.
    132 # Received messages that exceed this size will not be accepted by the broker.
    133 # The default value is 0, which means that all valid MQTT messages are
    134 # accepted. MQTT imposes a maximum payload size of 268435455 bytes.
    135 #message_size_limit 0
    136 
    137 # This option allows persistent clients (those with clean session set to false)
    138 # to be removed if they do not reconnect within a certain time frame.
    139 #
    140 # This is a non-standard option in MQTT V3.1 but allowed in MQTT v3.1.1.
    141 #
    142 # Badly designed clients may set clean session to false whilst using a randomly
    143 # generated client id. This leads to persistent clients that will never
    144 # reconnect. This option allows these clients to be removed.
    145 #
    146 # The expiration period should be an integer followed by one of h d w m y for
    147 # hour, day, week, month and year respectively. For example
    148 #
    149 # persistent_client_expiration 2m
    150 # persistent_client_expiration 14d
    151 # persistent_client_expiration 1y
    152 #
    153 # The default if not set is to never expire persistent clients.
    154 #persistent_client_expiration
    155 
    156 # Write process id to a file. Default is a blank string which means
    157 # a pid file shouldn't be written.
    158 # This should be set to /var/run/mosquitto.pid if mosquitto is
    159 # being run automatically on boot with an init script and
    160 # start-stop-daemon or similar.
    161 #pid_file
    162 
    163 # Set to true to queue messages with QoS 0 when a persistent client is
    164 # disconnected. These messages are included in the limit imposed by
    165 # max_queued_messages and max_queued_bytes
    166 # Defaults to false.
    167 # This is a non-standard option for the MQTT v3.1 spec but is allowed in
    168 # v3.1.1.
    169 #queue_qos0_messages false
    170 
    171 # Set to false to disable retained message support. If a client publishes a
    172 # message with the retain bit set, it will be disconnected if this is set to
    173 # false.
    174 #retain_available true
    175 
    176 # Disable Nagle's algorithm on client sockets. This has the effect of reducing
    177 # latency of individual messages at the potential cost of increasing the number
    178 # of packets being sent.
    179 #set_tcp_nodelay false
    180 
    181 # Time in seconds between updates of the $SYS tree.
    182 # Set to 0 to disable the publishing of the $SYS tree.
    183 #sys_interval 10
    184 
    185 # The MQTT specification requires that the QoS of a message delivered to a
    186 # subscriber is never upgraded to match the QoS of the subscription. Enabling
    187 # this option changes this behaviour. If upgrade_outgoing_qos is set true,
    188 # messages sent to a subscriber will always match the QoS of its subscription.
    189 # This is a non-standard option explicitly disallowed by the spec.
    190 #upgrade_outgoing_qos false
    191 
    192 # When run as root, drop privileges to this user and its primary
    193 # group.
    194 # Set to root to stay as root, but this is not recommended.
    195 # If run as a non-root user, this setting has no effect.
    196 # Note that on Windows this has no effect and so mosquitto should
    197 # be started by the user you wish it to run as.
    198 #user mosquitto
    199 
    200 # =================================================================
    201 # Default listener
    202 # =================================================================
    203 
    204 # IP address/hostname to bind the default listener to. If not
    205 # given, the default listener will not be bound to a specific
    206 # address and so will be accessible to all network interfaces.
    207 # bind_address ip-address/host name
    208 #bind_address
    209 
    210 # Port to use for the default listener.
    211 #port 1884
    212 
    213 # Bind the listener to a specific interface. This is similar to
    214 # bind_address above but is useful when an interface has multiple addresses or
    215 # the address may change. It is valid to use this with the bind_address option,
    216 # but take care that the interface you are binding to contains the address you
    217 # are binding to, otherwise you will not be able to connect.
    218 # Example: bind_interface eth0
    219 #bind_interface
    220 
    221 # When a listener is using the websockets protocol, it is possible to serve
    222 # http data as well. Set http_dir to a directory which contains the files you
    223 # wish to serve. If this option is not specified, then no normal http
    224 # connections will be possible.
    225 #http_dir
    226 
    227 # 系统资源的回收时间,0表示尽快处理
    228 store_clean_interval 0
    229 
    230 # The maximum number of client connections to allow. This is
    231 # a per listener setting.
    232 # Default is -1, which means unlimited connections.
    233 # Note that other process limits mean that unlimited connections
    234 # are not really possible. Typically the default maximum number of
    235 # connections possible is around 1024.
    236 # 允许的最大连接数,-1表示没有限制
    237 max_connections -1
    238 
    239 # Choose the protocol to use when listening.
    240 # This can be either mqtt or websockets.
    241 # Websockets support is currently disabled by default at compile time.
    242 # Certificate based TLS may be used with websockets, except that
    243 # only the cafile, certfile, keyfile and ciphers options are supported.
    244 #protocol mqtt
    245 
    246 # Set use_username_as_clientid to true to replace the clientid that a client
    247 # connected with with its username. This allows authentication to be tied to
    248 # the clientid, which means that it is possible to prevent one client
    249 # disconnecting another by using the same clientid.
    250 # If a client connects with no username it will be disconnected as not
    251 # authorised when this option is set to true.
    252 # Do not use in conjunction with clientid_prefixes.
    253 # See also use_identity_as_username.
    254 #use_username_as_clientid
    255 
    256 # -----------------------------------------------------------------
    257 # Certificate based SSL/TLS support
    258 # -----------------------------------------------------------------
    259 # The following options can be used to enable SSL/TLS support for
    260 # this listener. Note that the recommended port for MQTT over TLS
    261 # is 8883, but this must be set manually.
    262 #
    263 # See also the mosquitto-tls man page.
    264 
    265 # At least one of cafile or capath must be defined. They both
    266 # define methods of accessing the PEM encoded Certificate
    267 # Authority certificates that have signed your server certificate
    268 # and that you wish to trust.
    269 # cafile defines the path to a file containing the CA certificates.
    270 # capath defines a directory that will be searched for files
    271 # containing the CA certificates. For capath to work correctly, the
    272 # certificate files must have ".crt" as the file ending and you must run
    273 # "openssl rehash <path to capath>" each time you add/remove a certificate.
    274 #cafile
    275 #capath
    276 
    277 # Path to the PEM encoded server certificate.
    278 #certfile
    279 
    280 # Path to the PEM encoded keyfile.
    281 #keyfile
    282 
    283 
    284 # If you have require_certificate set to true, you can create a certificate
    285 # revocation list file to revoke access to particular client certificates. If
    286 # you have done this, use crlfile to point to the PEM encoded revocation file.
    287 #crlfile
    288 
    289 # If you wish to control which encryption ciphers are used, use the ciphers
    290 # option. The list of available ciphers can be obtained using the "openssl
    291 # ciphers" command and should be provided in the same format as the output of
    292 # that command.
    293 # If unset defaults to DEFAULT:!aNULL:!eNULL:!LOW:!EXPORT:!SSLv2:@STRENGTH
    294 #ciphers DEFAULT:!aNULL:!eNULL:!LOW:!EXPORT:!SSLv2:@STRENGTH
    295 
    296 # To allow the use of ephemeral DH key exchange, which provides forward
    297 # security, the listener must load DH parameters. This can be specified with
    298 # the dhparamfile option. The dhparamfile can be generated with the command
    299 # e.g. "openssl dhparam -out dhparam.pem 2048"
    300 #dhparamfile
    301 
    302 # By default a TLS enabled listener will operate in a similar fashion to a
    303 # https enabled web server, in that the server has a certificate signed by a CA
    304 # and the client will verify that it is a trusted certificate. The overall aim
    305 # is encryption of the network traffic. By setting require_certificate to true,
    306 # the client must provide a valid certificate in order for the network
    307 # connection to proceed. This allows access to the broker to be controlled
    308 # outside of the mechanisms provided by MQTT.
    309 #require_certificate false
    310 
    311 # This option defines the version of the TLS protocol to use for this listener.
    312 # The default value allows all of v1.3, v1.2 and v1.1. The valid values are
    313 # tlsv1.3 tlsv1.2 and tlsv1.1.
    314 #tls_version
    315 
    316 # If require_certificate is true, you may set use_identity_as_username to true
    317 # to use the CN value from the client certificate as a username. If this is
    318 # true, the password_file option will not be used for this listener.
    319 # This takes priority over use_subject_as_username.
    320 # See also use_subject_as_username.
    321 #use_identity_as_username false
    322 
    323 # If require_certificate is true, you may set use_subject_as_username to true
    324 # to use the complete subject value from the client certificate as a username.
    325 # If this is true, the password_file option will not be used for this listener.
    326 # See also use_identity_as_username
    327 #use_subject_as_username false
    328 
    329 # -----------------------------------------------------------------
    330 # Pre-shared-key based SSL/TLS support
    331 # -----------------------------------------------------------------
    332 # The following options can be used to enable PSK based SSL/TLS support for
    333 # this listener. Note that the recommended port for MQTT over TLS is 8883, but
    334 # this must be set manually.
    335 #
    336 # See also the mosquitto-tls man page and the "Certificate based SSL/TLS
    337 # support" section. Only one of certificate or PSK encryption support can be
    338 # enabled for any listener.
    339 
    340 # The psk_hint option enables pre-shared-key support for this listener and also
    341 # acts as an identifier for this listener. The hint is sent to clients and may
    342 # be used locally to aid authentication. The hint is a free form string that
    343 # doesn't have much meaning in itself, so feel free to be creative.
    344 # If this option is provided, see psk_file to define the pre-shared keys to be
    345 # used or create a security plugin to handle them.
    346 #psk_hint
    347 
    348 # When using PSK, the encryption ciphers used will be chosen from the list of
    349 # available PSK ciphers. If you want to control which ciphers are available,
    350 # use the "ciphers" option.  The list of available ciphers can be obtained
    351 # using the "openssl ciphers" command and should be provided in the same format
    352 # as the output of that command.
    353 #ciphers
    354 
    355 # Set use_identity_as_username to have the psk identity sent by the client used
    356 # as its username. Authentication will be carried out using the PSK rather than
    357 # the MQTT username/password and so password_file will not be used for this
    358 # listener.
    359 #use_identity_as_username false
    360 
    361 
    362 # =================================================================
    363 # Extra listeners
    364 # =================================================================
    365 
    366 # Listen on a port/ip address combination. By using this variable
    367 # multiple times, mosquitto can listen on more than one port. If
    368 # this variable is used and neither bind_address nor port given,
    369 # then the default listener will not be started.
    370 # The port number to listen on must be given. Optionally, an ip
    371 # address or host name may be supplied as a second argument. In
    372 # this case, mosquitto will attempt to bind the listener to that
    373 # address and so restrict access to the associated network and
    374 # interface. By default, mosquitto will listen on all interfaces.
    375 # Note that for a websockets listener it is not possible to bind to a host
    376 # name.
    377 # listener port-number [ip address/host name]
    378 #listener
    379 
    380 # Bind the listener to a specific interface. This is similar to
    381 # the [ip address/host name] part of the listener definition, but is useful
    382 # when an interface has multiple addresses or the address may change. It is
    383 # valid to use this with the [ip address/host name] part of the listener
    384 # definition, but take care that the interface you are binding to contains the
    385 # address you are binding to, otherwise you will not be able to connect.
    386 # Only available on Linux and requires elevated privileges.
    387 #
    388 # Example: bind_interface eth0
    389 #bind_interface
    390 
    391 # When a listener is using the websockets protocol, it is possible to serve
    392 # http data as well. Set http_dir to a directory which contains the files you
    393 # wish to serve. If this option is not specified, then no normal http
    394 # connections will be possible.
    395 #http_dir
    396 
    397 # The maximum number of client connections to allow. This is
    398 # a per listener setting.
    399 # Default is -1, which means unlimited connections.
    400 # Note that other process limits mean that unlimited connections
    401 # are not really possible. Typically the default maximum number of
    402 # connections possible is around 1024.
    403 #max_connections -1
    404 
    405 # The listener can be restricted to operating within a topic hierarchy using
    406 # the mount_point option. This is achieved be prefixing the mount_point string
    407 # to all topics for any clients connected to this listener. This prefixing only
    408 # happens internally to the broker; the client will not see the prefix.
    409 #mount_point
    410 
    411 # Choose the protocol to use when listening.
    412 # This can be either mqtt or websockets.
    413 # Certificate based TLS may be used with websockets, except that only the
    414 # cafile, certfile, keyfile and ciphers options are supported.
    415 #protocol mqtt
    416 
    417 # Set use_username_as_clientid to true to replace the clientid that a client
    418 # connected with with its username. This allows authentication to be tied to
    419 # the clientid, which means that it is possible to prevent one client
    420 # disconnecting another by using the same clientid.
    421 # If a client connects with no username it will be disconnected as not
    422 # authorised when this option is set to true.
    423 # Do not use in conjunction with clientid_prefixes.
    424 # See also use_identity_as_username.
    425 #use_username_as_clientid
    426 
    427 # Change the websockets headers size. This is a global option, it is not
    428 # possible to set per listener. This option sets the size of the buffer used in
    429 # the libwebsockets library when reading HTTP headers. If you are passing large
    430 # header data such as cookies then you may need to increase this value. If left
    431 # unset, or set to 0, then the default of 1024 bytes will be used.
    432 #websockets_headers_size
    433 
    434 # -----------------------------------------------------------------
    435 # Certificate based SSL/TLS support
    436 # -----------------------------------------------------------------
    437 # The following options can be used to enable certificate based SSL/TLS support
    438 # for this listener. Note that the recommended port for MQTT over TLS is 8883,
    439 # but this must be set manually.
    440 #
    441 # See also the mosquitto-tls man page and the "Pre-shared-key based SSL/TLS
    442 # support" section. Only one of certificate or PSK encryption support can be
    443 # enabled for any listener.
    444 
    445 # At least one of cafile or capath must be defined to enable certificate based
    446 # TLS encryption. They both define methods of accessing the PEM encoded
    447 # Certificate Authority certificates that have signed your server certificate
    448 # and that you wish to trust.
    449 # cafile defines the path to a file containing the CA certificates.
    450 # capath defines a directory that will be searched for files
    451 # containing the CA certificates. For capath to work correctly, the
    452 # certificate files must have ".crt" as the file ending and you must run
    453 # "openssl rehash <path to capath>" each time you add/remove a certificate.
    454 #cafile
    455 #capath
    456 
    457 # Path to the PEM encoded server certificate.
    458 #certfile
    459 
    460 # Path to the PEM encoded keyfile.
    461 #keyfile
    462 
    463 
    464 # If you wish to control which encryption ciphers are used, use the ciphers
    465 # option. The list of available ciphers can be optained using the "openssl
    466 # ciphers" command and should be provided in the same format as the output of
    467 # that command.
    468 #ciphers
    469 
    470 # If you have require_certificate set to true, you can create a certificate
    471 # revocation list file to revoke access to particular client certificates. If
    472 # you have done this, use crlfile to point to the PEM encoded revocation file.
    473 #crlfile
    474 
    475 # To allow the use of ephemeral DH key exchange, which provides forward
    476 # security, the listener must load DH parameters. This can be specified with
    477 # the dhparamfile option. The dhparamfile can be generated with the command
    478 # e.g. "openssl dhparam -out dhparam.pem 2048"
    479 #dhparamfile
    480 
    481 # By default an TLS enabled listener will operate in a similar fashion to a
    482 # https enabled web server, in that the server has a certificate signed by a CA
    483 # and the client will verify that it is a trusted certificate. The overall aim
    484 # is encryption of the network traffic. By setting require_certificate to true,
    485 # the client must provide a valid certificate in order for the network
    486 # connection to proceed. This allows access to the broker to be controlled
    487 # outside of the mechanisms provided by MQTT.
    488 #require_certificate false
    489 
    490 # If require_certificate is true, you may set use_identity_as_username to true
    491 # to use the CN value from the client certificate as a username. If this is
    492 # true, the password_file option will not be used for this listener.
    493 #use_identity_as_username false
    494 
    495 # -----------------------------------------------------------------
    496 # Pre-shared-key based SSL/TLS support
    497 # -----------------------------------------------------------------
    498 # The following options can be used to enable PSK based SSL/TLS support for
    499 # this listener. Note that the recommended port for MQTT over TLS is 8883, but
    500 # this must be set manually.
    501 #
    502 # See also the mosquitto-tls man page and the "Certificate based SSL/TLS
    503 # support" section. Only one of certificate or PSK encryption support can be
    504 # enabled for any listener.
    505 
    506 # The psk_hint option enables pre-shared-key support for this listener and also
    507 # acts as an identifier for this listener. The hint is sent to clients and may
    508 # be used locally to aid authentication. The hint is a free form string that
    509 # doesn't have much meaning in itself, so feel free to be creative.
    510 # If this option is provided, see psk_file to define the pre-shared keys to be
    511 # used or create a security plugin to handle them.
    512 #psk_hint
    513 
    514 # When using PSK, the encryption ciphers used will be chosen from the list of
    515 # available PSK ciphers. If you want to control which ciphers are available,
    516 # use the "ciphers" option.  The list of available ciphers can be optained
    517 # using the "openssl ciphers" command and should be provided in the same format
    518 # as the output of that command.
    519 #ciphers
    520 
    521 # Set use_identity_as_username to have the psk identity sent by the client used
    522 # as its username. Authentication will be carried out using the PSK rather than
    523 # the MQTT username/password and so password_file will not be used for this
    524 # listener.
    525 #use_identity_as_username false
    526 
    527 
    528 # =================================================================
    529 # Persistence
    530 # =================================================================
    531 
    532 # If persistence is enabled, save the in-memory database to disk
    533 # every autosave_interval seconds. If set to 0, the persistence
    534 # database will only be written when mosquitto exits. See also
    535 # autosave_on_changes.
    536 # Note that writing of the persistence database can be forced by
    537 # sending mosquitto a SIGUSR1 signal.
    538 #autosave_interval 1800
    539 
    540 # If true, mosquitto will count the number of subscription changes, retained
    541 # messages received and queued messages and if the total exceeds
    542 # autosave_interval then the in-memory database will be saved to disk.
    543 # If false, mosquitto will save the in-memory database to disk by treating
    544 # autosave_interval as a time in seconds.
    545 #autosave_on_changes false
    546 
    547 # Save persistent message data to disk (true/false).
    548 # This saves information about all messages, including
    549 # subscriptions, currently in-flight messages and retained
    550 # messages.
    551 # retained_persistence is a synonym for this option.
    552 # 持久化功能的开关
    553 persistence true
    554 
    555 # The filename to use for the persistent database, not including
    556 # the path.
    557 #persistence_file mosquitto.db
    558 
    559 # Location for persistent database. Must include trailing /
    560 # Default is an empty string (current directory).
    561 # Set to e.g. /var/lib/mosquitto/ if running as a proper service on Linux or
    562 # similar.
    563 #persistence_location
    564 
    565 
    566 # =================================================================
    567 # Logging
    568 # =================================================================
    569 
    570 # Places to log to. Use multiple log_dest lines for multiple
    571 # logging destinations.
    572 # Possible destinations are: stdout stderr syslog topic file
    573 #
    574 # stdout and stderr log to the console on the named output.
    575 #
    576 # syslog uses the userspace syslog facility which usually ends up
    577 # in /var/log/messages or similar.
    578 #
    579 # topic logs to the broker topic '$SYS/broker/log/<severity>',
    580 # where severity is one of D, E, W, N, I, M which are debug, error,
    581 # warning, notice, information and message. Message type severity is used by
    582 # the subscribe/unsubscribe log_types and publishes log messages to
    583 # $SYS/broker/log/M/susbcribe or $SYS/broker/log/M/unsubscribe.
    584 #
    585 # The file destination requires an additional parameter which is the file to be
    586 # logged to, e.g. "log_dest file /var/log/mosquitto.log". The file will be
    587 # closed and reopened when the broker receives a HUP signal. Only a single file
    588 # destination may be configured.
    589 #
    590 # Note that if the broker is running as a Windows service it will default to
    591 # "log_dest none" and neither stdout nor stderr logging is available.
    592 # Use "log_dest none" if you wish to disable logging.
    593 # 4种日志模式:stdout、stderr、syslog、topic
    594 # none 则表示不记日志,此配置可以提升些许性能
    595 log_dest none
    596 
    597 # Types of messages to log. Use multiple log_type lines for logging
    598 # multiple types of messages.
    599 # Possible types are: debug, error, warning, notice, information,
    600 # none, subscribe, unsubscribe, websockets, all.
    601 # Note that debug type messages are for decoding the incoming/outgoing
    602 # network packets. They are not logged in "topics".
    603 #log_type error
    604 #log_type warning
    605 #log_type notice
    606 #log_type information
    607 
    608 
    609 # If set to true, client connection and disconnection messages will be included
    610 # in the log.
    611 #connection_messages true
    612 
    613 # If using syslog logging (not on Windows), messages will be logged to the
    614 # "daemon" facility by default. Use the log_facility option to choose which of
    615 # local0 to local7 to log to instead. The option value should be an integer
    616 # value, e.g. "log_facility 5" to use local5.
    617 #log_facility
    618 
    619 # If set to true, add a timestamp value to each log message.
    620 #log_timestamp true
    621 
    622 # Set the format of the log timestamp. If left unset, this is the number of
    623 # seconds since the Unix epoch.
    624 # This is a free text string which will be passed to the strftime function. To
    625 # get an ISO 8601 datetime, for example:
    626 # log_timestamp_format %Y-%m-%dT%H:%M:%S
    627 #log_timestamp_format
    628 
    629 # Change the websockets logging level. This is a global option, it is not
    630 # possible to set per listener. This is an integer that is interpreted by
    631 # libwebsockets as a bit mask for its lws_log_levels enum. See the
    632 # libwebsockets documentation for more details. "log_type websockets" must also
    633 # be enabled.
    634 #websockets_log_level 0
    635 
    636 
    637 # =================================================================
    638 # Security
    639 # =================================================================
    640 
    641 # If set, only clients that have a matching prefix on their
    642 # clientid will be allowed to connect to the broker. By default,
    643 # all clients may connect.
    644 # For example, setting "secure-" here would mean a client "secure-
    645 # client" could connect but another with clientid "mqtt" couldn't.
    646 #clientid_prefixes
    647 
    648 # Boolean value that determines whether clients that connect
    649 # without providing a username are allowed to connect. If set to
    650 # false then a password file should be created (see the
    651 # password_file option) to control authenticated client access.
    652 #
    653 # Defaults to true if no other security options are set. If `password_file` or
    654 # `psk_file` is set, or if an authentication plugin is loaded which implements
    655 # username/password or TLS-PSK checks, then `allow_anonymous` defaults to
    656 # false.
    657 #
    658 #allow_anonymous true
    659 
    660 # -----------------------------------------------------------------
    661 # Default authentication and topic access control
    662 # -----------------------------------------------------------------
    663 
    664 # Control access to the broker using a password file. This file can be
    665 # generated using the mosquitto_passwd utility. If TLS support is not compiled
    666 # into mosquitto (it is recommended that TLS support should be included) then
    667 # plain text passwords are used, in which case the file should be a text file
    668 # with lines in the format:
    669 # username:password
    670 # The password (and colon) may be omitted if desired, although this
    671 # offers very little in the way of security.
    672 #
    673 # See the TLS client require_certificate and use_identity_as_username options
    674 # for alternative authentication options. If an auth_plugin is used as well as
    675 # password_file, the auth_plugin check will be made first.
    676 password_file  /etc/mosquitto/pwfile.example
    677 
    678 # Access may also be controlled using a pre-shared-key file. This requires
    679 # TLS-PSK support and a listener configured to use it. The file should be text
    680 # lines in the format:
    681 # identity:key
    682 # The key should be in hexadecimal format without a leading "0x".
    683 # If an auth_plugin is used as well, the auth_plugin check will be made first.
    684 #psk_file
    685 
    686 # Control access to topics on the broker using an access control list
    687 # file. If this parameter is defined then only the topics listed will
    688 # have access.
    689 # If the first character of a line of the ACL file is a # it is treated as a
    690 # comment.
    691 # Topic access is added with lines of the format:
    692 #
    693 # topic [read|write|readwrite] <topic>
    694 #
    695 # The access type is controlled using "read", "write" or "readwrite". This
    696 # parameter is optional (unless <topic> contains a space character) - if not
    697 # given then the access is read/write.  <topic> can contain the + or #
    698 # wildcards as in subscriptions.
    699 #
    700 # The first set of topics are applied to anonymous clients, assuming
    701 # allow_anonymous is true. User specific topic ACLs are added after a
    702 # user line as follows:
    703 #
    704 # user <username>
    705 #
    706 # The username referred to here is the same as in password_file. It is
    707 # not the clientid.
    708 #
    709 #
    710 # If is also possible to define ACLs based on pattern substitution within the
    711 # topic. The patterns available for substition are:
    712 #
    713 # %c to match the client id of the client
    714 # %u to match the username of the client
    715 #
    716 # The substitution pattern must be the only text for that level of hierarchy.
    717 #
    718 # The form is the same as for the topic keyword, but using pattern as the
    719 # keyword.
    720 # Pattern ACLs apply to all users even if the "user" keyword has previously
    721 # been given.
    722 #
    723 # If using bridges with usernames and ACLs, connection messages can be allowed
    724 # with the following pattern:
    725 # pattern write $SYS/broker/connection/%c/state
    726 #
    727 # pattern [read|write|readwrite] <topic>
    728 #
    729 # Example:
    730 #
    731 # pattern write sensor/%u/data
    732 #
    733 # If an auth_plugin is used as well as acl_file, the auth_plugin check will be
    734 # made first.
    735 acl_file /etc/mosquitto/aclfile.example
    736 
    737 # -----------------------------------------------------------------
    738 # External authentication and topic access plugin options
    739 # -----------------------------------------------------------------
    740 
    741 # External authentication and access control can be supported with the
    742 # auth_plugin option. This is a path to a loadable plugin. See also the
    743 # auth_opt_* options described below.
    744 #
    745 # The auth_plugin option can be specified multiple times to load multiple
    746 # plugins. The plugins will be processed in the order that they are specified
    747 # here. If the auth_plugin option is specified alongside either of
    748 # password_file or acl_file then the plugin checks will be made first.
    749 #
    750 #auth_plugin
    751 
    752 # If the auth_plugin option above is used, define options to pass to the
    753 # plugin here as described by the plugin instructions. All options named
    754 # using the format auth_opt_* will be passed to the plugin, for example:
    755 #
    756 # auth_opt_db_host
    757 # auth_opt_db_port
    758 # auth_opt_db_username
    759 # auth_opt_db_password
    760 
    761 
    762 # =================================================================
    763 # Bridges
    764 # =================================================================
    765 
    766 # A bridge is a way of connecting multiple MQTT brokers together.
    767 # Create a new bridge using the "connection" option as described below. Set
    768 # options for the bridges using the remaining parameters. You must specify the
    769 # address and at least one topic to subscribe to.
    770 #
    771 # Each connection must have a unique name.
    772 #
    773 # The address line may have multiple host address and ports specified. See
    774 # below in the round_robin description for more details on bridge behaviour if
    775 # multiple addresses are used. Note that if you use an IPv6 address, then you
    776 # are required to specify a port.
    777 #
    778 # The direction that the topic will be shared can be chosen by
    779 # specifying out, in or both, where the default value is out.
    780 # The QoS level of the bridged communication can be specified with the next
    781 # topic option. The default QoS level is 0, to change the QoS the topic
    782 # direction must also be given.
    783 #
    784 # The local and remote prefix options allow a topic to be remapped when it is
    785 # bridged to/from the remote broker. This provides the ability to place a topic
    786 # tree in an appropriate location.
    787 #
    788 # For more details see the mosquitto.conf man page.
    789 #
    790 # Multiple topics can be specified per connection, but be careful
    791 # not to create any loops.
    792 #
    793 # If you are using bridges with cleansession set to false (the default), then
    794 # you may get unexpected behaviour from incoming topics if you change what
    795 # topics you are subscribing to. This is because the remote broker keeps the
    796 # subscription for the old topic. If you have this problem, connect your bridge
    797 # with cleansession set to true, then reconnect with cleansession set to false
    798 # as normal.
    799 #connection <name>
    800 #address <host>[:<port>] [<host>[:<port>]]
    801 #topic <topic> [[[out | in | both] qos-level] local-prefix remote-prefix]
    802 
    803 
    804 # If a bridge has topics that have "out" direction, the default behaviour is to
    805 # send an unsubscribe request to the remote broker on that topic. This means
    806 # that changing a topic direction from "in" to "out" will not keep receiving
    807 # incoming messages. Sending these unsubscribe requests is not always
    808 # desirable, setting bridge_attempt_unsubscribe to false will disable sending
    809 # the unsubscribe request.
    810 #bridge_attempt_unsubscribe true
    811 
    812 # Set the version of the MQTT protocol to use with for this bridge. Can be one
    813 # of mqttv311 or mqttv11. Defaults to mqttv311.
    814 #bridge_protocol_version mqttv311
    815 
    816 # Set the clean session variable for this bridge.
    817 # When set to true, when the bridge disconnects for any reason, all
    818 # messages and subscriptions will be cleaned up on the remote
    819 # broker. Note that with cleansession set to true, there may be a
    820 # significant amount of retained messages sent when the bridge
    821 # reconnects after losing its connection.
    822 # When set to false, the subscriptions and messages are kept on the
    823 # remote broker, and delivered when the bridge reconnects.
    824 #cleansession false
    825 
    826 # Set the amount of time a bridge using the lazy start type must be idle before
    827 # it will be stopped. Defaults to 60 seconds.
    828 #idle_timeout 60
    829 
    830 # Set the keepalive interval for this bridge connection, in
    831 # seconds.
    832 #keepalive_interval 60
    833 
    834 # Set the clientid to use on the local broker. If not defined, this defaults to
    835 # 'local.<clientid>'. If you are bridging a broker to itself, it is important
    836 # that local_clientid and clientid do not match.
    837 #local_clientid
    838 
    839 # If set to true, publish notification messages to the local and remote brokers
    840 # giving information about the state of the bridge connection. Retained
    841 # messages are published to the topic $SYS/broker/connection/<clientid>/state
    842 # unless the notification_topic option is used.
    843 # If the message is 1 then the connection is active, or 0 if the connection has
    844 # failed.
    845 # This uses the last will and testament feature.
    846 #notifications true
    847 
    848 # Choose the topic on which notification messages for this bridge are
    849 # published. If not set, messages are published on the topic
    850 # $SYS/broker/connection/<clientid>/state
    851 #notification_topic
    852 
    853 # Set the client id to use on the remote end of this bridge connection. If not
    854 # defined, this defaults to 'name.hostname' where name is the connection name
    855 # and hostname is the hostname of this computer.
    856 # This replaces the old "clientid" option to avoid confusion. "clientid"
    857 # remains valid for the time being.
    858 #remote_clientid
    859 
    860 # Set the password to use when connecting to a broker that requires
    861 # authentication. This option is only used if remote_username is also set.
    862 # This replaces the old "password" option to avoid confusion. "password"
    863 # remains valid for the time being.
    864 #remote_password
    865 
    866 # Set the username to use when connecting to a broker that requires
    867 # authentication.
    868 # This replaces the old "username" option to avoid confusion. "username"
    869 # remains valid for the time being.
    870 #remote_username
    871 
    872 # Set the amount of time a bridge using the automatic start type will wait
    873 # until attempting to reconnect.
    874 # This option can be configured to use a constant delay time in seconds, or to
    875 # use a backoff mechanism based on "Decorrelated Jitter", which adds a degree
    876 # of randomness to when the restart occurs.
    877 #
    878 # Set a constant timeout of 20 seconds:
    879 # restart_timeout 20
    880 #
    881 # Set backoff with a base (start value) of 10 seconds and a cap (upper limit) of
    882 # 60 seconds:
    883 # restart_timeout 10 30
    884 #
    885 # Defaults to jitter with a base of 5 and cap of 30
    886 #restart_timeout 5 30
    887 
    888 # If the bridge has more than one address given in the address/addresses
    889 # configuration, the round_robin option defines the behaviour of the bridge on
    890 # a failure of the bridge connection. If round_robin is false, the default
    891 # value, then the first address is treated as the main bridge connection. If
    892 # the connection fails, the other secondary addresses will be attempted in
    893 # turn. Whilst connected to a secondary bridge, the bridge will periodically
    894 # attempt to reconnect to the main bridge until successful.
    895 # If round_robin is true, then all addresses are treated as equals. If a
    896 # connection fails, the next address will be tried and if successful will
    897 # remain connected until it fails
    898 #round_robin false
    899 
    900 # Set the start type of the bridge. This controls how the bridge starts and
    901 # can be one of three types: automatic, lazy and once. Note that RSMB provides
    902 # a fourth start type "manual" which isn't currently supported by mosquitto.
    903 #
    904 # "automatic" is the default start type and means that the bridge connection
    905 # will be started automatically when the broker starts and also restarted
    906 # after a short delay (30 seconds) if the connection fails.
    907 #
    908 # Bridges using the "lazy" start type will be started automatically when the
    909 # number of queued messages exceeds the number set with the "threshold"
    910 # parameter. It will be stopped automatically after the time set by the
    911 # "idle_timeout" parameter. Use this start type if you wish the connection to
    912 # only be active when it is needed.
    913 #
    914 # A bridge using the "once" start type will be started automatically when the
    915 # broker starts but will not be restarted if the connection fails.
    916 #start_type automatic
    917 
    918 # Set the number of messages that need to be queued for a bridge with lazy
    919 # start type to be restarted. Defaults to 10 messages.
    920 # Must be less than max_queued_messages.
    921 #threshold 10
    922 
    923 # If try_private is set to true, the bridge will attempt to indicate to the
    924 # remote broker that it is a bridge not an ordinary client. If successful, this
    925 # means that loop detection will be more effective and that retained messages
    926 # will be propagated correctly. Not all brokers support this feature so it may
    927 # be necessary to set try_private to false if your bridge does not connect
    928 # properly.
    929 #try_private true
    930 
    931 # -----------------------------------------------------------------
    932 # Certificate based SSL/TLS support
    933 # -----------------------------------------------------------------
    934 # Either bridge_cafile or bridge_capath must be defined to enable TLS support
    935 # for this bridge.
    936 # bridge_cafile defines the path to a file containing the
    937 # Certificate Authority certificates that have signed the remote broker
    938 # certificate.
    939 # bridge_capath defines a directory that will be searched for files containing
    940 # the CA certificates. For bridge_capath to work correctly, the certificate
    941 # files must have ".crt" as the file ending and you must run "openssl rehash
    942 # <path to capath>" each time you add/remove a certificate.
    943 #bridge_cafile
    944 #bridge_capath
    945 
    946 
    947 # If the remote broker has more than one protocol available on its port, e.g.
    948 # MQTT and WebSockets, then use bridge_alpn to configure which protocol is
    949 # requested. Note that WebSockets support for bridges is not yet available.
    950 #bridge_alpn
    951 
    952 # When using certificate based encryption, bridge_insecure disables
    953 # verification of the server hostname in the server certificate. This can be
    954 # useful when testing initial server configurations, but makes it possible for
    955 # a malicious third party to impersonate your server through DNS spoofing, for
    956 # example. Use this option in testing only. If you need to resort to using this
    957 # option in a production environment, your setup is at fault and there is no
    958 # point using encryption.
    959 #bridge_insecure false
    960 
    961 # Path to the PEM encoded client certificate, if required by the remote broker.
    962 #bridge_certfile
    963 
    964 # Path to the PEM encoded client private key, if required by the remote broker.
    965 #bridge_keyfile
    966 
    967 # -----------------------------------------------------------------
    968 # PSK based SSL/TLS support
    969 # -----------------------------------------------------------------
    970 # Pre-shared-key encryption provides an alternative to certificate based
    971 # encryption. A bridge can be configured to use PSK with the bridge_identity
    972 # and bridge_psk options. These are the client PSK identity, and pre-shared-key
    973 # in hexadecimal format with no "0x". Only one of certificate and PSK based
    974 # encryption can be used on one
    975 # bridge at once.
    976 #bridge_identity
    977 #bridge_psk
    978 
    979 allow_anonymous false
    980 
    981 # =================================================================
    982 # External config files
    983 # =================================================================
    984 
    985 # External configuration files may be included by using the
    986 # include_dir option. This defines a directory that will be searched
    987 # for config files. All files that end in '.conf' will be loaded as
    988 # a configuration file. It is best to have this as the last option
    989 # in the main file. This option will only be processed from the main
    990 # configuration file. The directory specified must not contain the
    991 # main configuration file.
    992 # Files within include_dir will be loaded sorted in case-sensitive
    993 # alphabetical order, with capital letters ordered first. If this option is
    994 # given multiple times, all of the files from the first instance will be
    995 # processed before the next instance. See the man page for examples.
    996 #include_dir
    mosquitto.conf

    ok,基本没问题了,测试一段时间了

  • 相关阅读:
    Hello Springboot
    Spring AOP
    代理模式
    Spring 面向注解开发
    Spring Bean 的配置
    IDEA 14 for Mac 提示要安装java 6的修改
    NAS DIY
    Maven Jetty SSL配置
    图书管理系统(jsp+nysql实现)
    互联网+XX项目技术架构
  • 原文地址:https://www.cnblogs.com/xiaotangtang/p/13433266.html
Copyright © 2011-2022 走看看