博客
关于我
nginx负载均衡多个tomcat实例
阅读量:127 次
发布时间:2019-02-27

本文共 3313 字,大约阅读时间需要 11 分钟。

 一、架构图

 

二、启动本地项目

      idea 2018暂时不能使用springboot的形式启动多个tomcat实例,就只好用命令实现多个应用实例的启动,首先切换至target目录使用默认端口9000启动:

      java -jar springboot-redis-cache-breakdown-0.0.1-SNAPSHOT.jar

     再启动一个实例:

 java -jar springboot-redis-cache-breakdown-0.0.1-SNAPSHOT.jar --server.port=9001

 

2个实例启动完毕,如下:

 

三、修改nginx配置

       注: 最好使用v8的ip,因为在之前我配的v8ip是固定的,ip为手动配置的,不变。添加的配置如下:

      添加2台代理的tomcat服务器:

      upstream callback{

      server 192.168.254.10:9000;
      server 192.168.254.10:9001;
     }

     location / {

         proxy_pass http://callback;
     }
 

    注:  location里的proxy_pass 地址如果不含有 / ,那么在访问的时候,location后的匹配符省略。

          如果proxy_pass的地址后面包含 /,那么在访问的时候,需要在转发到主机后,添加location 后面的匹配符。

user www www;worker_processes auto; #设置值和CPU核心数一致error_log /usr/local/webserver/nginx/logs/nginx_error.log crit; #日志位置和日志级别pid /usr/local/webserver/nginx/nginx.pid;#Specifies the value for maximum file descriptors that can be opened by this process.worker_rlimit_nofile 65535;events{  use epoll;  worker_connections 65535;}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';  #charset gb2312;       server_names_hash_bucket_size 128;  client_header_buffer_size 32k;  large_client_header_buffers 4 32k;  client_max_body_size 8m;       sendfile on;  tcp_nopush on;  keepalive_timeout 60;  tcp_nodelay on;  fastcgi_connect_timeout 300;  fastcgi_send_timeout 300;  fastcgi_read_timeout 300;  fastcgi_buffer_size 64k;  fastcgi_buffers 4 64k;  fastcgi_busy_buffers_size 128k;  fastcgi_temp_file_write_size 128k;  gzip on;   gzip_min_length 1k;  gzip_buffers 4 16k;  gzip_http_version 1.0;  gzip_comp_level 2;  gzip_types text/plain application/x-javascript text/css application/xml;  gzip_vary on;   #limit_zone crawler $binary_remote_addr 10m;  upstream callback{      server 192.168.254.10:9000;      server 192.168.254.10:9001;  } #下面是server虚拟主机的配置 server  {    listen 80;#监听端口    server_name bingbing.com;#域名    index index.html index.htm index.php;    root /usr/local/webserver/nginx/html;#站点目录    location / {     proxy_pass http://callback;   }      location ~ .*\.(php|php5)?$    {      #fastcgi_pass unix:/tmp/php-cgi.sock;      fastcgi_pass 127.0.0.1:9000;      fastcgi_index index.php;      include fastcgi.conf;    }    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$    {      expires 30d;  # access_log off;    }    location ~ .*\.(js|css)?$    {      expires 15d;   # access_log off;    }    access_log off;  }}

java代码为:

package com.hand.redis.web;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/callback")public class RedisStockController {    @Value("${server.port}")    private  String port;    /**     * 测试nginx是否可以代理     * @return     */    @GetMapping    public String testNginx(){        return "欢迎你,nginx!";    }    @GetMapping("/test01")    public String LoadBalance(){        return "欢迎你,nginx!实例为:"+port;    }}

访问如下地址,可以发现在不同的tomcat实例之间切换:

  总结: 能够访问nginx的前提是先要与windowsping通,安装好nginx后能够进入到nginx首页,最好使用手动配置的v8的ip进行访问,使用WLAN的ip可能会变,负载均衡将无法进行。

  使用nginx代理请求: 

  使用nat模式连通windows并能够连上外网: 

转载地址:http://lggf.baihongyu.com/

你可能感兴趣的文章
MYSQL遇到Deadlock found when trying to get lock,解决方案
查看>>
MYSQL遇到Deadlock found when trying to get lock,解决方案
查看>>
mysql部署错误
查看>>
MySQL配置信息解读(my.cnf)
查看>>
Mysql配置文件my.ini详解
查看>>
MySQL配置文件深度解析:10个关键参数及优化技巧---强烈要求的福利来咯。
查看>>
Mysql配置表名忽略大小写(SpringBoot连接表时提示不存在,实际是存在的)
查看>>
mysql配置读写分离并在若依框架使用读写分离
查看>>
MySQL里为什么会建议不要使用SELECT *?
查看>>
MySQL里的那些日志们
查看>>
MySQL锁
查看>>
MySQL锁与脏读、不可重复读、幻读详解
查看>>
MySQL锁机制
查看>>
mysql锁机制,主从复制
查看>>
Mysql锁机制,行锁表锁
查看>>
MySQL锁表问题排查
查看>>
Mysql锁(1):锁概述和全局锁的介绍
查看>>
Mysql锁(2):表级锁
查看>>
MySQL锁,锁的到底是什么?
查看>>
MySQL错误-this is incompatible with sql_mode=only_full_group_by完美解决方案
查看>>