Improve static resources and API requests loadbalancing.
This commit is contained in:
		
							parent
							
								
									7bb482bb65
								
							
						
					
					
						commit
						69263e3a95
					
				@ -153,6 +153,8 @@ sql:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
# Actor system parameters
 | 
					# Actor system parameters
 | 
				
			||||||
actors:
 | 
					actors:
 | 
				
			||||||
 | 
					  cluster:
 | 
				
			||||||
 | 
					    grpc_callback_thread_pool_size: "${ACTORS_CLUSTER_GRPC_CALLBACK_THREAD_POOL_SIZE:10}"
 | 
				
			||||||
  tenant:
 | 
					  tenant:
 | 
				
			||||||
    create_components_on_init: "${ACTORS_TENANT_CREATE_COMPONENTS_ON_INIT:true}"
 | 
					    create_components_on_init: "${ACTORS_TENANT_CREATE_COMPONENTS_ON_INIT:true}"
 | 
				
			||||||
  session:
 | 
					  session:
 | 
				
			||||||
 | 
				
			|||||||
@ -1,26 +0,0 @@
 | 
				
			|||||||
#!/bin/bash
 | 
					 | 
				
			||||||
#
 | 
					 | 
				
			||||||
# Copyright © 2016-2018 The Thingsboard Authors
 | 
					 | 
				
			||||||
#
 | 
					 | 
				
			||||||
# Licensed under the Apache License, Version 2.0 (the "License");
 | 
					 | 
				
			||||||
# you may not use this file except in compliance with the License.
 | 
					 | 
				
			||||||
# You may obtain a copy of the License at
 | 
					 | 
				
			||||||
#
 | 
					 | 
				
			||||||
#     http://www.apache.org/licenses/LICENSE-2.0
 | 
					 | 
				
			||||||
#
 | 
					 | 
				
			||||||
# Unless required by applicable law or agreed to in writing, software
 | 
					 | 
				
			||||||
# distributed under the License is distributed on an "AS IS" BASIS,
 | 
					 | 
				
			||||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
					 | 
				
			||||||
# See the License for the specific language governing permissions and
 | 
					 | 
				
			||||||
# limitations under the License.
 | 
					 | 
				
			||||||
#
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
dirsArray=("./haproxy/certs.d" "./haproxy/letsencrypt" "./tb-node/postgres" "./tb-node/cassandra" "./tb-node/log/tb1" "./tb-node/log/tb2")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
for dir in ${dirsArray[@]}
 | 
					 | 
				
			||||||
do
 | 
					 | 
				
			||||||
    if [ ! -d "$dir" ]; then
 | 
					 | 
				
			||||||
        echo creating dir $dir
 | 
					 | 
				
			||||||
        mkdir -p $dir
 | 
					 | 
				
			||||||
    fi
 | 
					 | 
				
			||||||
done
 | 
					 | 
				
			||||||
@ -180,8 +180,6 @@ services:
 | 
				
			|||||||
     - ./haproxy/certs.d:/usr/local/etc/haproxy/certs.d
 | 
					     - ./haproxy/certs.d:/usr/local/etc/haproxy/certs.d
 | 
				
			||||||
    ports:
 | 
					    ports:
 | 
				
			||||||
     - "80:80"
 | 
					     - "80:80"
 | 
				
			||||||
     - "8080"
 | 
					 | 
				
			||||||
     - "8090"
 | 
					 | 
				
			||||||
     - "443:443"
 | 
					     - "443:443"
 | 
				
			||||||
     - "1883:1883"
 | 
					     - "1883:1883"
 | 
				
			||||||
     - "9999:9999"
 | 
					     - "9999:9999"
 | 
				
			||||||
@ -191,7 +189,6 @@ services:
 | 
				
			|||||||
      HTTP_PORT: 80
 | 
					      HTTP_PORT: 80
 | 
				
			||||||
      HTTPS_PORT: 443
 | 
					      HTTPS_PORT: 443
 | 
				
			||||||
      MQTT_PORT: 1883
 | 
					      MQTT_PORT: 1883
 | 
				
			||||||
      TB_API_PORT: 8090
 | 
					 | 
				
			||||||
      FORCE_HTTPS_REDIRECT: "false"
 | 
					      FORCE_HTTPS_REDIRECT: "false"
 | 
				
			||||||
    links:
 | 
					    links:
 | 
				
			||||||
        - tb1
 | 
					        - tb1
 | 
				
			||||||
 | 
				
			|||||||
