2006-Beijing Amoy Day08

1. File upload optimization

1.1 URL optimization

Note: If you need to access the server through a network virtual path, you should follow the configuration below.

  1. Local disk path: D:\JT-SOFT\images\2020\09\30\a.jpg
  2. Network virtual path: http://image.jt.com\2020\09\30\a.jpg

1.2 Edit pro configuration file

Insert picture description here

1.3 Complete the dynamic assignment of attributes

package com.jt.service;

import com.jt.vo.ImageVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

@Service
@PropertySource("classpath:/properties/images.properties") //容器动态加载指定的配置文件
public class FileServiceImpl implements FileService{
    
    

    //由于属性的值后期可能会发生变化,所以应该动态的获取属性数据. 利用pro配置文件
    @Value("${image.rootDirPath}")
    private String rootDirPath;             //   = "D:/JT-SOFT/images";
    @Value("${image.urlPath}")
    private String urlPath;                 // = "http://image.jt.com";

    //1.2 准备图片的集合  包含了所有的图片类型.
    private static Set<String> imageTypeSet;
    static {
    
    
        imageTypeSet = new HashSet<>();
        imageTypeSet.add(".jpg");
        imageTypeSet.add(".png");
        imageTypeSet.add(".gif");
    }


    /**
     * 完善的校验的过程
     * 1. 校验是否为图片
     * 2. 校验是否为恶意程序
     * 3. 防止文件数量太多,分目录存储.
     * 4. 防止文件重名
     * 5. 实现文件上传.
     * @param uploadFile
     * @return
     */
    @Override
    public ImageVO upload(MultipartFile uploadFile) {
    
    
        //0.防止有多余的空格 所以先做去空格的处理
        rootDirPath.trim();
        urlPath.trim();

        //1.校验图片类型  jpg|png|gif..JPG|PNG....
        //1.1 获取当前图片的名称 之后截取其中的类型.   abc.jpg
        String fileName = uploadFile.getOriginalFilename();
        int index = fileName.lastIndexOf(".");
        String fileType = fileName.substring(index);
        //将数据转化为小写
        fileType = fileType.toLowerCase();
        //1.3 判断图片类型是否正确.
        if(!imageTypeSet.contains(fileType)){
    
    
            //图片类型不匹配
            return ImageVO.fail();
        }

        //2.校验是否为恶意程序 根据宽度/高度进行判断
        try {
    
    
            //2.1 利用工具API对象 读取字节信息.获取图片对象类型
            BufferedImage bufferedImage = ImageIO.read(uploadFile.getInputStream());
            //2.2 校验是否有宽度和高度
            int width = bufferedImage.getWidth();
            int height = bufferedImage.getHeight();
            if(width==0 || height==0){
    
    
                return ImageVO.fail();
            }

            //3.分目录存储  yyyy/MM/dd 分隔
            //3.1 将时间按照指定的格式要求 转化为字符串.
            String dateDir = new SimpleDateFormat("/yyyy/MM/dd/")
                             .format(new Date());
            //3.2 拼接文件存储的目录对象
            String fileDirPath = rootDirPath + dateDir;
            File dirFile = new File(fileDirPath);
            //3.3 动态创建目录
            if(!dirFile.exists()){
    
    
                dirFile.mkdirs();
            }

            //4.防止文件重名  uuid.jpg 动态拼接
            //4.1 动态生成uuid  实现文件名称拼接  名.后缀
            String uuid =
                    UUID.randomUUID().toString().replace("-", "");
            String realFileName = uuid + fileType;

            //5 实现文件上传
            //5.1 拼接文件真实路径 dir/文件名称.
            String realFilePath = fileDirPath + realFileName;
            //5.2 封装对象  实现上传
            File realFile = new File(realFilePath);
            uploadFile.transferTo(realFile);

            //实现文件上传成功!!! http://image.jt.com\2020\09\30\a.jpg
            String url = urlPath + dateDir + realFileName;
            return ImageVO.success(url,width,height);
        } catch (IOException e) {
    
    
            e.printStackTrace();
            return ImageVO.fail();
        }
    }
}

