day77_淘淘商城项目_10_ Linux下的Nginx代理详解(配置虚拟主机+实现反向代理+实现负载均衡+高可用) + 单点登录系统工程搭建 + SSO系统接口文档讲解_匠心笔记

课程计划

  • 使用Linux下的nginx实现网页静态化的测试
  • nginx的安装
  • nginx配置虚拟主机
  • nginx实现反向代理
  • nginx实现负载均衡
  • SSO系统工程搭建

1、目前访问系统使用的方式存在的问题

目前访问后台系统:
  http://localhost:8081/
目前访问首页系统:
  http://localhost:8082/
目前访问搜索结果页面:
  http://localhost:8085/
目前访问商品详情页面:
  http://localhost:8086/
在搜索结果页面search.jsp中,代码中写死了其他的系统的全路路径。如下:


问题:
  1、localhost只能访问本地,不能访问其他的服务器;系统应当要部署在测试环境生产环境。可以使用ip地址
  2、开发环境的ip地址和测试环境的ip地址是不一样的。每次环境变化的时候,都需要修改访问的ip地址
  3、页面加载资源或者请求其他系统的URL时,使用了全路径,一旦环境发生改变,资源无法加载,请求无法访问。
  4、ip地址没有意义,不容易记忆,用户不会通过ip地址进行访问,一般通过域名访问。可以使用nginx进行配置,达到使用域名访问的目的。

2、什么是nginx

  Nginx是一款高性能的http服务器/反向代理服务器电子邮件(IMAP/POP3)代理服务器。由俄罗斯的程序设计师Igor Sysoev用c语言所开发,官方测试nginx能够支撑5万并发链接(tomcat支持500并发链接),并且cpu、内存等资源消耗却非常低,运行非常稳定。开源。

3、nginx的应用场景

1、http服务器。Nginx是一个http服务可以独立提供http服务。可以做静态网页服务器
2、虚拟主机服务器。可以实现在一台服务器虚拟出多个网站。例如:个人网站使用的虚拟主机
3、反向代理,负载均衡服务器。当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群,可以使用nginx做反向代理。并且多台服务器可以平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。

4、nginx的安装

下载nginx,官方网站:http://nginx.org/


我们使用的版本是1.8.0版本。最新版本是1.15.7。
由于市面上linux发行版本过多,版本之间的压缩包格式不同,所以nginx提供nginx的源代码安装。需要我们在Linux上进行编译。

 

4.1、要求的安装环境

0、先有一个干净的Linux系统

[root@itheima ~]# ll
总用量 0
[root@itheima ~]

1、需要安装gcc、g++的环境。

[root@itheima ~]# yum -y install gcc
[root@itheima ~]# yum -y install g++

2、需要安装第三方的开发包。

  • PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。
yum install -y pcre
yum install -y pcre-devel

  注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。

  • zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。
yum install -y zlib
yum install -y zlib-devel
  • OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
    nginx不仅支持http协议,还支持https(即在SSL协议上传输http),所以需要在linux安装openssl库。
yum install -y openssl
yum install -y openssl-devel

4.2、安装步骤

第一步:把nginx的源码包上传到linux系统。
第二步:解压缩后删除源码包。

[root@itheima ~]ll
总用量 816
-rw-r--r--. 1 root root 832104 9月  26 23:20 nginx-1.8.0.tar.gz
[root@itheima ~]tar zxf nginx-1.8.0.tar.gz 
[root@itheima ~]ll
总用量 816
drwxr-xr-x. 8 1001 1001    158 4月  21 2015 nginx-1.8.0
-rw-r--r--. 1 root root 832104 9月  26 23:20 nginx-1.8.0.tar.gz
[root@itheima ~]rm -rf nginx-1.8.0.tar.gz 
[root@itheima ~]ll
总用量 0
drwxr-xr-x. 8 1001 1001 158 4月  21 2015 nginx-1.8.0
[root@itheima ~]

第三步:C语言编译需要一个Makefile文件,我们使用configure命令创建一个Makefile文件。

