软件架构-解密电商系统-分布式session

522 阅读2分钟

上次主要界面介绍了秒杀系统,分析了秒杀系统可能存在的几个性能瓶颈的地方:商品列表,详情页面,提交订单页面。这次主要说说分布式session的问题。

(一)秒杀增加高并发的方式

1.加入redis缓存的方式,判断是否redis中存在?第一次从数据库里面取,放入redis中,第二次的时候直接从redis里面取。明显并发可以上去。吞吐量也可以明显提高。如果商品价格发生变化,及时的维护redis中的缓存。

2.nginx的并发数,加大nginx的并发数。 增加work_rlimit_nofile和worker_connections数量,并禁用keepalive_timeout。

3.tomcat的并发数。加大tomcat的并发数。超时时间加大。

maxThreads="1000" 最大并发数 。 minSpareThreads="100"///初始化时创建的线程数。 maxSpareThreads="500"///一旦创建的线程超过这个值,Tomcat就会关闭不再需要的socket线程。 acceptCount="700"// 指定当所有可以使用的处理请求的线程数都被使用时,可以放到处理队列中的请求数,超过这个数的请求将不予处理。

<Connector port="8080" protocol="HTTP/1.1"  
minSpareThreads="100"   
maxSpareThreads="500"      
maxThreads="1000"   
acceptCount="700" 
connectionTimeout="20000"
redirectPort="8443" />

4.redis的连接池。

5.dubbo连接池与超时时间。

6.数据库的连接池加大。Too many connections

系统支持的最大连接max_connections
用户能最大连接进来的数量max_user_connections
别忘记在配置文件里添加否则重启失效
max_connections=1000;
max_user_connection=600

7.linux系统的句柄数--too many open files

ulimit -n 20480

(二)session共享插件适合十万级以下并发

github.com/ran-jit/tom… 支持tomcat7,tomcat8,tomcat9。

  • 选择它因为他支持tomcat8和tomcat9

  • 详细的介绍和下载路径

github.com/ran-jit/tom…

  • Steps to be done, 1.Copy the downloaded jars to your tomcat/lib directory.

tomcat/lib/

2.Add tomcat system property "catalina.base".

  • catalina.base="TOMCAT_LOCATION"

example: export catalina.base=/opt/tomcat

3.Copy the redis-data-cache.properties file to your tomcat/conf directory and update your Redis server details.

  • tomcat/conf/redis-data-cache.properties

Add the below two lines in your tomcat/conf/context.xml file.

<Valve className="tomcat.request.session.redis.SessionHandlerValve" />
<Manager className="tomcat.request.session.redis.SessionManager" />

Verify the session expiration time in tomcat/conf/web.xml file.

<session-config>
        <session-timeout>60</session-timeout>
</session-config>

PS:互联网公司都是堆机器,水平挂的机器多,redis多,tomcat多,nginx多,每个机器的配置一般,但是机器多分到每个机器上的就少了。其实还有内部的一部分优化。并不是调的越大越好,这个也是根据机器的性能的。需要根据机器来反复的尝试获得最佳值。