During the operation of the project, we all encounter a problem. When the project needs to be updated, we may need to temporarily shut down the server to update. But some situations may arise:
1. The user is still operating and is forcibly terminated (we can watch the log and wait for updates when no one is operating, but there may always be cases)
2. Users who do not know may think whether the website has been attacked, which reduces the degree of trust in the website, and thus leads to the loss of some potential customers, which is especially detrimental to financial Internet companies.
After checking some information, I decided to use Tomcat + Nginx + Redis to achieve load balancing and session sharing. The following records my practice process. If there are any mistakes or deficiencies, you are welcome to give pointers. If you don’t like it, don’t spray it.
1. A brief introduction and opening of Nginx
Nginx is a lightweight and high-performance Http and reverse proxy server . The so-called reverse proxy means that when a user initiates an access request, the proxy server receives it, then forwards the request to the official server, and returns the data processed by the official server to the client. At this time, the proxy server behaves as a server. This seems to be an extra step and a little troublesome, but in fact there are many benefits, which I will show in the demo below.
First, let's go to the Nginx official website to download a Nginx. I am on my own computer, so of course I downloaded the windows version. After the download is complete, you can directly put it in a certain disk, no installation is required. Next, we open cmd, enter the nginx directory, and enter start nginx.
We can see a window flash by so that nginx has been started and we can find its process in the task manager.
Now we enter localhost in the browser. You can see that a page appears, although it is a bit simple, but it is indeed the welcome page of nginx, similar to the welcome page of locahost:8080 just after tomcat has been started.
2. Use Nginx to implement reverse proxy
Now we build a maven project based on the SpringMVC + Spring + Mybaties framework, and the construction process will not be repeated. The function is very simple, that is, you can jump to a page, of course, you can also use other frameworks.
运行demo,我这tomcat端口是8080,在浏览器输入localhost:8080,出现我们的页面。
这时我们还是直接访问tomcat服务器的,现在我想通过nginx访问tomcat,即输入localhost就能显示我们demo的页面。
这就要我们去修改nginx的核心配置文件,在其目录下的conf文件夹下的nginx.conf文件,那么首先我们就要了解该文件中一些节点的作用。
worker_processes:工作进程个数,可配置多个
worker_connections:单个进程最大连接数
server:每一个server相当于一个代理服务器
lister:监听端口,默认80
server_name:当前服务的域名,可以有多个,用空格分隔(我们是本地所以是localhost)
location:表示匹配的路径,这时配置了/表示所有请求都被匹配到这里
index:当没有指定主页时,默认会选择这个指定的文件,可多个,空格分隔
proxy_pass:请求转向自定义的服务器列表
upstream name{ }:服务器集群名称
知道了节点作用后,我们就知道我们需要修改的文件中的server部分,这是它原有的代码,我删除了它注释部分。现在我们就能明白为什么输入localhost,
它访问的是它欢迎页面即index.html。
下面我们对这段代码进行一些小小修改。就是将请求转向我们定义的服务器。
随后在cmd中输入命令nginx -s reload即可重启nginx。
重启后,我们再输入localhost,可以看到跳转到的页面是我们demo的。
至此,反向代理已完成,这样所有请求都需经过代理服务器才能访问到正式服务器,某种程度上可以保护网站安全。
3.使用Nginx实现负载均衡
负载均衡即是代理服务器将接收的请求均衡的分发到各服务器中。
负载均衡的优势在访问量少或并发小的时候可能并不明显,且不说淘宝双11、铁道部抢票这种级别的访问量、高并发,就是一般网站的抢购活动时,也会给服务器造成很大压力,可能会造成服务器崩溃。而负载均衡可以很明显的减少甚至消除这种情况的出现,下面我们说说实现方法。
首先我们再开启一个tomcat服务器,这里区分一下就叫tomcat2吧,原先的叫tomcat1。将tomcat1上的项目,拷贝到tomcat2上,稍微修改下页面上的文字以便等下区分我们的请求被分发到了哪个tomcat上。tomcat2端口我这里为8081。在浏览器中输入localhost:8081。
服务器准备好了,我们要在server外部定义个服务器集群,即用到了上文中提到的upstream 标签。服务器集群名字取为test。
同时我们需要再修改下server,将定向的路径转到问你服务器集群上。
重启下nginx,在浏览器输入localhost,再多刷新几次,可以看到两个页面在来回切换。
这样即实现了负债均衡。假设我们服务器在运行过程中,其中一个tomcat挂了,仍然还有另一个可以访问。更新的时候也能先关闭只其中一个,轮流更新。另外还能有效缓解服务器压力,是不是很棒呢?
当然,以上nginx的配置是简单化的,实际上我们还可以配置nginx对静态资源的缓存等等,在此就不多加演示了。
4.小结
花了好些时间,总算陆陆续续要写好了,在此小结一下。
nginx作为一个反向代理服务器,能缓存我们项目的静态文件,并实现反向代理与均衡负载,可以有效减少服务器压力,即使项目不大,也可以使用。
大家另外应该都还发现了个问题,虽然这样请求能分别请求到两个tomcat上,如果是一般不需身份校检的或什么认证的方法尚可,但如果出现这类情况:
我们在tomcat1上进行了登录,这时用户session当然是存在tomcat1上的,而这时进入个人中心的请求请求到tomcat2上了,这时就会出现问题了。tomcat2会告诉你还未登录,这显然不是我们想看到的。
这就涉及到session共享了,如何让两个服务器上的session共用。我这里放到下次再说,作为码农比较忙,可能要过个好几天。另外我将这次的demo源码上传了,下次还要用,nginx配置就不传了,大家自己多动手试验。