加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java高并发解决方案

发布时间:2020-12-14 06:37:42 所属栏目:Java 来源:网络整理
导读:p style="margin-top:0px; margin-bottom:0.75em; padding-top:0px; padding-bottom:0px; font-size:16px; line-height:27.2px; text-indent:1em; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Tahoma,Arial,STXihei,'Microsoft YaHei',微

<p style="margin-top:0px; margin-bottom:0.75em; padding-top:0px; padding-bottom:0px; font-size:16px; line-height:27.2px; text-indent:1em; color:rgb(51,51,51); font-family:'Helvetica Neue',Helvetica,Tahoma,Arial,STXihei,'Microsoft YaHei',微软雅黑,sans-serif; background-color:rgb(254,254,254)">
高并发的时候是有很多用户在访问,导致出现系统数据不正确、丢失数据现象,所以想到 的是用队列解决,其实队列解决的方式也可以处理,比如我们在竞拍商品、转发评论微博或者是秒杀商品等,同一时间访问量特别大,队列在此起到特别的作用,将 所有请求放入队列,以毫秒计时单位,有序的进行,从而不会出现数据丢失系统数据不正确的情况。


<p style="margin-top:0px; margin-bottom:0.75em; padding-top:0px; padding-bottom:0px; font-size:16px; line-height:27.2px; text-indent:1em; color:rgb(51,254)">
今天我经过查资料,高并发的解决方法有俩种,一种是使用缓存、另一种是使用生成静态页面;还有就是从最基础的地方优化我们写代码减少不必要的资源浪费:(


<p style="margin-top:0px; margin-bottom:0.75em; padding-top:0px; padding-bottom:0px; font-size:16px; line-height:27.2px; text-indent:1em; color:rgb(51,254)">
1.不要频繁的new对象,对于在整个应用中只需要存在一个实例的类使用单例模式.对于String的连接操作,使用StringBuffer或者StringBuilder.对于utility类型的类通过静态方法来访问。


<p style="margin-top:0px; margin-bottom:0.75em; padding-top:0px; padding-bottom:0px; font-size:16px; line-height:27.2px; text-indent:1em; color:rgb(51,254)">
2. 避免使用错误的方式,如Exception可以控制方法推出,但是Exception要保留stacktrace消耗性能,除非必要不要使用 instanceof做条件判断,尽量使用比的条件判断方式.使用<a target="_blank" href="http://lib.csdn.net/base/javase" rel="nofollow" class="replace_word" title="Java SE知识库" style="text-decoration:none; color:rgb(223,52,52); font-weight:bold">Java中效率高的类,比如ArrayList比Vector性能好。)


<p style="margin-top:0px; margin-bottom:0.75em; padding-top:0px; padding-bottom:0px; font-size:16px; line-height:27.2px; text-indent:1em; color:rgb(51,254)">
首先缓存技术我一直没有使用过,我觉得应该是在用户请求时将数据保存在缓存中,下次请求时会检测缓存中是否有数据存在,防止多次请求服务器,导致服务器性能降低,严重导致服务器崩溃,这只是我自己的理解,详细的资料还是需要在网上收集;


<p style="margin-top:0px; margin-bottom:0.75em; padding-top:0px; padding-bottom:0px; font-size:16px; line-height:27.2px; text-indent:1em; color:rgb(51,254)">
使用生成静态页面我想大家应该不模式,我们见过很多网站当在请求的时候页面的后最已经变了,如“http://developer.51cto.com/art/201207/348766.htm”该页面其实是一个服务器请求地址,在转换成htm后,访问速度将提升,因为静态页面不带有服务器组件;在这里我就多多介绍一下:


<p style="margin-top:0px; margin-bottom:0.75em; padding-top:0px; padding-bottom:0px; font-size:16px; line-height:27.2px; text-indent:1em; color:rgb(51,254)">
一、什么是页面静态化:


<p style="margin-top:0px; margin-bottom:0.75em; padding-top:0px; padding-bottom:0px; font-size:16px; line-height:27.2px; text-indent:1em; color:rgb(51,254)">
简 单的说,我们如果访问一个链接,服务器对应的模块会处理这个请求,转到对应的jsp界面,最后生成我们想要看到的数据。这其中的缺点是显而易见的:因为每次请求服务器都会进行处理,如 果有太多的高并发请求,那么就会加重应用服务器的压力,弄不好就把服务器 搞down 掉了。那么如何去避免呢?如果我们把对 test.do 请求后的结果保存成一个 html 文件,然后每次用户都去访问,这样应用服务器的压力不就减少了?


<p style="margin-top:0px; margin-bottom:0.75em; padding-top:0px; padding-bottom:0px; font-size:16px; line-height:27.2px; text-indent:1em; color:rgb(51,254)">
那么静态页面从哪里来呢?总不能让我们每个页面都手动处理吧?这里就牵涉到我们要讲解的内容了,静态页面生成方案… 我们需要的是自动的生成静态页面,当用户访问,会自动生成 test.html,然后显示给用户。


<p style="margin-top:0px; margin-bottom:0.75em; padding-top:0px; padding-bottom:0px; font-size:16px; line-height:27.2px; text-indent:1em; color:rgb(51,254)">
二、下面我们在简单介绍一下要想掌握页面静态化方案应该掌握的知识点:


<p style="margin-top:0px; margin-bottom:0.75em; padding-top:0px; padding-bottom:0px; font-size:16px; line-height:27.2px; text-indent:1em; color:rgb(51,254)">
1、 基础- URL Rewrite


<p style="margin-top:0px; margin-bottom:0.75em; padding-top:0px; padding-bottom:0px; font-size:16px; line-height:27.2px; text-indent:1em; color:rgb(51,254)">
什么是 URL Rewrite 呢 ? URL 重写。用一个简单的例子来说明问题:输入网址,但是实际上访问的却是 abc.com/test.action,那我们就可以说 URL 被重写了。这项技术应用广泛,有许多开源的工具可以实现这个功能。


<p style="margin-top:0px; margin-bottom:0.75em; padding-top:0px; padding-bottom:0px; font-size:16px; line-height:27.2px; text-indent:1em; color:rgb(51,254)">
2、 基础- Servlet web.xml


<p style="margin-top:0px; margin-bottom:0.75em; padding-top:0px; padding-bottom:0px; font-size:16px; line-height:27.2px; text-indent:1em; color:rgb(51,254)">
如果你还不知道 web.xml 中一个请求和一个 servlet 是如何匹配到一起的,那么请搜索一下 servlet 的文档。这可不是乱说呀,有很多人就认为 /xyz/*.do 这样的匹配方式能有效。


<p style="margin-top:0px; margin-bottom:0.75em; padding-top:0px; padding-bottom:0px; font-size:16px; line-height:27.2px; text-indent:1em; color:rgb(51,254)">
如果你还不知道怎么编写一个 servlet,那么请搜索一下如何编写 servlet.这可不是说笑呀,在各种集成工具漫天飞舞的今天,很多人都不会去从零编写一个 servlet了。


<p style="margin-top:0px; margin-bottom:0.75em; padding-top:0px; padding-bottom:0px; font-size:16px; line-height:27.2px; text-indent:1em; color:rgb(51,254)">
三、基本的方案介绍


<p align="center" style="margin-top:0px; margin-bottom:0.75em; padding-top:0px; padding-bottom:0px; font-size:16px; line-height:27.2px; text-indent:1em; color:rgb(51,254)">

[html]??


lighttped====>squid(s) ====lighttpd

),并指定域名为res1.***.com,res2.***.com,采用ajp协议


(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读