Disclosure of Invention
The invention aims to overcome the defects of the prior art and provides a method for realizing load balancing based on multiple Broker in an MQTT protocol.
The technical scheme of the invention is as follows:
a method for realizing load balancing based on multiple Broker in MQTT protocol comprises the following steps:
s1, the device obtains the configuration information of gateway service by reading the certificate information of the device itself, and initiates a request carrying the configuration information of the Broker server connected with the device last time to the gateway service according to the obtained configuration information;
s2, after the gateway service receives the request sent by the device, the gateway service forwards the request to the service through the route rule configured by the gateway service;
s3, after receiving the request forwarded by the gateway service, the service reads the configuration file to obtain the configuration information lists of all Broker servers, assembles the configuration information lists of each Broker server into a redis key, and determines, according to the parameter value in the request forwarded by the gateway service, that:
a. if the parameter value in the request forwarded by the gateway service is empty, continuing to execute step S4;
b. if the parameter value in the request forwarded by the gateway service is not null, assembling the request parameter received by the service and forwarded by the gateway service into a key of redis, and performing a self-decreasing operation on the value of the key assembled by the request parameter into the redis through the redis;
s4, the service acquires the device connection number corresponding to each Broker server from the redis according to the key assembled by the configuration information list of each Broker server in the step S3;
s5, after acquiring the device connection number of each Broker server from the redis, the service acquires the detailed information of the Broker servers according to the load balancing algorithm set in the configuration;
s6, returning the detailed information of the Broker server acquired in the step S5 to the equipment sending the request through the network;
s7, after acquiring the detailed information of the Broker server, the equipment creates an MQTT client, creates and executes a connect connection message by using the acquired detailed information of the Broker server, and sends a connect request to the Broker server where the acquired detailed information of the Broker server is located;
s8, after the Broker server receives the connect request of the equipment, the Broker server acquires the configuration information of the Broker server from the configuration file, and assembles the acquired configuration information into a key of redis;
s9, the redis performs a self-increment operation on the value according to the current key;
s10, after the step S9 is completed, the Broker server assembles the data packet information which needs to be returned;
and S11, the Broker server returns the data packet information to the equipment, the successful connection of the equipment is fed back, the configuration information of the Broker server is locally stored in the equipment, and the follow-up equipment keeps heartbeat with the current Broker server.
Further, the specific implementation manner of step S1 is: the device obtains the configuration information of the gateway service from the certificate information of the device in a mode of reading file streams, and initiates a request carrying the configuration information of the Broker server connected with the device last time to the gateway service according to the obtained configuration information of the gateway service.
Further, the routing rule of step S2 performs mapping configuration through the request path; and after receiving the request, the gateway service performs mapping through a RequestMapping path.
Further, the configuration information recorded in the configuration information list of each Broker server in step S3 includes an IP/domain name and a port of each Broker server, and the IP/domain name of each Broker server corresponds to a key value.
Further, the specific implementation manner of the step S5 of acquiring the detailed information of the Broker server through the load balancing algorithm includes:
a. if the load balancing algorithm adopts a random algorithm, the business service randomly generates Broker server node information from the configuration information lists of all Broker servers by using random numbers according to the configuration information lists of all Broker servers obtained from the current configuration;
b. if the load balancing algorithm adopts a polling algorithm, the business service marks the configuration information list of each Broker server as different characters according to the configuration information lists of all the Broker servers acquired from the current configuration, acquires the value of a key from redis according to the specially-assigned key, and compares the value with the configuration information lists marked as different characters of the Broker servers to acquire node information of the Broker servers.
Further, the specific implementation b further acquires a configuration information list of the Broker server from the configuration, acquires a value of a key from redis according to the specific key2, and acquires the polled Broker server node information by comparing the value with the configuration list.
Further, the specific implementation manner of step S6 is to forward a request to invoke a service through the gateway service according to a routing rule, acquire the detailed information of the Broker server through the load balancing algorithm of step S5, and return the detailed information to the gateway service, where the gateway service returns the received detailed information of the Broker server to the device sending the request through the network.
Further, the certificate information of the device includes a gateway address and a port;
the request initiated by the device to the gateway service is an http or https request.
By adopting the scheme, the invention has the following beneficial effects:
1. before the connection of the MQTT connect, http or https is adopted to request the address of a dynamic Broker server, so that the mode is more flexible, the expansion of the Broker server is facilitated, the Broker server connected with the equipment can be dynamically switched without upgrading the equipment, and the equipment can be timely recovered to a normal state;
2. the address of the Broker server for establishing connection is not determined by the equipment and the certificate any more, but the initiative of the connection is given to the server, the Broker server connected with the equipment is determined by the balance algorithm and the scheme of the server, and the equipment connection number of the Broker server is controllable;
3. the method is beneficial to the expansion of the example of the Broker, the Broker is expanded in the later period, only the Broker example needs to be added and the Broker server needs to be added to the business service, the configuration is read through the business service, the problem of the Broker example expansion can be solved by combining the balance algorithm configured by the business service, the expansion is more flexible, the equipment connection number of the Broker server is controllable, and the balance algorithm can be variously customized according to the requirement.
Detailed Description
The invention is described in detail below with reference to the figures and the specific embodiments.
Referring to fig. 1 to 3, the present invention provides a method for implementing load balancing based on multiple brokers in MQTT protocol, including the following steps:
s1, the device obtains the configuration information of the gateway service by reading the certificate information of the device, and initiates an http or https request to the gateway service according to the obtained configuration information, wherein the request carries the configuration information of the Broker server connected with the device last time, and is used for obtaining the IP/address and port of the Broker server required by the MQTT client and recalculating the device connection number of the Broker server; specifically, the specific implementation manner of step S1 is: when the device is powered on and connected with WiFi, the device is equivalent to a microcomputer, and as the device is burned with a gateway address, an IP/domain name and a port which contain the mac address of the device, the gateway address of the device registered to a platform before the device leaves a factory, therefore, the device obtains the gateway address and the port of the gateway service from the certificate information burnt in the device (namely the certificate information of the device) by reading the file stream, namely, the certificate information of the device comprises a gateway address and a port, and initiates an http or https request carrying configuration information of a Broker server connected with the device last time to the gateway service according to the acquired gateway address and port, this request may be referred to as a pre-check request to establish a connect connection for an MQTT client, thereby obtaining the IP/address and the port of the Broker server required by the MQTT client, and recalculating the device connection number of the Broker server;
s2, after the gateway service receives the request sent by the device, the gateway service forwards the request to the service through the route rule configured by the gateway service, so that the service reads the configuration file to obtain the configuration information list of all the Broker servers; the routing rule in step S2 performs mapping configuration through the request path, and after receiving the request, the gateway service performs mapping through the RequestMapping path, for example: if the request path contains "/user", the gateway service requests the A service in the cloud service, and if the request path contains "/file", the gateway service requests the B service in the cloud service; specifically, one implementation code for configuring the routing rule is as follows:
spring:
application:
name: gateway
profiles:
active: dev
cloud:
gateway:
routes:
- id: first_route
uri: http://172.16.3.1:8899
predicates:
-Path =/user/. star// request address with user, request 172.16.3.1 server
filters:
- name: Hystrix
args:
name: default
fallbackUri: forward:/fallback
- id: second_route
uri: http://172.16.3.2:8888
predicates:
-Path =/file/. x// file in request address, request 172.16.3.2 server
filters:
# blown configuration
- name: Hystrix
args:
name: hystrix1
fallbackUri: forward:/fallback
;
S3, after receiving the request forwarded by the gateway service, the service reads the configuration file to obtain the configuration information lists of all the Broker servers, assembles the configuration information lists of each Broker server into a redis key, and judges according to the parameter values in the request forwarded by the gateway service:
a. if the parameter value in the request forwarded by the gateway service is null, which indicates that the device has not been connected before, i.e. the device is a new device, then the step S4 is continuously executed;
b. if the parameter value in the request forwarded by the gateway service is not null, which indicates that the device has previously performed connection, assembling the request parameter forwarded by the gateway service received by the service into a key1 of redis (which is a key of redis), and performing a self-subtraction operation on the value of the key1 through redis;
specifically, the configuration information recorded in the configuration information list of each Broker server in step S3 includes the IP/domain name and the port of each Broker server, and the configuration information list of all Broker servers is approximately: { [ "IP": "172.16.1.0", "port": "22" ] }, { [ "IP": "172.16.1.1", "port": "22" ] }; the IP/domain name of each Broker server corresponds to a key value;
s4, the service acquires the device connection number corresponding to each Broker server from the cache of redis according to the key assembled by the configuration information list of each Broker server in the step S3, and returns the device connection number of each Broker server to the service;
s5, after acquiring the device connection number of each Broker server from the redis, the service acquires the detailed information of the Broker servers according to the load balancing algorithm set in the configuration; specifically, the detailed specific implementation manner for acquiring the Broker server through the load balancing algorithm includes:
a. if the load balancing algorithm adopts a random algorithm, the business service randomly generates Broker server node information from the configuration information lists of all Broker servers by using random numbers according to the configuration information lists of all Broker servers obtained from the current configuration; specifically, an implementation code of random numbers is as follows:
Random random = new Random();
List<String> list = Lists.newArrayList();
add ("172.16.1.0"),/random elements
Add ("172.16.1.1"),/random elements
int index = random.nextInt(list.size());
String ip = list.get (index)// result obtained by stochastic algorithm
;
b. If the load balancing algorithm adopts a polling algorithm, the business service acquires configuration information lists { [ "IP": "172.16.1.0", "port": "22" ] }, { [ "IP": "172.16.1.1", "port": "22" ] } labels the configuration information lists as 0 and 1, respectively, i.e. configuration information list { [ "IP": "172.16.1.0", "port": "22" ] } is marked 0, and the configuration information list { [ "IP": "172.16.1.1", "port": "22" ] } is marked as 1, and a value of a specially-designated key (here, the key2 represents the specially-designated value) is obtained from a cache of the redis, the key2 is only used for obtaining which Broker server is currently polled, that is, when a polling algorithm is used by the load balancing algorithm, the service respectively marks the configuration information list of each Broker server as different characters, for example, by using numbers, according to the configuration information lists of all Broker servers currently obtained from the configuration, and obtains a value of key2 corresponding to the Broker server currently polled by the polling algorithm from the cache of the redis, and obtains a Broker server node information by comparing the value with the configuration information lists marked as different characters of the Broker servers and calculating a mapping relationship; for example: if the value of key2 is 0, return [ "IP": "172.16.1.1", "port": the Broker server node information of "22" ], that is, the Broker server node information of the Broker server corresponding to the configuration information list marked as 1 is returned, and the value of key2 of redis is set to 1; if the value of key2 is 1, return [ "IP": "172.16.1.0", "port": the Broker server node information of "22" ], that is, the Broker server node information of the Broker server corresponding to the configuration information list marked as 0 is returned, and the value of key2 of redis is set to 0;
s6, returning the detailed information of the Broker server acquired in the step S5 to the equipment terminal; the specific implementation manner of step S6 is to forward a request to call a service through the gateway service according to a routing rule, acquire the detailed information of the Broker server through the load balancing algorithm in step S5, and return the detailed information to the gateway service, where the gateway service returns the received detailed information of the Broker server to the device (i.e., the device terminal) sending the request through the network after receiving the detailed information of the Broker server;
s7, after acquiring the detailed information of the Broker server (i.e. the node information of the Broker server), the device creates an MQTT client, creates and executes a connect connection message by using the acquired detailed information of the Broker server, and sends a connect request to the Broker server where the acquired configuration information of the Broker server is; specifically, an implementation code for creating the MQTT client is as follows:
@Configuration
public class MqttConfig {
private static final Logger LOGGER = LoggerFactory.getLogger(MqttConfig.class);
public static final CHANNEL RECV = "recvMsgchannel"// CHANNEL of subscription message
public static final CHANNEL _ SEND = "sendMsgchannel"// CHANNEL on which message is issued
public static final String TOPIC = "topic";
/**
Obtaining configuration file information
**/
@Value("${mqtt.username}")
private String username;
@Value("${mqtt.password}")
private String password;
@Value("${mqtt.url}")
private String url;
@Value("${mqtt.send.clientId}")
private String senderClientId;
@Value("${mqtt.recv.clientId}")
private String recverClientId;
Connector of// MQTT client
@Bean
public MqttConnectOptions getMqttConnectOptions() {
MqttConnectOptions options = new MqttConnectOptions();
// whether session is cleared. false: the server will keep the connection record of the client, true: each time the server is connected with a new identity
options.setCleanSession(true);
options, setUserName (username);/connecting users
setPressword (password)
options & setServerURIs (url.split (","));/connected server urls
Setconnectiontimeout (10);// timeout (unit: s)
options, setkeepaliviterval (20);// keep alive heartbeat (unit: s), this method has no reconnection mechanism
return options;
}
// construct MQTT client
@Bean
public MqttPahoClientFactory mqttClientFactory() {
DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
factory.setConnectionOptions(getMqttConnectOptions());
return factory;
}
// MQTT producer client
@Bean
@ServiceActivator(inputChannel = CHANNEL_SEND)
public MessageHandler mqttMsgSend() {
MqttPahoMessageHandler messageHandler = new MqttPahoMessageHandler(senderClientId, mqttClientFactory());
messageHandler.setAsync(true);
return messageHandler;
}
}
;
S8, after the Broker server receives the connect request of the device, the Broker server acquires the configuration information of the Broker server from the configuration file, and assembles the acquired configuration information (namely the configuration information of the Broker server) into a key of redis;
s9, the redis performs a self-increment operation on the value according to the current key, and at the moment, the device connection number of the Broker server is increased by one; specifically, the autonomic operation of redis is atomic;
s10, after the step S9 is completed, the Broker server assembles the data packet information which needs to be returned;
and S11, the Broker server returns the data packet information to the equipment, the successful connection of the equipment is fed back, the configuration information of the Broker server is locally stored in the equipment, and the follow-up equipment keeps heartbeat with the current Broker server.
Compared with the prior art, the invention has the following beneficial effects:
1. before the connection of the MQTT connect, http or https is adopted to request the address of a dynamic Broker server, so that the mode is more flexible, the expansion of the Broker server is facilitated, the Broker server connected with the equipment can be dynamically switched without upgrading the equipment, and the equipment can be timely recovered to a normal state;
2. the address of the Broker server for establishing connection is not determined by the equipment and the certificate any more, but the initiative of the connection is given to the server, the Broker server connected with the equipment is determined by the balance algorithm and the scheme of the server, and the equipment connection number of the Broker server is controllable;
3. the method is beneficial to the expansion of the example of the Broker, the Broker is expanded in the later period, only the Broker example needs to be added and the Broker server needs to be added to the business service, the configuration is read through the business service, the problem of the Broker example expansion can be solved by combining the balance algorithm configured by the business service, the expansion is more flexible, the equipment connection number of the Broker server is controllable, and the balance algorithm can be variously customized according to the requirement.
The present invention is not limited to the above preferred embodiments, and any modifications, equivalent substitutions and improvements made within the spirit and principle of the present invention should be included in the protection scope of the present invention.