搜索
首页后端开发php教程Spring MVC代码实践之网站架构及演变

Spring MVC代码实践之网站架构及演变

Apr 02, 2018 am 10:03 AM
mvcspring架构网站

本篇文章给大家分享的是Spring MVC代码实践之网站架构及演变,内容挺不错的,希望可以帮助到有需要的朋友

网站架构及其演变过程

基础结构

网络传输分解方式:

  • 标准的 OSI 参考模型

  • TCP/IP 参考模型

640?

海量数据的解决方案

  • 缓存和页面静态化

  • 缓存

    • 通过程序直接保存在内存中

    • 使用缓存框架 (Encache、Redis、Memcache)

  • 页面静态化

    • 使用模板技术生成(Velocity、FreeMaker等)

  • 数据库优化

  • 表结构优化

  • SQL 语句优化

  • 分区

  • 分表

  • 索引优化

  • 使用存储过程代替直接操作过程

  • 分离活跃数据

  • 批量读取和延迟修改

  • 读写分离

    640

  • 分布式数据库

    640

  • NoSQL 和 Hadoop


高并发的解决方案

  • 应用和静态资源的分离:静态文件(图片、视频、JS、CSS等)放在专门的服务器上

  • 页面缓存(Nginx 服务器、Squid 服务器)

  • 集群与分布式

  • 反向代理

  • CDN

  • 底层优化:网络传输协议

常见协议和标准

TCP/IP 协议

IP:查找地址,对应着国际互联网

TCP:规范传输规则,对应着传输层

TCP 在传输之前会进行三次沟通,称 “三次握手”,传完数据断开的时候要进行四次沟通,称 “四次挥手”。

TCP 两个序号,三个标志位含义:

  • seq:表示所传数据的序号。TCP 传输时每一个字节都有一个序号,发送数据的时候会将数据的第一个序号发送给对方,接收方会按序号检查是否接收完整了,如果没接收完整就需要重新传送,这样就可以保证数据的完整性。

  • ack:表示确认号。接收端用它来给发送端反馈已经成功接收到的数据信息,它的值为希望接收的下一个数据包起始序号。

  • ACK:确认位,只有 ACK = 1 的时候 ack 才起作用。正常通信时 ACK 为 1,第一次发起请求时因为没有需要确认接收的数据所以 ACK 为 0。

  • SYN:同步位,用于在建立连接时同步序号。刚开始建立连接时并没有历史接收的数据,所以 ack 也就没有办法设置,这是按照正常的机制就无法运行了,SYN 的作用就是解决这个问题的,当接收端接收到 SYN = 1 的报文时就会直接将 ack 设置为接收到的 seq + 1 的值,注意这里的值并不是检验后设置的,而是根据 SYN 直接设置的,这样正常的机制就可以运行了,所以 SYN 叫同步位。SYN 会在前两次握手时都为 1,这是因为通信的双方的 ack 都需要设置一个初始值。

  • FIN:终止位,用来在数据传输完毕后释放连接。

640


DNS 的设置

DNS 解析

参考域名设置,如下是我在腾讯云域名的设置

640

记录类型:

A记录: 将域名指向一个IPv4地址(例如:8.8.8.8)

CNAME:将域名指向另一个域名(例如 www.54tianzhisheng.cn)

MX: 将域名指向邮件服务器地址

TXT: 可任意填写,长度限制255,通常做SPF记录(反垃圾邮件)

NS: 域名服务器记录,将子域名指定其他DNS服务器解析

AAAA:将域名指向一个iPv6地址(例如:ff06:0:0:0:0:0:0:c3)

SRV:记录提供特定服务的服务器(例如xmpp-server.tcp)

显性URL:将域名301重定向到另一个地址

隐性URL:类似显性URL,但是会隐藏真实目标地址

主机记录:

要解析 www.54tianzhisheng.cn,请填写 www。主机记录就是域名前缀,常见用法有:

www: *解析后的域名为 www.54tianzhisheng.cn。

*@: 直接解析主域名 54tianzhisheng.cn。

