Fork me on GitHub

三.宜立方商城——nginx搭建

一.课程计划:

1
2
3
4
5
6
7
第三天:
1、商品类目选择
2、图片上传
a)图片服务器FastDFS
b)图片上传功能实现
3、富文本编辑器的使用KindEditor
4、商品添加功能完成

主讲nginx

二.商品类目选择:

  2.1 原型页面分析:

image

  2.2 前端代码分析:

image

image

image

image

image

image

image

image

展示商品分类列表,使用EasyUI的tree控件展示。

初始化tree请求的url:/item/cat/list

参数:

初始化tree时只需要把第一级节点展示,子节点异步加载。

long id(父节点id)

返回值:json。数据格式

1
2
3
4
5
6
7
8
9
[{    
"id": 1,
"text": "Node 1",
"state": "closed"
},{
"id": 2,
"text": "Node 2",
"state": "closed"
}]

state:如果节点下有子节点“closed”,如果没有子节点“open”

创建一个pojo来描述tree的节点信息,包含三个属性id、text、state。放到e3-common工程中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class EasyUITreeNode implements Serializable{

//数据库中主键
private long id;
//数据库中的分类名字name
private String text;
//分类是否为父节点
private String state;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public String getText() {
return text;
}

public void setText(String text) {
this.text = text;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

@Override
public String toString() {
return "EasyUITreeNode{" +
"id=" + id +
", text='" + text + '\'' +
", state='" + state + '\'' +
'}';
}
}
查询的表:
tb_item_cat


查询列:
Id、name、isparent


查询条件:parentId

###   2.3 dao层:
tb_item_cat
可以使用逆向工程生成的代码

###   2.4 Service层:

参数:long parentId(你想啊,你需要拿着这个父id去查询父id为这个值的信息)


业务逻辑:

1、根据parentId查询节点列表

2、转换成EasyUITreeNode列表。(根据EasyUI所需要的json格式)

3、返回。

返回值:List

    2.4.1 Service接口:

1
2
3
4
5
public interface ItemCatService {

List<EasyUITreeNode> getItemCatList(long parentId);

}

    2.4.2 Service实现类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* 商品分类管理
*/
@Service
public class ItemCatServiceImpl implements ItemCatService{

@Autowired
private TbItemCatMapper tbItemCatMapper;

@Override
public List<EasyUITreeNode> getItemCatList(long parentId) {
//根据parentID查询子结果
TbItemCatExample tbItemCatExample = new TbItemCatExample();
TbItemCatExample.Criteria criteria = tbItemCatExample.createCriteria();
//设置查询条件
//所有字段都会有方法(别怕)
criteria.andParentIdEqualTo(parentId);
List<TbItemCat> tbItemCatList = tbItemCatMapper.selectByExample(tbItemCatExample);
//把列表转换成EasyUITreeNode列表
List<EasyUITreeNode> list = new ArrayList<EasyUITreeNode>();
for (TbItemCat tbItemCat: tbItemCatList) {
EasyUITreeNode easyUITreeNode = new EasyUITreeNode();
easyUITreeNode.setId(tbItemCat.getId());
easyUITreeNode.setText(tbItemCat.getName());
easyUITreeNode.setState(tbItemCat.getIsParent()==true?"closed":"open");
list.add(easyUITreeNode);
}
//返回结果
return list;
}
}

image

image

    2.4.3 Controller层:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 商品分类管理Controller
*/
@Controller
public class ItemCatController {
@Autowired
private ItemCatService itemCatService;

@RequestMapping("/item/cat/list")
@ResponseBody
public List<EasyUITreeNode>
getItemCatList(@RequestParam(name = "id",defaultValue = "0") Long parentId){
//调用服务查询结点列表
List<EasyUITreeNode> itemCatList = itemCatService.getItemCatList(parentId);
return itemCatList;
}
}

  2.5 发布服务:

image

  2.6 引用服务:

image

  2.7 效果分析:

image

image

image

image

image

三.图片上传分析:

  • 传统方式:

image

  • 集群方式:

image


解决方案:

搭建一个图片服务器,专门保存图片。可以使用分布式文件系统==FastDFS==。

四.什么事nginx:


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

五.应用场景:


1、http服务器。Nginx是一个http服务可以独立提供http服务。可以做网页静态服务器。

2、虚拟主机。可以实现在一台服务器虚拟出多个网站。例如个人网站使用的虚拟主机。

3、反向代理,负载均衡。当网站的访问量达到一定程度后,单台服务器不能满足用户的请求时,需要用多台服务器集群可以使用nginx做反向代理。并且多台服务器可以平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。

六.nginx安装:

下载nginx:

官方网站:

http://nginx.org/

使用的版本是1.8.0版本。

我是安装在了Linux上

image

Nginx提供的源码。

  6.1 要求的安装环境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

1、需要安装gcc的环境。yum install gcc-c++
2、第三方的开发包。
PCRE
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。
yum install -y pcre pcre-devel
注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。
zlib
zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。
yum install -y zlib zlib-devel

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

  6.2 安装步骤:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
第一步:把nginx的源码包上传到linux系统
第二步:解压缩
[root@localhost ~]# tar zxf nginx-1.8.0.tar.gz
第三步:使用configure命令创建一makeFile文件。
./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@localhost sbin]# mkdir /var/temp/nginx/client -p

