发表于: 2019-08-29 20:15:57
1 652
任务六任务总结
任务六耗时10天,其中耽误时间最多的还是在解决报错上面,解决完之后发现这些其实都是配置文件、依赖等方面的问题,总而言之,就是对memcache和redis不熟悉导致的;
任务六主要就是memcache和redis的使用,建立缓存,创建一块临时的内存用来存在从数据库中调取的数据,这样后续请求可以直接从缓存中取得数据,不用再向数据库发送请求,减少了数据库的压力,基本能正常应对大数据高并发的情况;
任务其实就是对缓存的理解,使用到的工具就是memcache和redis,个人理解这两个工具其实差不多,memcache是将数据全部放到内存中,这样数据的大小就会有限制,不能超过内存的大小,
redis有部分数据是存在到硬盘的,这样能保证数据的持久性,实现数据的持久化;
另外,redis在存放数据的类型上也比memcache多得多,且redis官方只能在linux系统运行,memcache则Windows和linux都支持;
关于memcache的配置:
<!--<?xml version="1.0" encoding="UTF-8"?>-->
<!--<beans xmlns="http://www.springframework.org/schema/beans"-->
<!-- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"-->
<!-- xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"-->
<!-- xsi:schemaLocation="http://www.springframework.org/schema/beans-->
<!-- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd-->
<!-- http://www.springframework.org/schema/context-->
<!-- http://www.springframework.org/schema/context/spring-context-2.5.xsd-->
<!-- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd-->
<!-- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">-->
<!-- <bean id="propertyConfigurer"-->
<!-- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">-->
<!-- <property name="locations">-->
<!-- <list>-->
<!-- <value>classpath:/memcache.properties</value>-->
<!-- </list>-->
<!-- </property>-->
<!-- </bean>-->
<!-- <bean id="memcachedPool" class="com.alibaba.druid."-->
<!-- factory-method="getInstance" init-method="initialize" destroy-method="shutDown">-->
<!-- <constructor-arg>-->
<!-- <value>neeaMemcachedPool</value>-->
<!-- </constructor-arg>-->
<!-- <!– 可以配置多服务器负载均衡 –>-->
<!-- <property name="servers">-->
<!-- <list>-->
<!-- <value>${memcache.server}</value>-->
<!-- </list>-->
<!-- </property>-->
<!-- <!– 初始连接数 –>-->
<!-- <property name="initConn">-->
<!-- <value>${memcache.initConn}</value>-->
<!-- </property>-->
<!-- <!– 最小连接数 –>-->
<!-- <property name="minConn">-->
<!-- <value>${memcache.minConn}</value>-->
<!-- </property>-->
<!-- <!– 最大连接数 –>-->
<!-- <property name="maxConn">-->
<!-- <value>${memcache.maxConn}</value>-->
<!-- </property>-->
<!-- <!– 连接池守护线程的睡眠时间 –>-->
<!-- <property name="maintSleep">-->
<!-- <value>${memcache.maintSleep}</value>-->
<!-- </property>-->
<!-- <property name="nagle">-->
<!-- <value>${memcache.nagle}</value>-->
<!-- </property>-->
<!-- <property name="socketTO">-->
<!-- <value>${memcache.socketTO}</value>-->
<!-- </property>-->
<!-- </bean>-->
<!-- <bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient">-->
<!-- <constructor-arg>-->
<!-- <value>neeaMemcachedPool</value>-->
<!-- </constructor-arg>-->
<!-- </bean>-->
<!--</beans>-->
#memcached的服务器地址
memcache.server=127.0.0.1:11211
#初始连接数
memcache.initConn=10
memcache.minConn=5
memcache.maxConn=50
#设置连接池维护线程的睡眠时间
memcache.maintSleep=3000
#设置是否使用Nagle算法(Socket的参数),如果是true在写数据时不缓冲,立即发送出去
memcache.nagle=false
#设置socket的读取等待超时时间
memcache.socketTO=3000
以及controller接口:
// @RequestMapping(value = "/home",method = RequestMethod.GET)
// public String getAllStu(ModelMap modelMap) throws Exception {
// List<Stu> stus;
// if (memcachedClient.get("stus") == null){
// stus = stuService.getAllStu();
// stus.subList(0, 4);
// memcachedClient.set("stus",0, stus);
// modelMap.addAttribute("stus", stus);
// }else{
// stus = (List<Stu>)memcachedClient.get("stus");
// }
// return "home" ;
// }
然后是redis:
redis.host=127.0.0.1
redis.port=6379
redis.sentinel.port=26879
redis.pwd=
redis.database=0
redis.timeout=1000
redis.userPool=true
redis.pool.maxIdle=100
redis.pool.minIdle=10
redis.pool.maxTotal=200
redis.pool.maxWaitMillis=10000
redis.pool.minEvictableIdleTimeMillis=300000
redis.pool.numTestsPerEvictionRun=10
redis.pool.timeBetweenEvictionRunsMillis=30000
redis.pool.testOnBorrow=true
redis.pool.testOnReturn=true
redis.pool.testWhileIdle=true
<context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/>
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<property name="minIdle" value="${redis.pool.minIdle}" />
<property name="maxTotal" value="${redis.pool.maxTotal}" />
<property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />
<property name="minEvictableIdleTimeMillis" value="${redis.pool.minEvictableIdleTimeMillis}"/>
<property name="numTestsPerEvictionRun" value="${redis.pool.numTestsPerEvictionRun}"/>
<property name="timeBetweenEvictionRunsMillis" value="${redis.pool.timeBetweenEvictionRunsMillis}"/>
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
<property name="testOnReturn" value="${redis.pool.testOnReturn}" />
<property name="testWhileIdle" value="${redis.pool.testWhileIdle}"/>
</bean>
<bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >
<property name="poolConfig" ref="poolConfig" />
<property name="hostName" value="${redis.host}" />
<property name="port" value="${redis.port}" />
<property name="password" value="${redis.pwd}" />
<property name="usePool" value="${redis.userPool}" />
<property name="database" value="${redis.database}" />
<property name="timeout" value="${redis.timeout}" />
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
接口:
//首页
@RequestMapping(value = "/home",method = RequestMethod.GET)
public String getAllStu(ModelMap modelMap) throws Exception {
List<Stu> stus ;
if(redisTemplate.hasKey("stus")!=true){//缓存查询没有命中
stus = stuService.getAllStu();
stus.subList(0,4);
if(stus!=null){
ValueOperations<Serializable,Object> operations = redisTemplate.opsForValue();
operations.set("stus",stus);
} else{
redisTemplate.opsForValue().set("stus",null,60, TimeUnit.SECONDS);
}
modelMap.addAttribute("stus",stus);
}else {
ValueOperations<Serializable,Object> operations = redisTemplate.opsForValue();
Object object = operations.get("stus");
stus = (List<Stu>)object;
modelMap.addAttribute("stus", stus);
}
return "home" ;
}
深度思考:
1.后台只允许有列表页和详情页,列表页分为搜索区和列表区和操作区,原因是什么?有没有其他设计方式,相比之下各自的好处是什么?
2.什么叫集群?缓存应该在什么情况下使用集群?有哪些实现集群的方案?
答:集群是多个服务器支持一个项目运行,实现集群的方案有分布式,即将一个项目拆分成多个板块,每个服务器运行其中一个板块;
3.什么是压测,为什么要进行压力测试?JMETER工具的使用
答:压测即压力测试,为的是测试服务器在高并发情况下的极限,从而更好的分配资源,使得服务器数据库效率最大化;
4.Memcache和Redis可否做集群?什么样的情况下应该做集群?
5.什么是脏数据,缓存中是否可能产生脏数据,如果出现脏数据该怎么处理?
答:脏数据是指缓存中已经过期或者无意义的数据,设置缓存过期时效;
6.插入,更新和查询数据的时候,读写缓存和DB的顺序应该是怎么样的?
答:插入应该是不写入缓存,更新是先更新DB,然后在更新缓存中对应的数据,查询是先查询缓存,没有在去查询数据库;
7.JVM缓存和Memcache这种缓存的区别在哪里?是否可以不使用Memcache,只用虚拟机内存做缓存?
8.缓存应该在Service里,还是应该存放在Controller里,为什么?
答:controller,service中会涉及到与数据库的交互,放到controller易于维护;
9.什么叫穿透DB?什么情况下会发生,穿透DB后会发生什么事情?
答:穿透DB是指有一个数据非常火爆,一直在承担着高并发,大量的并发集中对这个数据进行访问,在这个数据的缓存失效的瞬间,所有的访问都会瞬间转移到数据库,击破缓存直接访问数据库就是穿透DB,穿透DB后会使数据库在瞬间承担极大的访问量,可能导致数据库崩溃;
10.什么叫命中率?正常来讲,命中率应该控制在多少?
答:命中率是指可以通过缓存获得想要的数据;
11.什么样的数据适合存在缓存中?缓存的淘汰算法有哪些?
12.什么叫一致性哈希,通常用来解决什么问题?
答:分布式系统中其中一个节点失效,也不会影响整个系统的运行;
13.缓存的失效策略有哪几种,分别适合什么场景?
14.Memcache和Redis的区别是什么?
答:memcache是将数据全部放到内存中,这样数据的大小就会有限制,不能超过内存的大小,redis有部分数据是存在到硬盘的,这样能保证数据的持久性,实现数据的持久化;另外,redis在存放数据的类型上也比memcache多得多,且redis官方只能在linux系统运行,memcache则Windows和linux都支持;
15.怎么预估自己系统可承载的日活数?
16.什么是JMeter?Jmeter是否可以在多台机器上分布式部署?为什么要分布式部署?
答:JMeter是压力测试工具,可以分布式部署;
17.什么是TPS,什么是每秒并发数,什么是90%Line?分别应该到达多少算符合系统上线的要求?
答:TPS是每秒吞吐量,每秒可以处理的请求数量,每秒并发数是每秒收到的请求数量,90%Line是90%的请求平均响应时间;
评论