Improve static resources and API requests loadbalancing.

This commit is contained in:
Igor Kulikov 2018-11-07 19:09:55 +02:00
parent 7bb482bb65
commit 69263e3a95
12 changed files with 63 additions and 78 deletions

View File

@ -153,6 +153,8 @@ sql:
# Actor system parameters
actors:
cluster:
grpc_callback_thread_pool_size: "${ACTORS_CLUSTER_GRPC_CALLBACK_THREAD_POOL_SIZE:10}"
tenant:
create_components_on_init: "${ACTORS_TENANT_CREATE_COMPONENTS_ON_INIT:true}"
session:

View File

@ -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

View File

@ -180,8 +180,6 @@ services:
- ./haproxy/certs.d:/usr/local/etc/haproxy/certs.d
ports:
- "80:80"
- "8080"
- "8090"
- "443:443"
- "1883:1883"
- "9999:9999"
@ -191,7 +189,6 @@ services:
HTTP_PORT: 80
HTTPS_PORT: 443
MQTT_PORT: 1883
TB_API_PORT: 8090
FORCE_HTTPS_REDIRECT: "false"
links:
- tb1

View File

@ -15,8 +15,6 @@
# limitations under the License.
#
./check-dirs.sh
while [[ $# -gt 0 ]]
do
key="$1"

View File

@ -15,8 +15,6 @@
# limitations under the License.
#
./check-dirs.sh
set -e
source compose-utils.sh

View File

@ -15,8 +15,6 @@
# limitations under the License.
#
./check-dirs.sh
for i in "$@"
do
case $i in

View File

@ -56,13 +56,20 @@ frontend http-in
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 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 }
use_backend letsencrypt_http if letsencrypt_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
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
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-web-backend if acl_static !acl_static_rulenode
default_backend tb-web-backend
frontend http-api-in
bind *:${TB_API_PORT}
default_backend tb-api-backend
default_backend tb-api-backend
backend letsencrypt_http
server letsencrypt_http_srv 127.0.0.1:8080

View File

@ -1,8 +1,7 @@
HTTP_BIND_ADDRESS=0.0.0.0
HTTP_BIND_PORT=8080
TB_HOST=haproxy
TB_PORT=8090
TB_ENABLE_PROXY=false
LOGGER_LEVEL=info
LOG_FOLDER=logs
LOGGER_FILENAME=tb-web-ui-%DATE%.log

View File

@ -66,7 +66,7 @@ ospackage {
}
// Copy web files
from("target/package/linux/web") {
from("target/web") {
into "web"
}

View File

@ -20,6 +20,7 @@ server:
# Server bind port
port: "HTTP_BIND_PORT"
thingsboard:
enableProxy: "TB_ENABLE_PROXY"
# ThingsBoard node host
host: "TB_HOST"
# ThingsBoard node port

View File

@ -20,6 +20,7 @@ server:
# Server bind port
port: "8090"
thingsboard:
enableProxy: "false"
# ThingsBoard node host
host: "localhost"
# ThingsBoard node port

View File

@ -31,14 +31,19 @@ var server;
const bindAddress = config.get('server.address');
const bindPort = config.get('server.port');
const thingsboardEnableProxy = config.get('thingsboard.enableProxy');
const thingsboardHost = config.get('thingsboard.host');
const thingsboardPort = config.get('thingsboard.port');
logger.info('Bind address: %s', bindAddress);
logger.info('Bind port: %s', bindPort);
logger.info('ThingsBoard Enable Proxy: %s', thingsboardEnableProxy);
logger.info('ThingsBoard host: %s', thingsboardHost);
logger.info('ThingsBoard port: %s', thingsboardPort);
const useApiProxy = thingsboardEnableProxy === "true";
var webDir = path.join(__dirname, 'web');
if (typeof process.env.WEB_FOLDER === 'string') {
@ -49,47 +54,51 @@ var server;
const app = express();
server = http.createServer(app);
const apiProxy = httpProxy.createProxyServer({
target: {
host: thingsboardHost,
port: thingsboardPort
}
});
if (useApiProxy) {
const apiProxy = httpProxy.createProxyServer({
target: {
host: thingsboardHost,
port: thingsboardPort
}
});
apiProxy.on('error', function (err, req, res) {
logger.warn('API proxy error: %s', err.message);
res.writeHead(500);
if (err.code && err.code === 'ECONNREFUSED') {
res.end('Unable to connect to ThingsBoard server.');
} else {
res.end('Thingsboard server connection error: ' + err.code ? err.code : '');
}
});
apiProxy.on('error', function (err, req, res) {
logger.warn('API proxy error: %s', err.message);
res.writeHead(500);
if (err.code && err.code === 'ECONNREFUSED') {
res.end('Unable to connect to ThingsBoard server.');
} else {
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('/api/*', (req, res) => {
logger.debug(req.method + ' ' + req.originalUrl);
apiProxy.web(req, res);
});
app.all('/static/rulenode/*', (req, res) => {
apiProxy.web(req, res);
});
app.all('/static/rulenode/*', (req, res) => {
apiProxy.web(req, res);
});
}
app.use(historyApiFallback());
app.use('/static', express.static(staticDir));
const root = path.join(webDir, 'public');
app.get('*', (req, res) => {
apiProxy.web(req, res);
});
app.use(express.static(root));
server.on('upgrade', (req, socket, head) => {
apiProxy.ws(req, socket, head);
});
if (useApiProxy) {
app.get('*', (req, res) => {
apiProxy.web(req, res);
});
server.on('upgrade', (req, socket, head) => {
apiProxy.ws(req, socket, head);
});
}
server.listen(bindPort, bindAddress, (error) => {
if (error) {