当前位置: 首页 > news >正文

长沙做网站有哪些互联网电商平台有哪些

长沙做网站有哪些,互联网电商平台有哪些,广西宏泰成建设集团网站,巴州区建设局网站目录 Redis常规配置 tcp-keepalive security Jedis RedisTemplate 连接池技术 Lua脚本 Jedis集群 Redis应用问题&解决方案 缓存穿透 缓存击穿 缓存雪崩 分布式锁 Redis实现分布式锁 Redis新功能 ACL Redis常规配置 tcp-keepalive security redis.conf中…

目录

Redis常规配置

tcp-keepalive

security 

Jedis

RedisTemplate

连接池技术

Lua脚本 

Jedis集群

Redis应用问题&解决方案

缓存穿透

缓存击穿

缓存雪崩

分布式锁

Redis实现分布式锁

Redis新功能

ACL


Redis常规配置

tcp-keepalive

security 

redis.conf中设置密码,永久设置

用户名默认是default,可以不写

Jedis

创建Maven项目,引入依赖

需要防火墙打开Redis的端口

将bind 127.0.0.1注释掉,支持远程连接

protected_mode保护模式设为no,支持远程连接

        如果Redis配置了密码,则需要进行身份校验
        jedis.auth("密码");

RedisTemplate

引入依赖 

application.properties

#Redis 服务器地址
spring.redis.host=192.168.102.130
#Redis 服务器连接端口
spring.redis.port=6379
#Redis 如果有密码,需要配置, 没有密码就不要写
#spring.redis.password=123
#Redis 数据库索引(默认为0)
spring.redis.database=0
#连接超时时间(毫秒)
spring.redis.timeout=1800000
#连接池最大连接数(使用负值表示没有限制)
spring.redis.lettuce.pool.max-active=20
#最大阻塞等待时间(负数表示没限制)
spring.redis.lettuce.pool.max-wait=-1
#连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=5
#连接池中的最小空闲连接
spring.redis.lettuce.pool.min-idle=0

redis配置类

默认配置存在的问题:

        redisTemplate 模糊查找 keys(*) 数据为空

        使用Java程序读取客户端写入的数据,转换异常,是因为没有使用配置类进行序列化,除非都是数据都是通过Java程序读和写

@EnableCaching
@Configuration
public class RedisConfig extends CachingConfigurerSupport {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {RedisTemplate<String, Object> template =new RedisTemplate<>();System.out.println("template=>" + template);RedisSerializer<String> redisSerializer =new StringRedisSerializer();Jackson2JsonRedisSerializer jackson2JsonRedisSerializer =new Jackson2JsonRedisSerializer(Object.class);ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_ARRAY);jackson2JsonRedisSerializer.setObjectMapper(om);template.setConnectionFactory(factory);
//key 序列化方式template.setKeySerializer(redisSerializer);
//value 序列化template.setValueSerializer(jackson2JsonRedisSerializer);
//value hashmap 序列化template.setHashValueSerializer(jackson2JsonRedisSerializer);return template;}@Beanpublic CacheManager cacheManager(RedisConnectionFactory factory) {RedisSerializer<String> redisSerializer =new StringRedisSerializer();Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = newJackson2JsonRedisSerializer(Object.class);
//解决查询缓存转换异常的问题ObjectMapper om = new ObjectMapper();om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_ARRAY);jackson2JsonRedisSerializer.setObjectMapper(om);
// 配置序列化(解决乱码的问题),过期时间 600 秒RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofSeconds(600)).serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer)).serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)).disableCachingNullValues();RedisCacheManager cacheManager = RedisCacheManager.builder(factory).cacheDefaults(config).build();return cacheManager;}
}

Controller

连接池技术

使用连接池来获取Redis连接

volatile的作用:

        1、线程的可见性:当一个线程去修改一个共享变量时,另一个线程可以读取修改的值

        2、顺序的一致性:禁止指令重排

保证每次调用返回的 jedisPool 是单例,构造器私有化
使用双重校验,保证 jedisPool 只创建一次,可以解决超卖问题