1
2
第四步:make
第五步:make install

  6.3 启动nginx:

1
2
3
4
5
6
7
8
9
10
11
12
进入sbin目录<br/>
[root@localhost sbin]# ./nginx

关闭nginx:
[root@localhost sbin]# ./nginx -s stop
推荐使用:
[root@localhost sbin]# ./nginx -s quit

重启nginx:
1、先关闭后启动。
2、刷新配置文件:
[root@localhost sbin]# ./nginx -s reload

  6.4 访问nginx:

http://http://192.168.157.128

默认是80端口。
注意:是否关闭防火墙

七.配置虚拟主机


就是在一台服务器启动多个网站。

如何区分不同的网站:

1、域名不同

2、端口不同

  7.1 通过端口区分不同虚拟机:


Nginx的配置文件:

/usr/local/nginx/conf/nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#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;


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

==一个server节点就是一个虚拟主机==

==Html是nginx安装目录下的html目录==

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#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;


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;
}
}
server {
listen 81;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

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

重新加载配置文件
[root@localhost nginx]# sbin/nginx -s reload

重新加载配置文件很重要

  7.2 通过域名区分虚拟主机:

    7.2.1 什么是域名

1
2
3
4
5
域名就是网站。
www.baidu.com
www.taobao.com
www.jd.com
Tcp/ip
1
2
3
4
5
6
7
8
9
10
11
12
Dns服务器:把域名解析为ip地址。保存的就是域名和ip的映射关系。
一级域名:
Baidu.com
Taobao.com
Jd.com
二级域名:
www.baidu.com
Image.baidu.com
Item.baidu.com
三级域名:
1.Image.baidu.com
Aaa.image.baidu.com

一个域名对应一个ip地址,一个ip地址可以被多个域名绑定。

本地测试可以修改hosts文件。

修改window的hosts文件:(C:\Windows\System32\drivers\etc)

可以配置域名和ip的映射关系,如果hosts文件中配置了域名和ip的对应关系,不需要走dns服务器。

image

    7.2.2 Nginx的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#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;


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;
}
}
server {
listen 81;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html-81;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.taobao.com;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html-taobao;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.baidu.com;

#charset koi8-r;

#access_log logs/host.access.log main;

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

八.反向代理

  8.1 什么是反向代理

正向代理:

image

就好比说,我们现在在一个机房当中,但是我们上不了网,但是有一台主机可以上网,于是我们可以用这台主机来做了一个代理服务,就是说,我们比如请求访问百度(www.baidu.com),将这个请求发送到这台代理机上,它帮我们去请求资源,明白?

反向代理:
image

我们可以这样理解,就是我们现在有很多网站,比如淘宝,天猫,京东(记住,是很多不同的网站),那么当网络上的请求来了之后,经过nginx,它会根据请求,具体的去访问某个资源。

反向代理服务器决定哪台服务器提供服务。

反向代理服务器不提供服务器(也就是说,它不干活,就是一个转发)。也是请求的转发。

  8.2 Nginx实现反向代理

两个域名指向同一台nginx服务器,用户访问不同的域名显示不同的网页内容。

两个域名是www.sian.com.cn和www.sohu.com

nginx服务器使用虚拟机92.168.157.128

image

第一步:安装两个tomcat,分别运行在8080和8081端口。

第二步:启动两个tomcat。

第三步:反向代理服务器的配置

image

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

第五步:配置域名

在hosts文件中添加域名和ip的映射关系

image

九.负载均衡

如果一个服务由多条服务器提供,需要把负载分配到不同的服务器处理,需要负载均衡。

1
2
3
4
upstream tomcat2 {
server 192.168.157.128:8081;
server 192.168.157.128:8082;
}

在配置一台tomcat,这就是tomcat集群,再加nginx负载均衡

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

1
2
3
4
upstream tomcat2 {
server 192.168.157.128:8081;
server 192.168.157.128:8082 weight=2;
}