ngnix+tomcat转发、负载均衡

@


前言

使用ngnix转发所有服务的网址到各个服务器,然后在各个服务器同时部署多个服务。

一、流程及图

1.简易流程图如下

在这里插入图片描述

一、nginx服务器配置:

1.下载并安装nginx

1.1 将下载好的nginx包传到服务器/usr/local/目录下


# cd /usr/local

// -y表示遇到判断全部yes,autoconf表示自动配置,automake表示自动编译。
# yum -y install gcc gcc-c++ autoconf automake  //gcc、gcc-c++的库文件
# yum install -y pcre pcre-devel         //安装Nginx依赖包
# yum install -y zlib zlib-devel

# tar -zxvf nginx-1.10.2.tar.gz         //解压缩
# cd nginx-1.10.2                //切换到该目录下
# ./configure                  //配置
# make
# make install                 //安装

// 检验是否完成安装
# cd /usr/local
# ls                      //如果存在nginx文件夹,则安装成功

// 注意:最后结束前不要去改解压后的nginx文件,否则安装不成功

2.nginx配置


#设置低权限用户,为了安全而设置的
#user  nobody;
#工作衍生进程数
worker_processes  10;

#设置错误文件存放路径
# error_log  logs/error.log;
# error_log  logs/error.log  notice;
# error_log  logs/error.log  info;

#设置pid存放路径(pid是控制系统中重要文件)
pid  logs/nginx.pid;

#设置最大连接数
events {
    worker_connections  102400;
}


http {
	#文件扩展名与文件类型映射表
    include       mime.types;
	map $time_iso8601 $logdate {
    '~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd;
    default    'date-not-found';
	}
	
	#默认文件类型
    default_type  application/octet-stream;
	limit_req_zone $binary_remote_addr zone=qps1:1m  rate=800r/s;
	
 	#用来设置日志格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" '
       '"$upstream_addr" "$request_time" "$upstream_response_time"';
	access_log off;
	
	#用来指定日志文件的存放路径
    # access_log  logs/access-$logdate.log;  
	# error_log  logs/error-$logdate.log;
	
	
  	#开启高效文件传输模式
    sendfile            on;
    
    #防止网络阻塞
    tcp_nopush      	on;
	underscores_in_headers on;

    #长连接超时时间,单位是秒
    keepalive_timeout  120;
    
	#(配置请求体缓存区大小) 
	client_max_body_size 50M; 
	
	#(设置客户端请求体最大值)
	#client_body_buffer_size 128k; 
	#fastcgi_intercept_errors on;
	
	#开启gzip压缩,开启后,访问网页会自动压缩
    #gzip  on;
    
    #主要是用于设置一组可以在proxy_pass和fastcgi_pass指令中使用额代理服务器,默认负载均衡方式为轮询
	upstream mysvr {
	  #设置同一个cookie的两次/多次请求,请求的是同一台服务器
	  #ip_hash;
	 
	  #1.down 表示单前的server暂时不参与负载
      #server 192.168.1.116  down;
      
      #3.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
      #server 192.168.2.21  backup; 
      
      #weigth参数表示权值,权值越高被分配到的几率越大,weight 默认为1.weight越大,负载的权重就越大。     
	  #max_fails 配置的是熔断机制失败次数,即当访问一个服务器的时候,出现三次失败,就发生熔断
	  #fail_timeout 熔断时间,当服务器熔断超过10s后,nginx会重新尝试连接熔断的服务器
	  server 192.168.2.21:7798  weight=3 max_fails=6 fail_timeout=60;   # 服务1
	  server 192.168.2.21:7798  weight=2 max_fails=6 fail_timeout=60;   # 服务2
	  server 192.168.2.22:7798  weight=2 max_fails=6 fail_timeout=60;   # 服务3	  
    }
    
    
    #指定服务器的名称和参数
    server {
        listen       80;
        server_name  www.aaa.com;
		
		#proxy_connect_timeout    600;
		#proxy_read_timeout       600;
		#proxy_send_timeout       600;
		
		#设置字符
        #charset koi8-r;
        #access_log  logs/host.access.log  main;

        #location / {
        #    root   html;
        #    index  index.html index.htm;
        #}
		
		location /ams.cloud/report/getError.do {
			return 200 '{code: "-1", message: "系统错误",success : false}';
		}
		
		#location / 指用根目录做负载均衡
		location / {
			#设置代理
			proxy_set_header Host $host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass  http://127.0.0.1:8898;
			#proxy_buffering off;
        }
		
		location /ams.api {
			
			#set $version $http_version;
			# 强制版本升级
		    #if ( $version != "2.0" ) {
			#	return 200 '{code: "-1", message: "版本过低请升级",success : false}';
		    #}
			
			limit_req zone=qps1 burst=6000;
			proxy_set_header Host $host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass  http://mysvr;
        }
		
		
		location /CLodopfuncs.js {
			proxy_pass http://localhost:8000/CLodopfuncs.js;
		}
		
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
 }

二、服务器1、2、3配置(以一台讲解)

1.jdk安装与配置

2.tomcat

1.tomcat安装

2.tomcat配置

1.1 server.xml
<?xml version="1.0" encoding="UTF-8"?>

<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="off" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />


  <GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>


  <Service name="Catalina">
    <Connector 
	  port="8898" protocol="org.apache.coyote.http11.Http11AprProtocol"
      connectionTimeout="5000"
      minSpareThreads="20"
      maxThreads="2048"
      maxSpareThreads="2048"
      acceptCount="2048"
      minProcessors="20"
      maxProcessors="2048"
      enableLookups="false"
      maxHttpHeaderSize ="102400"
	  maxPostSize="83886080"
      redirectPort="8443" />
	  

    <Engine name="Catalina" defaultHost="localhost">

      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

1.2 web.xml
<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1">
  
    <servlet>
        <servlet-name>default</servlet-name>
        <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
        <init-param>
            <param-name>debug</param-name>
            <param-value>0</param-value>
        </init-param>
        <init-param>
            <param-name>listings</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>


    <servlet>
        <servlet-name>jsp</servlet-name>
        <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
        <init-param>
            <param-name>fork</param-name>
            <param-value>false</param-value>
        </init-param>
        <init-param>
            <param-name>xpoweredBy</param-name>
            <param-value>false</param-value>
        </init-param>
        <load-on-startup>3</load-on-startup>
    </servlet>


    <!-- The mapping for the default servlet -->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- The mappings for the JSP servlet -->
    <servlet-mapping>
        <servlet-name>jsp</servlet-name>
        <url-pattern>*.jsp</url-pattern>
        <url-pattern>*.jspx</url-pattern>
    </servlet-mapping>


    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

随心所往,看见未来。Follow your heart,see night!

欢迎点赞、关注、留言,一起学习、交流!

posted @ 2022-07-23 22:28  folyh  阅读(215)  评论(0编辑  收藏  举报