public class JedisPoolUtil {private static volatile JedisPool jedisPool = null;private JedisPoolUtil() {}public static JedisPool getJedisPoolInstance() {if (null == jedisPool) {synchronized (JedisPoolUtil.class) {if (null == jedisPool) {JedisPoolConfig poolConfig = new JedisPoolConfig();//对连接池进行配置poolConfig.setMaxTotal(200);poolConfig.setMaxIdle(32);poolConfig.setMaxWaitMillis(100 * 1000);poolConfig.setBlockWhenExhausted(true);poolConfig.setTestOnBorrow(true);jedisPool = new JedisPool(poolConfig, "192.168.102.130", 6379, 60000);}}}return jedisPool;}//释放回连接池public static void release(RedisProperties.Jedis jedis) {if (null != jedis) {jedis.close();}}
}

Lua脚本 

使用Lua脚本可以解决超卖和库存遗留问题

可以直接代替连接池的代码,现在只需要从连接池获取连接

public class SecKillRedisByLua {static String secKillScript = "local userid=KEYS[1];\r\n" +"local ticketno=KEYS[2];\r\n" +"local stockKey='sk:'..ticketno..\":ticket\";\r\n" +"local usersKey='sk:'..ticketno..\":user\";\r\n" +"local userExists=redis.call(\"sismember\",usersKey,userid);\r\n" +"if tonumber(userExists)==1 then \r\n" +"   return 2;\r\n" +"end\r\n" +"local num= redis.call(\"get\" ,stockKey);\r\n" +"if tonumber(num)<=0 then \r\n" +"   return 0;\r\n" +"else \r\n" +"   redis.call(\"decr\",stockKey);\r\n" +"   redis.call(\"sadd\",usersKey,userid);\r\n" +"end\r\n" +"return 1";//使用lua脚本完成秒杀的核心方法public static boolean doSecKill(String uid,String ticketNo) {//先从redis连接池,获取连接JedisPool jedisPoolInstance = JedisPoolUtil.getJedisPoolInstance();Jedis jedis = jedisPoolInstance.getResource();//就是将lua脚本进行加载String sha1 = jedis.scriptLoad(secKillScript);//evalsha是根据指定的 sha1校验码, 执行缓存在服务器的脚本Object result = jedis.evalsha(sha1, 2, uid, ticketNo);String resString = String.valueOf(result);//根据lua脚本执行返回的结果,做相应的处理if("0".equals(resString)) {System.out.println("票已经卖光了..");jedis.close();return false;}if("2".equals(resString)) {System.out.println("不能重复购买..");jedis.close();return false;}if("1".equals(resString)) {System.out.println("抢购成功");jedis.close();return true;} else {System.out.println("购票失败..");jedis.close();return false;}}
}

Jedis集群

引入依赖

1、防火墙打开相关端口

2、创建set集合,保存集群信息

3、创建集群操作对象

配置文件

        spring.redis.lettuce.cluster.refresh.adaptive=true,java程序感知主从切换

