{"id":7724,"date":"2024-04-17T07:01:01","date_gmt":"2024-04-16T23:01:01","guid":{"rendered":""},"modified":"2024-04-17T07:01:01","modified_gmt":"2024-04-16T23:01:01","slug":"linux \u5b9a\u65f6\u4efb\u52a1pjp,\u3010redis\u3011\u5206\u5e03\u5f0f\u9501\u5b9e\u73b0\uff0c\u4e0e\u5206\u5e03\u5f0f\u5b9a\u65f6\u4efb\u52a1","status":"publish","type":"post","link":"https:\/\/mushiming.com\/7724.html","title":{"rendered":"linux \u5b9a\u65f6\u4efb\u52a1pjp,\u3010redis\u3011\u5206\u5e03\u5f0f\u9501\u5b9e\u73b0\uff0c\u4e0e\u5206\u5e03\u5f0f\u5b9a\u65f6\u4efb\u52a1"},"content":{"rendered":"
\u5982\u679c\u4f60\u8fd8\u4e0d\u77e5\u9053redis\u7684\u57fa\u672c\u547d\u4ee4\u4e0e\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\uff0c\u8bf7\u770b<\/p>\n
\u5199\u5728\u524d\u9762<\/p>\n
redis\u8fa3\u4e48\u591a\u6570\u636e\u7ed3\u6784\uff0c\u8fd9\u4e48\u591a\u547d\u4ee4\uff0c\u5177\u4f53\u4e00\u70b9\uff0c\u90fd\u53ef\u4ee5\u5e94\u7528\u5728\u4ec0\u4e48\u573a\u666f\u5462\uff1f\u7528\u6765\u89e3\u51b3\u4ec0\u4e48\u5177\u4f53\u7684\u95ee\u9898\uff1f<\/p>\n
\u5206\u5e03\u5f0f\u9501<\/p>\n
redis\u662f\u7f51\u7edc\u5355\u7ebf\u7a0b\u7684\uff0c\u5b83\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u8d1f\u8d23\u63a5\u53d7\u8bf7\u6c42\uff0c\u8fd9\u4e2a\u7279\u6027\u5373\u964d\u4f4e\u4e86redis\u672c\u8eab\u7684\u5f00\u53d1\u6210\u672c\uff0c\u4e5f\u63d0\u9ad8\u4e86redis\u7684\u53ef\u7528\u6027\u3002<\/p>\n
\u5206\u5e03\u5f0f\u73af\u5883\u4e0b\uff0c\u6570\u636e\u4e00\u81f4\u6027\u95ee\u9898\u4e00\u76f4\u662f\u4e00\u4e2a\u6bd4\u8f83\u91cd\u8981\u7684\u8bdd\u9898\uff0c\u5206\u5e03\u5f0f\u4e0e\u5355\u673a\u60c5\u51b5\u4e0b\u6700\u5927\u7684\u4e0d\u540c\u5728\u4e8e\u5176\u4e0d\u662f\u591a\u7ebf\u7a0b\u800c\u662f\u591a\u8fdb\u7a0b\u3002<\/p>\n
\u591a\u7ebf\u7a0b\u7531\u4e8e\u53ef\u4ee5\u5171\u4eab\u5806\u5185\u5b58\uff0c\u56e0\u6b64\u53ef\u4ee5\u7b80\u5355\u7684\u91c7\u53d6\u5185\u5b58\u4f5c\u4e3a\u6807\u8bb0\u5b58\u50a8\u4f4d\u7f6e\uff0c\u4f8b\u5982cas\uff0cjava\u7684synchronize\u3002\u800c\u8fdb\u7a0b\u4e4b\u95f4\u53ef\u80fd\u4e0d\u5728\u540c\u4e00\u53f0\u7269\u7406\u673a\u4e0a\uff0c\u56e0\u6b64\u9700\u8981\u5c06\u6807\u8bb0\u5b58\u50a8\u5728\u4e00\u4e2a\u6240\u6709\u8fdb\u7a0b\u90fd\u80fd\u770b\u5230\u7684\u5730\u65b9\u3002<\/p>\n
\u5e38\u89c1\u7684\u573a\u666f\uff0c\u79d2\u6740\u573a\u666f\u4e2d\u7684\u5e93\u5b58\u8d85\u5356\u95ee\u9898\u3001\u591a\u673a\u5b9a\u65f6\u4efb\u52a1\u7684\u5e76\u53d1\u6267\u884c\u95ee\u9898\u7b49\u3002<\/p>\n
\u5e93\u5b58\u8d85\u5356\u95ee\u9898<\/p>\n
\u5047\u5982\u8ba2\u5355\u670d\u52a1\u90e8\u7f72\u4e86\u591a\u4e2a\u5b9e\u4f8b\u3002<\/p>\n
\u73b0\u5728\u505a\u4e00\u4e2a\u5546\u54c1\u79d2\u6740\u6d3b\u52a8\uff0c\u5546\u54c1\u4e00\u5171\u53ea\u67092\u4e2a\uff0c\u540c\u65f6\u8d2d\u4e70\u7684\u7528\u6237\u5219\u53ef\u80fd\u6709\u51e0\u5343\u4e0a\u4e07\u3002<\/p>\n
\u7406\u60f3\u72b6\u6001\u4e0b\u7b2c\u4e00\u4e2a\u548c\u7b2c\u4e8c\u4e2a\u7528\u6237\u80fd\u8d2d\u4e70\u6210\u529f\uff0c\u5176\u4ed6\u7528\u6237\u63d0\u793a\u8d2d\u4e70\u5931\u8d25\uff0c<\/p>\n
\u5b9e\u9645\u53ef\u80fd\u51fa\u73b0\u7684\u60c5\u51b5\u662f\uff0c\u591a\u4e2a\u7528\u6237\u90fd\u540c\u65f6\u67e5\u5230\u5546\u54c1\u8fd8\u6ca1\u5356\u5b8c\uff0c\u7b2c\u4e00\u4e2a\u7528\u6237\u4e70\u5230\uff0c\u66f4\u65b0\u5e93\u5b58\u4e4b\u524d\uff0c\u7b2c\u4e8c\u4e2a\u7528\u6237\u53c8\u4e0b\u4e86\u8ba2\u5355\uff0c\u5bfc\u81f4\u51fa\u9519\u3002<\/p>\n
\u4e0b\u9762\u7528java\u4ee3\u7801\u505a\u4e00\u4e2a\u6f14\u793a\uff1a<\/p>\n
java\u5b9e\u4f8b\u90fd\u53ef\u4ee5\u88ab\u6b63\u5e38\u8fd0\u884c\u5728jdk1.8+\uff0c\u4f7f\u7528jedis\u8fde\u63a5redis\u5b9e\u4f8b<\/p>\n
import redis.clients.jedis.Jedis;<\/p>\n
import redis.clients.jedis.JedisPool;<\/p>\n
import redis.clients.jedis.JedisPoolConfig;<\/p>\n
\/**<\/p>\n
* JedisPool\u8fde\u63a5<\/p>\n
* @author taifeng zhang<\/p>\n
* *\/<\/p>\n
public class JedisPoolConnect {
\n <\/p>\n
public static JedisPool jedispool;<\/p>\n
\/**<\/p>\n
* \u8fde\u63a5\u5e76\u8fd4\u56dejedis\u5b9e\u4f8b<\/p>\n
* *\/<\/p>\n
public static Jedis connectJedis () {
\n <\/p>\n
if (jedispool == null) {
\n <\/p>\n
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();<\/p>\n
jedisPoolConfig.setMinIdle(1);<\/p>\n
jedisPoolConfig.setMaxIdle(10);<\/p>\n
jedisPoolConfig.setTestOnBorrow(true);<\/p>\n
jedispool = new JedisPool(jedisPoolConfig, \"127.0.0.1\", 6379);<\/p>\n
}<\/p>\n
return jedispool.getResource();<\/p>\n
}<\/p>\n
}<\/p>\n
import redis.clients.jedis.*;<\/p>\n
import redis.clients.jedis.Jedis;<\/p>\n
\/**<\/p>\n
* \u4e00\u4e2a\u7b80\u5355\u7684\u8d85\u5356\u6f14\u793a\u7a0b\u5e8f<\/p>\n
* *\/<\/p>\n
public class MarketWrong {
\n <\/p>\n
public static String GOODS_LEN_KEY = \"jedis:market:demo\";<\/p>\n
private final Integer DECR_THREAD_LEN = 16;<\/p>\n
public void superMarket () {
\n <\/p>\n
\/\/ \u5f00\u7ebf\u7a0b\u53bb\u51cf\u5e93\u5b58<\/p>\n
int i = DECR_THREAD_LEN;<\/p>\n
while (i > 0) {
\n <\/p>\n
new Thread(() -> {
\n <\/p>\n
boolean hasGoods = true;<\/p>\n
while (hasGoods) { \/\/ \u5f53\u5e93\u5b58\u5927\u4e8e0\u7684\u65f6\u5019<\/p>\n
int goodsLen = getGoodsLen();<\/p>\n
if (goodsLen > 0) {
\n <\/p>\n
decrGoodsLen(); \/\/ \u4e00\u822c\u8fdb\u6765\u4e4b\u540e\u5c31\u76f4\u63a5\u51cf\u53bb\u5e93\u5b58\u4e86<\/p>\n
System.out.println(\"\u73b0\u5728\u5e93\u5b58\u4e3a\" + getGoodsLen());<\/p>\n
try {
\n <\/p>\n
Thread.sleep(100); \/\/\u6a21\u62df\u4e2d\u95f4\u5904\u7406\u6d41\u7a0b<\/p>\n
} catch (Exception e) {
\n <\/p>\n
System.out.println(\"\u6267\u884c\u51cf\u5e93\u5b58\u9519\u8bef\" + e.getMessage() + e.getLocalizedMessage() + e.getStackTrace());<\/p>\n
} finally {
\n <\/p>\n
\/\/ \u6700\u540e\u903b\u8f91<\/p>\n
}<\/p>\n
} else {
\n <\/p>\n
System.out.println(\"======\u5356\u5b8c\u5566=======\");<\/p>\n
hasGoods = false;<\/p>\n
}<\/p>\n
}<\/p>\n
}).start();<\/p>\n
i--;<\/p>\n
}<\/p>\n
}<\/p>\n
public void setGoodsLen (Integer len) {
\n <\/p>\n
Jedis jedis = JedisPoolConnect.connectJedis();<\/p>\n
try {
\n <\/p>\n
jedis.set(GOODS_LEN_KEY, String.valueOf(len));<\/p>\n
} finally {
\n <\/p>\n
jedis.close();<\/p>\n
}<\/p>\n
}<\/p>\n
private Integer getGoodsLen () {
\n <\/p>\n
Jedis jedis = JedisPoolConnect.connectJedis();<\/p>\n
try {
\n <\/p>\n
String val = jedis.get(GOODS_LEN_KEY);<\/p>\n
if (val != null) {
\n <\/p>\n
return Integer.parseInt(val);<\/p>\n
}<\/p>\n
} finally {
\n <\/p>\n
jedis.close();<\/p>\n
}<\/p>\n
return 0;<\/p>\n
}<\/p>\n
private void decrGoodsLen () {
\n <\/p>\n
Jedis jedis = JedisPoolConnect.connectJedis();<\/p>\n
try {
\n <\/p>\n
\/\/ \u5e93\u5b58\u51cf1<\/p>\n
jedis.decr(GOODS_LEN_KEY);<\/p>\n
} finally {
\n <\/p>\n
jedis.close();<\/p>\n
}<\/p>\n
}<\/p>\n
}<\/p>\n
\u7528junit\u6d4b\u8bd5\u4e0a\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n
import org.junit.Test;<\/p>\n
import org.springframework.boot.test.context.SpringBootTest;<\/p>\n
@SpringBootTest<\/p>\n
public class MarketWrongTests {
\n <\/p>\n
\/**<\/p>\n
* \u6d4b\u8bd5\u8d85\u5356\u5c0f\u7a0b\u5e8f<\/p>\n
*\/<\/p>\n
@Test<\/p>\n
public void superMarket () throws Exception {
\n <\/p>\n
MarketWrong marketWrong = new MarketWrong();<\/p>\n
\/\/ \u8fd9\u6b21\u5c31\u5356500\u4ef6\u5427<\/p>\n
marketWrong.setGoodsLen(500);<\/p>\n
marketWrong.superMarket();<\/p>\n
Thread.sleep(60000); \/\/ \u5356\u4e00\u5206\u949f<\/p>\n
}<\/p>\n
}<\/p>\n
\u8fd0\u884c\u8f93\u51fa\uff0c\u6bcf\u6b21\u5e93\u5b58\u90fd\u4f1a\u53d8\u4e3a\u8d1f\u6570\uff0c\u5f00\u4e8616\u4e2a\u7ebf\u7a0b\u540c\u65f6\u4e70\u4e1c\u897f\uff1a<\/p>\n
\/\/ \u7701\u7565\u4e86\u51e0\u4e07\u884c<\/p>\n
\u73b0\u5728\u5e93\u5b58\u4e3a8<\/p>\n
\u73b0\u5728\u5e93\u5b58\u4e3a8<\/p>\n
\u73b0\u5728\u5e93\u5b58\u4e3a4<\/p>\n
\u73b0\u5728\u5e93\u5b58\u4e3a4<\/p>\n
\u73b0\u5728\u5e93\u5b58\u4e3a4<\/p>\n
\u73b0\u5728\u5e93\u5b58\u4e3a4<\/p>\n
\u73b0\u5728\u5e93\u5b58\u4e3a3<\/p>\n
\u73b0\u5728\u5e93\u5b58\u4e3a-5<\/p>\n
\u73b0\u5728\u5e93\u5b58\u4e3a-5<\/p>\n
\u73b0\u5728\u5e93\u5b58\u4e3a-5<\/p>\n
\u73b0\u5728\u5e93\u5b58\u4e3a-5<\/p>\n
\u73b0\u5728\u5e93\u5b58\u4e3a-5<\/p>\n
\u73b0\u5728\u5e93\u5b58\u4e3a-5<\/p>\n
\u73b0\u5728\u5e93\u5b58\u4e3a-5<\/p>\n
\u73b0\u5728\u5e93\u5b58\u4e3a-5<\/p>\n
======\u5356\u5b8c\u5566=======<\/p>\n
======\u5356\u5b8c\u5566=======<\/p>\n
======\u5356\u5b8c\u5566=======<\/p>\n
\u4e0a\u9762\u7684\u4ee3\u7801\u793a\u4f8b\u4e2d\uff0c\u5e93\u5b58\u6570\u636e\u662f\u5171\u4eab\u8d44\u6e90(\u5b58\u5230redis\u4e86\uff0c\u76f8\u5f53\u4e8e\u6570\u636e\u5e93)\uff0c\u9762\u5bf9\u9ad8\u5e76\u53d1\u60c5\u5f62\uff0c\u9700\u8981\u4fdd\u8bc1\u5bf9\u8d44\u6e90\u7684\u8bbf\u95ee\u6b21\u5e8f\u3002\u5728\u5355\u673a\u73af\u5883Java\u63d0\u4f9b\u57fa\u4e8e\u5185\u5b58\u7684\u9501\u6765\u5904\u7406\u5e76\u53d1\u95ee\u9898\uff0c\u4f46\u662f\u8fd9\u4e9bAPI\u5728\u5206\u5e03\u5f0f\u573a\u666f\u4e2d\u5c31\u65e0\u80fd\u4e3a\u529b\u4e86\u3002\u4e5f\u5c31\u662f\u8bf4\u5355\u7eaf\u7684\u5185\u5b58\u9501\u5e76\u4e0d\u80fd\u63d0\u4f9b\u8fd9\u79cd\u591a\u673a\u5668\u5e76\u53d1\u670d\u52a1\u7684\u80fd\u529b\u3002\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u7531\u4e8e\u5206\u5e03\u5f0f\u7cfb\u7edf\u7684\u5206\u5e03\u6027\uff0c\u5373\u591a\u7ebf\u7a0b\u548c\u591a\u8fdb\u7a0b\u5e76\u4e14\u5206\u5e03\u5728\u4e0d\u540c\u673a\u5668\u4e2d\uff0csynchronized\u548clock\u8fd9\u4e24\u79cd\u9501\u5c06\u5931\u53bb\u539f\u6709\u9501\u7684\u6548\u679c\uff0c\u9700\u8981\u6211\u4eec\u81ea\u5df1\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501\u3002<\/p>\n
\u4e5f\u5c31\u662f\u8bf4\u5e93\u5b58\u7684\u9012\u51cf\u5fc5\u987b\u662f\u987a\u5e8f\u7684<\/p>\n
\u5e38\u89c1\u7684\u9501\u65b9\u6848\u5982\u4e0b\uff1a<\/p>\n
\u57fa\u4e8e\u6570\u636e\u5e93\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501 \u57fa\u4e8e\u7f13\u5b58\uff0c\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501\uff0c\u5982redis \u57fa\u4e8eZookeeper\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501<\/p>\n
\u4e0b\u9762\u5b9e\u73b0\u4e00\u4e2aredis\u7684\u9501\uff0c\u5256\u6790\u4e00\u628aredis\u662f\u5982\u4f55\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501\u7684\uff1a<\/p>\n
import redis.clients.jedis.Jedis;<\/p>\n
import redis.clients.jedis.JedisCluster;<\/p>\n
import redis.clients.jedis.JedisPool;<\/p>\n
import redis.clients.jedis.JedisPoolConfig;<\/p>\n
import redis.clients.jedis.params.SetParams;<\/p>\n
import java.util.ArrayList;<\/p>\n
import java.util.Arrays;<\/p>\n
\/**<\/p>\n
* redis\u9501\u5b9e\u73b0<\/p>\n
* @author taifeng zhang<\/p>\n
* *\/<\/p>\n
public class RedisLock {
\n <\/p>\n
private static String REDIS_LOCK_KEY = \"redis:lock:key\";<\/p>\n
\/**<\/p>\n
*\u8bbe\u7f6elockkey<\/p>\n
* *\/<\/p>\n
public static void setRedisLockKey(String redisLockKey) {
\n <\/p>\n
REDIS_LOCK_KEY = redisLockKey;<\/p>\n
}<\/p>\n
\/**<\/p>\n
* \u5c1d\u8bd5\u83b7\u53d6\u9501<\/p>\n
* @param ov \u53ef\u4ee5\u6307\u5b9a\u4e00\u4e2a\u9501\u6807\u8bc6\uff0c\u9501\u7684\u552f\u4e00\u503c\uff0c\u533a\u5206\u6bcf\u4e2a\u9501\u7684\u6240\u6709\u8005\u8eab\u4efd<\/p>\n
* @param timeout \u83b7\u53d6\u9501\u7684\u8d85\u65f6\u65f6\u95f4<\/p>\n
* *\/<\/p>\n
public boolean tryLock (String ov, int timeout) {
\n <\/p>\n
Jedis jedis = JedisPoolConnect.connectJedis();<\/p>\n
try {
\n <\/p>\n
\/\/ set nx ex<\/p>\n
SetParams setParams = new SetParams();<\/p>\n
setParams.nx();<\/p>\n
setParams.ex(timeout);<\/p>\n
Object val = jedis.set(REDIS_LOCK_KEY, ov, setParams); \/\/ set [key] nx ex [timeout]<\/p>\n
return val != null;<\/p>\n
} finally {
\n <\/p>\n
jedis.close();<\/p>\n
}<\/p>\n
}<\/p>\n
\/**<\/p>\n
* \u4f7f\u7528lua\u811a\u672c\u91ca\u653e\u9501<\/p>\n
* @param ov \u91ca\u653e\u4e4b\u524d\u5148\u786e\u5b9a\u89e3\u9501\u4eba\u7684\u8eab\u4efd\uff0c\u6240\u4ee5\u8981\u7528\u5230lua\u7684\u539f\u5b50\u7279\u6027<\/p>\n
* *\/<\/p>\n
public boolean tryUnlock (String ov) {
\n <\/p>\n
Jedis jedis = JedisPoolConnect.connectJedis();<\/p>\n
try {
\n <\/p>\n
String DISTRIBUTE_LOCK_SCRIPT_UNLOCK_VAL = \"if (redis.call('get', KEYS[1]) == ARGV[1]) then return redis.call('del', KEYS[1]) else return 0 end\";<\/p>\n
String sha1 = jedis.scriptLoad(DISTRIBUTE_LOCK_SCRIPT_UNLOCK_VAL);<\/p>\n
String[] keys = {REDIS_LOCK_KEY};<\/p>\n
String[] args = {ov};<\/p>\n
Integer val = Integer.parseInt(jedis.evalsha(sha1,new ArrayList<>(Arrays.asList(keys)),new ArrayList<>(Arrays.asList(args))).toString());<\/p>\n
return val > 0;<\/p>\n
} finally {
\n <\/p>\n
jedis.close();<\/p>\n
}<\/p>\n
}<\/p>\n
}<\/p>\n
\u5b9e\u73b0\u539f\u5219\u6709\u51e0\u70b9\uff1a 1\u3001\u539f\u5b50\u76f8\u5173\u64cd\u4f5c\u6b65\u9aa4\u5fc5\u987b\u5168\u90e8\u5305\u62ec\u5728\u9501\u5185 2\u3001\u6bcf\u4e2a\u9501\u90fd\u6709\u4e00\u4e2a\u552f\u4e00\u7684value\uff0c\u6807\u8bc6\u52a0\u9501\u4eba\u7684\u8eab\u4efd\u3002 3\u3001\u52a0\u8d85\u65f6\u65f6\u95f4\u9632\u6b62\u6b7b\u9501 (\u8d85\u65f6\u65f6\u95f4\u8981\u5408\u7406)<\/p>\n
\u52a0\u9501\u4ee3\u7801\u89e3\u6790<\/p>\n
\/**<\/p>\n
* \u5c1d\u8bd5\u83b7\u53d6\u9501<\/p>\n
* @param ov \u53ef\u4ee5\u6307\u5b9a\u4e00\u4e2a\u9501\u6807\u8bc6\uff0c\u9501\u7684\u552f\u4e00\u503c\uff0c\u533a\u5206\u6bcf\u4e2a\u9501\u7684\u6240\u6709\u8005\u8eab\u4efd<\/p>\n
* @param timeout \u83b7\u53d6\u9501\u7684\u8d85\u65f6\u65f6\u95f4<\/p>\n
* *\/<\/p>\n
public boolean tryLock (String ov, int timeout) {
\n <\/p>\n
Jedis jedis = JedisPoolConnect.connectJedis();<\/p>\n
try {
\n <\/p>\n
\/\/ set nx ex<\/p>\n
SetParams setParams = new SetParams();<\/p>\n
setParams.nx();<\/p>\n
setParams.ex(timeout);<\/p>\n
Object val = jedis.set(REDIS_LOCK_KEY, ov, setParams); \/\/ \u7528 set [key] nx ex [timeout] \u547d\u4ee4\u6a21\u62df\u52a0\u9501<\/p>\n
return val != null;<\/p>\n
} finally {
\n <\/p>\n
jedis.close();<\/p>\n
}<\/p>\n
}<\/p>\n
\u52a0\u9501\u7684\u4ee3\u7801\u5f88\u7b80\u5355\uff0c\u5176\u5b9e\u5c31\u662f\u5229\u7528redis\u547d\u4ee4 set [key] nx ex [timeout] \u7684\u7279\u6027\uff0c\u5df2\u6709\u503c\u7684\u65f6\u5019\u8fd4\u56de\u503c\u4e3anil\uff0c\u5982\u679c\u6267\u884c\u8fd9\u4e2a\u547d\u4ee4\u7684\u7ed3\u679c\u662fnull\uff0c\u90a3\u5c31\u53ef\u4ee5\u8ba4\u4e3a\u8d44\u6e90\u5df2\u7ecf\u88ab\u4e0a\u9501<\/p>\n
\u540c\u65f6\uff0cset\u4e5f\u5c06REDIS_LOCK_KEY\u8bbe\u7f6e\u4e3a\u4e00\u4e2a\u552f\u4e00\u503c\uff0c\u5728\u89e3\u9501\u7684\u65f6\u5019\u6216\u8005\u9501\u91cd\u5165\u7684\u65f6\u5019\u5224\u65ad\u8eab\u4efd\u4f7f\u7528\u3002<\/p>\n
\u89e3\u9501\u4ee3\u7801\u89e3\u6790<\/p>\n
\/**<\/p>\n
* \u4f7f\u7528lua\u811a\u672c\u91ca\u653e\u9501<\/p>\n
* @param ov \u91ca\u653e\u4e4b\u524d\u5148\u786e\u5b9a\u89e3\u9501\u4eba\u7684\u8eab\u4efd\uff0c\u6240\u4ee5\u8981\u7528\u5230lua\u7684\u539f\u5b50\u7279\u6027<\/p>\n
* *\/<\/p>\n
public boolean tryUnlock (String ov) {
\n <\/p>\n
Jedis jedis = JedisPoolConnect.connectJedis();<\/p>\n
try {
\n <\/p>\n
String DISTRIBUTE_LOCK_SCRIPT_UNLOCK_VAL = \"if (redis.call('get', KEYS[1]) == ARGV[1]) then return redis.call('del', KEYS[1]) else return 0 end\";<\/p>\n
String sha1 = jedis.scriptLoad(DISTRIBUTE_LOCK_SCRIPT_UNLOCK_VAL);<\/p>\n
String[] keys = {REDIS_LOCK_KEY};<\/p>\n
String[] args = {ov};<\/p>\n
Integer val = Integer.parseInt(jedis.evalsha(sha1,new ArrayList<>(Arrays.asList(keys)),new ArrayList<>(Arrays.asList(args))).toString());<\/p>\n
return val > 0;<\/p>\n
} finally {
\n <\/p>\n
jedis.close();<\/p>\n
}<\/p>\n
}<\/p>\n
\u89e3\u9501\u4ee3\u7801\u7684\u7cbe\u9ad3\u662f\u8fd9\u53e5lua\u811a\u672c\uff1a<\/p>\n
if (redis.call('get', KEYS[1]) == ARGV[1]) then<\/p>\n
return redis.call('del', KEYS[1])<\/p>\n
else return 0<\/p>\n
\u4eceredis\u8bfb\u53d6key\u7684\u503c\uff0c\u5982\u679c\u5b83\u7b49\u4e8e\u4f20\u5165\u7684\u552f\u4e00key\uff0c\u5219\u53ef\u4ee5\u91ca\u653e\u9501\uff0c\u5426\u5219\u8fd4\u56de0<\/p>\n
\u4e3a\u4ec0\u4e48\u8981\u68c0\u67e5\u552f\u4e00key\u518d\u91ca\u653e\u9501\u5462\uff1f\u4e3b\u8981\u662f\u4e3a\u4e86\u8fd9\u4e48\u4e00\u4e2a\u573a\u666f\uff1a<\/p>\n
A\u7528\u6237\u6765\u83b7\u53d6\u4e86\u9501<\/p>\n
B\u7528\u6237\u6765\u83b7\u53d6\u9501\uff0c\u9501\u5df2\u7ecf\u88aba\u62ff\u8d70\u4e86\uff0c\u7b49\u5f85\u9501<\/p>\n
A\u7528\u6237\u53ef\u80fd\u56e0\u4e3a\u7a81\u7136\u53d1\u751f\u7f51\u7edc\u5ef6\u8fdf\uff0c\u8d85\u8fc7\u4e86\u8d85\u65f6\u65f6\u95f4\uff0c\u8fd9\u65f6\u5019\u9501\u56e0\u4e3a\u8d85\u65f6\u81ea\u52a8\u91ca\u653e\u4e86\u3002<\/p>\n
B\u7528\u6237\u83b7\u53d6\u4e86\u9501<\/p>\n
A\u7528\u6237\u8fd9\u65f6\u5019\u7f51\u7edc\u6062\u590d\u4e86\u3002\u3002\u3002\u8fd9\u65f6\u5019A\u7528\u6237\u8981\u91ca\u653e\u9501\uff0c\u5982\u679c\u91ca\u653e\u6210\u529f\u5c31\u4f1a\u5bfc\u81f4\u8fde\u9501\u53cd\u5e94\uff0cb\u7528\u6237\u88ab\u89e3\u9501\uff0cb\u53c8\u53ef\u80fd\u53bb\u89e3\u9501c<\/p>\n
\u6240\u4ee5\u6bcf\u6b21\u52a0\u9501\u89e3\u9501\u90fd\u9700\u8981\u9a8c\u8bc1\u83b7\u53d6\u9501\u7684\u7528\u6237\u8eab\u4efd\uff0c\u4e00\u822c\u5b58\u653e\u5728key\u7684value\u91cc\u9762\uff0c\u5728\u91ca\u653e\u9501\u4e4b\u524d\u5148\u68c0\u67e5\uff0c\u4e5f\u5c31\u662f check and set<\/p>\n
\u9501\u7684\u91cd\u5165<\/p>\n
\u4e0a\u9762\u8c08\u5230\uff0c\u6211\u4eec\u8bb0\u5f55\u4e86\u6bcf\u4e2a\u9501\u7684\u7528\u6237\u8eab\u4efd\uff0c\u90a3\u662f\u4e0d\u662f\u540c\u4e00\u4e2a\u7528\u6237\u4e00\u6b21\u64cd\u4f5c\u9700\u8981\u4e24\u6b21\u9501\uff0c\u662f\u53ef\u4ee5\u91cd\u7528\u7684\u5462\uff1f<\/p>\n
\u7b54\u6848\u662fok\u7684<\/p>\n
\u6211\u4eec\u53ef\u4ee5\u5728trylock\u4e2d\u52a0\u4e00\u4e2alua\u811a\u672c\u7528\u6765\u5148check \u518d set\uff0c\u5982\u679c\u5224\u65adcheck\u4e0e\u7528\u6237\u7b26\u5408\uff0c\u5219\u76f4\u63a5\u8fd4\u56detrue\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n
public boolean tryLock (String ov, int timeout) {
\n <\/p>\n
Jedis jedis = JedisPoolConnect.connectJedis();<\/p>\n
try {
\n <\/p>\n
\/\/ \u52a0\u4e0a\u9501\u7684\u91cd\u5165\u7279\u6027<\/p>\n
String DISTRIBUTE_LOCK_SCRIPT_UNLOCK_VAL = \"if (redis.call('get', KEYS[1]) == ARGV[1]) then return 1 else return 0 end\"; \/\/ \u5982\u679c\u5f53\u524d\u9501\u7684\u503c\u7b49\u4e8eov\u7684\u8bdd\uff0c\u8ba4\u4e3a\u6765\u83b7\u53d6\u9501\u7684\u8fd8\u662f\u540c\u4e00\u4e2a\u4eba<\/p>\n
String sha1 = jedis.scriptLoad(DISTRIBUTE_LOCK_SCRIPT_UNLOCK_VAL);<\/p>\n
String[] keys = {REDIS_LOCK_KEY};<\/p>\n
String[] args = {ov};<\/p>\n
Integer val = Integer.parseInt(jedis.evalsha(sha1,new ArrayList<>(Arrays.asList(keys)),new ArrayList<>(Arrays.asList(args))).toString());<\/p>\n
if (val > 0) { \/\/ \u5224\u5b9a\u6210\u529f\u540e\uff0c\u9501\u5c31\u91cd\u5165\u4e86\uff0c\u5373\u65e0\u9700\u7b2c\u4e8c\u6b21\u83b7\u53d6\u9501<\/p>\n
return true;<\/p>\n
}<\/p>\n
\/\/ set nx ex<\/p>\n
SetParams setParams = new SetParams();<\/p>\n
setParams.nx();<\/p>\n
setParams.ex(timeout);<\/p>\n
Object val = jedis.set(REDIS_LOCK_KEY, ov, setParams); \/\/ set [key] nx ex [timeout]<\/p>\n
return val != null;<\/p>\n
} finally {
\n <\/p>\n
jedis.close();<\/p>\n
}<\/p>\n
}<\/p>\n
\u6700\u540e\u6211\u4eec\u770b\u770b\u5173\u4e8e\u8d85\u5356\u95ee\u9898\uff0c\u6211\u4eec\u5c06\u4ee3\u7801\u52a0\u4e0a\u9501 \u6ce8\u610f\u4e24\u4e2atodo\u7684\u5730\u65b9\u3002<\/p>\n
import redis.clients.jedis.*;<\/p>\n
import redis.clients.jedis.Jedis;<\/p>\n
public class MarketWrong {
\n <\/p>\n
public static String GOODS_LEN_KEY = \"jedis:market:demo\";<\/p>\n
private final Integer DECR_THREAD_LEN = 16;<\/p>\n
RedisLock redisLock = new RedisLock();<\/p>\n
public void superMarket () {
\n <\/p>\n
\/\/ \u5f00\u7ebf\u7a0b\u53bb\u51cf\u5e93\u5b58<\/p>\n
int i = DECR_THREAD_LEN;<\/p>\n
while (i > 0) {
\n <\/p>\n
int whilekey = i;<\/p>\n
new Thread(() -> {
\n <\/p>\n
int n;<\/p>\n
int j = 0;<\/p>\n
boolean hasGoods = true;<\/p>\n
while (hasGoods) { \/\/ \u5f53\u5e93\u5b58\u5927\u4e8e0\u7684\u65f6\u5019<\/p>\n
String ov = whilekey + \"-\" + j;<\/p>\n
\/\/ todo \u52a0\u9501<\/p>\n
while (!redisLock.tryLock(ov, 20)) { \/\/ \u5982\u679c\u83b7\u53d6\u4e0d\u5230\u9501\u5c31\u7b49\u5f85<\/p>\n
}<\/p>\n
int goodsLen = getGoodsLen();<\/p>\n
if (goodsLen > 0) {
\n <\/p>\n
decrGoodsLen(); \/\/ \u4e00\u822c\u8fdb\u6765\u4e4b\u540e\u5c31\u76f4\u63a5\u51cf\u53bb\u5e93\u5b58\u4e86<\/p>\n
System.out.println(\"\u73b0\u5728\u5e93\u5b58\u4e3a\" + getGoodsLen());<\/p>\n
redisLock.tryUnlock(ov); \/\/ todo \u89e3\u9664\u9501<\/p>\n
try {
\n <\/p>\n
Thread.sleep(100); \/\/\u6a21\u62df\u4e2d\u95f4\u5904\u7406\u6d41\u7a0b<\/p>\n
} catch (Exception e) {
\n <\/p>\n
System.out.println(\"\u6267\u884c\u51cf\u5e93\u5b58\u9519\u8bef\" + e.getMessage() + e.getLocalizedMessage() + e.getStackTrace());<\/p>\n
} finally {
\n <\/p>\n
\/\/ \u6700\u540e\u903b\u8f91<\/p>\n
}<\/p>\n
} else {
\n <\/p>\n
System.out.println(\"======\u5356\u5b8c\u5566=======\");<\/p>\n
hasGoods = false;<\/p>\n
}<\/p>\n
j++; \/\/ \u9700\u8981\u8fd9\u4e2a\u7528\u6765\u751f\u6210ov\uff0c\u76f8\u5f53\u4e8e\u6a21\u62df\u6bcf\u4e00\u4e2a\u4e70\u5bb6\u7684id<\/p>\n
}<\/p>\n
}).start();<\/p>\n
i--;<\/p>\n
}<\/p>\n
}<\/p>\n
\/**<\/p>\n
* \u4e00\u4e2a\u7b80\u5355\u7684\u8d85\u5356\u6f14\u793a\u7a0b\u5e8f<\/p>\n
* *\/<\/p>\n
public void setGoodsLen (Integer len) {
\n <\/p>\n
Jedis jedis = JedisPoolConnect.connectJedis();<\/p>\n
try {
\n <\/p>\n
jedis.set(GOODS_LEN_KEY, String.valueOf(len));<\/p>\n
} finally {
\n <\/p>\n
jedis.close();<\/p>\n
}<\/p>\n
}<\/p>\n
private Integer getGoodsLen () {
\n <\/p>\n
Jedis jedis = JedisPoolConnect.connectJedis();<\/p>\n
try {
\n <\/p>\n
String val = jedis.get(GOODS_LEN_KEY);<\/p>\n
if (val != null) {
\n <\/p>\n
return Integer.parseInt(val);<\/p>\n
}<\/p>\n
} finally {
\n <\/p>\n
jedis.close();<\/p>\n
}<\/p>\n
return 0;<\/p>\n
}<\/p>\n
private void decrGoodsLen () {
\n <\/p>\n
Jedis jedis = JedisPoolConnect.connectJedis();<\/p>\n
try {
\n <\/p>\n
\/\/ \u5e93\u5b58\u51cf1<\/p>\n
jedis.decr(GOODS_LEN_KEY);<\/p>\n
} finally {
\n <\/p>\n
jedis.close();<\/p>\n
}<\/p>\n
}<\/p>\n
}<\/p>\n
\u52a0\u4e0a\u9501\u4e4b\u540e\u518d\u6d4b\u8bd5\uff0c\u8d85\u5356\u95ee\u9898\u5df2\u89e3\u51b3,\u6ce8\u610f\u73b0\u5728\u7684\u8f93\u51fa\u662f\u7ebf\u6027\u9012\u589e\u7684\uff0c\u56e0\u4e3a\u5f00\u7ebf\u7a0b\u7684\u6a21\u62df\u65b9\u5f0f\u5c31\u662f\u5e76\u53d1\u5904\u7406\uff0c\u6bcf\u6b2116\u4e2a\u7ebf\u7a0b\u51e0\u4e4e\u662f\u540c\u65f6\u8fdb\u884c\u7684\uff0c\u6240\u4ee5\u5728\u6ca1\u6709\u9501\u7684\u65f6\u5019\uff0c\u5e76\u53d1\u8bfb\u53d6\u7684goodslen\u5f88\u6709\u53ef\u80fd\u90fd\u662f16\u4e2a\u7ebf\u7a0b\u4e00\u6837\u7684\u3002<\/p>\n
\u6240\u4ee5redis\u7684\u8fd9\u4e2a\u9501\u7684\u5b9e\u73b0\u4e5f\u53eb\uff1a \u5206\u5e03\u5f0f\u4e92\u65a5\u9501<\/p>\n
\u73b0\u5728\u5e93\u5b58\u4e3a8<\/p>\n
\u73b0\u5728\u5e93\u5b58\u4e3a7<\/p>\n
\u73b0\u5728\u5e93\u5b58\u4e3a6<\/p>\n
\u73b0\u5728\u5e93\u5b58\u4e3a5<\/p>\n
\u73b0\u5728\u5e93\u5b58\u4e3a4<\/p>\n
\u73b0\u5728\u5e93\u5b58\u4e3a3<\/p>\n
\u73b0\u5728\u5e93\u5b58\u4e3a2<\/p>\n
\u73b0\u5728\u5e93\u5b58\u4e3a1<\/p>\n
\u73b0\u5728\u5e93\u5b58\u4e3a0<\/p>\n
======\u5356\u5b8c\u5566=======<\/p>\n
======\u5356\u5b8c\u5566=======<\/p>\n
======\u5356\u5b8c\u5566=======<\/p>\n
redis\u5b9e\u73b0\u7684\u5206\u5e03\u5f0f\u4e92\u65a5\u9501\u5e76\u4e0d\u5b8c\u7f8e\uff0c\u4f46\u5728\u5927\u591a\u6570\u5e94\u7528\u573a\u666f\u4e0b\u591f\u7528\u4e86\uff0c\u53e6\u5916\u8fd8\u53ef\u4ee5\u4f7f\u7528zookeeper\u751a\u81f3mysql\u6765\u5b9e\u73b0\u3002<\/p>\n
\u5206\u5e03\u5f0f\u5b9a\u65f6\u4efb\u52a1\u95ee\u9898<\/p>\n
\u5206\u5e03\u5f0f\u573a\u666f\u4e0b\uff0c\u8fd8\u6709\u53e6\u5916\u4e00\u4e2a\u95ee\u9898--\u5b9a\u65f6\u4efb\u52a1\u5e76\u53d1\u95ee\u9898\uff0c\u5f53\u6211\u4eec\u7684\u5e94\u7528\u91c7\u7528\u5206\u5e03\u5f0f\u90e8\u7f72\u7684\u65f6\u5019\uff0c\u5c31\u5fc5\u7136\u4f1a\u6709\u5404\u79cd\u5b9a\u65f6\u4efb\u52a1\u88ab\u90e8\u7f72\u5230\u4e0d\u540c\u7684\u673a\u5668\u5b9e\u4f8b\u4e0a\uff0c\u5982\u679c\u4e24\u53f0\u673a\u5668\u540c\u65f6\u8fd0\u884c\u540c\u4e00\u4e2a\u5b9a\u65f6\u4efb\u52a1\u7684\u8bdd\uff0c\u4efb\u52a1\u5c31\u6267\u884c\u4e86\u4e24\u6b21\u3002<\/p>\n
\u8fd9\u4e2a\u95ee\u9898\u53ef\u80fd\u66f4\u590d\u6742\u4e00\u70b9\uff0c\u4ec5\u4ec5\u662f\u52a0\u4e00\u4e2a\u9501\u6709\u53ef\u80fd\u4f1a\u574f\u4e8b\u513f\uff0c\u56e0\u4e3a\u5b9a\u65f6\u4efb\u52a1\u7684\u591a\u673a\u5206\u5e03\u4f1a\u4ea7\u751f\u51e0\u4e2a\u9700\u8981\u89e3\u51b3\u7684\u95ee\u9898\uff1a<\/p>\n
\u591a\u53f0\u673a\u5668\u7684\u65f6\u95f4\u4e00\u81f4\u6027\u95ee\u9898<\/p>\n
\u5982\u679c\u591a\u53f0\u673a\u5668\u7684\u65f6\u533a\u4e0d\u4e00\u81f4\uff0c\u90a3\u9501\u57fa\u672c\u4e0a\u65e0\u4ece\u8c08\u8d77\u4e86\u3002 \u6216\u8005\u65f6\u533a\u4e00\u81f4\uff0c\u4f46\u53ef\u80fd\u670d\u52a1\u5668\u65f6\u95f4\u76f8\u5dee\u51e0\u79d2\u949f\uff0c\u90a3\u4e48\u4e5f\u6709\u53ef\u80fd\u5bfc\u81f4\u9501\u4e22\u5931\u3002<\/p>\n
\u9501\u672a\u91ca\u653e\u95ee\u9898(\u670d\u52a1\u5668\u5b95\u673a\u600e\u4e48\u529e)<\/p>\n
\u90a3\u4e48\u5982\u679cserverA\u5728\u52a0\u9501\u7684\u8fc7\u7a0b\u4e2d\uff0c\u51fa\u73b0\u5b95\u673a\u600e\u4e48\u529e\uff0c\u662f\u5426\u4f1a\u4e00\u76f4\u5904\u4e8e\u52a0\u9501\u72b6\u6001<\/p>\n
\u547d\u540d\u7a7a\u95f4\u95ee\u9898<\/p>\n
\u6bcf\u4e2a\u5b9a\u65f6\u4efb\u52a1\u5e94\u8be5\u6709\u4e0d\u540c\u7684\u9501\u547d\u540d\uff0c\u9632\u6b62\u51fa\u73b0\u540c\u540d\u9501\u3002<\/p>\n
\u8fd8\u662f\u8ba9\u6211\u4eec\u770b\u4e00\u4e2ajava\u4ee3\u7801\u7684\u4f8b\u5b50 \u6ce8\u610f\uff0credis\u8fde\u63a5\u548c\u9501\u4ee3\u7801\u6709\u590d\u7528\u4e0a\u9762\u4e00\u8282\u7684<\/p>\n
import org.springframework.scheduling.annotation.EnableScheduling;<\/p>\n
import org.springframework.scheduling.annotation.Scheduled;<\/p>\n
import org.springframework.stereotype.Component;<\/p>\n
import redis.clients.jedis.Jedis;<\/p>\n
@Component<\/p>\n
@EnableScheduling<\/p>\n
public class ScheduleDemo {
\n <\/p>\n
private String sourceKey = \"redis:schedule:test:key\";<\/p>\n
private void sendEmail (String serviceKey) throws InterruptedException {
\n <\/p>\n
Jedis jedis = JedisPoolConnect.connectJedis();<\/p>\n
try {
\n <\/p>\n
Integer sendPatch = 0; \/\/ \u4eceredis\u8bfb\u53d6\u6765\u6a21\u62df\u53d1\u9001\u7684\u6279\u6b21<\/p>\n
Object val = jedis.get(sourceKey);<\/p>\n
if (val != null) {
\n <\/p>\n
sendPatch = Integer.parseInt(val.toString());<\/p>\n
}<\/p>\n
Thread.sleep(2000);<\/p>\n
System.out.println(\"\u6279\u6b21[\" + sendPatch +\"]====\u53d1\u9001\u90ae\u4ef6====\" + serviceKey);<\/p>\n
jedis.incr(sourceKey); \/\/ \u6279\u6b21\u52a01<\/p>\n
} finally {
\n <\/p>\n
jedis.close();<\/p>\n
}<\/p>\n
}<\/p>\n
\/\/ \u6a21\u62dfservice<\/p>\n
@Scheduled(cron = \"0 27 09 * * ?\") \/\/ \u3010cron\u6539\u4e3a\u540e\u9762\u7684\u65f6\u95f4\u3011<\/p>\n
public void serviceA () throws InterruptedException {
\n <\/p>\n
this.sendEmail(\"service\");<\/p>\n
}<\/p>\n
}<\/p>\n
\u5c06\u8fd9\u6bb5\u4ee3\u7801\u6253\u5f00\u4e24\u4e2a\u5b9e\u4f8b\u8fd0\u884c\u3010ps\uff0c\u4f60\u53ef\u4ee5\u5728idea\u4e2d\u53f3\u4e0a\u89d2\u76f4\u63a5\u914d\u4e24\u4e2aconfig\u5c31\u53ef\u4ee5\u4e86\u3011<\/p>\n
<\/p>\n
\u770b\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n
<\/p>\n
<\/p>\n
\u90ae\u4ef61\u88ab\u540c\u65f6\u53d1\u9001\u4e86\u4e24\u6b21\uff0c\u8fd9\u662f\u4e0d\u53ef\u63a5\u53d7\u7684\u3002<\/p>\n
ok\uff0c\u6709\u7684\u540c\u5b66\u73b0\u5728\u5c31\u60f3\u5230\u4e86\uff0c\u52a0\u4e2a\u9501\u5c31\u5b8c\u4e8b\u4e86<\/p>\n
\u6211\u4eec\u5c06\u53d1\u9001\u4ee3\u7801\u52a0\u4e0a\u4e00\u4e2a\u9501\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff1a\u5728sendmail\u91cc\u52a0\u4e00\u4e2aredis\u5206\u5e03\u5f0f\u9501<\/p>\n
private void sendEmail (String serviceKey) throws InterruptedException {
\n <\/p>\n
if (!redisLock.tryLock(serviceKey, 30)) {
\n <\/p>\n
return; \/\/ todo \u83b7\u53d6\u4e0d\u5230\u9501\u5c31\u53d6\u6d88\uff0c\u540c\u4e00\u4e2a\u5b9a\u65f6\u4efb\u52a1\u53ea\u9700\u8981\u6267\u884c\u4e00\u6b21<\/p>\n
}<\/p>\n
Jedis jedis = JedisPoolConnect.connectJedis();<\/p>\n
try {
\n <\/p>\n
Integer sendPatch = 0; \/\/ \u4eceredis\u8bfb\u53d6\u6765\u6a21\u62df\u53d1\u9001\u7684\u6279\u6b21<\/p>\n
Object val = jedis.get(sourceKey);<\/p>\n
if (val != null) {
\n <\/p>\n
sendPatch = Integer.parseInt(val.toString());<\/p>\n
}<\/p>\n
Thread.sleep(2000);<\/p>\n
System.out.println(\"\u6279\u6b21[\" + sendPatch +\"]====\u53d1\u9001\u90ae\u4ef6====\" + serviceKey);<\/p>\n
jedis.incr(sourceKey); \/\/ \u6279\u6b21\u52a01<\/p>\n
redisLock.tryUnlock(serviceKey); \/\/ todo \u89e3\u9501<\/p>\n
} finally {
\n <\/p>\n
jedis.close();<\/p>\n
}<\/p>\n
}<\/p>\n
\u5982\u679c\u83b7\u53d6\u4e0d\u5230\u9501\uff0c\u90a3\u4e48\u53d6\u6d88\u8fd9\u4e2a\u4efb\u52a1\u7684\u6267\u884c\uff0c\u770b\u8d77\u6765\u5f88\u5b8c\u7f8e\u5bf9\u4e0d\u5bf9\uff1f<\/p>\n
\u5b9e\u9645\u4e0a\u6ca1\u6709\u89e3\u51b3\u7684\u95ee\u9898\u8fd8\u6709\u5f88\u591a\u3002<\/p>\n
\u591a\u4e2a\u5b9a\u65f6\u4efb\u52a1\u7684\u591a\u4e2a\u5e76\u53d1\u6267\u884csendmail\uff0ckey\u5982\u4f55\u4fdd\u8bc1\u552f\u4e00\uff1f<\/p>\n
\u53ef\u4ee5\u4f7f\u7528\u5b9e\u4f8b\u7684ip+\u7aef\u53e3\u505a\u552f\u4e00key\uff0c\u8fd9\u6837\u80fd\u591f\u4fdd\u8bc1\u591a\u4e2a\u5b9e\u4f8b\u7684\u552f\u4e00\u6027<\/p>\n
\u4e24\u53f0\u670d\u52a1\u5668\u65f6\u95f4\u5dee\u8d85\u8fc730s\u600e\u4e48\u529e\uff1f<\/p>\n
\u901a\u8fc7\u4e2d\u95f4\u5a92\u4ecb\u6765\u786e\u5b9a\u65f6\u95f4\u3002\u6216\u8005\u5728\u670d\u52a1\u5668\u4e2d\u675c\u7edd\u8fd9\u4e2a\u95ee\u9898<\/p>\n
\u6700\u91cd\u8981\u7684\u95ee\u9898\u8fd8\u662f\u5728\u4e8e\uff0c\u4e24\u53f0\u670d\u52a1\u5668\u7684\u65f6\u95f4\u6709\u53ef\u80fd\u6709\u7ec6\u5fae\u5dee\u522b\uff0c\u4ed6\u4eec\u672c\u8eab\u5c31\u6709\u53ef\u80fd\u4e0d\u662f\u5e76\u53d1\u7684<\/p>\n
\u8fd9\u4e00\u70b9\u5728\u5206\u5e03\u5f0f\u5b9a\u65f6\u4efb\u52a1\u9886\u57df\u91cc\u5f88\u91cd\u8981\u3002<\/p>\n
\u4ec5\u4ec5\u662f\u52a0\u4e86\u4e00\u4e2a\u540c\u6b65\u9501\u662f\u8fdc\u8fdc\u4e0d\u591f\u7684<\/p>\n
\u89e3\u51b3\u65b9\u6848\u53ef\u4ee5\u662f\u6839\u636e\u4e1a\u52a1\u7684\u4e0d\u540c\u6765\u8bbe\u7f6e\u4e0d\u540c\u7684\u9501\u8d85\u65f6\u65f6\u95f4\uff0c\u4f8b\u5982\u67d0\u4e2a\u4e1a\u52a1\u5b9a\u65f6\u4efb\u52a1\uff0c\u6bcf\u5929\u53ea\u53ef\u4ee5\u6267\u884c\u4e00\u6b21\uff0c\u90a3\u4e48\u5c06\u8d85\u65f6\u65f6\u95f4\u8bbe\u7f6e\u4e3a1\u4e2a\u5c0f\u65f6\u6700\u4fdd\u9669\uff0c\u5982\u679c\u67d0\u4e2a\u5b9a\u65f6\u4efb\u52a1\u6bcf\u5206\u949f\u6267\u884c\uff0c\u6267\u884c\u64cd\u4f5c\u65f6\u95f4\u5927\u7ea620s\uff0c\u90a3\u4f60\u53ef\u4ee5\u5c06\u8d85\u65f6\u65f6\u95f4\u8bbe\u7f6e\u621030s\u3002<\/p>\n
\u53e6\u4e00\u4e2a\u89e3\u51b3\u65b9\u6848\u662f\u8bbe\u7f6e\u4e00\u4e2a\u7edf\u4e00\u7684\u3001\u4e2d\u5fc3\u7ea7\u522b\u7684\u5b9a\u65f6\u4efb\u52a1\uff0c\u4efb\u52a1\u8d1f\u8d23\u6d3e\u53d1\u6d88\u606f\uff0c\u901a\u8fc7\u6d88\u606f\u961f\u5217\u7684\u65b9\u5f0f\u6765\u505a\u5b9a\u65f6\uff0c\u8fd9\u91cc\u5c31\u4e0d\u7ec6\u8868\uff0c\u8fd9\u79cd\u65b9\u5f0f\u6bd4\u8f83\u9002\u5408\u5f02\u6784\u3001\u6216\u8005\u8de8\u7f51\u7edc\u3001\u8de8\u673a\u623f\u7ea7\u522b\u7684\u5206\u5e03\u5f0f\u3002<\/p>\n
\u53ef\u4ee5\u5bf9redis\u9501\u505a\u4e00\u6b21\u5c0f\u5c0f\u7684\u6539\u7248\u5347\u7ea7\uff0c\u4f7f\u7528aop\u52a0\u6ce8\u89e3\u6765\u5b8c\u6210\u9501\u7684\u914d\u7f6e\uff1a<\/p>\n
\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5\u7ea7\u522b\u7684aop\u6ce8\u89e3<\/p>\n
import java.lang.annotation.ElementType;<\/p>\n
import java.lang.annotation.Retention;<\/p>\n
import java.lang.annotation.RetentionPolicy;<\/p>\n
import java.lang.annotation.Target;<\/p>\n
\/**<\/p>\n
* redis lock<\/p>\n
* @author taifeng zhang<\/p>\n
* *\/<\/p>\n
@Retention(RetentionPolicy.RUNTIME)<\/p>\n
@Target(ElementType.METHOD)<\/p>\n
public @interface RedisLockAop {
\n <\/p>\n
String key();<\/p>\n
\/**<\/p>\n
* \u4e24\u79cd\u7c7b\u578b\u53ef\u9009<\/p>\n
* wait = \u7b49\u5f85\u9501<\/p>\n
* return = \u53d6\u6d88\u6267\u884c<\/p>\n
* *\/<\/p>\n
String type() default \"wait\";<\/p>\n
int timeout() default 30;<\/p>\n
}<\/p>\n
\u7136\u540e\u901a\u8fc7aop\uff0c\u53bb\u4e3a\u52a0\u4e86\u6ce8\u89e3\u7684\u65b9\u6cd5\u505a\u9501\u64cd\u4f5c<\/p>\n
import com.halfway.halfway.redis.RedisLock;<\/p>\n
import org.aspectj.lang.ProceedingJoinPoint;<\/p>\n
import org.aspectj.lang.annotation.After;<\/p>\n
import org.aspectj.lang.annotation.Around;<\/p>\n
import org.aspectj.lang.annotation.Aspect;<\/p>\n
import org.springframework.context.annotation.Configuration;<\/p>\n
import org.springframework.stereotype.Component;<\/p>\n
\/**<\/p>\n
* redislock aop\u5b9e\u73b0<\/p>\n
* @author by taifeng zhang<\/p>\n
* *\/<\/p>\n
@Component<\/p>\n
@Aspect<\/p>\n
public class RedisLockAopAspect {
\n <\/p>\n
private RedisLock redisLock = new RedisLock();<\/p>\n
@Around(\"@within(com.halfway.halfway.redis.lockAop.RedisLockAop) && @annotation(lock)\")<\/p>\n
public Object excuteAop (ProceedingJoinPoint pjp, RedisLockAop lock) throws Throwable {
\n <\/p>\n
if (\"wait\".equals(lock.type())) {
\n <\/p>\n
while (!redisLock.tryLock(lock.key(), lock.timeout())) {} \/\/ todo \u7b49\u5f85\u9501<\/p>\n
} else if (\"return\".equals(lock.type())) {
\n <\/p>\n
if (!redisLock.tryLock(lock.key(), lock.timeout())) {
\n <\/p>\n
return null; \/\/ todo \u53d6\u6d88\u6267\u884c<\/p>\n
}<\/p>\n
} else {
\n <\/p>\n
throw new NullPointerException(\"type\u53ea\u53ef\u4ee5\u662fwait\u6216\u8005return\");<\/p>\n
}<\/p>\n
Object val = pjp.proceed();<\/p>\n
redisLock.tryUnlock(lock.key());<\/p>\n
return val;<\/p>\n
}<\/p>\n
}<\/p>\n
\u8fd9\u4e2a\u65b9\u5f0f\u7684\u597d\u5904\u662f\u9501\u4e0e\u4ee3\u7801\u89e3\u8026\uff0c\u65e0\u9700\u5173\u6ce8\u9501\u7684\u5185\u90e8\u5b9e\u73b0\u53d8\u5316<\/p>\n
@Scheduled(cron = \"0\/30 * * * * ?\")<\/p>\n
@RedisLockAop(key = \"serviceIp:port\", type=\"return\", timeout=15)<\/p>\n
public void serviceA () throws InterruptedException {
\n <\/p>\n
this.sendEmail(\"service\");<\/p>\n
}<\/p>\n
\u672c\u5730\u9501\u3001redis\u5206\u5e03\u5f0f\u9501\u3001zk\u5206\u5e03\u5f0f\u9501<\/p>\n
\u672c\u5730\u9501.redis\u5206\u5e03\u5f0f\u9501.zk\u5206\u5e03\u5f0f\u9501 https:\/\/www.cnblogs.com\/yjq-code\/p\/dotnetlock.html \u4e3a\u4ec0\u4e48\u8981\u7528\u9501? \u5927\u578b\u7ad9\u70b9\u5728\u9ad8\u5e76\u53d1\u7684\u60c5\u51b5\u4e0b,\u4e3a\u4e86\u4fdd\u6301\u6570\u636e\u6700 ...<\/p>\n
\u5173\u4e8e\u5206\u5e03\u5f0f\u9501\u539f\u7406\u7684\u4e00\u4e9b\u5b66\u4e60\u4e0e\u601d\u8003-redis\u5206\u5e03\u5f0f\u9501\uff0czookeeper\u5206\u5e03\u5f0f\u9501<\/p>\n
\u9996\u5148\u5206\u5e03\u5f0f\u9501\u548c\u6211\u4eec\u5e73\u5e38\u8bb2\u5230\u7684\u9501\u539f\u7406\u57fa\u672c\u4e00\u6837,\u76ee\u7684\u5c31\u662f\u786e\u4fdd,\u5728\u591a\u4e2a\u7ebf\u7a0b\u5e76\u53d1\u65f6,\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u5728\u540c\u4e00\u523b\u64cd\u4f5c\u8fd9\u4e2a\u4e1a\u52a1\u6216\u8005\u8bf4\u65b9\u6cd5.\u53d8\u91cf. \u5728\u4e00\u4e2a\u8fdb\u7a0b\u4e2d,\u4e5f\u5c31\u662f\u4e00\u4e2ajvm \u6216\u8005\u8bf4\u5e94\u7528\u4e2d,\u6211\u4eec\u5f88\u5bb9\u6613\u53bb\u5904\u7406\u63a7\u5236,\u5728j ...<\/p>\n
.net\u4e0b \u672c\u5730\u9501\u3001redis\u5206\u5e03\u5f0f\u9501\u3001zk\u5206\u5e03\u5f0f\u9501\u7684\u5b9e\u73b0<\/p>\n
\u4e3a\u4ec0\u4e48\u8981\u7528\u9501? \u5927\u578b\u7ad9\u70b9\u5728\u9ad8\u5e76\u53d1\u7684\u60c5\u51b5\u4e0b,\u4e3a\u4e86\u4fdd\u6301\u6570\u636e\u6700\u7ec8\u4e00\u81f4\u6027\u5c31\u9700\u8981\u7528\u5230\u6280\u672f\u65b9\u6848\u6765\u652f\u6301.\u6bd4\u5982:\u5206\u5e03\u5f0f\u9501.\u5206\u5e03\u5f0f\u4e8b\u52a1.\u6709\u65f6\u5019\u6211\u4eec\u5728\u4e3a\u4e86\u4fdd\u8bc1\u67d0\u4e00\u4e2a\u65b9\u6cd5\u6bcf\u6b21\u53ea\u80fd\u88ab\u4e00\u4e2a\u8c03\u7528\u8005\u4f7f\u7528\u7684\u65f6\u5019,\u8fd9\u65f6\u5019\u6211\u4eec\u4e5f\u53ef\u4ee5\u9501\u6765 ...<\/p>\n
Java\u5206\u5e03\u5f0f\u9501,\u641e\u61c2\u5206\u5e03\u5f0f\u9501\u5b9e\u73b0\u770b\u8fd9\u7bc7\u6587\u7ae0\u5c31\u5bf9\u4e86<\/p>\n
\u968f\u7740\u5fae\u5904\u7406\u673a\u6280\u672f\u7684\u53d1\u5c55,\u4eba\u4eec\u53ea\u9700\u82b1\u51e0\u767e\u7f8e\u5143\u5c31\u80fd\u4e70\u5230\u4e00\u4e2aCPU\u82af\u7247,\u8fd9\u4e2a\u82af\u7247\u6bcf\u79d2\u949f\u6267\u884c\u7684\u6307\u4ee4\u6bd480\u5e74\u4ee3\u6700\u5927\u7684\u5927\u578b\u673a\u7684\u5904\u7406\u673a\u6bcf\u79d2\u949f\u6240\u6267\u884c\u7684\u6307\u4ee4\u8fd8\u591a.\u5982\u679c\u4f60\u613f\u610f\u4ed8\u51fa\u4e24\u500d\u7684\u4ef7\u94b1,\u5c06\u5f97\u5230\u540c\u6837\u7684CPU,\u4f46\u5b83\u5374\u4ee5\u66f4 ...<\/p>\n
\u4e00\u4e2aRedis\u5b9e\u73b0\u7684\u5206\u5e03\u5f0f\u9501<\/p>\n
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.redis.conne ...<\/p>\n
Redis\u5b9e\u73b0\u7f13\u5b58\u4e0e\u5206\u5e03\u5f0f\u9501<\/p>\n
\u7f13\u5b58\u4e0e\u5206\u5e03\u5f0f\u9501 \u54ea\u4e9b\u6570\u636e\u9002\u5408\u653e\u5165\u7f13\u5b58 \u5373\u65f6\u6027.\u6570\u636e\u4e00\u81f4\u6027\u8981\u6c42\u4e0d\u9ad8\u7684 \u8bbf\u95ee\u91cf\u5927\u4e14\u66f4\u65b0\u9891\u7387\u4e0d\u9ad8\u7684\u6570\u636e \u9009\u62e9redis\u505a\u4e3a\u7f13\u5b58\u4e2d\u95f4\u4ef6 or ...<\/p>\n
\u4f7f\u7528redis\u6784\u5efa\u53ef\u9760\u5206\u5e03\u5f0f\u9501<\/p>\n
\u5173\u4e8e\u5206\u5e03\u5f0f\u9501\u7684\u6982\u5ff5,\u5177\u4f53\u5b9e\u73b0\u65b9\u5f0f,\u76f4\u63a5\u53c2\u9605\u4e0b\u9762\u4e24\u4e2a\u5e16\u5b50,\u8fd9\u91cc\u5c31\u4e0d\u591a\u4ecb\u7ecd\u4e86. \u5206\u5e03\u5f0f\u9501\u7684\u591a\u79cd\u5b9e\u73b0\u65b9\u5f0f \u5206\u5e03\u5f0f\u9501\u603b\u7ed3 \u5bf9\u4e8e\u5206\u5e03\u5f0f\u9501\u7684\u51e0\u79cd\u5b9e\u73b0\u65b9\u5f0f\u7684\u4f18\u52a3,\u8fd9\u91cc\u518d\u5217\u4e3e\u4e0b 1. \u6570\u636e\u5e93\u5b9e\u73b0\u65b9\u5f0f \u4f18\u70b9:\u6613\u7406\u89e3 \u7f3a ...<\/p>\n
Redis\u5206\u5e03\u5f0f\u9501\u670d\u52a1(\u516b)<\/p>\n
\u9605\u8bfb\u76ee\u5f55: \u6982\u8ff0 \u5206\u5e03\u5f0f\u9501 \u591a\u5b9e\u4f8b\u5206\u5e03\u5f0f\u9501 \u603b\u7ed3 \u6982\u8ff0 \u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b,\u901a\u5e38\u4f1a\u4f7f\u7528\u9501\u6765\u4fdd\u8bc1\u6709\u4e14\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u6765\u64cd\u4f5c\u5171\u4eab\u8d44\u6e90.\u6bd4\u5982: object obj = new object(); lock (ob ...<\/p>\n
\u5206\u5e03\u5f0f\u7f13\u5b58\u6280\u672fredis\u5b66\u4e60\u7cfb\u5217(\u4e94)\u2014\u2014redis\u5b9e\u6218(redis\u4e0espring\u6574\u5408\uff0c\u5206\u5e03\u5f0f\u9501\u5b9e\u73b0)<\/p>\n
\u672c\u6587\u662fredis\u5b66\u4e60\u7cfb\u5217\u7684\u7b2c\u4e94\u7bc7,\u70b9\u51fb\u4e0b\u9762\u94fe\u63a5\u53ef\u56de\u770b\u7cfb\u5217\u6587\u7ae0 <\/p>\n
\u4ec0\u4e48\u662f\u5206\u5e03\u5f0f\u9501\u53ca\u6b63\u786e\u4f7f\u7528redis\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501<\/p>\n
\u5206\u5e03\u5f0f\u9501 \u5206\u5e03\u5f0f\u9501\u5176\u5b9e\u53ef\u4ee5\u7406\u89e3\u4e3a:\u63a7\u5236\u5206\u5e03\u5f0f\u7cfb\u7edf\u6709\u5e8f\u7684\u53bb\u5bf9\u5171\u4eab\u8d44\u6e90\u8fdb\u884c\u64cd\u4f5c,\u901a\u8fc7\u4e92\u65a5\u6765\u4fdd\u6301\u4e00\u81f4\u6027. \u4e3e\u4e2a\u4e0d\u592a\u6070\u5f53\u7684\u4f8b\u5b50:\u5047\u8bbe\u5171\u4eab\u7684\u8d44\u6e90\u5c31\u662f\u4e00\u4e2a\u623f\u5b50,\u91cc\u9762\u6709\u5404\u79cd\u4e66,\u5206\u5e03\u5f0f\u7cfb\u7edf\u5c31\u662f\u8981\u8fdb\u5c4b\u770b\u4e66\u7684\u4eba,\u5206\u5e03\u5f0f\u9501 ...<\/p>\n
\u968f\u673a\u63a8\u8350<\/p>\n
\u3010\u4e5d\u5ea6OJ\u3011\u9898\u76ee1054\uff1a\u5b57\u7b26\u4e32\u5185\u6392\u5e8f<\/p>\n
\u9898\u76ee\u63cf\u8ff0: \u8f93\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32,\u957f\u5ea6\u5c0f\u4e8e\u7b49\u4e8e200,\u7136\u540e\u5c06\u8f93\u51fa\u6309\u5b57\u7b26\u987a\u5e8f\u5347\u5e8f\u6392\u5e8f\u540e\u7684\u5b57\u7b26\u4e32. \u8f93\u5165: \u6d4b\u8bd5\u6570\u636e\u6709\u591a\u7ec4,\u8f93\u5165\u5b57\u7b26\u4e32. \u8f93\u51fa: \u5bf9\u4e8e\u6bcf\u7ec4\u8f93\u5165,\u8f93\u51fa\u5904\u7406\u540e\u7684\u7ed3\u679c. \u6837\u4f8b\u8f93\u5165: bacd \u6837\u4f8b\u8f93\u51fa ...<\/p>\n
\u6d4f\u89c8\u5668\u7aef\u83b7\u53d6\u5c40\u57df\u7f51IP\u5730\u5740\uff0c\u672c\u673a\u7684MAC\uff0c\u4ee5\u53ca\u673a\u5668\u540d<\/p>\n
\u539f\u6587\u94fe\u63a5:http:\/\/www.orlion.ga\/59\/ \u53ea\u9488\u5bf9IE\u4e14\u5ba2\u6237\u7aef\u7684IE\u5141\u8bb8AcitiveX\u8fd0\u884c code: <\/p>\n
... <\/p>\n
EOS\u5355\u5411N\u5bf91\u5173\u8054<\/p>\n
1. N\u7aef\u5b9e\u4f53\u4e2d\u7528\u4e8e\u5173\u8054\u7684\u5c5e\u6027\u53ef\u4ee5\u662f\u4e3b\u952e\u4e5f\u53ef\u4ee5\u662f\u975e\u4e3b\u952e,1\u7aef\u7684\u5173\u8054\u5b57\u6bb5\u5fc5\u987b\u662f\u4e3b\u952e(\u53ef\u4ee5\u662f\u5355\u4e3b\u952e\u4e5f\u53ef\u4ee5\u662f\u590d\u5408\u4e3b\u952e). \u5982\u4e0b\u56fe\u5173\u8054\u5b57\u6bb5:orgid 2.\u5f53\u5728N\u7aef\u9009\u62e9\u4e86\u7528\u4e8e\u5173\u8054\u7684\u5c5e\u6027,\u90a3\u4e48\u8fd9\u4e9b\u5c5e\u6027\u5728N\u7aef\u5b9e\u4f53 ...<\/p>\n
\u4ec0\u4e48\u662f\u4f5c\u7528\u57df\u94fe\uff0c\u4ec0\u4e48\u662f\u539f\u578b\u94fe\uff0c\u5b83\u4eec\u7684\u533a\u522b\uff0c\u5728js\u4e2d\u5b83\u4eec\u5177\u4f53\u6307\u4ec0\u4e48\uff1f<\/p>\n
\u4ec0\u4e48\u662f\u4f5c\u7528\u57df\u94fe,\u4ec0\u4e48\u662f\u539f\u578b\u94fe. \u4f5c\u7528\u57df\u662f\u9488\u5bf9\u53d8\u91cf\u7684,\u6bd4\u5982\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u51fd\u6570,\u51fd\u6570\u91cc\u9762\u53c8\u5305\u542b\u4e86\u4e00\u4e2a\u51fd\u6570,\u90a3\u4e48\u73b0\u5728\u5c31\u6709\u4e09\u4e2a\u4f5c\u7528\u57df \u5168\u5c40\u4f5c\u7528\u57df==>\u51fd\u65701\u4f5c\u7528\u57df==>\u51fd\u65702\u4f5c\u7528\u57df \u4f5c\u7528\u57df\u7684\u7279\u70b9\u5c31 ...<\/p>\n
ajax \u7559\u8a00\u677f<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"linux \u5b9a\u65f6\u4efb\u52a1pjp,\u3010redis\u3011\u5206\u5e03\u5f0f\u9501\u5b9e\u73b0\uff0c\u4e0e\u5206\u5e03\u5f0f\u5b9a\u65f6\u4efb\u52a1\u5982\u679c\u4f60\u8fd8\u4e0d\u77e5\u9053redis\u7684\u57fa\u672c\u547d\u4ee4\u4e0e\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5\uff0c\u8bf7\u770b\u5199\u5728\u524d\u9762red...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7724"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=7724"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7724\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=7724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=7724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=7724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}