[root@itheima nginx-1.8.0]# make
make: *** 没有指明目标并且找不到 makefile。 停止。
[root@itheima nginx-1.8.0]# pwd
/root/nginx-1.8.0
[root@itheima nginx-1.8.0]# ./configure \
--prefix=/usr/local/nginx \
--pid-path=/var
/run/nginx/nginx.pid \
--lock-path=/var
/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

注意:启动nginx之前,上边将临时文件目录指定为/var/temp/nginx,需要先在/var下创建temp及nginx目录。命令如下:

[root@itheima nginx-1.8.0]# mkdir /var/temp/nginx -p       #-p表示创建多级目录

第四步:编译

[root@itheima nginx-1.8.0]# make

第五步:安装

[root@itheima nginx-1.8.0]# make install

第六步:查看安装好的nginx

4.3、启动nginx和关闭nginx

进入sbin目录

[root@itheima sbin]# pwd
/usr/local/nginx/sbin
[root@itheima sbin]# ./nginx

查看nginx的进程


关闭nginx
[root@itheima sbin]# ./nginx -s stop    #正常关闭
[root@itheima sbin]# kill 56612         #正常关闭
[root@itheima sbin]# kill -9 56612      #暴力关闭

在正常提供服务的时候,由于nginx是整个网站的入口,如果关闭了ngin,那么整个网站受影响很大,所以我们不推荐关闭nginx。
我们可以使用命令reload在不关机的状态下刷新配置。

[root@itheima sbin]# ./nginx -s reload  #在不关机的状态下刷新配置

4.4、访问nginx

访问地址:http://192.168.25.141
默认是80端口,默认可以不用写。
CentOS 7.X 默认的防火墙不是iptables,而是firewalld。我们可以试一下systemctl stop firewalld关闭防火墙,但是生产环境下不推荐该方式。

systemctl stop firewalld.service        #停止firewall
systemctl disable firewalld.service     #禁止firewall开机启动
firewall-cmd --state                    #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)

CentOS 6.X 是iptables,可以使用vim /etc/sysconfig/iptables修改配置即可。
本博主的是CentOS 7.X 防火墙使用的是firewalld,我们使用命令的方式来添加端口(修改后需要重启firewalld服务):

[root@itheima zones]# pwd
/etc/firewalld/zones
[root@itheima zones]# firewall-cmd --permanent --add-port=80/tcp  
success
[root@itheima zones]# service firewalld restart
Redirecting to /bin/systemctl restart firewalld.service
[root@itheima zones]

浏览器显示效果如下:

5、nginx的3个命令

启动命令

./nginx

关闭命令

./nginx -s stop 
或者
./nginx -s quit

刷新配置文件

./nginx -s reload

修改了nginx.conf文件之后,刷新配置文件,可以不重启nginx,能马上生效。
我们重启虚拟机后,再次重启nginx会报错,解决问题链接:https://www.cnblogs.com/chenmingjun/p/10052205.html

6、nginx配置虚拟主机

就是在一台服务器上虚拟出多个网站。
如何区分不同的网站:
  1、端口不同
  2、域名不同

6.1、通过端口区分虚拟主机

nginx的配置文件配置项如下:
文件位置:/usr/local/nginx/conf/nginx.conf
文件具体内容如下:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;
pid         /usr/local/nginx/logs/nginx.pid;

events {
    worker_connections  1024;
}


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"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #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;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

我们需要关心的地方


可以配置多个server,即配置了多个虚拟的主机。

6.1.1、使用xftp配置远程编辑器

直接使用vim命令不太方便,一般需要修改远程工程中的文件,除了使用vim命令外,还可以配置文本编辑器,直接作用于远程文件。
第一步:使用Xshell5连接到虚拟机,先新建远程文件输出。
第二步:选择【工具】 --> 【选项】 --> 【高级】 --> 【设置(S)…】 --> 取消勾选“将记事本用作文本编辑器”


第三步:重启Xshell 5,打开Xftp 5选择要打开的文件右键 --> 以Notepad++编辑

6.1.2、使用EditPlus编辑器配置连接FTP服务器(推荐方式)

使用EditPlus安装FTP插件后,注意:高版本的EditPlus已经自带FTP插件了。
然后打开EditPlus,点击【文件】 --> 【FTP】--> 【设置FTP服务器…】


