redis clusters + tomcat9 + nginx load balancing multiple servers

The experimental environment

Environment and software needed

Configuration Environment Tomcat required jar package
Windows Server2019 Datacenter tomcat-redis-session-manager-master-2.0.0.jar
Tomcat9 jedis-2.7.3.jar
Redis3.2 commons-pool2-2.3.jar
nginx1.17.7 Based here to download the different versions of Tomcat compiled jar package
JDK1.8 Links: https://pan.baidu.com/s/1PjPZWIiz6mnAOxPiHWhhQw extraction code: o2e8

server configuration

Server (server shut down three firewall) Master-slave relationship The required configuration environment
192.168.100.211 the Lord Redis(master)+Nginx
192.168.100.210 From Redis+Tomcat9+JDK1.8
192.168.100.209 From Redis+Tomcat9+JDK1.8

Redis configuration

Installation Redis

windows need to download the version from github

Download: https://github.com/microsoftarchive/redis/releases

The downloaded Redis archive are extracted to three servers

Configuration Redis.windows.conf

The primary server (192.168.100.211) arranged Redis

##最基础配置
port 6379(Redis默认端口,配置Redis端口,每台服务器Redis端口不能相同)
bind 127.0.0.1 192.168.100.211(主服务器ip地址)
protected-mode no(默认为yes需要修改为no)
----------------
##可额外添加的配置
requirepass foobared 设置主服务器密码
maxclients 10000     默认最大链接数
maxmemory <bytes>    最大内存容量(字节) 

Redis configuration from the server (192.168.100.209,192.168.100.210)

Note that the server should configure how many number of times, the steps are the same for each server (hereinafter, both servers are example configurations out for comparison)

##192.168.100.209
##最基础配置
port 6380(配置Redis端口,每台服务器Redis端口不能相同)
bind 127.0.0.1 192.168.100.209
slaveof 192.168.100.211 6379(绑定主服务器)
protected-mode no (默认为yes需要修改为no)
slave-read-only no(原先是yes必须要修改为no不然部署项目,节点只有读的权限没有写的权限)
----------------
##可额外添加配置
masterauth <master-password>(如果主服务器设置了密码,那么从服务器要配置主服务器密码,否则链接不上)
##192.168.100.210
##最基础配置
port 6381(配置Redis端口,每台服务器Redis端口不能相同)
bind 127.0.0.1 192.168.100.210
slaveof 192.168.100.211 6379(绑定主服务器)
protected-mode no (默认为yes需要修改为no)
slave-read-only no(原先是yes必须要修改为no不然部署项目,节点只有读的权限没有写的权限)
----------------
##可额外添加配置
masterauth <master-password>(如果主服务器设置了密码,那么从服务器要配置主服务器密码,否则链接不上)

At this point the master server from all profiles have been configured

Redis startup script writing

If you click directly redis-server.exe is not that he is unable to run direct us to write a good profile, so we need to start to write scripts to run Redis

Each server to do the same operation, where a master server 192.168.100.211, for example

In the configuration file directory, create Startup.bat file, the code is as follows

title Redis-x64-3.2.100(文件夹名字)
redis-server.exe redis.windows.conf

1.png startup script

In the parent directory profile directory, create a Start6379.bat (named easily play), as follows

cd Redis-x64-3.2.100(文件夹名字)
startup.bat

Startup script 2.png

run

In turn run from the main server and each server directory Start6379.bat ...... if it means the success of the following

Primary server

Master server run successfully .png

From the server

1.png successfully run from the server

From a server running a successful 2.png

Configuring Tomcat from the server

Add the required documents

The downloaded file, select the corresponding Tomcat version, the three jar package lib directory to copy the file lib Tomcat installation directory

How many Tomcat server operation is repeated several times

Write Tomcat configuration file

Open Tomcat installation directory conf / context.xml file , the <context></context>add the following configuration

##192.168.100.209
----------------
   <Valve className="com.naritech.nicole.gump.RedisSessionHandlerValve" />
   <Manager className="com.naritech.nicole.gump.RedisSessionManager"
     host="192.168.100.209"(从服务器IP)
     port="6380"(从服务器Redis端口)
     database="0"
     maxInactiveInterval="60"
     />   
##192.168.100.210
----------------
   <Valve className="com.naritech.nicole.gump.RedisSessionHandlerValve" />
   <Manager className="com.naritech.nicole.gump.RedisSessionManager"
     host="192.168.100.210"(从服务器IP)
     port="6381"(从服务器Redis端口)
     database="0"
     maxInactiveInterval="60"
     />   

run

After adding this configuration file we restart Tomcat check the default table if the server can be accessed from page to Tomcat

Configure the primary server Nginx

#允许生成的进程数,一般设置为当前机器总Cpu核心数的1-2倍
worker_processes  2;
#添加配置
upstream mycluster{
#集群有几台服务器就配置几台,weight表示权重,权重越大访问几率越大
#这里添加已经配置启动好的两台tomcat服务器
#down表示当前的server暂时不参与负载
#backup其他设备都宕机或者繁忙的时候,就使用backup标注的备用机器
server 192.168.100.209:8080 weight=1;
server 192.168.100.210:8080 weight=2;
ip_hash;
}
在server里添加如下配置
----------------
 server {
    #可以修改listen为8080
        listen       80;
        #可以为域名
        server_name  192.168.100.211;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        
      #localhost删除掉原有的root然后添加以下东西
        location / {
          root   html;
            #默认访问的首页
            index  index.html index.htm;
            proxy_redirect off;
             # 请求头中Host信息
            proxy_set_header Host $host;
             # 真实的客户端IP
            proxy_set_header X-Real-IP $remote_addr;
            # 代理路由信息,此处取IP有安全隐患
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
           proxy_pass http://mycluster; #反向代理地址 
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html://mycluster;
        }
    }

The overall profile as follows


#允许生成的进程数,一般设置为当前机器总Cpu核心数的1-2倍
worker_processes  2;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
        
#添加配置
upstream mycluster{
#集群有几台服务器就配置几台,weight表示权重,权重越大访问几率越大
#这里添加已经配置启动好的两台tomcat服务器
#down表示当前的server暂时不参与负载
#backup其他设备都宕机或者繁忙的时候,就使用backup标注的备用机器
server 192.168.100.209:8080 weight=1;
server 192.168.100.210:8080 weight=2;
ip_hash;
}

    server {
    #修改listen为8080
        listen       80;
        server_name  192.168.100.211;

       #localhost删除掉原有的root然后添加以下东西
        location / {
          root   html;
            #默认访问的首页
            index  index.html index.htm;
            proxy_redirect off;
             # 请求头中Host信息
            proxy_set_header Host $host;
             # 真实的客户端IP
            proxy_set_header X-Real-IP $remote_addr;
            # 代理路由信息,此处取IP有安全隐患
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
           proxy_pass http://mycluster; #反向代理地址
        }
        
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html://mycluster;
        }
    }
}

Deployment run

1, the project to extract the Tomcat installation from each server under \ webapps \ ROOT directory of

2, runs sequentially in the order: the master server Redis> per> Nginx from each server from the Tomcat server Redis> master server

3, access localhost, run the project successfully

Guess you like

Origin www.cnblogs.com/wxxmy/p/12176575.html