Commit 69263e3a953d4d75c728ab02e5727be909a89281

Authored by Igor Kulikov
1 parent 7bb482bb

Improve static resources and API requests loadbalancing.

... ... @@ -153,6 +153,8 @@ sql:
153 153
154 154 # Actor system parameters
155 155 actors:
  156 + cluster:
  157 + grpc_callback_thread_pool_size: "${ACTORS_CLUSTER_GRPC_CALLBACK_THREAD_POOL_SIZE:10}"
156 158 tenant:
157 159 create_components_on_init: "${ACTORS_TENANT_CREATE_COMPONENTS_ON_INIT:true}"
158 160 session:
... ...
1   -#!/bin/bash
2   -#
3   -# Copyright © 2016-2018 The Thingsboard Authors
4   -#
5   -# Licensed under the Apache License, Version 2.0 (the "License");
6   -# you may not use this file except in compliance with the License.
7   -# You may obtain a copy of the License at
8   -#
9   -# http://www.apache.org/licenses/LICENSE-2.0
10   -#
11   -# Unless required by applicable law or agreed to in writing, software
12   -# distributed under the License is distributed on an "AS IS" BASIS,
13   -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   -# See the License for the specific language governing permissions and
15   -# limitations under the License.
16   -#
17   -
18   -dirsArray=("./haproxy/certs.d" "./haproxy/letsencrypt" "./tb-node/postgres" "./tb-node/cassandra" "./tb-node/log/tb1" "./tb-node/log/tb2")
19   -
20   -for dir in ${dirsArray[@]}
21   -do
22   - if [ ! -d "$dir" ]; then
23   - echo creating dir $dir
24   - mkdir -p $dir
25   - fi
26   -done
... ... @@ -180,8 +180,6 @@ services:
180 180 - ./haproxy/certs.d:/usr/local/etc/haproxy/certs.d
181 181 ports:
182 182 - "80:80"
183   - - "8080"
184   - - "8090"
185 183 - "443:443"
186 184 - "1883:1883"
187 185 - "9999:9999"
... ... @@ -191,7 +189,6 @@ services:
191 189 HTTP_PORT: 80
192 190 HTTPS_PORT: 443
193 191 MQTT_PORT: 1883
194   - TB_API_PORT: 8090
195 192 FORCE_HTTPS_REDIRECT: "false"
196 193 links:
197 194 - tb1
... ...
... ... @@ -15,8 +15,6 @@
15 15 # limitations under the License.
16 16 #
17 17
18   -./check-dirs.sh
19   -
20 18 while [[ $# -gt 0 ]]
21 19 do
22 20 key="$1"
... ...
... ... @@ -15,8 +15,6 @@
15 15 # limitations under the License.
16 16 #
17 17
18   -./check-dirs.sh
19   -
20 18 set -e
21 19
22 20 source compose-utils.sh
... ...
... ... @@ -15,8 +15,6 @@
15 15 # limitations under the License.
16 16 #
17 17
18   -./check-dirs.sh
19   -
20 18 for i in "$@"
21 19 do
22 20 case $i in
... ...
... ... @@ -56,13 +56,20 @@ frontend http-in
56 56
57 57 reqadd X-Forwarded-Proto:\ http
58 58
  59 + acl acl_static path_beg /static/ /index.html
  60 + acl acl_static path /
  61 + acl acl_static_rulenode path_beg /static/rulenode/
  62 +
59 63 acl transport_http_acl path_beg /api/v1/
60 64 acl letsencrypt_http_acl path_beg /.well-known/acme-challenge/
  65 +
61 66 redirect scheme https if !letsencrypt_http_acl !transport_http_acl { env(FORCE_HTTPS_REDIRECT) -m str true }
  67 +
62 68 use_backend letsencrypt_http if letsencrypt_http_acl
63 69 use_backend tb-http-backend if transport_http_acl
  70 + use_backend tb-web-backend if acl_static !acl_static_rulenode
64 71
65   - default_backend tb-web-backend
  72 + default_backend tb-api-backend
66 73
67 74 frontend https_in
68 75 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
72 79 reqadd X-Forwarded-Proto:\ https
73 80
74 81 acl transport_http_acl path_beg /api/v1/
75   - use_backend tb-http-backend if transport_http_acl
76 82
77   - default_backend tb-web-backend
  83 + acl acl_static path_beg /static/ /index.html
  84 + acl acl_static path /
  85 + acl acl_static_rulenode path_beg /static/rulenode/
78 86
79   -frontend http-api-in
80   - bind *:${TB_API_PORT}
  87 + use_backend tb-http-backend if transport_http_acl
  88 + use_backend tb-web-backend if acl_static !acl_static_rulenode
81 89
82   - default_backend tb-api-backend
  90 + default_backend tb-api-backend
83 91
84 92 backend letsencrypt_http
85 93 server letsencrypt_http_srv 127.0.0.1:8080
... ...
1 1
2 2 HTTP_BIND_ADDRESS=0.0.0.0
3 3 HTTP_BIND_PORT=8080
4   -TB_HOST=haproxy
5   -TB_PORT=8090
  4 +TB_ENABLE_PROXY=false
6 5 LOGGER_LEVEL=info
7 6 LOG_FOLDER=logs
8 7 LOGGER_FILENAME=tb-web-ui-%DATE%.log
... ...
... ... @@ -66,7 +66,7 @@ ospackage {
66 66 }
67 67
68 68 // Copy web files
69   - from("target/package/linux/web") {
  69 + from("target/web") {
70 70 into "web"
71 71 }
72 72
... ...
... ... @@ -20,6 +20,7 @@ server:
20 20 # Server bind port
21 21 port: "HTTP_BIND_PORT"
22 22 thingsboard:
  23 + enableProxy: "TB_ENABLE_PROXY"
23 24 # ThingsBoard node host
24 25 host: "TB_HOST"
25 26 # ThingsBoard node port
... ...
... ... @@ -20,6 +20,7 @@ server:
20 20 # Server bind port
21 21 port: "8090"
22 22 thingsboard:
  23 + enableProxy: "false"
23 24 # ThingsBoard node host
24 25 host: "localhost"
25 26 # ThingsBoard node port
... ...
... ... @@ -31,14 +31,19 @@ var server;
31 31 const bindAddress = config.get('server.address');
32 32 const bindPort = config.get('server.port');
33 33
  34 + const thingsboardEnableProxy = config.get('thingsboard.enableProxy');
  35 +
34 36 const thingsboardHost = config.get('thingsboard.host');
35 37 const thingsboardPort = config.get('thingsboard.port');
36 38
37 39 logger.info('Bind address: %s', bindAddress);
38 40 logger.info('Bind port: %s', bindPort);
  41 + logger.info('ThingsBoard Enable Proxy: %s', thingsboardEnableProxy);
39 42 logger.info('ThingsBoard host: %s', thingsboardHost);
40 43 logger.info('ThingsBoard port: %s', thingsboardPort);
41 44
  45 + const useApiProxy = thingsboardEnableProxy === "true";
  46 +
42 47 var webDir = path.join(__dirname, 'web');
43 48
44 49 if (typeof process.env.WEB_FOLDER === 'string') {
... ... @@ -49,47 +54,51 @@ var server;
49 54 const app = express();
50 55 server = http.createServer(app);
51 56
52   - const apiProxy = httpProxy.createProxyServer({
53   - target: {
54   - host: thingsboardHost,
55   - port: thingsboardPort
56   - }
57   - });
58   -
59   - apiProxy.on('error', function (err, req, res) {
60   - logger.warn('API proxy error: %s', err.message);
61   - res.writeHead(500);
62   - if (err.code && err.code === 'ECONNREFUSED') {
63   - res.end('Unable to connect to ThingsBoard server.');
64   - } else {
65   - res.end('Thingsboard server connection error: ' + err.code ? err.code : '');
66   - }
67   - });
68   -
69   - const root = path.join(webDir, 'public');
70   -
71   - const staticDir = path.join(root, 'static');
  57 + if (useApiProxy) {
  58 + const apiProxy = httpProxy.createProxyServer({
  59 + target: {
  60 + host: thingsboardHost,
  61 + port: thingsboardPort
  62 + }
  63 + });
  64 +
  65 + apiProxy.on('error', function (err, req, res) {
  66 + logger.warn('API proxy error: %s', err.message);
  67 + res.writeHead(500);
  68 + if (err.code && err.code === 'ECONNREFUSED') {
  69 + res.end('Unable to connect to ThingsBoard server.');
  70 + } else {
  71 + res.end('Thingsboard server connection error: ' + err.code ? err.code : '');
  72 + }
  73 + });
  74 + }
72 75
73   - app.all('/api/*', (req, res) => {
74   - logger.debug(req.method + ' ' + req.originalUrl);
75   - apiProxy.web(req, res);
76   - });
  76 + if (useApiProxy) {
  77 + app.all('/api/*', (req, res) => {
  78 + logger.debug(req.method + ' ' + req.originalUrl);
  79 + apiProxy.web(req, res);
  80 + });
77 81
78   - app.all('/static/rulenode/*', (req, res) => {
79   - apiProxy.web(req, res);
80   - });
  82 + app.all('/static/rulenode/*', (req, res) => {
  83 + apiProxy.web(req, res);
  84 + });
  85 + }
81 86
82 87 app.use(historyApiFallback());
83 88
84   - app.use('/static', express.static(staticDir));
  89 + const root = path.join(webDir, 'public');
85 90
86   - app.get('*', (req, res) => {
87   - apiProxy.web(req, res);
88   - });
  91 + app.use(express.static(root));
89 92
90   - server.on('upgrade', (req, socket, head) => {
91   - apiProxy.ws(req, socket, head);
92   - });
  93 + if (useApiProxy) {
  94 + app.get('*', (req, res) => {
  95 + apiProxy.web(req, res);
  96 + });
  97 +
  98 + server.on('upgrade', (req, socket, head) => {
  99 + apiProxy.ws(req, socket, head);
  100 + });
  101 + }
93 102
94 103 server.listen(bindPort, bindAddress, (error) => {
95 104 if (error) {
... ...