由于条件原因,我是在同一台服务器上利用了三个tomcat配置geoserver集群(多台服务器配置基本上差不多)【为了后面好区分将三个tomcat分别命名为tomcat-geo1,tomcat-geo2,tomcat-geo3】。
一、准备工作
1、分别修改三个tomcat的server.xml,将其端口修改为:8083,8084,8085。
2、设置tomcat允许跨域
每一个tomcat安装目录下 conf > web.xml中添加如下这段:
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
3、下载geoserver版本及依赖
geoserver-2.19.1-war
geoserver-2.19-SNAPSHOT-activeMQ-broker-plugin
geoserver-2.19-SNAPSHOT-jms-cluster-plugin
下载地址:https://build.geoserver.org/geoserver
二、部署geoserver
1、将geoserver-2.19.1-war分别放入三个tomcat的webapps下。
2、启动三个tomcat ,分别访问测试下,IP:端口/geoserver,进入如下页面则表示成功:
3、数据同步
虽然现在三个geoserver服务都可以使用了,但是它们的数据并没有同步,为了避免geoserver服务过多的问题,
所以设置一个统一存放数据的位置,最好的方式是将该目录设置到一台NFS文件服务器上,但此处因为我们只有
一台机器,我们就创建一个统一的数据目录,命名为GEOSERVER_DATA_DIR。
所以此处需要对每个geoserver的数据目录重新指定一下,为了方便,我将缓存也都存放在同一目录中。
在每个geoserver的WEB-INF/web.xml中添加这段:
<!-- 数据目录 -->
<context-param>
<param-name>GEOSERVER_DATA_DIR</param-name>
<param-value>/DataDisk/geoserver/geoserver_data_dir</param-value>
</context-param>
<!-- 缓存目录 -->
<context-param>
<param-name>GEOWEBCACHE_CACHE_DIR</param-name>
<param-value>/DataDisk/geoserver/geowebcache</param-value>
</context-param>
然后重启geoserver服务。
4、解压geoserver-2.19-SNAPSHOT-jms-cluster-plugin,然后将其中的jar全部复制到geoserver的WEB-INF/lib目录下。
5、将geoserver-2.19-SNAPSHOT-activeMQ-broker-plugin.war放入tomcat-geo1的webapps目录中,删除其中的
activemqBroker-2.16-SNAPSHOT\WEB-INF\classes目录下standalone-broker.properties和
applicationContext.xml。(经过多次测试发现这两个文件有无都无所谓)
6、配置geoserver的cluster,这里由于前面我设置了统一数据目录,所以三个geoserver生成的cluster文件夹也在
该目录下。打开cluster文件夹,里面应该有三个clusterConfig文件夹,各自放着cluster.properties和embedded-
broker.properties文件。
①、在clusterConfig文件夹中新建broker.xml文件,内容如下(直接复制即可,无需修改):
broker.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--DOCTYPE beans PUBLIC "-//ACTIVEMQ//DTD//EN" "http://activemq.org/dtd/activemq.dtd" -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jms="http://www.springframework.org/schema/jms" xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core.xsd">
<!-- Allows us to use system properties as variables in this configuration
file -->
<bean id="activemq.propertyPlaceholderConfigurer"
class="org.geoserver.cluster.impl.utils.JMSPropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="searchSystemEnvironment" value="true" />
<property name="ignoreUnresolvablePlaceholders" value="false" />
<property name="ignoreResourceNotFound" value="true" />
<constructor-arg index="0" type="Resource">
<value>embedded-broker.properties</value>
</constructor-arg>
<constructor-arg index="1" type="JMSConfiguration"
ref="JMSConfiguration" />
</bean>
<broker id="broker" persistent="${activemq.broker.persistent}"
useJmx="${activemq.jmx.useJmx}" xmlns="http://activemq.apache.org/schema/core"
dataDirectory="${activemq.base}" tmpDataDirectory="${activemq.base}/tmp"
startAsync="false" start="false" brokerName="${instanceName}">
<!-- The managementContext is used to configure how ActiveMQ is exposed
in JMX. By default, ActiveMQ uses the MBean server that is started by the
JVM. For more information, see: http://activemq.apache.org/jmx.html -->
<managementContext>
<managementContext createConnector="${activemq.jmx.createConnector}"
connectorPort="${activemq.jmx.port}" connectorHost="${activemq.jmx.host}" />
</managementContext>
<!--destinationInterceptors>
<virtualDestinationInterceptor>
<virtualDestinations>
<virtualTopic name=">" prefix="Consumer.*.VirtualTopic."
selectorAware="false" />
</virtualDestinations>
</virtualDestinationInterceptor>
</destinationInterceptors-->
<!-- Configure message persistence for the broker. The default persistence
mechanism is the KahaDB store (identified by the kahaDB tag). For more information,
see: http://activemq.apache.org/persistence.html -->
<amq:persistenceAdapter>
<kahaDB directory="${activemq.base}/kahadb"
lockKeepAlivePeriod="0" />
<!-- <jdbcPersistenceAdapter dataDirectory="activemq-data" dataSource="#postgres-ds"
lockKeepAlivePeriod="0" /> -->
</amq:persistenceAdapter>
<!-- The systemUsage controls the maximum amount of space the broker will
use before slowing down producers. For more information, see: http://activemq.apache.org/producer-flow-control.html -->
<systemUsage>
<systemUsage>
<memoryUsage>
<memoryUsage limit="${activemq.broker.systemUsage.memoryUsage}" />
</memoryUsage>
<storeUsage>
<storeUsage limit="${activemq.broker.systemUsage.storeUsage}" />
</storeUsage>
<tempUsage>
<tempUsage limit="${activemq.broker.systemUsage.tempUsage}" />
</tempUsage>
</systemUsage>
</systemUsage>
<!-- The transport connectors expose ActiveMQ over a given protocol to
clients and other brokers. For more information, see: http://activemq.apache.org/configuring-transports.html -->
<transportConnectors>
<transportConnector name="openwire"
uri="${activemq.transportConnectors.server.uri}" discoveryUri="${activemq.transportConnectors.server.discoveryURI}" />
</transportConnectors>
<networkConnectors xmlns="http://activemq.apache.org/schema/core">
<networkConnector
uri="${activemq.transportConnectors.server.discoveryURI}" />
</networkConnectors>
<!-- destroy the spring context on shutdown to stop jetty -->
<shutdownHooks>
<bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.hooks.SpringContextHook" />
</shutdownHooks>
</broker>
<!-- This xbean configuration file supports all the standard spring xml
configuration options -->
<!-- <bean id="postgres-ds" class="org.postgresql.ds.PGPoolingDataSource"> -->
<!-- <property name="serverName" value="localhost" /> -->
<!-- <property name="databaseName" value="activemq" /> -->
<!-- <property name="portNumber" value="5432" /> -->
<!-- <property name="user" value="postgres" /> -->
<!-- <property name="password" value="postgres" /> -->
<!-- <property name="dataSourceName" value="postgres" /> -->
<!-- <property name="initialConnections" value="1" /> -->
<!-- <property name="maxConnections" value="30" /> -->
<!-- </bean> -->
<!-- Oracle DataSource Sample Setup -->
<!-- <bean id="oracle-ds" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:AMQDB"/> <property
name="username" value="scott"/> <property name="password" value="tiger"/>
<property name="poolPreparedStatements" value="true"/> </bean> -->
</beans>
②、修改embedded-broker.properties
③、修改cluster.properties
注意:如果有多个geoserver服务,但统一目录中的cluster文件夹下总的只有一个cluster.perties文件,解决方
法:找到每个tomcat下bin目录中的catalina.sh(linux下)或者catalina.bat(windows下)文件,再在文件中找到
JAVA_OPTS="$JAVA_OPTS $JSSE_OPTS"这话,在它的后面添加"JAVA_OPTS=%JAVA_OPTS% -D
CLUSTER_CONFIG_DIR=/DataDisk/geoserver/geoserver_data_dir/cluster/clusterConfig文件夹名"。然后重启
tomcat就有多个cluster.perties文件了。
7、重启全部geoserver服务,查看配置是否生效以及数据是否同步,如果每个geoserver的cluster Setting都如下
图,则说明配置成功。
8.验证
在任意一个geoserver上创建一个workspace,其他geoserver服务也同步。
三、session共享配置
1、下载tomcat9,nginx,redis的session共享包
链接: https://pan.baidu.com/s/16uh2aS_jR4QVj-mtBionTA
提取码: d66b
2、将里面的4个jar包导到t每个omcat的lib目录下。
3、修改redis-data-cache.properties文件,根据自己redis信息进行修改,修改完成将其放到每个tomcat的conf文件夹下。
4、在修改每个tomcat下conf文件夹下的context.xml文件
<Valve className="tomcat.request.session.redis.SessionHandlerValve" />
<Manager className="tomcat.request.session.redis.SessionManager"/>
5、重启各tomcat,在redis中查看结果,验证其session是否共享。
四、nginx负载均衡配置
修改nginx.conf配置
http节点中添加如下配置
upstream mygeoserver {
server localhost:8083 max_fails=3 fail_timeout=30s;
server localhost:8084 max_fails=3 fail_timeout=30s;
server localhost:8085 max_fails=3 fail_timeout=30s;
}
配置location(这里因为需求问题所以配置有点复杂)
location ^~ /geoserver/ {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
# 解决504错误,单位:秒
proxy_send_timeout 900s;
proxy_read_timeout 900s;
send_timeout 900s;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect http:// $scheme://;
proxy_pass http://mygeoserver/geoserver/;
index index.html index.htm index.jsp;
}
在浏览器地址栏中输入:IP+nginx中server端口+/geoserver即可访问。
参考地址:
https://docs.geoserver.geo-solutions.it/edu/en/clustering/index.html
https://docs.geoserver.geo-solutions.it/edu/en/clustering/clustering/active/installation.html