分布式锁进化的基本原理我们可以同时去一个地方“占一个洞”,如果被占了,就执行逻辑。否则,您必须等到锁被释放。“站坑”可以到redis,到数据库,到任何一个人人都能访问到的地方。等待可以这样旋转。Phase1publicMap>getCatalogJsonDbWithRedisLock(){//Phase1Booleanlock=stringRedisTemplate.opsForValue().setIfAbsent("lock","111");//获取锁并执行业务if(lock){Map>categoriesDb=getCategoryMap();//删除锁,如果在此之前发生错误或者宕机会导致死锁stringRedisTemplate.delete("lock");返回类别Db;}else{//没有拿到锁,等100ms再试try{Thread.sleep(100);}catch(InterruptedExceptione){e.printStackTrace();}返回getCatalogJsonDbWithRedisLock();}}publicMap>getCategoryMap(){ValueOperationsops=stringRedisTemplate.opsForValue();StringcatalogJson=ops.get("catalogJson");如果(StringUtils.isEmpty(目录Json)){System.out.println("缓存未命中,准备查询数据库...");Map>categoriesDb=getCategoriesDb();字符串toJSONString=JSON.toJSONString(categoriesDb);操作。设置(“catalogJson”,toJSONString);返回类别Db;}System.out.println("缓存命中...");Map>listMap=JSON.parseObject(catalogJson,newTypeReference