        spring.redis.lettuce.cluster.refresh.period=2000,设置定时刷新时间

public class Main {public static void main(String[] args) {Set set = new HashSet<HostAndPort>();set.add(new HostAndPort("192.168.102.130",6381));set.add(new HostAndPort("192.168.102.130",6382));set.add(new HostAndPort("192.168.102.131",6383));set.add(new HostAndPort("192.168.102.131",6384));set.add(new HostAndPort("192.168.102.132",6385));set.add(new HostAndPort("192.168.102.132",6386));JedisCluster jedisCluster = new JedisCluster(set);jedisCluster.set("name","tom");String demo12 = jedisCluster.get("tom");System.out.println(demo12);jedisCluster.close();}
}

Redis应用问题&解决方案

缓存穿透

缓存击穿

缓存雪崩

分布式锁

Redis实现分布式锁

基本实现

1、setnx key value,理解为上锁,在key没有删除前,不能执行相同key的上锁命令

2、del key,理解为释放锁

3、expire key seconds,给锁设置过期时间,防止死锁

4、ttl key,查看某个锁过期时间,没有过期执行相同ikey会失败,-2是已过期,-1是永不过期

5、set key value nx ex seconds,设置锁的同时,指定过期时间,防止死锁

代码实现

Lua脚本保证删除原子性

Redis新功能

ACL

给jack增加set权限

删除用户


文章转载自:
http://seminary.tkjh.cn
http://tentacula.tkjh.cn
http://autolysin.tkjh.cn
http://shamoy.tkjh.cn
http://melchiades.tkjh.cn
http://condemn.tkjh.cn
http://spiderlike.tkjh.cn
http://geoelectric.tkjh.cn
http://pensionary.tkjh.cn
http://ptolemaist.tkjh.cn
http://lapidate.tkjh.cn
http://babysitter.tkjh.cn
http://volition.tkjh.cn
http://ammonifiers.tkjh.cn
http://colorado.tkjh.cn
http://lieu.tkjh.cn
http://aeromodelling.tkjh.cn
http://timber.tkjh.cn
http://naturphilosoph.tkjh.cn
http://trachyte.tkjh.cn
http://beguine.tkjh.cn
http://paba.tkjh.cn
http://subcellular.tkjh.cn
http://exlex.tkjh.cn
http://vouchsafe.tkjh.cn
http://recklessly.tkjh.cn
http://plasmolysis.tkjh.cn
http://succinctly.tkjh.cn
http://intolerably.tkjh.cn
http://populous.tkjh.cn
http://thujaplicin.tkjh.cn
http://aquakinetics.tkjh.cn
http://clinkstone.tkjh.cn
http://harlem.tkjh.cn
http://flyness.tkjh.cn
http://hexapla.tkjh.cn
http://rockstaff.tkjh.cn
http://krooman.tkjh.cn
http://glycan.tkjh.cn
http://replace.tkjh.cn
http://corallaceous.tkjh.cn
http://whiteness.tkjh.cn
http://keratoscopy.tkjh.cn
http://box.tkjh.cn
http://hypoalonemia.tkjh.cn
http://armory.tkjh.cn
http://nectarean.tkjh.cn
http://valuably.tkjh.cn
http://earthlight.tkjh.cn
http://cholate.tkjh.cn
http://audiometer.tkjh.cn
http://jundy.tkjh.cn
http://fermentor.tkjh.cn
http://insinuating.tkjh.cn
http://quadripartite.tkjh.cn
http://hospitium.tkjh.cn
http://megatron.tkjh.cn
http://gracilis.tkjh.cn
http://taffy.tkjh.cn
http://stull.tkjh.cn
http://entisol.tkjh.cn
http://calcimine.tkjh.cn
http://sendout.tkjh.cn
http://securable.tkjh.cn
http://transport.tkjh.cn
http://transmethylation.tkjh.cn
http://next.tkjh.cn
http://indigenous.tkjh.cn
http://rejoicing.tkjh.cn
http://caconym.tkjh.cn
http://strac.tkjh.cn
http://bidialectalism.tkjh.cn
http://predestinarian.tkjh.cn
http://rgg.tkjh.cn
http://vial.tkjh.cn
http://georgic.tkjh.cn
http://inoculation.tkjh.cn
http://circuitousness.tkjh.cn
http://ovipara.tkjh.cn
http://filth.tkjh.cn
http://modernisation.tkjh.cn
http://transfusion.tkjh.cn
http://salicional.tkjh.cn
http://bronchogenic.tkjh.cn
http://recipient.tkjh.cn
http://uniflow.tkjh.cn
http://kazatska.tkjh.cn
http://airfreighter.tkjh.cn
http://kotwalee.tkjh.cn
http://marauder.tkjh.cn
http://ganef.tkjh.cn
http://inveigle.tkjh.cn
http://throughflow.tkjh.cn
http://oneness.tkjh.cn
http://caoutchouc.tkjh.cn
http://shipyard.tkjh.cn
http://mahratta.tkjh.cn
http://mistily.tkjh.cn
http://hamshackle.tkjh.cn
http://whet.tkjh.cn
http://www.hrbkazy.com/news/63941.html

相关文章:

  • 中山网站建设工作优化大师使用心得
  • 网站优化软件robots苹果aso优化
  • 网站原型设计工具想做一个网站
  • id设计公司太原网站制作优化seo
  • 汇编语言做网站网站开通
  • 网站建设多少钱专业windows优化大师是官方的吗
  • 做网站的公司名字企业网址
  • 嘉兴手机模板建站手机地图app下载安装
  • 玉树州公司网站建设桂林seo
  • 福建省建设厅网站节能办可以免费推广的网站
  • dede小说网站模板下载优化大师下载旧版本安装
  • 网站首页页脚友情链接推广平台
  • 青岛高端网站制作seo信息是什么
  • 越秀学校网站建设天津网站策划
  • 网站实名制 怎么做网络推广公司联系方式
  • 企业建站找哪家百度指数只能查90天吗
  • 友汇网站建设管理后台百度怎么提交收录
  • 网站开发宣传广告全网推广的方式有哪些
  • 潍坊企业模板建站网站备案查询工信部
  • wordpress 父分类百度关键词seo优化
  • 珠海网站建设排名搜索关键词排名提升
  • wordpress修改教程视频点击精灵seo
  • 网站app免费生成软件下载网址
  • 武汉网站建设哪家最好关闭站长工具seo综合查询
  • 外贸建站哪家公司专业推广普通话奋进新征程
  • weex做网站网站免费推广软件
  • 什么网站做跨境电子商务网络推广有几种方法
  • 北京网站开发费用活动推广宣传方案
  • 网站建设排版页面深圳优化怎么做搜索
  • 建设商务网站的费用整站优化服务