2 Description of reverse proxy mechanism

2.1 Why do you need a reverse proxy

Requirement: When the file upload is completed, the business returns to the page with the virtual address path
url address: http://image.jt.com/2020/09/30/a.jpg
real picture address: file:///D:/JT-SOFT/image/2020/09/30/d534bed912c748b0ac979ee40222490a.jpg
question: how to let users access the picture that finds the real disk address through url.

2.2 Reverse proxy mechanism

2.2.1 Introduction to reverse proxy

The reverse proxy server is located between the user and the target server , but for the user, the reverse proxy server is equivalent to the target server, that is, the user can directly access the reverse proxy server to obtain the resources of the target server . At the same time, the user does not need to know the address of the target server , nor does it need to make any settings on the user side. The reverse proxy server is usually used as a Web acceleration, that is, the reverse proxy is used as the front end of the Web server to reduce the load of the network and the server, and improve the access efficiency.
Summary:
1. Located between the user (client) and the server.
2. The user accesses the reverse proxy server, thinking it is the real server information.
3. The user is not clear about who the real server information is.
4. Generally reverse The proxy mechanism protects the real server information, so it is also called a server-side proxy.
Insert picture description here

2.3 Forward agency mechanism

2.3.1 Requirements introduction

1. Broadband: The telecommunications operator account/password can only be used by one machine.
2. Router: A local area network is created inside the home so that the devices in the local area network can communicate with the outside world through the function of the router.

2.3.2 Introduction to Forward Agent

Forward proxy means a server located between the client and the origin server. In order to obtain content from the origin server, the client sends a request to the proxy and specifies the target (origin server), and then the proxy forwards it to the origin server Request and return the obtained content to the client. The client can use the forward proxy.
Summary:
1. The forward proxy is located between the client and the server.
2. The client determines the address of the target server before initiating the request.
3. The server does not know which client is accessing me, thinking it is only the router.
4 .The forward proxy protects the customer’s information, so it is also called the client proxy
Insert picture description here

2.4 Nginx

2.4.1 Introduction to Nginx

Nginx (engine x) is a high-performance HTTP and reverse proxy web server, and also provides IMAP/POP3/SMTP services. Nginx was developed by Igor Sesoyev for the second most visited site in Russia, Rambler.ru (Russian: Рамблер). The first public version 0.1.0 was released on October 4, 2004.
It releases its source code in the form of a BSD-like license, and is known for its stability, rich feature set, sample configuration files and low system resource consumption. On June 1, 2011, nginx 1.0.4 was released.
**Nginx is a lightweight web server/reverse proxy server and email (IMAP/POP3) proxy server,** issued under the BSD-like protocol. Its characteristics are that it occupies less memory and has strong concurrency capabilities. In fact, nginx's concurrency capabilities perform better in the same type of web server. Mainland Chinese users of nginx websites include: Baidu, JD.com, Sina, NetEase, Tencent, Taobao, etc.

Features:
1. The memory is less than 2M, and the tomcat server occupies about 600M
. 2. The concurrency capacity is 30,000-50,000 times/sec. The tomcat server is about 150-220

2.4.2 Nginx installation and use

Insert picture description here
Note:
1. Do not put nginx in the C drive and the system directory. Pay attention to the Chinese path and spaces.
2. The nginx server starts up very quickly, and the window will flash back. Only start it once
. 3. Nginx startup will occupy port 80.
4. The nginx command must be executed in the directory where nginx.exe is located.

2.4.2 Nginx commands

1). Start the command start nginx
2). Restart the command nginx -s reload
3). Stop the command nginx -s stop

2.4.3 Nginx server startup item description

Insert picture description here

2.4.4 About nginx reverse proxy instructions


http {
    
    
	# 一个反向代理就是一个server
    server {
    
    
    	#监听 80端口
        listen       80;
        #监听的域名  域名不能重复.
        server_name  localhost;
		#执行的反向代理的动作   / 拦截所有的路径
        location / {
    
    
        	# root关键字  代理的是一个目录
            root   html;
            #默认跳转页面
            index  index.html index.htm;
        }	
	}

}