@ -15,8 +15,6 @@
 | 
				
			|||||||
# limitations under the License.
 | 
					# limitations under the License.
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
 | 
					
 | 
				
			||||||
./check-dirs.sh
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
while [[ $# -gt 0 ]]
 | 
					while [[ $# -gt 0 ]]
 | 
				
			||||||
do
 | 
					do
 | 
				
			||||||
key="$1"
 | 
					key="$1"
 | 
				
			||||||
 | 
				
			|||||||
@ -15,8 +15,6 @@
 | 
				
			|||||||
# limitations under the License.
 | 
					# limitations under the License.
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
 | 
					
 | 
				
			||||||
./check-dirs.sh
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
set -e
 | 
					set -e
 | 
				
			||||||
 | 
					
 | 
				
			||||||
source compose-utils.sh
 | 
					source compose-utils.sh
 | 
				
			||||||
 | 
				
			|||||||
@ -15,8 +15,6 @@
 | 
				
			|||||||
# limitations under the License.
 | 
					# limitations under the License.
 | 
				
			||||||
#
 | 
					#
 | 
				
			||||||
 | 
					
 | 
				
			||||||
./check-dirs.sh
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
for i in "$@"
 | 
					for i in "$@"
 | 
				
			||||||
do
 | 
					do
 | 
				
			||||||
case $i in
 | 
					case $i in
 | 
				
			||||||
 | 
				
			|||||||
@ -56,13 +56,20 @@ frontend http-in
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 reqadd X-Forwarded-Proto:\ http
 | 
					 reqadd X-Forwarded-Proto:\ http
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 acl acl_static path_beg /static/ /index.html
 | 
				
			||||||
 | 
					 acl acl_static path /
 | 
				
			||||||
 | 
					 acl acl_static_rulenode path_beg /static/rulenode/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 acl transport_http_acl path_beg /api/v1/
 | 
					 acl transport_http_acl path_beg /api/v1/
 | 
				
			||||||
 acl letsencrypt_http_acl path_beg /.well-known/acme-challenge/
 | 
					 acl letsencrypt_http_acl path_beg /.well-known/acme-challenge/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 redirect scheme https if !letsencrypt_http_acl !transport_http_acl { env(FORCE_HTTPS_REDIRECT) -m str true }
 | 
					 redirect scheme https if !letsencrypt_http_acl !transport_http_acl { env(FORCE_HTTPS_REDIRECT) -m str true }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 use_backend letsencrypt_http if letsencrypt_http_acl
 | 
					 use_backend letsencrypt_http if letsencrypt_http_acl
 | 
				
			||||||
 use_backend tb-http-backend if transport_http_acl
 | 
					 use_backend tb-http-backend if transport_http_acl
 | 
				
			||||||
 | 
					 use_backend tb-web-backend if acl_static !acl_static_rulenode
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 default_backend tb-web-backend
 | 
					 default_backend tb-api-backend
 | 
				
			||||||
 | 
					
 | 
				
			||||||
frontend https_in
 | 
					frontend https_in
 | 
				
			||||||
  bind *:${HTTPS_PORT} ssl crt /usr/local/etc/haproxy/default.pem crt /usr/local/etc/haproxy/certs.d ciphers ECDHE-RSA-AES256-SHA:RC4-SHA:RC4:HIGH:!MD5:!aNULL:!EDH:!AESGCM
 | 
					  bind *:${HTTPS_PORT} ssl crt /usr/local/etc/haproxy/default.pem crt /usr/local/etc/haproxy/certs.d ciphers ECDHE-RSA-AES256-SHA:RC4-SHA:RC4:HIGH:!MD5:!aNULL:!EDH:!AESGCM
 | 
				
			||||||
@ -72,14 +79,15 @@ frontend https_in
 | 
				
			|||||||
  reqadd X-Forwarded-Proto:\ https
 | 
					  reqadd X-Forwarded-Proto:\ https
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  acl transport_http_acl path_beg /api/v1/
 | 
					  acl transport_http_acl path_beg /api/v1/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  acl acl_static path_beg /static/ /index.html
 | 
				
			||||||
 | 
					  acl acl_static path /
 | 
				
			||||||
 | 
					  acl acl_static_rulenode path_beg /static/rulenode/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  use_backend tb-http-backend if transport_http_acl
 | 
					  use_backend tb-http-backend if transport_http_acl
 | 
				
			||||||
 | 
					  use_backend tb-web-backend if acl_static !acl_static_rulenode
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  default_backend tb-web-backend
 | 
					  default_backend tb-api-backend
 | 
				
			||||||
 | 
					 | 
				
			||||||
frontend http-api-in
 | 
					 | 
				
			||||||
 bind *:${TB_API_PORT}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 default_backend tb-api-backend
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
backend letsencrypt_http
 | 
					backend letsencrypt_http
 | 
				
			||||||
  server letsencrypt_http_srv 127.0.0.1:8080
 | 
					  server letsencrypt_http_srv 127.0.0.1:8080
 | 
				
			||||||
 | 
				
			|||||||
@ -1,8 +1,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
HTTP_BIND_ADDRESS=0.0.0.0
 | 
					HTTP_BIND_ADDRESS=0.0.0.0
 | 
				
			||||||
HTTP_BIND_PORT=8080
 | 
					HTTP_BIND_PORT=8080
 | 
				
			||||||
TB_HOST=haproxy
 | 
					TB_ENABLE_PROXY=false
 | 
				
			||||||
TB_PORT=8090
 | 
					 | 
				
			||||||
LOGGER_LEVEL=info
 | 
					LOGGER_LEVEL=info
 | 
				
			||||||
LOG_FOLDER=logs
 | 
					LOG_FOLDER=logs
 | 
				
			||||||
LOGGER_FILENAME=tb-web-ui-%DATE%.log
 | 
					LOGGER_FILENAME=tb-web-ui-%DATE%.log
 | 
				
			||||||
 | 
				
			|||||||
@ -66,7 +66,7 @@ ospackage {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Copy web files
 | 
					    // Copy web files
 | 
				
			||||||
    from("target/package/linux/web") {
 | 
					    from("target/web") {
 | 
				
			||||||
        into "web"
 | 
					        into "web"
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -20,6 +20,7 @@ server:
 | 
				
			|||||||
  # Server bind port
 | 
					  # Server bind port
 | 
				
			||||||
  port: "HTTP_BIND_PORT"
 | 
					  port: "HTTP_BIND_PORT"
 | 
				
			||||||
thingsboard:
 | 
					thingsboard:
 | 
				
			||||||
 | 
					  enableProxy: "TB_ENABLE_PROXY"
 | 
				
			||||||
  # ThingsBoard node host
 | 
					  # ThingsBoard node host
 | 
				
			||||||
  host: "TB_HOST"
 | 
					  host: "TB_HOST"
 | 
				
			||||||
  # ThingsBoard node port
 | 
					  # ThingsBoard node port
 | 
				
			||||||
 | 
				
			|||||||
@ -20,6 +20,7 @@ server:
 | 
				
			|||||||
  # Server bind port
 | 
					  # Server bind port
 | 
				
			||||||
  port: "8090"
 | 
					  port: "8090"
 | 
				
			||||||
thingsboard:
 | 
					thingsboard:
 | 
				
			||||||
 | 
					  enableProxy: "false"
 | 
				
			||||||
  # ThingsBoard node host
 | 
					  # ThingsBoard node host
 | 
				
			||||||
  host: "localhost"
 | 
					  host: "localhost"
 | 
				
			||||||
  # ThingsBoard node port
 | 
					  # ThingsBoard node port
 | 
				
			||||||
 | 
				
			|||||||
@ -31,14 +31,19 @@ var server;
 | 
				
			|||||||
        const bindAddress = config.get('server.address');
 | 
					        const bindAddress = config.get('server.address');
 | 
				
			||||||
        const bindPort = config.get('server.port');
 | 
					        const bindPort = config.get('server.port');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const thingsboardEnableProxy = config.get('thingsboard.enableProxy');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const thingsboardHost = config.get('thingsboard.host');
 | 
					        const thingsboardHost = config.get('thingsboard.host');
 | 
				
			||||||
        const thingsboardPort = config.get('thingsboard.port');
 | 
					        const thingsboardPort = config.get('thingsboard.port');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        logger.info('Bind address: %s', bindAddress);
 | 
					        logger.info('Bind address: %s', bindAddress);
 | 
				
			||||||
        logger.info('Bind port: %s', bindPort);
 | 
					        logger.info('Bind port: %s', bindPort);
 | 
				
			||||||
 | 
					        logger.info('ThingsBoard Enable Proxy: %s', thingsboardEnableProxy);
 | 
				
			||||||
        logger.info('ThingsBoard host: %s', thingsboardHost);
 | 
					        logger.info('ThingsBoard host: %s', thingsboardHost);
 | 
				
			||||||
        logger.info('ThingsBoard port: %s', thingsboardPort);
 | 
					        logger.info('ThingsBoard port: %s', thingsboardPort);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const useApiProxy = thingsboardEnableProxy === "true";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        var webDir = path.join(__dirname, 'web');
 | 
					        var webDir = path.join(__dirname, 'web');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (typeof process.env.WEB_FOLDER === 'string') {
 | 
					        if (typeof process.env.WEB_FOLDER === 'string') {
 | 
				
			||||||
@ -49,47 +54,51 @@ var server;
 | 
				
			|||||||
        const app = express();
 | 
					        const app = express();
 | 
				
			||||||
        server = http.createServer(app);
 | 
					        server = http.createServer(app);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const apiProxy = httpProxy.createProxyServer({
 | 
					        if (useApiProxy) {
 | 
				
			||||||
            target: {
 | 
					            const apiProxy = httpProxy.createProxyServer({
 | 
				
			||||||
                host: thingsboardHost,
 | 
					                target: {
 | 
				
			||||||
                port: thingsboardPort
 | 
					                    host: thingsboardHost,
 | 
				
			||||||
            }
 | 
					                    port: thingsboardPort
 | 
				
			||||||
        });
 | 
					                }
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        apiProxy.on('error', function (err, req, res) {
 | 
					            apiProxy.on('error', function (err, req, res) {
 | 
				
			||||||
            logger.warn('API proxy error: %s', err.message);
 | 
					                logger.warn('API proxy error: %s', err.message);
 | 
				
			||||||
            res.writeHead(500);
 | 
					                res.writeHead(500);
 | 
				
			||||||
            if (err.code && err.code === 'ECONNREFUSED') {
 | 
					                if (err.code && err.code === 'ECONNREFUSED') {
 | 
				
			||||||
                res.end('Unable to connect to ThingsBoard server.');
 | 
					                    res.end('Unable to connect to ThingsBoard server.');
 | 
				
			||||||
            } else {
 | 
					                } else {
 | 
				
			||||||
                res.end('Thingsboard server connection error: ' + err.code ? err.code : '');
 | 
					                    res.end('Thingsboard server connection error: ' + err.code ? err.code : '');
 | 
				
			||||||
            }
 | 
					                }
 | 
				
			||||||
        });
 | 
					            });
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const root = path.join(webDir, 'public');
 | 
					        if (useApiProxy) {
 | 
				
			||||||
 | 
					            app.all('/api/*', (req, res) => {
 | 
				
			||||||
 | 
					                logger.debug(req.method + ' ' + req.originalUrl);
 | 
				
			||||||
 | 
					                apiProxy.web(req, res);
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const staticDir = path.join(root, 'static');
 | 
					            app.all('/static/rulenode/*', (req, res) => {
 | 
				
			||||||
 | 
					                apiProxy.web(req, res);
 | 
				
			||||||
        app.all('/api/*', (req, res) => {
 | 
					            });
 | 
				
			||||||
            logger.debug(req.method + ' ' + req.originalUrl);
 | 
					        }
 | 
				
			||||||
            apiProxy.web(req, res);
 | 
					 | 
				
			||||||
        });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        app.all('/static/rulenode/*', (req, res) => {
 | 
					 | 
				
			||||||
            apiProxy.web(req, res);
 | 
					 | 
				
			||||||
        });
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        app.use(historyApiFallback());
 | 
					        app.use(historyApiFallback());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        app.use('/static', express.static(staticDir));
 | 
					        const root = path.join(webDir, 'public');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        app.get('*', (req, res) => {
 | 
					        app.use(express.static(root));
 | 
				
			||||||
            apiProxy.web(req, res);
 | 
					 | 
				
			||||||
        });
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        server.on('upgrade', (req, socket, head) => {
 | 
					        if (useApiProxy) {
 | 
				
			||||||
            apiProxy.ws(req, socket, head);
 | 
					            app.get('*', (req, res) => {
 | 
				
			||||||
        });
 | 
					                apiProxy.web(req, res);
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            server.on('upgrade', (req, socket, head) => {
 | 
				
			||||||
 | 
					                apiProxy.ws(req, socket, head);
 | 
				
			||||||
 | 
					            });
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        server.listen(bindPort, bindAddress, (error) => {
 | 
					        server.listen(bindPort, bindAddress, (error) => {
 | 
				
			||||||
            if (error) {
 | 
					            if (error) {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user