概述
為了提高服務(wù)的訪問(wèn)速度,減輕geoserver服務(wù)的壓力,同時(shí)避免服務(wù)節(jié)點(diǎn)出現(xiàn)問(wèn)題而影響服務(wù)訪問(wèn)的穩(wěn)定性,我們通常會(huì)通過(guò)部署多個(gè)geoserver來(lái)解決,但是部署了多個(gè)geoserver后,我們需要一個(gè)統(tǒng)一的接口提供出來(lái)供使用,nginx很好地可以這樣的需求,本文講講如何通過(guò)nginx實(shí)現(xiàn)多geoserver服務(wù)的負(fù)載均衡。
實(shí)現(xiàn)效果
實(shí)現(xiàn)
1. 多geoserver部署
為了保持geoserver的服務(wù)一致,我們先配置好一個(gè)geoserver服務(wù),配置好之后將部署的tomcat復(fù)制,克隆多個(gè)出來(lái),本文為演示復(fù)制了兩個(gè)(共三個(gè)geoserver),修改Tomcat的端口,使三個(gè)端口不沖突,復(fù)制好之后分別啟動(dòng)三個(gè)Tomcat。
2. nginx配置
修改nginx.conf文件,配置信息如下:
#user??nobody; worker_processes??1; #error_log??logs/error.log; #error_log??logs/error.log??notice; #error_log??logs/error.log??info; #pid????????logs/nginx.pid; events?{ ????worker_connections??1024; } http?{ ????include???????mime.types; ????default_type??application/octet-stream; ????#log_format??main??'$remote_addr?-?$remote_user?[$time_local]?"$request"?' ????#??????????????????'$status?$body_bytes_sent?"$http_referer"?' ????#??????????????????'"$http_user_agent"?"$http_x_forwarded_for"'; ????#access_log??logs/access.log??main; ????sendfile????????on; ????#tcp_nopush?????on; ????#keepalive_timeout??0; ????keepalive_timeout??65; ????#gzip??on; ???? ????#?反向代理配置 ????upstream?server_list{ ???????#?這個(gè)是tomcat的訪問(wèn)路徑 ???????server?localhost:8081; ???????server?localhost:8082; ???????server?localhost:8083; ????} ????server?{ ????????listen???????80; ????????server_name??localhost; ????? ????????location?/?{ ????????????add_header?'Access-Control-Allow-Origin'?$http_origin; ????????????add_header?'Access-Control-Allow-Credentials'?'true'; ????????????add_header?'Access-Control-Allow-Methods'?'GET,?POST,?OPTIONS'; ????????????add_header?'Access-Control-Allow-Headers'?'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'; ????????????add_header?'Access-Control-Expose-Headers'?'Content-Length,Content-Range'; ????????????if?($request_method?=?'OPTIONS')?{ ????????????????add_header?'Access-Control-Max-Age'?1728000; ????????????????add_header?'Content-Type'?'text/plain;?charset=utf-8'; ????????????????add_header?'Content-Length'?0; ????????????????return?204; ????????????} ????????????root???html; ????????????proxy_pass?http://server_list; ????????????index??index.html?index.htm; ????????} ???????? ????????error_page???500?502?503?504??/50x.html; ????????location?=?/50x.html?{ ????????????root???html; ????????} ????} }
配置好nginx后,啟動(dòng)nginx。
3. 前端調(diào)用
根據(jù)上述的配置,nginx的端口為80,因此geoserver的地址為http://localhost/geoserver,在ol中的調(diào)用代碼如下:
nbsp;html> ??<meta> ??<title>OpenLayers?map?preview</title> ??<link> ??<link> ??<script></script><div></div> <script> const options = { center: [52102781.07568731, 4456849.777083951], zoom: 3, minZoom: 0, maxZoom: 18 } const base = new ol.layer.Tile({ visible: true, source: new ol.source.OSM() }); const wms = new ol.layer.Tile({ source: new ol.source.TileWMS({ url: 'http://localhost/geoserver/mapbox/wms', params: {'LAYERS': 'mapbox:city', 'TILED': true}, serverType: 'geoserver', transition: 0 }) }) window.map = new ol.Map({ controls: ol.control.defaults({ attribution: false }).extend([new ol.control.ScaleLine()]), target: 'map', layers: [base, wms], view: new ol.View({ center: options.center, zoom: options.zoom, minZoom: options.minZoom, maxZoom: options.maxZoom }) }); </script>
? 版權(quán)聲明
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載。
THE END