2.5 Nginx realizes picture echo

2.5.1 NGINX configuration

# 图片服务器代理   image.jt.com:80
	server {
    
    
		listen 80;
		server_name image.jt.com;

		location / {
    
    
			# 转向目录
			root D:/JT-SOFT/images;
		}
	}

2.5.2 Modify the hosts file

1. Description of the
Insert picture description here
HOSTS file : 2. The location of the HOSTS file
Insert picture description here
3). Run as a super administrator
Insert picture description here

Insert picture description here

# 京淘配置  
#左侧写IP地址   右侧写域名  中间使用空格分隔
127.0.0.1   image.jt.com
127.0.0.1   manage.jt.com

#实现 nginx的
#192.168.126.129   image.jt.com
#192.168.126.129   manage.jt.com
127.0.0.1   www.jt.com
#Bug 有时在使用该软件时可能会出现丢失字母的现象.
127.0.0.1   sso.jt.com

3.nginx implements tomcat cluster deployment

3.1 Project deployment

Insert picture description here

3.2 Server reverse proxy

	#配置京淘后台管理服务器
	# manage.jt.com  localhost:8091服务器
	server {
    
    
		listen 80;
		server_name manage.jt.com;

		location / {
    
    

			#映射服务器
			proxy_pass  http://localhost:8091;
		}
	}

After modifying the nignx server, restart nginx
Insert picture description here

3.3 Dynamically obtain the current server port number

@RestController
public class PortController {
    
    

    //从spring服务器中动态的获取端口号
    @Value("${server.port}")
    private Integer port;

    @RequestMapping("/getPort")
    public String getPort(){
    
    

        return "当前服务器访问的端口号:"+port;
    }
}

3.4 Project packaging

Insert picture description here
Insert picture description here
After that, the jar package file is dynamically obtained from the target directory of the project to prepare for cluster deployment.
Insert picture description here

3.4 Project release order

Note: The current command execution will occupy the dos command window to print console information. When the dos command window is closed, the server stops.
Exit the dos command window:ctrl + c

java:     java     -jar   8081.war 

Insert picture description here

3.4 nginx load balancing implementation

3.4.1 Polling mechanism

Note: Follow the order of the configuration file to access the server.

#配置京淘后台管理服务器
	# manage.jt.com  localhost:8091服务器
	server {
    
    
		listen 80;
		server_name manage.jt.com;

		location / {
    
    

			#映射服务器
			#proxy_pass  http://localhost:8091;
			proxy_pass   http://jtWindows;    
		}
	}

	# 配置tomcat服务器集群  1.轮询策略
	upstream jtWindows {
    
    

		#server代表服务器地址
		server 127.0.0.1:8081;
		server 127.0.0.1:8082;	
	}

3.4.2 Weighting mechanism

Description: According to the weight setting, let the server with better performance process more requests.

# 配置tomcat服务器集群  1.轮询策略  2.权重策略
	upstream jtWindows {
    
    

		#server代表服务器地址
		server 127.0.0.1:8081 weight=8;
		server 127.0.0.1:8082 weight=2;	
	}

3.4.3 IPHASH strategy (understand)

Because some data is bound to the server, the user must later be required to use the IPHASH strategy when accessing the specified server

# 配置tomcat服务器集群  1.轮询策略  2.权重策略  3.iphash策略
	upstream jtWindows {
    
    

		#server代表服务器地址
		ip_hash;
		server 127.0.0.1:8081 weight=8;
		server 127.0.0.1:8082 weight=2;	
	}

operation

1. Install VMware virtual machine program
Insert picture description here

2. Check the network card settings.
If there is no network card, change to a vmwar version to install...
Insert picture description here
3. Boot the Linux system
Insert picture description here
Insert picture description here
Insert picture description here

Problem description:
Enter the BIOS system and enable the virtualization settings. The motherboard system boots F1/F2...
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_16804847/article/details/108880060