点击【添加】,输入要链接的FTP服务器的信息,如下图:

再点击【高级设置(G)…】

然后点击【确定】,回到主面板,点击磁盘下拉框,出现

弹出是否信任该站点,点击【是】

这样我们就连接上Linux了,如下图:

然后可以打开我们需要编辑的文件,编辑后保存,就会自动同步至服务器Linux上了。

6.1.3、添加虚拟主机

第一步:修改配置文件nginx.conf,添加如下内容:

################################################# nginx配置虚拟主机,通过端口区分虚拟主机
    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
################################################# nginx配置虚拟主机,通过端口区分虚拟主机
    server {
        listen       81;
        server_name  localhost;

        location / {
            root   html81;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html81;
        }
    }

第二步:需要在服务器Linux上nginx的目录下新建测试目录/html81,为了便于区分,稍微修改该目录下的首页内容

[root@itheima nginx]# ll
总用量 4
drwxr-xr-x. 2 root root 4096 12月  1 21:00 conf
drwxr-xr-x. 2 root root   40 12月  1 16:27 html
drwxr-xr-x. 2 root root   19 12月  1 16:27 sbin
[root@itheima nginx]# cp -r html/ html81
[root@itheima nginx]# ll
总用量 4
drwxr-xr-x. 2 root root 4096 12月  1 21:00 conf
drwxr-xr-x. 2 root root   40 12月  1 16:27 html
drwxr-xr-x. 2 root root   40 12月  1 21:05 html81
drwxr-xr-x. 2 root root   19 12月  1 16:27 sbin
[root@itheima nginx]

第三步:每次配置文件修改之后都需要重新加载配置文件。

[root@itheima nginx]# sbin/nginx -s reload

第四步:修改防火墙配置
CentOS 7.X 默认的防火墙不是iptables,而是firewalld。我们可以试一下systemctl stop firewalld关闭防火墙,但是不推荐该方式。
CentOS 6.X 是iptables,可以使用vim /etc/sysconfig/iptables修改配置即可。
本博主的是CentOS7,防火墙使用的是firewalld,我们使用命令的方式来添加端口(修改后需要重启firewalld服务):

[root@itheima zones]# pwd
/etc/firewalld/zones
[root@itheima zones]# firewall-cmd --permanent --add-port=81/tcp  
success
[root@itheima zones]# service firewalld restart
Redirecting to /bin/systemctl restart firewalld.service
[root@itheima zones]

第四步:在浏览器上进行测试
访问地址:http://192.168.25.141:81/
浏览器显示效果如下:

6.2、通过域名区分虚拟主机

6.2.1、什么是域名

域名就是网站。
  www.baidu.com
  www.taobao.com
  www.jd.com
顶级域名:
  com
  cn
  org
一级域名:
  baidu.com
  taobao.com
  jd.com
二级域名:
  www.baidu.com
  image.baidu.com
  tieba.baidu.com
三级域名:
  1.image.baidu.com
  aaa.image.baidu.com
域名访问网站,其本质还是通过tcp/ip协议访问。


DNS服务器:把域名解析为ip地址。保存的就是域名和ip的映射关系,可以简单的理解为一个MAP<KEY,VALUE>集合。
一个域名对应一个ip地址,一个ip地址可以被多个域名绑定。

本地测试可以修改hosts文件。
修改window的hosts文件,文件位置:C:\Windows\System32\drivers\etc
可以配置域名和ip的映射关系,如果hosts文件中配置了域名和ip的对应关系,就不走DNS服务器了。
手动修改hosts文件比较麻烦,一般企业中使用hosts切换的工具SwitchHosts,用来方便切换hosts文件中的配置。


使用时必须以管理员权限运行。打开软件,如下图所示:

6.2.2、nginx的配置

################################################# nginx配置虚拟主机,通过端口区分虚拟主机
    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
################################################# nginx配置虚拟主机,通过端口区分虚拟主机
    server {
        listen       81;
        server_name  localhost;

        location / {
            root   html81;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html81;
        }
    }

################################################# nginx配置虚拟主机,通过域名区分虚拟主机
    server {
        listen       80;
        server_name  www.sohu.com;

        location / {
            root   html-sohu;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html-sohu;
        }
    }
