1.实现图片回显
1.1准备虚拟路径
1.1.1 编辑image.properties
说明:在jt-manager中创建image.properties文件,在其中编辑关于图片配置的所有信息.
1.1.2 编辑FileServiceImpl
说明:实现虚拟路径配置,并且设定属性动态赋值操作.
@Service @PropertySource("classpath:/properties/image.properties") public class FileServiceImpl implements FileService{
<span class="token comment">//定义图片的类型集合.</span> <span class="token keyword">private</span> <span class="token keyword">static</span> Set<span class="token generics function"><span class="token punctuation"><</span>String<span class="token punctuation">></span></span> imageTypeSet <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashSet</span><span class="token operator"><</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//为属性动态赋值.</span> <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"${image.localDirPath}"</span><span class="token punctuation">)</span> <span class="token keyword">private</span> String localDirPath<span class="token punctuation">;</span> <span class="token comment">// = "D:/JT-SOFT/images"; //定义本地磁盘目录</span> <span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"${image.urlPath}"</span><span class="token punctuation">)</span> <span class="token keyword">private</span> String urlPath<span class="token punctuation">;</span> <span class="token comment">//= "http://image.jt.com"; //定义了虚拟路径的域名</span> <span class="token keyword">static</span> <span class="token punctuation">{</span> imageTypeSet<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">".jpg"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//字母小写类型</span> imageTypeSet<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">".png"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> imageTypeSet<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token string">".gif"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//其他的省略.....</span> <span class="token punctuation">}</span> <span class="token comment">/** * 1.校验文件有效性 .jpg|.png|.gif....... * 2.校验文件是否为恶意程序 (木马.exe).jpg * 3.提高用户检索图片的效率 分目录存储. * 4.为了防止重名图片的提交 自定义文件名称. * 5.实现图片的物理上传 本地磁盘中. * 6.准备一个访问图片的虚拟路径 * * @param uploadFile * @return */</span> <span class="token annotation punctuation">@Override</span> <span class="token keyword">public</span> ImageVO <span class="token function">upload</span><span class="token punctuation">(</span>MultipartFile uploadFile<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token comment">//1.校验图片类型 1.利用正则表达式进行校验 2.利用集合进行校验 Set 数据是否存在即可.</span> <span class="token comment">//1.1 获取图片名称 abc.jpg ABC.JPG</span> String fileName <span class="token operator">=</span> uploadFile<span class="token punctuation">.</span><span class="token function">getOriginalFilename</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> fileName <span class="token operator">=</span> fileName<span class="token punctuation">.</span><span class="token function">toLowerCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//将所有的字母都小写</span> <span class="token comment">//1.2 获取图片的类型</span> <span class="token keyword">int</span> index <span class="token operator">=</span> fileName<span class="token punctuation">.</span><span class="token function">lastIndexOf</span><span class="token punctuation">(</span><span class="token string">"."</span><span class="token punctuation">)</span><span class="token punctuation">;</span> String fileType <span class="token operator">=</span> fileName<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span>index<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//.jpg</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>imageTypeSet<span class="token punctuation">.</span><span class="token function">contains</span><span class="token punctuation">(</span>fileType<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment">//如果类型不匹配</span> <span class="token keyword">return</span> ImageVO<span class="token punctuation">.</span><span class="token function">fail</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//图片上传失败.</span> <span class="token punctuation">}</span> <span class="token comment">//2.如何判断文件是否为恶意程序? 文件是否有图片的特有属性!!!!</span> <span class="token comment">//2.1将上传的文件类型利用图片的API进行转化 如果转化不成功则一定不是图片.</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> BufferedImage bufferedImage <span class="token operator">=</span> ImageIO<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>uploadFile<span class="token punctuation">.</span><span class="token function">getInputStream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//2.2校验是否有图片的特有属性 高度/宽度</span> <span class="token keyword">int</span> width <span class="token operator">=</span> bufferedImage<span class="token punctuation">.</span><span class="token function">getWidth</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">int</span> height <span class="token operator">=</span> bufferedImage<span class="token punctuation">.</span><span class="token function">getHeight</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//2.3校验宽度和高度是否有值.</span> <span class="token keyword">if</span><span class="token punctuation">(</span>width <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">||</span> height <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> ImageVO<span class="token punctuation">.</span><span class="token function">fail</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> ImageVO<span class="token punctuation">.</span><span class="token function">fail</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//返回失败即可</span> <span class="token punctuation">}</span> <span class="token comment">//3.实现分目录存储</span> <span class="token comment">// 方案1: 利用hash之后每隔2-3位截取之后拼接</span> <span class="token comment">// 方案2: 以时间为单位进行分隔 /yyyy/MM/dd/</span> <span class="token comment">//3.1 利用工具API将时间转化为指定的格式</span> String datePath <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">SimpleDateFormat</span><span class="token punctuation">(</span><span class="token string">"/yyyy/MM/dd/"</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//3.2 动态生成文件目录 2部分=根目录+时间目录</span> String localDir <span class="token operator">=</span> localDirPath <span class="token operator">+</span> datePath<span class="token punctuation">;</span> <span class="token comment">//3.3判断目录是否存在, 如果不存在则新建目录</span> File dirFile <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>localDir<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>dirFile<span class="token punctuation">.</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span> dirFile<span class="token punctuation">.</span><span class="token function">mkdirs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//如果不存在,则新建目录</span> <span class="token punctuation">}</span> <span class="token comment">//4. 防止文件重名,需要自定义文件名称 UUID</span> <span class="token comment">//4.1生成uuid</span> String uuid <span class="token operator">=</span> UUID<span class="token punctuation">.</span><span class="token function">randomUUID</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string">"-"</span><span class="token punctuation">,</span> <span class="token string">""</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//4.2动态生成文件名称 uuid +.jpg</span> String uuidFileName <span class="token operator">=</span> uuid <span class="token operator">+</span> fileType<span class="token punctuation">;</span> <span class="token comment">//5.实现文件上传 准备文件全路径 目录+文件名称</span> String realFilePath <span class="token operator">=</span> localDir <span class="token operator">+</span> uuidFileName<span class="token punctuation">;</span> <span class="token comment">//5.1封装文件真实对象</span> File imageFile <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>realFilePath<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//5.2实现文件上传</span> <span class="token keyword">try</span> <span class="token punctuation">{</span> uploadFile<span class="token punctuation">.</span><span class="token function">transferTo</span><span class="token punctuation">(</span>imageFile<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span> e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> ImageVO<span class="token punctuation">.</span><span class="token function">fail</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//告知文件上传失败</span> <span class="token punctuation">}</span> <span class="token comment">//6.实现路径拼接</span> <span class="token comment">//图片存储的根目录 D:\JT-SOFT\images\2020\08\07\0e2ee0014382423b93c53d9b2decc5ec.jpg</span> <span class="token comment">//拼接指定的虚拟路径.</span> String url <span class="token operator">=</span> urlPath <span class="token operator">+</span> datePath <span class="token operator">+</span> uuidFileName<span class="token punctuation">;</span> <span class="token keyword">return</span> ImageVO<span class="token punctuation">.</span><span class="token function">success</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
- 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
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
1.2 方向代理/正向代理说明
1.2.1 关于代理映射的说明
1.2.2 反向代理(必会)
反向代理服务器位于用户与目标服务器之间,但是对于用户而言,反向代理服务器就相当于目标服务器,即用户直接访问反向代理服务器就可以获得目标服务器的资源。同时,用户不需要知道目标服务器的地址,也无须在用户端作任何设定。反向代理服务器通常可用来作为Web加速,即使用反向代理作为Web服务器的前置机来降低网络和服务器的负载,提高访问效率。
特点:
1. 反向代理服务器位于用户和目标服务器之间.
2. 反向代理服务器就相当于目标服务器.(用户以为代理服务器就是真实服务器)
3. 用户通过反向代理服务器获取资源 而不是直接访问真实服务器.
4. 用户不清楚真实的服务器到底是谁.
5. 反向代理是服务器端代理.保护真实服务器信息.
特征: 用户不清楚真实目标服务器是谁.
情景题:
人物介绍:
A. 你 年薪800万 废物利用回收公司CEO 今年60 寻以为18-25岁 适龄女青年 共度余生
B. 女生 : 特漂亮 身材好 标准: 事业有成 年龄无所谓(为人低调 害羞)
C. 女闺蜜: 告诉她 帮我瞧瞧这老头 到底还能多久. 看看我委屈几年…
约会地点: 三里屯 夜色酒吧 8号卡座 A-----女闺蜜------女生
1.2.3 正向代理(必会)
正向代理,意思是一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转交请求并将获得的内容返回给客户端。客户端才能使用正向代理。
特点:
1.代理服务器位于用户和服务器之间
2.用户发起请求之前已经清楚的知道谁的目标服务器.
3.用户通过代理服务器到指定的目标服务器获取资源.
4.正向代理保护的是客户端信息**,是客户端代理**
1.2.4 关于正向/反向代理总结
- 正向代理是客户端代理,用户清楚的知道访问的服务器是谁. 保护了客户端信息
- 反向代理是服务器端代理.用户不清楚访问的真实服务到底是谁. 保护了服务端信息\
1.3 nginx
1.3.1 nginx介绍
Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。
其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
特点: 占有内存少 不超过2M ,并发能力强 3-5万次/秒
1.3.2 Nginx下载
1.3.3 Nginx的安装
注意事项: Nginx安装目录 不要装到C盘,并且路径不要有中文 空格 及系统目录.
1.3.4 Nginx 启动
说明:双击nginx.exe 之后检查系统的启动项,
访问网址: localhost:80
1.3.5 关于nginx 进程项说明
内存占用大的是主进程.
手动关闭Nginx 方式:先关闭守护,之后关闭主
1.3.6 关于nginx 操作命令
命令执行位置: 执行命令时需要在nginx的根目录中执行.
之后地址栏中 键入 cmd进入dos命令创建中.
- 启动命令 start nginx
- 重启命令 nginx -s reload
- 关闭命令 nginx -s stop
1.3.7 关于Nginx反向代理配置
http {
#http协议内,可以有多个服务 每个服务就是一个server
server {
# 监听用户访问的端口 默认是80端口
listen 80;
# server_name 服务名称 浏览器的访问域名
server_name localhost;
#配置反向代理的实现 / 拦截所有的用户请求
location / {
# root代表关键字 代表反向代理的目录
root html;
# index 代表默认的访问页面
index index.html index.htm;
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
1.4 配置nginx图片服务器
1.4.1 反向代理说明
1.url地址: http://image.jt.com/2020/08/07/cc10f60491234317adf800aeafc6af1f.png
2.本地磁盘地址: D:\JT-SOFT\images/2020/08/07/cc10f60491234317adf800aeafc6af1f.png
目的:通过域名访问真实的磁盘地址.
可以利用nginx的反向代理完成配置.
1.4.2 编辑Nginx配置文件
现象: nginx -s reload指令 可以展现报错信息.
start nginx 无论启动是否成功 ,都不能展现报错.
习惯: 启动nginx之后,最好执行重启指令.
# 配置图片服务器 server { listen 80; server_name image.jt.com;
##通过网址转向指定的目录 注意<span class="token operator">/</span>的写法 location <span class="token operator">/</span> <span class="token punctuation">{</span> root D<span class="token operator">:</span><span class="token operator">/</span>JT<span class="token operator">-</span>SOFT<span class="token operator">/</span>images<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
1.4.3 网络资源访问请求路径
说明:通过hosts文件可以配置域名与IP地址的映射.那么在本机中可以实现反向代理,如果将来想要在公网中进行反向代理.则必须购买域名.
1.4.4 修改HOSTS文件
说明: 每个操作系统针对开发人员,在本地都会有一个hosts文件可以编辑.
windows: C:\Windows\System32\drivers\etc
利用工具软件管理HOSTS文件:
京淘配置
#左侧写IP地址 右侧写域名 中间使用空格分隔 配置内容
127.0.0.1 image.jt.com
127.0.0.1 manager.jt.com
127.0.0.1 www.jt.com
127.0.0.1 sso.jt.com
1.4.5 修改hosts文件权限
方式1:
方式2: 以超级管理员的方式运行Switch hosts软件
方式3: 添加指定的用户权限 步骤 1.获取当前计算机的名称 PC 2.添加用户信息.
注意事项: 计算机名称不要写中文.
1.4.6 关于Nginx图片回显异常说明
- 检查HOSTS文件配置正常.
# 京淘配置
#左侧写IP地址 右侧写域名 中间使用空格分隔
127.0.0.1 image.jt.com
127.0.0.1 manager.jt.com
127.0.0.1 www.jt.com
127.0.0.1 sso.jt.com
- 1
- 2
- 3
- 4
- 5
- 6
- 检查NGINX配置文件是否正确
# 配置图片服务器 server { listen 80; server_name image.jt.com;
##通过网址转向指定的目录 注意<span class="token operator">/</span>的写法 location <span class="token operator">/</span> <span class="token punctuation">{</span> root D<span class="token operator">:</span><span class="token operator">/</span>JT<span class="token operator">-</span>SOFT<span class="token operator">/</span>images<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 重启nginx
在重启之前检查是否有多余的nginx服务项,如果有则关闭.
- hosts文件是否生效
- 根据请求路径 检查代码中路径的拼接是否正常.
http://image.jt.com/2020/08/07/cc10f60491234317adf800aeafc6af1f.png
D:\JT-SOFT\images/2020/08/07/cc10f60491234317adf800aeafc6af1f.png
1.5 nginx域名代理
1.5.1业务需求
要求:用户通过http://manager.jt.com的方式 访问localhost:8091的服务器.
利用反向代理的方式实现该功能.
1.5.2配置nginx
# 商品管理服务器 监听的是用户 manager.jt.com:80 映射的是url地址 server { listen 80; server_name manager.jt.com;
#进行反向代理 location <span class="token operator">/</span> <span class="token punctuation">{</span> #映射的url请求网址<span class="token punctuation">.</span> proxy_pass http<span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span>localhost<span class="token operator">:</span><span class="token number">8091</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
1.5.3 访问效果
2. Nginx属性学习
2.1 Nginx负载均衡原理
2.2Nginx 集群搭建
说明: 要求启动3台服务器,运行jt-manager项目 端口号分别为8081/8082/8083, 通过/getPort请求,获取当前服务器端口号信息并且字符串返回.
2.2.1 动态获取当前服务器端口号
目的:为了能够在以后的负载均衡访问时看到自己访问的服务器到底是谁.
package com.jt.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PortController {
<span class="token comment">//考点:动态复制操作</span>
<span class="token annotation punctuation">@Value</span><span class="token punctuation">(</span><span class="token string">"${server.port}"</span><span class="token punctuation">)</span>
<span class="token keyword">private</span> Integer port<span class="token punctuation">;</span>
<span class="token annotation punctuation">@RequestMapping</span><span class="token punctuation">(</span><span class="token string">"/getPort"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> String <span class="token function">getPort</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token string">"当前访问的端口号:"</span><span class="token operator">+</span>port<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
2.2.2 发布3台tomcat服务器
要求端口号:8081/8082/8083.
步骤:
1.将项目打包 将项目先clean- build----install
2.准备部署路径,之后将war包上传
3.利用命令运行服务器
4.访问测试
2.3 轮询策略
说明: 根据nginx的配置文件,依次访问服务器.
# 商品管理服务器 监听的是用户 manager.jt.com:80 映射的是url地址 server { listen 80; server_name manager.jt.com;
#进行反向代理 location <span class="token operator">/</span> <span class="token punctuation">{</span> #映射的url请求网址<span class="token punctuation">.</span> #proxy_pass http<span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span>localhost<span class="token operator">:</span><span class="token number">8091</span><span class="token punctuation">;</span> proxy_pass http<span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span>jtWindows<span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> #配置集群 <span class="token number">1.</span>默认方式 轮询策略 upstream jtWindows <span class="token punctuation">{</span> server localhost<span class="token operator">:</span><span class="token number">8081</span><span class="token punctuation">;</span> server localhost<span class="token operator">:</span><span class="token number">8082</span><span class="token punctuation">;</span> server localhost<span class="token operator">:</span><span class="token number">8083</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
2.4 权重策略
说明: 性能越高的服务器,则承担的压力也就越高.
#配置集群 1.默认方式 轮询策略 2.权重策略 upstream jtWindows {
server localhost<span class="token operator">:</span><span class="token number">8081</span> weight<span class="token operator">=</span><span class="token number">6</span><span class="token punctuation">;</span> server localhost<span class="token operator">:</span><span class="token number">8082</span> weight<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">;</span> server localhost<span class="token operator">:</span><span class="token number">8083</span> weight<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
2.5 IPHASH策略
需求: 有时在访问时可能会固定的访问某台服务器. 将用户与服务器进行绑定. 以后用户只能访问该服务器.
算法: IPHASH算法.
配置:
#配置集群 1.默认方式 轮询策略 2.权重策略 3.IPhash策略
upstream jtWindows {
ip_hash;
server localhost:8081 weight=6;
server localhost:8082 weight=3;
server localhost:8083 weight=1;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
弊端:
1. 用户与服务器进行绑定,当服务器宕机时则可能影响用户的使用.
2. 由于访问是通过算数计算的结果,可能导致严重的负载不均的现象.
2.3Nginx 高级属性
2.3.1 down属性
说明:nginx在不做任何的配置时,无法感知服务器是否正常运行.所有会频繁的访问故障机.
解决方案: 可以利用down属性 来标识故障机.
#配置集群 1.默认方式 轮询策略 2.权重策略 3.IPhash策略
upstream jtWindows {
#ip_hash; server localhost:8081 weight=6;
server localhost:8081 down;
server localhost:8082 ;
server localhost:8083 ;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
2.3.2 备用机策略
说明:正常情况下一般服务器部署时需要准备几台服务器当做备用.正常的情况下该服务器不参与工作.只有当主机遇忙时/或者宕机时.该服务器才会工作.
配置方式:
#配置集群 1.默认方式 轮询策略 2.权重策略 3.IPhash策略
upstream jtWindows {
#ip_hash; server localhost:8081 weight=6;
server localhost:8081 down;
server localhost:8082 down;
server localhost:8083 backup;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
2.3.3 nginx高可用配置
说明:当tomcat服务器宕机时,可以通过如下的配置,实现服务器的自动的切换.无需人为的干预.
配置信息:
fail_timeout=60s; 设定超时时间(定义一个周期)
max_fails=1 设定最大的访问失败次数.
#配置集群 1.默认方式 轮询策略 2.权重策略 3.IPhash策略
upstream jtWindows {
#ip_hash; server localhost:8081 weight=6;
server localhost:8081 max_fails=1 fail_timeout=60s;
server localhost:8082 max_fails=1 fail_timeout=60s;
server localhost:8083 max_fails=1 fail_timeout=60s;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
配置说明:
如果nginx访问某台服务器时,如果该服务器访问失败,失败的次数达到最大失败次数时.在60秒之内,nginx不会再次访问故障机.直到下一个周期.
</div><div data-report-view="{"mod":"1585297308_001","dest":"https://blog.csdn.net/qq_16804847/article/details/107855254","extend1":"pc","ab":"new"}"><div></div></div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-ff98e99283.css" rel="stylesheet">
</div>