*: 泛解析,匹配其他所有域名 *.54tianzhisheng.cn。

mail: 将域名解析为 mail.54tianzhisheng.cn,通常用于解析邮箱服务器。

二级域名: 如:abc.54tianzhisheng.cn,填写abc。

手机网站: 如:m.54tianzhisheng.cn,填写m。


Java 中 Socket 的用法

普通 Soket 的用法

Socket 分为 ServerSocket 和 Socket 两大类。

ServerSocket 用于服务器端,可以通过 accept 方法监听请求,监听到请求后返回 Socket;

Socket 用户具体完成数据传输,客户端直接使用 Socket 发送请求并传输数据。

随便写了个单方面发送消息的 demo:

客户端:

  1. <span style="color:rgb(199,146,234);line-height:20px;font-size:13px !important;white-space: !important;">import</span><span style="color:rgb(238,255,255);line-height:20px;font-size:13px !important;white-space: !important;"> java</span><span style="color:rgb(204,204,204);line-height:20px;font-size:13px !important;white-space: !important;">.</span><span style="color:rgb(238,255,255);line-height:20px;font-size:13px !important;white-space: !important;">io</span><span style="color:rgb(204,204,204);line-height:20px;font-size:13px !important;white-space: !important;">.</span><span style="color:rgb(126,165,247);line-height:20px;font-size:13px !important;white-space: !important;">IOException</span><span style="color:rgb(204,204,204);line-height:20px;font-size:13px !important;white-space: !important;">;</span>

  2. <span style="color:rgb(199,146,234);line-height:20px;font-size:13px !important;white-space: !important;">import</span><span style="color:rgb(238,255,255);line-height:20px;font-size:13px !important;white-space: !important;"> java</span><span style="color:rgb(204,204,204);line-height:20px;font-size:13px !important;white-space: !important;">.</span><span style="color:rgb(238,255,255);line-height:20px;font-size:13px !important;white-space: !important;">io</span><span style="color:rgb(204,204,204);line-height:20px;font-size:13px !important;white-space: !important;">.</span><span style="color:rgb(126,165,247);line-height:20px;font-size:13px !important;white-space: !important;">OutputStream</span><span style="color:rgb(204,204,204);line-height:20px;font-size:13px !important;white-space: !important;">;</span>

  3. <span style="color:rgb(199,146,234);line-height:20px;font-size:13px !important;white-space: !important;">import</span><span style="color:rgb(238,255,255);line-height:20px;font-size:13px !important;white-space: !important;"> java</span><span style="color:rgb(204,204,204);line-height:20px;font-size:13px !important;white-space: !important;">.</span><span style="color:rgb(238,255,255);line-height:20px;font-size:13px !important;white-space: !important;">net</span><span style="color:rgb(204,204,204);line-height:20px;font-size:13px !important;white-space: !important;">.</span><span style="color:rgb(126,165,247);line-height:20px;font-size:13px !important;white-space: !important;">Socket</span><span style="color:rgb(204,204,204);line-height:20px;font-size:13px !important;white-space: !important;">;</span>

  4. <span style="color:rgb(79,103,117);line-height:20px;font-size:13px !important;white-space: !important;">/**</span>

  5. <span style="color:rgb(79,103,117);line-height:20px;font-size:13px !important;white-space: !important;"> * Created by 10412 on 2017/5/2.</span>

  6. <span style="color:rgb(79,103,117);line-height:20px;font-size:13px !important;white-space: !important;"> * TCP客户端:</span>

  7. <span style="color:rgb(79,103,117);line-height:20px;font-size:13px !important;white-space: !important;"> ①:建立tcp的socket服务,最好明确具体的地址和端口。这个对象在创建时,就已经可以对指定ip和端口进行连接(三次握手)。</span>

  8. <span style="color:rgb(79,103,117);line-height:20px;font-size:13px !important;white-space: !important;"> ②:如果连接成功,就意味着通道建立了,socket流就已经产生了。只要获取到socket流中的读取流和写入流即可,只要通过getInputStream和getOutputStream就可以获取两个流对象。</span>

  9. <span style="color:rgb(79,103,117);line-height:20px;font-size:13px !important;white-space: !important;"> ③:关闭资源。</span>

  10. */
    //单方面的输入!
    public class TcpClient
    {
        public static void main(String[] args) {
            try {
                Socket s = new Socket("127.0.0.1", 9999);
                OutputStream o = s.getOutputStream();
                o.write("tcp sssss".getBytes());
                s.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

服务器端:

  1. <span style="color:rgb(199,146,234);line-height:20px;font-size:13px !important;white-space: !important;">import</span><span style="color:rgb(238,255,255);line-height:20px;font-size:13px !important;white-space: !important;"> java</span><span style="color:rgb(204,204,204);line-height:20px;font-size:13px !important;white-space: !important;">.</span><span style="color:rgb(238,255,255);line-height:20px;font-size:13px !important;white-space: !important;">io</span><span style="color:rgb(204,204,204);line-height:20px;font-size:13px !important;white-space: !important;">.</span><span style="color:rgb(126,165,247);line-height:20px;font-size:13px !important;white-space: !important;">IOException</span><span style="color:rgb(204,204,204);line-height:20px;font-size:13px !important;white-space: !important;">;</span>

  2. <span style="color:rgb(199,146,234);line-height:20px;font-size:13px !important;white-space: !important;">import</span><span style="color:rgb(238,255,255);line-height:20px;font-size:13px !important;white-space: !important;"> java</span><span style="color:rgb(204,204,204);line-height:20px;font-size:13px !important;white-space: !important;">.</span><span style="color:rgb(238,255,255);line-height:20px;font-size:13px !important;white-space: !important;">io</span><span style="color:rgb(204,204,204);line-height:20px;font-size:13px !important;white-space: !important;">.</span><span style="color:rgb(126,165,247);line-height:20px;font-size:13px !important;white-space: !important;">InputStream</span><span style="color:rgb(204,204,204);line-height:20px;font-size:13px !important;white-space: !important;">;</span>

  3. <span style="color:rgb(199,146,234);line-height:20px;font-size:13px !important;white-space: !important;">import</span><span style="color:rgb(238,255,255);line-height:20px;font-size:13px !important;white-space: !important;"> java</span><span style="color:rgb(204,204,204);line-height:20px;font-size:13px !important;white-space: !important;">.</span><span style="color:rgb(238,255,255);line-height:20px;font-size:13px !important;white-space: !important;">net</span><span style="color:rgb(204,204,204);line-height:20px;font-size:13px !important;white-space: !important;">.</span><span style="color:rgb(126,165,247);line-height:20px;font-size:13px !important;white-space: !important;">ServerSocket</span><span style="color:rgb(204,204,204);line-height:20px;font-size:13px !important;white-space: !important;">;</span>

  4. <span style="color:rgb(199,146,234);line-height:20px;font-size:13px !important;white-space: !important;">import</span><span style="color:rgb(238,255,255);line-height:20px;font-size:13px !important;white-space: !important;"> java</span><span style="color:rgb(204,204,204);line-height:20px;font-size:13px !important;white-space: !important;">.</span><span style="color:rgb(238,255,255);line-height:20px;font-size:13px !important;white-space: !important;">net</span><span style="color:rgb(204,204,204);line-height:20px;font-size:13px !important;white-space: !important;">.</span><span style="color:rgb(126,165,247);line-height:20px;font-size:13px !important;white-space: !important;">Socket</span><span style="color:rgb(204,204,204);line-height:20px;font-size:13px !important;white-space: !important;">;</span>

  5. /**
     * Created by 10412 on 2017/5/2.
     */
    public class TcpServer
    {
        public static void main(String[] args) {
            try {
                ServerSocket ss = new ServerSocket(9999);//建立服务端的socket服务
                Socket s = ss.accept();//获取客户端对象
                String ip = s.getInetAddress().getHostAddress();
                int port = s.getPort();
                System.out.println(ip + " : " + port + " connected");
                // 可以通过获取到的socket对象中的socket流和具体的客户端进行通讯。
                InputStream ins = s.getInputStream();//读取客户端的数据,使用客户端对象的socket读取流
                byte[] bytes = new byte[1024];
                int len = ins.read(bytes);
                String text = new String(bytes, 0, len);
                System.out.println(text);
                //关闭资源
                s.close();
                ss.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

以上是Spring MVC代码实践之网站架构及演变的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
使用数据库存储会话的优点是什么?使用数据库存储会话的优点是什么?Apr 24, 2025 am 12:16 AM

使用数据库存储会话的主要优势包括持久性、可扩展性和安全性。1.持久性:即使服务器重启,会话数据也能保持不变。2.可扩展性:适用于分布式系统,确保会话数据在多服务器间同步。3.安全性:数据库提供加密存储,保护敏感信息。

您如何在PHP中实现自定义会话处理?您如何在PHP中实现自定义会话处理?Apr 24, 2025 am 12:16 AM

在PHP中实现自定义会话处理可以通过实现SessionHandlerInterface接口来完成。具体步骤包括:1)创建实现SessionHandlerInterface的类,如CustomSessionHandler;2)重写接口中的方法(如open,close,read,write,destroy,gc)来定义会话数据的生命周期和存储方式;3)在PHP脚本中注册自定义会话处理器并启动会话。这样可以将数据存储在MySQL、Redis等介质中,提升性能、安全性和可扩展性。

什么是会话ID?什么是会话ID?Apr 24, 2025 am 12:13 AM

SessionID是网络应用程序中用来跟踪用户会话状态的机制。1.它是一个随机生成的字符串,用于在用户与服务器之间的多次交互中保持用户的身份信息。2.服务器生成并通过cookie或URL参数发送给客户端,帮助在用户的多次请求中识别和关联这些请求。3.生成通常使用随机算法保证唯一性和不可预测性。4.在实际开发中,可以使用内存数据库如Redis来存储session数据,提升性能和安全性。

您如何在无状态环境(例如API)中处理会议?您如何在无状态环境(例如API)中处理会议?Apr 24, 2025 am 12:12 AM

在无状态环境如API中管理会话可以通过使用JWT或cookies来实现。1.JWT适合无状态和可扩展性,但大数据时体积大。2.Cookies更传统且易实现,但需谨慎配置以确保安全性。

您如何防止与会议有关的跨站点脚本(XSS)攻击?您如何防止与会议有关的跨站点脚本(XSS)攻击?Apr 23, 2025 am 12:16 AM

要保护应用免受与会话相关的XSS攻击,需采取以下措施:1.设置HttpOnly和Secure标志保护会话cookie。2.对所有用户输入进行输出编码。3.实施内容安全策略(CSP)限制脚本来源。通过这些策略,可以有效防护会话相关的XSS攻击,确保用户数据安全。

您如何优化PHP会话性能?您如何优化PHP会话性能?Apr 23, 2025 am 12:13 AM

优化PHP会话性能的方法包括:1.延迟会话启动,2.使用数据库存储会话,3.压缩会话数据,4.管理会话生命周期,5.实现会话共享。这些策略能显着提升应用在高并发环境下的效率。

什么是session.gc_maxlifetime配置设置?什么是session.gc_maxlifetime配置设置?Apr 23, 2025 am 12:10 AM

thesession.gc_maxlifetimesettinginphpdeterminesthelifespanofsessiondata,setInSeconds.1)它'sconfiguredinphp.iniorviaini_set().2)abalanceIsiseededeedeedeedeedeedeedto to to avoidperformance andununununununexpectedLogOgouts.3)

您如何在PHP中配置会话名?您如何在PHP中配置会话名?Apr 23, 2025 am 12:08 AM

在PHP中,可以使用session_name()函数配置会话名称。具体步骤如下:1.使用session_name()函数设置会话名称,例如session_name("my_session")。2.在设置会话名称后,调用session_start()启动会话。配置会话名称可以避免多应用间的会话数据冲突,并增强安全性,但需注意会话名称的唯一性、安全性、长度和设置时机。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)