################################################# nginx配置虚拟主机,通过域名区分虚拟主机
    server {
        listen       80;
        server_name  www.163.com;

        location / {
            root   html-163;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html-163;
        }
    }

本地主机域名的配置:

    192.168.25.141 www.sohu.com
    192.168.25.141 www.163.com

7、nginx实现反向代理

7.1、什么是反向代理


参考链接:https://www.cnblogs.com/chenmingjun/p/9187251.html
反向代理服务器决定哪台服务器提供服务。
反向代理服务器不提供服务。只是请求的转发。

7.2、nginx实现反向代理

两个域名指向同一台nginx服务器,用户访问不同的域名显示不同的网页内容。
两个域名是www.163.com.cn和www.sohu.com
nginx服务器使用虚拟机,ip地址:192.168.25.141


第一步:安装两个tomcat,分别运行在8080和8081端口。
本博主的是CentOS7,防火墙使用的是firewalld,我们使用命令的方式来添加所用到的端口(修改后需要重启firewalld服务):
[root@itheima zones]# pwd
/etc/firewalld/zones
[root@itheima zones]# firewall-cmd --permanent --add-port=8080/tcp
success
[root@itheima zones]# firewall-cmd --permanent --add-port=8081/tcp
success
[root@itheima zones]# service firewalld restart
Redirecting to /bin/systemctl restart firewalld.service
[root@itheima zones]

为了区分方便,我们稍微修改下tomcat的欢迎页。
第二步:分别启动两个tomcat。
访问测试:
  http://192.168.25.141:8080/
  http://192.168.25.141:8081/
第三步:反向代理服务器的配置,在nginx.conf中配置

################################################# nginx实现反向代理(先通过域名区分虚拟主机)
    upstream tomcat1 {
        server   192.168.25.141:8080;
    }
    upstream tomcat2 {
        server   192.168.25.141:8081;
    }
#################################################
    server {
        listen       80;
        server_name  www.test1.com;

        location / {
            proxy_pass   http://tomcat1;
            index  index.html index.htm;
        }
    }
#################################################
    server {
        listen       80;
        server_name  www.test2.com;

        location / {
            proxy_pass   http://tomcat2;
            index  index.html index.htm;
        }
    }

第四步:nginx重新加载配置文件

[root@itheima nginx]# sbin/nginx -s reload

第五步:在本地配置域名,在hosts文件中添加域名和ip的映射关系

192.168.25.141 www.test1.com
192.168.25.141 www.test2.com

浏览器显示效果:

8、使用域名访问淘淘商城后台系统

1、先配置本地域名和ip地址的映射:

127.0.0.1 www.taotao.com

2、使用本地Windows版本的nginx配置nginx.conf,添加server节点如下:

    upstream tomcat1 {
        server 127.0.0.1:8082;
    }     

    server {
        listen       80;
        server_name  www.taotao.com;

    location / {
        proxy_pass   http://tomcat1;
        index  index.html index.htm;
    }

3、Windows下启动nginx
双击nginx.exe
4、查看测试效果

9、nginx实现负载均衡

如果一个服务由多台服务器(服务器集群)提供,需要把负载分配到不同的服务器处理,需要负载均衡。
我们新添加一个tomcat,端口为8082,提供的服务跟tomcat(8081)提供的服务一样。
在nginx.conf中配置如下:

    upstream tomcat2 {
        server 192.168.25.141:8081;
        server 192.168.25.141:8082;
    }

即默认的策略就是轮询的方式。
可以根据服务器的实际情况调整服务器权重。权重越高分配的请求越多,权重越低,请求越少。默认是都是1。

    upstream tomcat2 {
        server   192.168.25.141:8081;
        server   192.168.25.141:8082 weight=5;
    }

详细代码如下:

################################################# nginx实现反向代理(先通过域名区分虚拟主机)
    upstream tomcat1 {
        server   192.168.25.141:8080;
    }
    upstream tomcat2 { #如果一个服务由多台服务器(服务器集群)提供,需要把负载分配到不同的服务器处理,需要nginx实现负载均衡。
        server   192.168.25.141:8081;
        server   192.168.25.141:8082 weight=10;
    }
#################################################
    server {
        listen       80;
        server_name  www.test1.com;

        location / {
            proxy_pass   http://tomcat1;
            index  index.html index.htm;
        }
    }
#################################################
    server {
        listen       80;
        server_name  www.test2.com;

        location / {
            proxy_pass   http://tomcat2;
            index  index.html index.htm;
        }
    }

扩展小知识:
  负载均衡分为:软负载、硬负载。
  nginx是第七层应用层负载,最高可以支持5万并发,不支持搭建集群,因为nginx负载是网站入口,软负载。
  如果非得让nginx搭建集群,那么网站入口需要使用硬负载F5,F5是第四层传输层负载,硬负载。
  如果非得让nginx搭建集群,还可以使用LVS(Linux虚拟服务器,章文嵩),也是第四层传输层负载,但却是软负载。可以达到F5硬负载的60%的性能。
  还可以通过CDN,全称是Content Delivery Network,即内容分发网络。即在网络节点的各个地方加缓存。

10、nginx的高可用(了解)

要实现nginx的高可用,需要实现备份机

10.1、什么是负载均衡高可用

  nginx作为负载均衡器,所有请求都到了nginx,可见nginx处于非常重点的位置,如果nginx服务器宕机,则后端web服务将无法提供服务,影响严重。
  为了屏蔽负载均衡服务器的宕机,需要建立一个备份机。主服务器和备份机上都运行高可用(High Availability)监控程序,通过传送诸如“I am alive”这样的信息来监控对方的运行状况。当备份机不能在一定的时间内收到这样的信息时,它就接管主服务器的服务IP并继续提供负载均衡服务;当备份管理器又从主管理器收到“I am alive”这样的信息时,它就释放服务IP地址,这样的主服务器就开始再次提供负载均衡服务。

10.2、keepalived + nginx 实现主备

10.2.1、什么是keepalived

  keepalived是集群管理中保证集群高可用的一个服务软件,用来防止单点故障
  keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器

10.2.2、keepalived的工作原理

  keepalived是以VRRP协议为实现基础的,VRRP全称 Virtual Router Redundancy Protocol,即虚拟路由冗余协议
  虚拟路由冗余协议,可以认为是实现路由器高可用的协议,即将N台提供相同功能的路由器组成一个路由器组,这个组里面有一个master和多个backup,master上面有一个对外提供服务的vip(VIP = Virtual IP Address,虚拟IP地址,该路由器所在局域网内其他机器的默认路由为该vip),master会发组播,当backup收不到VRRP包时就认为master宕掉了,这时就需要根据VRRP的优先级来选举一个backup当master。这样的话就可以保证路由器的高可用了。
  keepalived主要有三个模块,分别是core、check和VRRP。core模块为keepalived的核心,负责主进程的启动、维护以及全局配置文件的加载和解析。check负责健康检查,包括常见的各种检查方式。VRRP模块是来实现VRRP协议的。
  详细参考:Keepalived权威指南中文.pdf

10.2.3、keepalived + nginx 实现主备过程

初始状态


主机宕机

主机恢复

10.2.4、高可用环境

两台nginx,一主一备:192.168.101.3和192.168.101.4
两台tomcat服务器:192.168.101.5、192.168.101.6

10.2.5、安装keepalived

分别在主备nginx上安装keepalived,参考“安装手册”进行安装:

11、SSO系统的分析

11.1、什么是SSO系统

  SSO英文全称Single Sign On,单点登录。SSO是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统。它包括可以将这次主要的登录映射到其他应用中用于同一个用户的登录的机制。它是目前比较流行的企业业务整合的解决方案之一。

11.2、为什么要有单点登录系统

11.2.1、传统的登录实现方式


此方式在只有一个web工程时是没有问题。

11.2.2、集群环境下的登录实现方式


集群环境下会出现要求用户多次登录的情况。
解决方案:
  1、配置tomcat集群。配置tomcatSession复制。节点数不要超过5个。
  2、可以使用Session服务器,保存Session信息,使每个节点是无状态。需要模拟Session
  单点登录系统是使用Redis模拟Session,实现Session的统一管理,解决Session的共享问题.

12、SSO系统的实现


需要创建一个sso服务工程,可以参考taotao-manager创建。

12.1、服务层工程搭建

taotao-sso(pom聚合工程)
   |--taotao-sso-interface(jar)
   |--taotao-sso-service(war)
可以参考taotao-manager创建。

12.1.1、taotao-sso


pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
    http://maven.apache.org/xsd/maven-4.0.0.xsd"
>

    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.taotao</groupId>
        <artifactId>taotao-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>taotao-sso</artifactId>
    <packaging>pom</packaging>
    <modules>
        <module>taotao-sso-interface</module>
        <module>taotao-sso-service</module>
    </modules>
    <dependencies>
        <!-- 配置对taotao-common的依赖-->
        <dependency>
            <groupId>com.taotao</groupId>
            <artifactId>taotao-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- 配置Tomcat插件  -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <configuration>
                    <port>8087</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

12.1.2、taotao-sso-interface


jar

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
    http://maven.apache.org/xsd/maven-4.0.0.xsd"
>

    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.taotao</groupId>
        <artifactId>taotao-sso</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>taotao-sso-interface</artifactId>
    <dependencies>
        <!-- 配置对taotao-manager-pojo的依赖 -->
        <dependency>
            <groupId>com.taotao</groupId>
            <artifactId>taotao-manager-pojo</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

12.1.3、taotao-sso-service


pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
    http://maven.apache.org/xsd/maven-4.0.0.xsd"
>

    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.taotao</groupId>
        <artifactId>taotao-sso</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>taotao-sso-service</artifactId>
    <packaging>war</packaging>
    <dependencies>
        <!-- 配置对taotao-manager-dao的依赖 -->
        <dependency>
            <groupId>com.taotao</groupId>
            <artifactId>taotao-manager-dao</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!-- 配置对taotao-sso-interface的依赖:服务层发布服务要通过该接口 -->
        <dependency>
            <groupId>com.taotao</groupId>
            <artifactId>taotao-sso-interface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!-- 配置对spring的依赖 -->
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <!-- 配置对dubbo的依赖 -->
        <!-- dubbo相关 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <!-- 排除对低版本jar包的依赖 -->
            <exclusions>
                <exclusion>
                    <artifactId>spring</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>netty</artifactId>
                    <groupId>org.jboss.netty</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>
        <!-- 配置对Redis的Java客户端jedis的依赖 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
    </dependencies>
</project>

12.1.4、框架整合

12.2、表现层工程搭建

12.2.1、taotao-sso-web

表现层工程包含登录和注册页面,需要调用sso服务实现。
taotao-sso-web(war包)
可以参考taotao-manager-web创建。


pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
    http://maven.apache.org/xsd/maven-4.0.0.xsd"
>

    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.taotao</groupId>
        <artifactId>taotao-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>taotao-sso-web</artifactId>
    <packaging>war</packaging>
    <dependencies>
        <!-- 配置对taotao-common的依赖 -->
        <dependency>
            <groupId>com.taotao</groupId>
            <artifactId>taotao-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!-- 配置对taotao-sso-interface的依赖:表现层调用服务要通过该接口 -->
        <dependency>
            <groupId>com.taotao</groupId>
            <artifactId>taotao-sso-interface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <!-- JSP相关 -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jsp-api</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- 配置对dubbo的依赖 -->
        <!-- dubbo相关 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <!-- 排除对低版本jar包的依赖 -->
            <exclusions>
                <exclusion>
                    <artifactId>spring</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>netty</artifactId>
                    <groupId>org.jboss.netty</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
        </dependency>
        <!-- 单元测试 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- 配置Tomcat插件  -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <configuration>
                    <port>8088</port>
                    <path>/</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

12.2.2、框架整合


至此,SSO系统基本工程搭建完成!

12.3、SSO系统接口分析

给手机APP提供服务,是RestFul形式的服务。其实就是Http协议传json数据。
公司里的APP部门会给我们后台部门一个接口文档,如下图所示:


要仔细阅读接口说明文档。
posted @ 2018-12-02 17:28  黑泽君  阅读(1230)  评论(2编辑  收藏  举报