{"id":78725,"date":"2026-02-28T00:19:06","date_gmt":"2026-02-27T16:19:06","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/78725.html"},"modified":"2026-02-28T00:19:06","modified_gmt":"2026-02-27T16:19:06","slug":"%e9%ab%98%e5%b9%b6%e5%8f%91%e7%b3%bb%e7%bb%9f%e4%b8%ad%e5%b8%b8%e9%81%87%e5%88%b0%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/78725.html","title":{"rendered":"\u9ad8\u5e76\u53d1\u7cfb\u7edf\u4e2d\u5e38\u9047\u5230\u7684\u95ee\u9898"},"content":{"rendered":"<h3>\u4e00\u3001\u7f13\u5b58\u95ee\u9898<\/h3>\n<h4>1.1 \u7f13\u5b58\u7a7f\u900f<\/h4>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u4f7f\u7528\u5e03\u9686\u8fc7\u6ee4\u5668&#xff08;Bloom Filter&#xff09;\u62e6\u622a\u4e0d\u5b58\u5728\u7684\u6570\u636e\u8bf7\u6c42\u3002<\/p>\n<p>\u64cd\u4f5c\u6b65\u9aa4&#xff1a;<\/p>\n<li>\n<p>\u521d\u59cb\u5316\u5e03\u9686\u8fc7\u6ee4\u5668&#xff0c;\u5c06\u6570\u636e\u5e93\u4e2d\u5b58\u5728\u7684\u6570\u636eID\u5168\u90e8\u653e\u5165\u5e03\u9686\u8fc7\u6ee4\u5668\u3002<\/p>\n<\/li>\n<li>\n<p>\u67e5\u8be2\u8bf7\u6c42\u5230\u8fbe\u65f6&#xff0c;\u5148\u68c0\u67e5\u5e03\u9686\u8fc7\u6ee4\u5668\u662f\u5426\u5b58\u5728\u8be5key\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u5e03\u9686\u8fc7\u6ee4\u5668\u5224\u65ad\u4e0d\u5b58\u5728&#xff0c;\u5219\u76f4\u63a5\u8fd4\u56de\u7a7a&#xff0c;\u907f\u514d\u67e5\u8be2\u6570\u636e\u5e93\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u5b58\u5728&#xff0c;\u5219\u8d70\u7f13\u5b58\u3001\u6570\u636e\u5e93\u67e5\u8be2\u903b\u8f91\u3002<\/p>\n<\/li>\n<p>Java\u4ee3\u7801\u793a\u4f8b&#xff08;\u4f7f\u7528Google Guava\u7684BloomFilter&#xff09;&#xff1a;<\/p>\n<p>java<\/p>\n<p>import com.google.common.hash.BloomFilter;<br \/>\nimport com.google.common.hash.Funnels;<br \/>\nimport org.springframework.stereotype.Service;<br \/>\nimport javax.annotation.PostConstruct;<br \/>\nimport java.nio.charset.Charset;<br \/>\nimport java.util.List;<\/p>\n<p>&#064;Service<br \/>\npublic class ProductService {<br \/>\n    private BloomFilter&lt;String&gt; bloomFilter;<br \/>\n    private final int EXPECTED_INSERTIONS &#061; 1000000; \/\/ \u9884\u8ba1\u6570\u636e\u91cf<br \/>\n    private final double FPP &#061; 0.01; \/\/ \u8bef\u5224\u7387<\/p>\n<p>    &#064;PostConstruct<br \/>\n    public void init() {<br \/>\n        bloomFilter &#061; BloomFilter.create(<br \/>\n                Funnels.stringFunnel(Charset.defaultCharset()),<br \/>\n                EXPECTED_INSERTIONS,<br \/>\n                FPP);<br \/>\n        \/\/ \u4ece\u6570\u636e\u5e93\u52a0\u8f7d\u6240\u6709\u5b58\u5728\u7684\u5546\u54c1ID&#xff0c;\u6dfb\u52a0\u5230\u5e03\u9686\u8fc7\u6ee4\u5668<br \/>\n        List&lt;String&gt; allProductIds &#061; loadAllProductIdsFromDB();<br \/>\n        for (String id : allProductIds) {<br \/>\n            bloomFilter.put(id);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    public Product getProduct(String productId) {<br \/>\n        \/\/ 1. \u5e03\u9686\u8fc7\u6ee4\u5668\u5224\u65ad\u4e0d\u5b58\u5728&#xff0c;\u76f4\u63a5\u8fd4\u56denull<br \/>\n        if (!bloomFilter.mightContain(productId)) {<br \/>\n            return null;<br \/>\n        }<br \/>\n        \/\/ 2. \u67e5\u8be2\u7f13\u5b58&#xff08;\u7701\u7565\u5177\u4f53\u4ee3\u7801&#xff09;<br \/>\n        Product product &#061; getFromCache(productId);<br \/>\n        if (product !&#061; null) {<br \/>\n            return product;<br \/>\n        }<br \/>\n        \/\/ 3. \u7f13\u5b58\u4e0d\u5b58\u5728&#xff0c;\u67e5\u8be2\u6570\u636e\u5e93<br \/>\n        product &#061; getFromDB(productId);<br \/>\n        if (product !&#061; null) {<br \/>\n            \/\/ \u653e\u5165\u7f13\u5b58&#xff0c;\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4<br \/>\n            putToCache(productId, product);<br \/>\n        } else {<br \/>\n            \/\/ \u53ef\u9009&#xff1a;\u7f13\u5b58\u7a7a\u5bf9\u8c61&#xff0c;\u4f46\u5e03\u9686\u8fc7\u6ee4\u5668\u5df2\u62e6\u622a\u5927\u90e8\u5206&#xff0c;\u53ef\u4e0d\u7f13\u5b58\u7a7a\u503c<br \/>\n        }<br \/>\n        return product;<br \/>\n    }<br \/>\n}<\/p>\n<hr \/>\n<h4>1.2 \u7f13\u5b58\u96ea\u5d29<\/h4>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u8bbe\u7f6e\u4e0d\u540c\u7684\u8fc7\u671f\u65f6\u95f4&#xff08;\u57fa\u7840\u8fc7\u671f\u65f6\u95f4 &#043; \u968f\u673a\u503c&#xff09;\u3002<\/p>\n<p>\u64cd\u4f5c\u6b65\u9aa4&#xff1a;<\/p>\n<li>\n<p>\u5728\u653e\u5165\u7f13\u5b58\u65f6&#xff0c;\u4e3akey\u8bbe\u7f6e\u4e00\u4e2a\u57fa\u7840\u8fc7\u671f\u65f6\u95f4&#xff08;\u59821\u5c0f\u65f6&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u5728\u6b64\u57fa\u7840\u4e0a\u589e\u52a0\u4e00\u4e2a\u968f\u673a\u79d2\u6570&#xff08;\u59820~300\u79d2&#xff09;&#xff0c;\u4f7fkey\u7684\u8fc7\u671f\u65f6\u95f4\u5206\u6563\u5f00\u3002<\/p>\n<\/li>\n<li>\n<p>\u907f\u514d\u5927\u91cfkey\u540c\u65f6\u5931\u6548\u3002<\/p>\n<\/li>\n<p>Java\u4ee3\u7801\u793a\u4f8b&#xff08;\u4f7f\u7528Spring Data Redis&#xff09;&#xff1a;<\/p>\n<p>java<\/p>\n<p>import org.springframework.data.redis.core.RedisTemplate;<br \/>\nimport org.springframework.stereotype.Service;<br \/>\nimport java.util.concurrent.TimeUnit;<\/p>\n<p>&#064;Service<br \/>\npublic class CacheService {<br \/>\n    &#064;Autowired<br \/>\n    private RedisTemplate&lt;String, Object&gt; redisTemplate;<\/p>\n<p>    public void putWithRandomExpire(String key, Object value, long baseTimeout, TimeUnit unit) {<br \/>\n        long timeout &#061; unit.toSeconds(baseTimeout);<br \/>\n        \/\/ \u589e\u52a0\u968f\u673a\u503c&#xff0c;\u8303\u56f40~300\u79d2<br \/>\n        long random &#061; (long) (Math.random() * 300);<br \/>\n        long finalTimeout &#061; timeout &#043; random;<br \/>\n        redisTemplate.opsForValue().set(key, value, finalTimeout, TimeUnit.SECONDS);<br \/>\n    }<br \/>\n}<\/p>\n<hr \/>\n<h4>1.3 \u7f13\u5b58\u5931\u6548&#xff08;\u7f13\u5b58\u51fb\u7a7f&#xff09;<\/h4>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u4f7f\u7528\u4e92\u65a5\u9501&#xff08;\u5206\u5e03\u5f0f\u9501&#xff09;\u4fdd\u8bc1\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u53bb\u52a0\u8f7d\u6570\u636e\u3002<\/p>\n<p>\u64cd\u4f5c\u6b65\u9aa4&#xff1a;<\/p>\n<li>\n<p>\u67e5\u8be2\u7f13\u5b58&#xff0c;\u5982\u679c\u547d\u4e2d\u5219\u8fd4\u56de\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u672a\u547d\u4e2d&#xff0c;\u5c1d\u8bd5\u83b7\u53d6\u5206\u5e03\u5f0f\u9501\u3002<\/p>\n<\/li>\n<li>\n<p>\u83b7\u53d6\u9501\u6210\u529f\u540e&#xff0c;\u518d\u6b21\u67e5\u8be2\u7f13\u5b58&#xff08;\u9632\u6b62\u5728\u7b49\u5f85\u9501\u7684\u8fc7\u7a0b\u4e2d\u5176\u4ed6\u7ebf\u7a0b\u5df2\u7ecf\u52a0\u8f7d\u4e86\u6570\u636e&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u7f13\u5b58\u4ecd\u4e0d\u5b58\u5728&#xff0c;\u5219\u67e5\u8be2\u6570\u636e\u5e93&#xff0c;\u5e76\u66f4\u65b0\u7f13\u5b58\u3002<\/p>\n<\/li>\n<li>\n<p>\u91ca\u653e\u9501\u3002<\/p>\n<\/li>\n<p>Java\u4ee3\u7801\u793a\u4f8b&#xff08;\u4f7f\u7528Redisson\u5b9e\u73b0\u5206\u5e03\u5f0f\u9501&#xff09;&#xff1a;<\/p>\n<p>java<\/p>\n<p>import org.redisson.api.RLock;<br \/>\nimport org.redisson.api.RedissonClient;<br \/>\nimport org.springframework.beans.factory.annotation.Autowired;<br \/>\nimport org.springframework.stereotype.Service;<\/p>\n<p>&#064;Service<br \/>\npublic class ProductService {<br \/>\n    &#064;Autowired<br \/>\n    private RedissonClient redissonClient;<\/p>\n<p>    public Product getProductWithLock(String productId) {<br \/>\n        \/\/ 1. \u67e5\u8be2\u7f13\u5b58<br \/>\n        Product product &#061; getFromCache(productId);<br \/>\n        if (product !&#061; null) {<br \/>\n            return product;<br \/>\n        }<\/p>\n<p>        \/\/ 2. \u5c1d\u8bd5\u83b7\u53d6\u5206\u5e03\u5f0f\u9501<br \/>\n        String lockKey &#061; &#034;lock:product:&#034; &#043; productId;<br \/>\n        RLock lock &#061; redissonClient.getLock(lockKey);<br \/>\n        try {<br \/>\n            \/\/ \u5c1d\u8bd5\u52a0\u9501&#xff0c;\u6700\u591a\u7b49\u5f853\u79d2&#xff0c;\u9501\u8fc7\u671f\u65f6\u95f410\u79d2<br \/>\n            if (lock.tryLock(3, 10, TimeUnit.SECONDS)) {<br \/>\n                try {<br \/>\n                    \/\/ 3. \u518d\u6b21\u67e5\u8be2\u7f13\u5b58&#xff0c;\u53ef\u80fd\u5176\u4ed6\u7ebf\u7a0b\u5df2\u7ecf\u52a0\u8f7d<br \/>\n                    product &#061; getFromCache(productId);<br \/>\n                    if (product !&#061; null) {<br \/>\n                        return product;<br \/>\n                    }<br \/>\n                    \/\/ 4. \u67e5\u8be2\u6570\u636e\u5e93<br \/>\n                    product &#061; getFromDB(productId);<br \/>\n                    if (product !&#061; null) {<br \/>\n                        putToCache(productId, product);<br \/>\n                    }<br \/>\n                    return product;<br \/>\n                } finally {<br \/>\n                    lock.unlock();<br \/>\n                }<br \/>\n            } else {<br \/>\n                \/\/ \u83b7\u53d6\u9501\u5931\u8d25&#xff0c;\u7b49\u5f85\u91cd\u8bd5\u6216\u8fd4\u56de\u7a7a<br \/>\n                Thread.sleep(100);<br \/>\n                return getProductWithLock(productId); \/\/ \u9012\u5f52\u91cd\u8bd5<br \/>\n            }<br \/>\n        } catch (InterruptedException e) {<br \/>\n            Thread.currentThread().interrupt();<br \/>\n            return null;<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<hr \/>\n<h4>1.4 \u70ed\u70b9\u7f13\u5b58\u91cd\u5efa<\/h4>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u903b\u8f91\u8fc7\u671f &#043; \u5f02\u6b65\u5237\u65b0&#xff08;\u7f13\u5b58\u6c38\u4e0d\u8fc7\u671f&#xff0c;\u4f46\u5b58\u50a8\u903b\u8f91\u8fc7\u671f\u65f6\u95f4&#xff09;\u3002<\/p>\n<p>\u64cd\u4f5c\u6b65\u9aa4&#xff1a;<\/p>\n<li>\n<p>\u7f13\u5b58\u4e2d\u4fdd\u5b58\u4e00\u4e2a\u5305\u88c5\u5bf9\u8c61&#xff0c;\u5305\u542b\u5b9e\u9645\u6570\u636e\u548c\u903b\u8f91\u8fc7\u671f\u65f6\u95f4\u3002<\/p>\n<\/li>\n<li>\n<p>\u67e5\u8be2\u65f6&#xff0c;\u5982\u679c\u903b\u8f91\u65f6\u95f4\u672a\u8fc7\u671f&#xff0c;\u76f4\u63a5\u8fd4\u56de\u6570\u636e\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u903b\u8f91\u65f6\u95f4\u5df2\u8fc7\u671f&#xff0c;\u5219\u5f02\u6b65\u7ebf\u7a0b\u53bb\u52a0\u8f7d\u6700\u65b0\u6570\u636e\u66f4\u65b0\u7f13\u5b58&#xff0c;\u540c\u65f6\u8fd4\u56de\u65e7\u6570\u636e\u7ed9\u5f53\u524d\u8bf7\u6c42&#xff08;\u907f\u514d\u7b49\u5f85&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528\u5206\u5e03\u5f0f\u9501\u9632\u6b62\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u5237\u65b0\u3002<\/p>\n<\/li>\n<p>Java\u4ee3\u7801\u793a\u4f8b&#xff1a;<\/p>\n<p>java<\/p>\n<p>import com.fasterxml.jackson.annotation.JsonIgnore;<br \/>\nimport lombok.Data;<br \/>\nimport java.util.concurrent.ExecutorService;<br \/>\nimport java.util.concurrent.Executors;<\/p>\n<p>&#064;Data<br \/>\nclass CacheWrapper&lt;T&gt; {<br \/>\n    private T data;<br \/>\n    private long expireTime; \/\/ \u903b\u8f91\u8fc7\u671f\u65f6\u95f4\u6233&#xff08;\u6beb\u79d2&#xff09;<br \/>\n}<\/p>\n<p>&#064;Service<br \/>\npublic class HotProductService {<br \/>\n    private ExecutorService executor &#061; Executors.newFixedThreadPool(10);<br \/>\n    &#064;Autowired<br \/>\n    private RedisTemplate&lt;String, Object&gt; redisTemplate;<\/p>\n<p>    public Product getHotProduct(String productId) {<br \/>\n        String key &#061; &#034;hot:product:&#034; &#043; productId;<br \/>\n        CacheWrapper&lt;Product&gt; wrapper &#061; (CacheWrapper&lt;Product&gt;) redisTemplate.opsForValue().get(key);<br \/>\n        if (wrapper &#061;&#061; null) {<br \/>\n            \/\/ \u7f13\u5b58\u4e0d\u5b58\u5728&#xff0c;\u76f4\u63a5\u52a0\u8f7d&#xff08;\u540c\u6b65&#xff09;<br \/>\n            return loadProductToCache(productId);<br \/>\n        }<br \/>\n        if (wrapper.getExpireTime() &gt; System.currentTimeMillis()) {<br \/>\n            \/\/ \u903b\u8f91\u65f6\u95f4\u672a\u8fc7\u671f&#xff0c;\u76f4\u63a5\u8fd4\u56de<br \/>\n            return wrapper.getData();<br \/>\n        }<br \/>\n        \/\/ \u903b\u8f91\u8fc7\u671f&#xff0c;\u5f02\u6b65\u5237\u65b0<br \/>\n        executor.submit(() -&gt; {<br \/>\n            \/\/ \u5c1d\u8bd5\u83b7\u53d6\u5206\u5e03\u5f0f\u9501&#xff0c;\u907f\u514d\u5e76\u53d1\u5237\u65b0<br \/>\n            String lockKey &#061; &#034;lock:refresh:&#034; &#043; productId;<br \/>\n            RLock lock &#061; redissonClient.getLock(lockKey);<br \/>\n            if (lock.tryLock()) {<br \/>\n                try {<br \/>\n                    \/\/ \u518d\u6b21\u68c0\u67e5\u662f\u5426\u5df2\u88ab\u5176\u4ed6\u7ebf\u7a0b\u5237\u65b0<br \/>\n                    CacheWrapper&lt;Product&gt; latest &#061; (CacheWrapper&lt;Product&gt;) redisTemplate.opsForValue().get(key);<br \/>\n                    if (latest !&#061; null &amp;&amp; latest.getExpireTime() &gt; System.currentTimeMillis()) {<br \/>\n                        return;<br \/>\n                    }<br \/>\n                    \/\/ \u52a0\u8f7d\u6570\u636e\u5e93<br \/>\n                    Product product &#061; getFromDB(productId);<br \/>\n                    CacheWrapper&lt;Product&gt; newWrapper &#061; new CacheWrapper&lt;&gt;();<br \/>\n                    newWrapper.setData(product);<br \/>\n                    newWrapper.setExpireTime(System.currentTimeMillis() &#043; 3600_000); \/\/ \u8bbe\u7f6e1\u5c0f\u65f6\u540e\u8fc7\u671f<br \/>\n                    redisTemplate.opsForValue().set(key, newWrapper);<br \/>\n                } finally {<br \/>\n                    lock.unlock();<br \/>\n                }<br \/>\n            }<br \/>\n        });<br \/>\n        \/\/ \u8fd4\u56de\u65e7\u6570\u636e<br \/>\n        return wrapper.getData();<br \/>\n    }<br \/>\n}<\/p>\n<hr \/>\n<h4>1.5 \u7f13\u5b58\u6570\u636e\u5e93\u53cc\u5199\u4e0d\u4e00\u81f4<\/h4>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u5148\u66f4\u65b0\u6570\u636e\u5e93&#xff0c;\u518d\u5220\u9664\u7f13\u5b58&#xff08;\u7ecf\u5178\u7684Cache-Aside\u6a21\u5f0f&#xff09;&#xff0c;\u914d\u5408\u5ef6\u8fdf\u53cc\u5220\u6216Binlog\u76d1\u542c\u3002<\/p>\n<p>\u64cd\u4f5c\u6b65\u9aa4&#xff08;\u5ef6\u8fdf\u53cc\u5220&#xff09;&#xff1a;<\/p>\n<li>\n<p>\u5220\u9664\u7f13\u5b58\u3002<\/p>\n<\/li>\n<li>\n<p>\u66f4\u65b0\u6570\u636e\u5e93\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f11\u7720\u4e00\u5c0f\u6bb5\u65f6\u95f4&#xff08;\u5982500ms&#xff09;&#xff0c;\u518d\u6b21\u5220\u9664\u7f13\u5b58&#xff0c;\u786e\u4fdd\u5728\u66f4\u65b0\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u4ea7\u751f\u7684\u5e76\u53d1\u8bfb\u8bf7\u6c42\u5e26\u6765\u7684\u810f\u7f13\u5b58\u88ab\u6e05\u9664\u3002<\/p>\n<\/li>\n<p>Java\u4ee3\u7801\u793a\u4f8b&#xff1a;<\/p>\n<p>java<\/p>\n<p>&#064;Service<br \/>\npublic class ProductUpdateService {<br \/>\n    &#064;Autowired<br \/>\n    private RedisTemplate&lt;String, Object&gt; redisTemplate;<br \/>\n    &#064;Autowired<br \/>\n    private JdbcTemplate jdbcTemplate;<\/p>\n<p>    public void updateProduct(Product product) {<br \/>\n        String cacheKey &#061; &#034;product:&#034; &#043; product.getId();<br \/>\n        \/\/ \u7b2c\u4e00\u6b21\u5220\u9664\u7f13\u5b58<br \/>\n        redisTemplate.delete(cacheKey);<\/p>\n<p>        \/\/ \u66f4\u65b0\u6570\u636e\u5e93<br \/>\n        jdbcTemplate.update(&#034;UPDATE product SET name &#061; ? WHERE id &#061; ?&#034;, product.getName(), product.getId());<\/p>\n<p>        \/\/ \u5ef6\u8fdf\u4e00\u6bb5\u65f6\u95f4\u540e\u518d\u6b21\u5220\u9664<br \/>\n        ScheduledExecutorService scheduler &#061; Executors.newScheduledThreadPool(1);<br \/>\n        scheduler.schedule(() -&gt; {<br \/>\n            redisTemplate.delete(cacheKey);<br \/>\n        }, 500, TimeUnit.MILLISECONDS);<br \/>\n    }<br \/>\n}<\/p>\n<p>\u66f4\u53ef\u9760\u7684\u65b9\u6848&#xff1a;\u76d1\u542cMySQL binlog&#xff08;\u4f7f\u7528Canal&#xff09;&#xff0c;\u5f02\u6b65\u5220\u9664\u7f13\u5b58\u3002<\/p>\n<p>\u64cd\u4f5c\u6b65\u9aa4&#xff1a;<\/p>\n<li>\n<p>\u90e8\u7f72Canal&#xff0c;\u4f2a\u88c5\u4e3aMySQL slave&#xff0c;\u63a5\u6536binlog\u53d8\u66f4\u3002<\/p>\n<\/li>\n<li>\n<p>\u5728Java\u5e94\u7528\u4e2d\u96c6\u6210Canal\u5ba2\u6237\u7aef&#xff0c;\u89e3\u6790binlog\u4e2d\u7684\u66f4\u65b0\u4e8b\u4ef6\u3002<\/p>\n<\/li>\n<li>\n<p>\u6839\u636e\u53d8\u66f4\u7684\u6570\u636e\u6784\u9020\u7f13\u5b58key&#xff0c;\u5e76\u5220\u9664\u7f13\u5b58\u3002<\/p>\n<\/li>\n<p>Java\u4ee3\u7801\u793a\u4f8b&#xff08;Canal\u5ba2\u6237\u7aef&#xff09;&#xff1a;<\/p>\n<p>java<\/p>\n<p>import com.alibaba.otter.canal.client.CanalConnector;<br \/>\nimport com.alibaba.otter.canal.client.CanalConnectors;<br \/>\nimport com.alibaba.otter.canal.protocol.Message;<br \/>\nimport com.alibaba.otter.canal.protocol.CanalEntry.*;<br \/>\nimport org.springframework.beans.factory.annotation.Autowired;<br \/>\nimport org.springframework.stereotype.Component;<br \/>\nimport javax.annotation.PostConstruct;<br \/>\nimport java.net.InetSocketAddress;<br \/>\nimport java.util.List;<\/p>\n<p>&#064;Component<br \/>\npublic class CanalClient {<br \/>\n    &#064;Autowired<br \/>\n    private RedisTemplate&lt;String, Object&gt; redisTemplate;<\/p>\n<p>    &#064;PostConstruct<br \/>\n    public void run() {<br \/>\n        CanalConnector connector &#061; CanalConnectors.newSingleConnector(<br \/>\n                new InetSocketAddress(&#034;127.0.0.1&#034;, 11111), &#034;example&#034;, &#034;&#034;, &#034;&#034;);<br \/>\n        while (true) {<br \/>\n            connector.connect();<br \/>\n            connector.subscribe(&#034;.*\\\\\\\\..*&#034;);<br \/>\n            connector.rollback();<br \/>\n            while (true) {<br \/>\n                Message message &#061; connector.getWithoutAck(100); \/\/ \u83b7\u53d6100\u6761\u6570\u636e<br \/>\n                long batchId &#061; message.getId();<br \/>\n                if (batchId &#061;&#061; -1 || message.getEntries().isEmpty()) {<br \/>\n                    try { Thread.sleep(1000); } catch (InterruptedException e) {}<br \/>\n                    continue;<br \/>\n                }<br \/>\n                List&lt;Entry&gt; entries &#061; message.getEntries();<br \/>\n                for (Entry entry : entries) {<br \/>\n                    if (entry.getEntryType() &#061;&#061; EntryType.ROWDATA) {<br \/>\n                        RowChange rowChange;<br \/>\n                        try {<br \/>\n                            rowChange &#061; RowChange.parseFrom(entry.getStoreValue());<br \/>\n                        } catch (Exception e) {<br \/>\n                            continue;<br \/>\n                        }<br \/>\n                        EventType eventType &#061; rowChange.getEventType();<br \/>\n                        if (eventType &#061;&#061; EventType.UPDATE || eventType &#061;&#061; EventType.INSERT || eventType &#061;&#061; EventType.DELETE) {<br \/>\n                            String tableName &#061; entry.getHeader().getTableName();<br \/>\n                            for (RowData rowData : rowChange.getRowDatasList()) {<br \/>\n                                if (&#034;product&#034;.equals(tableName)) {<br \/>\n                                    \/\/ \u83b7\u53d6\u4e3b\u952eID&#xff08;\u5047\u8bbe\u5217\u540d\u4e3aid&#xff09;<br \/>\n                                    String id &#061; null;<br \/>\n                                    for (Column col : rowData.getAfterColumnsList()) {<br \/>\n                                        if (&#034;id&#034;.equals(col.getName())) {<br \/>\n                                            id &#061; col.getValue();<br \/>\n                                            break;<br \/>\n                                        }<br \/>\n                                    }<br \/>\n                                    if (id !&#061; null) {<br \/>\n                                        redisTemplate.delete(&#034;product:&#034; &#043; id);<br \/>\n                                    }<br \/>\n                                }<br \/>\n                            }<br \/>\n                        }<br \/>\n                    }<br \/>\n                }<br \/>\n                connector.ack(batchId);<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<hr \/>\n<h3>\u4e8c\u3001\u6d88\u606f\u4e2d\u95f4\u4ef6\u95ee\u9898<\/h3>\n<h4>2.1 \u6d88\u606f\u4e22\u5931<\/h4>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u751f\u4ea7\u8005\u542f\u7528\u786e\u8ba4\u6a21\u5f0f&#xff0c;\u6d88\u8d39\u8005\u624b\u52a8\u63d0\u4ea4offset&#xff0c;MQ\u6301\u4e45\u5316\u3002<\/p>\n<p>\u64cd\u4f5c\u6b65\u9aa4&#xff08;\u4ee5RocketMQ\u4e3a\u4f8b&#xff09;&#xff1a;<\/p>\n<li>\n<p>\u751f\u4ea7\u8005\u8bbe\u7f6esetRetryTimesWhenSendFailed&#xff0c;\u5e76\u4f7f\u7528\u540c\u6b65\u53d1\u9001\u7b49\u5f85\u53d1\u9001\u7ed3\u679c\u3002<\/p>\n<\/li>\n<li>\n<p>\u6d88\u8d39\u8005\u8bbe\u7f6e\u6d88\u8d39\u6a21\u5f0f\u4e3aConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET&#xff0c;\u5e76\u624b\u52a8\u63d0\u4ea4offset\u3002<\/p>\n<\/li>\n<li>\n<p>\u6d88\u606f\u5904\u7406\u6210\u529f\u540e&#xff0c;\u518d\u6267\u884ccommitSync()\u3002<\/p>\n<\/li>\n<p>Java\u4ee3\u7801\u793a\u4f8b&#xff08;RocketMQ\u751f\u4ea7\u8005&#xff09;&#xff1a;<\/p>\n<p>java<\/p>\n<p>import org.apache.rocketmq.client.producer.DefaultMQProducer;<br \/>\nimport org.apache.rocketmq.client.producer.SendResult;<br \/>\nimport org.apache.rocketmq.common.message.Message;<\/p>\n<p>public class OrderProducer {<br \/>\n    public static void main(String[] args) throws Exception {<br \/>\n        DefaultMQProducer producer &#061; new DefaultMQProducer(&#034;order_producer_group&#034;);<br \/>\n        producer.setNamesrvAddr(&#034;127.0.0.1:9876&#034;);<br \/>\n        producer.setRetryTimesWhenSendFailed(3);<br \/>\n        producer.start();<br \/>\n        Message msg &#061; new Message(&#034;order_topic&#034;, &#034;order&#034;, &#034;orderId_123&#034;.getBytes());<br \/>\n        SendResult result &#061; producer.send(msg);<br \/>\n        if (result.getSendStatus() &#061;&#061; SendStatus.SEND_OK) {<br \/>\n            System.out.println(&#034;\u6d88\u606f\u53d1\u9001\u6210\u529f&#034;);<br \/>\n        } else {<br \/>\n            System.out.println(&#034;\u6d88\u606f\u53d1\u9001\u5931\u8d25&#xff0c;\u9700\u91cd\u8bd5\u6216\u8bb0\u5f55\u65e5\u5fd7&#034;);<br \/>\n        }<br \/>\n        producer.shutdown();<br \/>\n    }<br \/>\n}<\/p>\n<p>\u6d88\u8d39\u8005&#xff08;\u624b\u52a8ack&#xff09;&#xff1a;<\/p>\n<p>java<\/p>\n<p>import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;<br \/>\nimport org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;<br \/>\nimport org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;<br \/>\nimport org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;<br \/>\nimport org.apache.rocketmq.common.message.MessageExt;<br \/>\nimport java.util.List;<\/p>\n<p>public class OrderConsumer {<br \/>\n    public static void main(String[] args) throws Exception {<br \/>\n        DefaultMQPushConsumer consumer &#061; new DefaultMQPushConsumer(&#034;order_consumer_group&#034;);<br \/>\n        consumer.setNamesrvAddr(&#034;127.0.0.1:9876&#034;);<br \/>\n        consumer.subscribe(&#034;order_topic&#034;, &#034;*&#034;);<br \/>\n        consumer.registerMessageListener(new MessageListenerConcurrently() {<br \/>\n            &#064;Override<br \/>\n            public ConsumeConcurrentlyStatus consumeMessage(List&lt;MessageExt&gt; msgs, ConsumeConcurrentlyContext context) {<br \/>\n                for (MessageExt msg : msgs) {<br \/>\n                    try {<br \/>\n                        \/\/ \u5904\u7406\u6d88\u606f<br \/>\n                        process(msg);<br \/>\n                    } catch (Exception e) {<br \/>\n                        \/\/ \u5904\u7406\u5931\u8d25&#xff0c;\u7a0d\u540e\u91cd\u8bd5<br \/>\n                        return ConsumeConcurrentlyStatus.RECONSUME_LATER;<br \/>\n                    }<br \/>\n                }<br \/>\n                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS; \/\/ \u624b\u52a8ack<br \/>\n            }<br \/>\n        });<br \/>\n        consumer.start();<br \/>\n    }<br \/>\n}<\/p>\n<hr \/>\n<h4>2.2 \u6d88\u606f\u91cd\u590d\u6d88\u8d39<\/h4>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u5e42\u7b49\u6027\u8bbe\u8ba1&#xff08;\u4f7f\u7528\u4e1a\u52a1\u552f\u4e00ID\u53bb\u91cd&#xff09;\u3002<\/p>\n<p>\u64cd\u4f5c\u6b65\u9aa4&#xff1a;<\/p>\n<li>\n<p>\u5728\u6d88\u606f\u4f53\u4e2d\u5305\u542b\u4e00\u4e2a\u5168\u5c40\u552f\u4e00\u7684\u4e1a\u52a1ID&#xff08;\u5982\u8ba2\u5355\u53f7\u3001\u6d88\u606fID&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u6d88\u8d39\u524d&#xff0c;\u5148\u68c0\u67e5\u8be5ID\u662f\u5426\u5df2\u5904\u7406\u8fc7&#xff08;\u4f7f\u7528\u6570\u636e\u5e93\u552f\u4e00\u7d22\u5f15\u6216Redis setnx&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u5df2\u5904\u7406&#xff0c;\u5219\u5ffd\u7565&#xff1b;\u5426\u5219\u6267\u884c\u4e1a\u52a1\u903b\u8f91&#xff0c;\u5e76\u8bb0\u5f55\u5904\u7406\u72b6\u6001\u3002<\/p>\n<\/li>\n<p>Java\u4ee3\u7801\u793a\u4f8b&#xff08;Redis\u53bb\u91cd&#xff09;&#xff1a;<\/p>\n<p>java<\/p>\n<p>&#064;Service<br \/>\npublic class OrderMessageConsumer {<br \/>\n    &#064;Autowired<br \/>\n    private RedisTemplate&lt;String, String&gt; redisTemplate;<\/p>\n<p>    public void handleMessage(MessageExt msg) {<br \/>\n        String msgId &#061; msg.getKeys(); \/\/ RocketMQ\u7684\u6d88\u606fkey\u4f5c\u4e3a\u552f\u4e00ID<br \/>\n        \/\/ \u4f7f\u7528Redis\u7684setnx&#xff0c;\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4&#xff08;\u9632\u6b62\u65e0\u9650\u589e\u957f&#xff09;<br \/>\n        Boolean success &#061; redisTemplate.opsForValue().setIfAbsent(&#034;processed:msg:&#034; &#043; msgId, &#034;1&#034;, 1, TimeUnit.DAYS);<br \/>\n        if (success !&#061; null &amp;&amp; success) {<br \/>\n            \/\/ \u7b2c\u4e00\u6b21\u5904\u7406<br \/>\n            processOrder(msg);<br \/>\n        } else {<br \/>\n            \/\/ \u5df2\u7ecf\u5904\u7406\u8fc7&#xff0c;\u76f4\u63a5\u5ffd\u7565<br \/>\n            System.out.println(&#034;\u91cd\u590d\u6d88\u606f&#xff0c;\u5df2\u5ffd\u7565&#xff1a;&#034; &#043; msgId);<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<hr \/>\n<h4>2.3 \u6d88\u606f\u79ef\u538b<\/h4>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u4e34\u65f6\u589e\u52a0\u6d88\u8d39\u8005\u6570\u91cf&#xff0c;\u5e76\u4f18\u5316\u6d88\u8d39\u903b\u8f91&#xff08;\u6279\u91cf\u6d88\u8d39&#xff09;\u3002<\/p>\n<p>\u64cd\u4f5c\u6b65\u9aa4&#xff1a;<\/p>\n<li>\n<p>\u6269\u5bb9\u6d88\u8d39\u8005\u5b9e\u4f8b&#xff08;\u6c34\u5e73\u6269\u5c55&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u8c03\u6574\u6d88\u8d39\u8005\u7684\u6d88\u8d39\u7ebf\u7a0b\u6570&#xff08;\u5982RocketMQ\u7684consumeThreadMin\u548cconsumeThreadMax&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528\u6279\u91cf\u62c9\u53d6\u65b9\u5f0f&#xff0c;\u4e00\u6b21\u5904\u7406\u591a\u6761\u6d88\u606f\u3002<\/p>\n<\/li>\n<p>Java\u4ee3\u7801\u793a\u4f8b&#xff08;RocketMQ\u6279\u91cf\u6d88\u8d39\u8bbe\u7f6e&#xff09;&#xff1a;<\/p>\n<p>java<\/p>\n<p>DefaultMQPushConsumer consumer &#061; new DefaultMQPushConsumer(&#034;consumer_group&#034;);<br \/>\nconsumer.setConsumeThreadMin(20);<br \/>\nconsumer.setConsumeThreadMax(20);<br \/>\nconsumer.setConsumeMessageBatchMaxSize(10); \/\/ \u6bcf\u6b21\u6700\u591a\u62c9\u53d610\u6761<\/p>\n<p>\u6279\u91cf\u5904\u7406\u903b\u8f91&#xff1a;<\/p>\n<p>java<\/p>\n<p>public ConsumeConcurrentlyStatus consumeMessage(List&lt;MessageExt&gt; msgs, ConsumeConcurrentlyContext context) {<br \/>\n    \/\/ \u6279\u91cf\u5904\u7406&#xff0c;\u4f8b\u5982\u6279\u91cf\u63d2\u5165\u6570\u636e\u5e93<br \/>\n    List&lt;Order&gt; orders &#061; parseToOrders(msgs);<br \/>\n    orderDao.batchInsert(orders);<br \/>\n    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;<br \/>\n}<\/p>\n<hr \/>\n<h4>2.4 \u6d88\u606f\u4e71\u5e8f<\/h4>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u4f7f\u7528\u987a\u5e8f\u6d88\u606f&#xff08;RocketMQ\u7684MessageQueueSelector\u5c06\u76f8\u540c\u4e1a\u52a1ID\u7684\u6d88\u606f\u53d1\u9001\u5230\u540c\u4e00\u961f\u5217&#xff0c;\u6d88\u8d39\u8005\u5355\u7ebf\u7a0b\u6d88\u8d39\u8be5\u961f\u5217&#xff09;\u3002<\/p>\n<p>\u64cd\u4f5c\u6b65\u9aa4&#xff1a;<\/p>\n<li>\n<p>\u751f\u4ea7\u8005\u6307\u5b9a\u961f\u5217\u9009\u62e9\u5668&#xff0c;\u4fdd\u8bc1\u76f8\u540c\u8ba2\u5355\u53f7\u7684\u6d88\u606f\u8fdb\u5165\u540c\u4e00\u961f\u5217\u3002<\/p>\n<\/li>\n<li>\n<p>\u6d88\u8d39\u8005\u6ce8\u518cMessageListenerOrderly&#xff0c;\u6bcf\u4e2a\u961f\u5217\u53ea\u6709\u4e00\u4e2a\u7ebf\u7a0b\u6d88\u8d39&#xff0c;\u4fdd\u8bc1\u987a\u5e8f\u3002<\/p>\n<\/li>\n<p>Java\u4ee3\u7801\u793a\u4f8b&#xff08;\u751f\u4ea7\u8005\u987a\u5e8f\u6d88\u606f&#xff09;&#xff1a;<\/p>\n<p>java<\/p>\n<p>SendResult sendResult &#061; producer.send(msg, new MessageQueueSelector() {<br \/>\n    &#064;Override<br \/>\n    public MessageQueue select(List&lt;MessageQueue&gt; mqs, Message msg, Object arg) {<br \/>\n        Long orderId &#061; (Long) arg; \/\/ \u8ba2\u5355\u53f7\u4f5c\u4e3a\u9009\u62e9\u53c2\u6570<br \/>\n        int index &#061; (int) (orderId % mqs.size());<br \/>\n        return mqs.get(index);<br \/>\n    }<br \/>\n}, orderId);<\/p>\n<p>\u6d88\u8d39\u8005\u987a\u5e8f\u6d88\u8d39&#xff1a;<\/p>\n<p>java<\/p>\n<p>consumer.registerMessageListener(new MessageListenerOrderly() {<br \/>\n    &#064;Override<br \/>\n    public ConsumeOrderlyStatus consumeMessage(List&lt;MessageExt&gt; msgs, ConsumeOrderlyContext context) {<br \/>\n        for (MessageExt msg : msgs) {<br \/>\n            process(msg); \/\/ \u5355\u7ebf\u7a0b\u5904\u7406\u540c\u4e00\u961f\u5217\u7684\u6d88\u606f<br \/>\n        }<br \/>\n        return ConsumeOrderlyStatus.SUCCESS;<br \/>\n    }<br \/>\n});<\/p>\n<hr \/>\n<h4>2.5 \u6d88\u606f\u56de\u6eda&#xff08;\u56de\u6f9c&#xff09;<\/h4>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u4f7f\u7528\u5206\u5e03\u5f0f\u4e8b\u52a1\u6d88\u606f&#xff08;RocketMQ\u4e8b\u52a1\u6d88\u606f&#xff09;\u3002<\/p>\n<p>\u64cd\u4f5c\u6b65\u9aa4&#xff1a;<\/p>\n<li>\n<p>\u53d1\u9001\u534a\u6d88\u606f&#xff08;prepare&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u6267\u884c\u672c\u5730\u4e8b\u52a1\u3002<\/p>\n<\/li>\n<li>\n<p>\u6839\u636e\u672c\u5730\u4e8b\u52a1\u7ed3\u679c\u63d0\u4ea4\u6216\u56de\u6eda\u6d88\u606f\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u672c\u5730\u4e8b\u52a1\u72b6\u6001\u672a\u77e5&#xff0c;MQ\u4f1a\u56de\u67e5\u751f\u4ea7\u8005\u786e\u8ba4\u3002<\/p>\n<\/li>\n<p>Java\u4ee3\u7801\u793a\u4f8b&#xff08;RocketMQ\u4e8b\u52a1\u6d88\u606f\u751f\u4ea7\u8005&#xff09;&#xff1a;<\/p>\n<p>java<\/p>\n<p>import org.apache.rocketmq.client.producer.TransactionMQProducer;<br \/>\nimport org.apache.rocketmq.client.producer.TransactionSendResult;<br \/>\nimport org.apache.rocketmq.common.message.Message;<\/p>\n<p>public class TransactionProducer {<br \/>\n    public static void main(String[] args) throws Exception {<br \/>\n        TransactionMQProducer producer &#061; new TransactionMQProducer(&#034;transaction_producer_group&#034;);<br \/>\n        producer.setNamesrvAddr(&#034;127.0.0.1:9876&#034;);<br \/>\n        producer.setTransactionListener(new TransactionListener() {<br \/>\n            &#064;Override<br \/>\n            public LocalTransactionState executeLocalTransaction(Message msg, Object arg) {<br \/>\n                \/\/ \u6267\u884c\u672c\u5730\u4e8b\u52a1&#xff08;\u5982\u6570\u636e\u5e93\u64cd\u4f5c&#xff09;<br \/>\n                try {<br \/>\n                    \/\/ \u66f4\u65b0\u8ba2\u5355\u72b6\u6001<br \/>\n                    updateOrderStatus((String) arg);<br \/>\n                    return LocalTransactionState.COMMIT_MESSAGE;<br \/>\n                } catch (Exception e) {<br \/>\n                    return LocalTransactionState.ROLLBACK_MESSAGE;<br \/>\n                }<br \/>\n            }<\/p>\n<p>            &#064;Override<br \/>\n            public LocalTransactionState checkLocalTransaction(MessageExt msg) {<br \/>\n                \/\/ \u56de\u67e5\u672c\u5730\u4e8b\u52a1\u72b6\u6001<br \/>\n                String orderId &#061; msg.getKeys();<br \/>\n                if (isOrderCommitted(orderId)) {<br \/>\n                    return LocalTransactionState.COMMIT_MESSAGE;<br \/>\n                } else {<br \/>\n                    return LocalTransactionState.ROLLBACK_MESSAGE;<br \/>\n                }<br \/>\n            }<br \/>\n        });<br \/>\n        producer.start();<br \/>\n        Message msg &#061; new Message(&#034;order_topic&#034;, &#034;order&#034;, &#034;order_123&#034;.getBytes());<br \/>\n        TransactionSendResult result &#061; producer.sendMessageInTransaction(msg, &#034;order_123&#034;);<br \/>\n        producer.shutdown();<br \/>\n    }<br \/>\n}<\/p>\n<hr \/>\n<h3>\u4e09\u3001\u5206\u5e03\u5f0f\u95ee\u9898<\/h3>\n<h4>3.1 \u8111\u88c2<\/h4>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u4f7f\u7528Zookeeper\u7b49\u5f3a\u4e00\u81f4\u6027\u670d\u52a1&#xff0c;\u914d\u7f6e\u6cd5\u5b9a\u4eba\u6570&#xff08;quorum&#xff09;&#xff0c;\u5e76\u76d1\u63a7\u96c6\u7fa4\u72b6\u6001\u3002<\/p>\n<p>\u64cd\u4f5c\u6b65\u9aa4&#xff08;\u4ee5Zookeeper\u4e3a\u4f8b&#xff09;&#xff1a;<\/p>\n<li>\n<p>\u914d\u7f6eZookeeper\u96c6\u7fa4\u7684zoo.cfg&#xff0c;\u8bbe\u7f6einitLimit\u3001syncLimit\u548cserver.x\u5217\u8868\u3002<\/p>\n<\/li>\n<li>\n<p>\u5ba2\u6237\u7aef\u8fde\u63a5Zookeeper&#xff0c;\u76d1\u542c\u96c6\u7fa4\u8282\u70b9\u53d8\u5316\u3002<\/p>\n<\/li>\n<li>\n<p>\u5f53\u68c0\u6d4b\u5230\u96c6\u7fa4\u5206\u88c2\u65f6&#xff0c;\u901a\u8fc7\u9009\u4e3e\u7b97\u6cd5\u786e\u4fdd\u53ea\u6709\u4e00\u4e2a\u4e3b\u8282\u70b9\u3002<\/p>\n<\/li>\n<p>Java\u4ee3\u7801\u793a\u4f8b&#xff08;Zookeeper\u5ba2\u6237\u7aef\u76d1\u63a7\u8282\u70b9\u53d8\u5316&#xff09;&#xff1a;<\/p>\n<p>java<\/p>\n<p>import org.apache.zookeeper.*;<\/p>\n<p>public class ZookeeperWatcher implements Watcher {<br \/>\n    private ZooKeeper zooKeeper;<\/p>\n<p>    public void connect() throws Exception {<br \/>\n        zooKeeper &#061; new ZooKeeper(&#034;127.0.0.1:2181&#034;, 3000, this);<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    public void process(WatchedEvent event) {<br \/>\n        if (event.getType() &#061;&#061; Event.EventType.None &amp;&amp; event.getState() &#061;&#061; Event.KeeperState.Disconnected) {<br \/>\n            System.out.println(&#034;Zookeeper\u8fde\u63a5\u65ad\u5f00&#xff0c;\u53ef\u80fd\u53d1\u751f\u8111\u88c2&#034;);<br \/>\n            \/\/ \u89e6\u53d1\u544a\u8b66\u6216\u5207\u6362\u7b56\u7565<br \/>\n        }<br \/>\n    }<\/p>\n<p>    \/\/ \u521b\u5efa\u4e34\u65f6\u8282\u70b9\u4f5c\u4e3aleader\u9009\u4e3e<br \/>\n    public void electLeader() throws Exception {<br \/>\n        String path &#061; &#034;\/election\/node-&#034;;<br \/>\n        zooKeeper.create(path, &#034;data&#034;.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);<br \/>\n        \/\/ \u83b7\u53d6\u6240\u6709\u5b50\u8282\u70b9&#xff0c;\u6700\u5c0f\u7684\u4e3aleader<br \/>\n        \/\/ &#8230; \u5177\u4f53\u9009\u4e3e\u903b\u8f91<br \/>\n    }<br \/>\n}<\/p>\n<hr \/>\n<h4>3.2 \u7f8a\u7fa4\u6548\u5e94<\/h4>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u4f7f\u7528Zookeeper\u65f6&#xff0c;\u6240\u6709\u5ba2\u6237\u7aef\u76d1\u542c\u540c\u4e00\u8282\u70b9&#xff0c;\u8282\u70b9\u53d8\u5316\u65f6\u5927\u91cf\u5ba2\u6237\u7aef\u88ab\u5524\u9192\u3002\u53ef\u4ee5\u6539\u4e3a\u6bcf\u4e2a\u5ba2\u6237\u7aef\u76d1\u542c\u81ea\u5df1\u7684\u4e13\u5c5e\u5b50\u8282\u70b9&#xff0c;\u6216\u8005\u6dfb\u52a0\u968f\u673a\u5ef6\u8fdf\u3002<\/p>\n<p>\u64cd\u4f5c\u6b65\u9aa4&#xff1a;<\/p>\n<li>\n<p>\u5ba2\u6237\u7aef\u521b\u5efa\u81ea\u5df1\u7684\u4e34\u65f6\u987a\u5e8f\u8282\u70b9\u3002<\/p>\n<\/li>\n<li>\n<p>\u53ea\u76d1\u542c\u524d\u4e00\u4e2a\u8282\u70b9\u7684\u53d8\u5316&#xff0c;\u800c\u4e0d\u662f\u7236\u8282\u70b9\u3002<\/p>\n<\/li>\n<li>\n<p>\u5f53\u524d\u4e00\u4e2a\u8282\u70b9\u6d88\u5931\u65f6&#xff0c;\u624d\u89e6\u53d1\u5f53\u524d\u8282\u70b9\u7684\u64cd\u4f5c\u3002<\/p>\n<\/li>\n<p>Java\u4ee3\u7801\u793a\u4f8b&#xff08;Zookeeper\u5206\u5e03\u5f0f\u9501\u907f\u514d\u7f8a\u7fa4\u6548\u5e94&#xff09;&#xff1a;<\/p>\n<p>java<\/p>\n<p>public class DistributedLock {<br \/>\n    private ZooKeeper zooKeeper;<br \/>\n    private String lockPath &#061; &#034;\/lock&#034;;<br \/>\n    private String currentPath;<br \/>\n    private String watchPath;<\/p>\n<p>    public void lock() throws Exception {<br \/>\n        currentPath &#061; zooKeeper.create(lockPath &#043; &#034;\/lock-&#034;, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);<br \/>\n        List&lt;String&gt; children &#061; zooKeeper.getChildren(lockPath, false);<br \/>\n        Collections.sort(children);<br \/>\n        if (currentPath.equals(lockPath &#043; &#034;\/&#034; &#043; children.get(0))) {<br \/>\n            \/\/ \u83b7\u5f97\u9501<br \/>\n            return;<br \/>\n        }<br \/>\n        \/\/ \u76d1\u542c\u524d\u4e00\u4e2a\u8282\u70b9<br \/>\n        int index &#061; children.indexOf(currentPath.substring(lockPath.length() &#043; 1));<br \/>\n        watchPath &#061; lockPath &#043; &#034;\/&#034; &#043; children.get(index &#8211; 1);<br \/>\n        zooKeeper.exists(watchPath, new Watcher() {<br \/>\n            &#064;Override<br \/>\n            public void process(WatchedEvent event) {<br \/>\n                if (event.getType() &#061;&#061; Event.EventType.NodeDeleted) {<br \/>\n                    \/\/ \u524d\u4e00\u4e2a\u8282\u70b9\u5220\u9664&#xff0c;\u5f53\u524d\u8282\u70b9\u83b7\u5f97\u9501<br \/>\n                    synchronized (DistributedLock.this) {<br \/>\n                        DistributedLock.this.notify();<br \/>\n                    }<br \/>\n                }<br \/>\n            }<br \/>\n        });<br \/>\n        synchronized (this) {<br \/>\n            wait();<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<hr \/>\n<h4>3.3 \u54c8\u5e0c\u78b0\u649e<\/h4>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u4f7f\u7528\u4e00\u81f4\u6027\u54c8\u5e0c\u7b97\u6cd5&#xff0c;\u5e76\u5f15\u5165\u865a\u62df\u8282\u70b9\u3002<\/p>\n<p>\u64cd\u4f5c\u6b65\u9aa4&#xff1a;<\/p>\n<li>\n<p>\u6784\u5efa\u4e00\u4e2a\u4e00\u81f4\u6027\u54c8\u5e0c\u73af&#xff0c;\u5c06\u7269\u7406\u8282\u70b9\u6620\u5c04\u5230\u73af\u4e0a\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e3a\u6bcf\u4e2a\u7269\u7406\u8282\u70b9\u521b\u5efa\u591a\u4e2a\u865a\u62df\u8282\u70b9&#xff08;\u5982160\u4e2a&#xff09;&#xff0c;\u5206\u6563\u5728\u73af\u4e0a\u3002<\/p>\n<\/li>\n<li>\n<p>\u8ba1\u7b97key\u7684\u54c8\u5e0c\u503c&#xff0c;\u987a\u65f6\u9488\u627e\u5230\u6700\u8fd1\u7684\u865a\u62df\u8282\u70b9&#xff0c;\u518d\u6620\u5c04\u5230\u7269\u7406\u8282\u70b9\u3002<\/p>\n<\/li>\n<p>Java\u4ee3\u7801\u793a\u4f8b&#xff08;\u7b80\u5355\u4e00\u81f4\u6027\u54c8\u5e0c\u5b9e\u73b0&#xff09;&#xff1a;<\/p>\n<p>java<\/p>\n<p>import java.security.MessageDigest;<br \/>\nimport java.security.NoSuchAlgorithmException;<br \/>\nimport java.util.*;<\/p>\n<p>public class ConsistentHashing {<br \/>\n    private final TreeMap&lt;Long, String&gt; virtualNodes &#061; new TreeMap&lt;&gt;();<br \/>\n    private final int virtualReplicas &#061; 160;<br \/>\n    private final List&lt;String&gt; physicalNodes;<\/p>\n<p>    public ConsistentHashing(List&lt;String&gt; physicalNodes) {<br \/>\n        this.physicalNodes &#061; physicalNodes;<br \/>\n        for (String node : physicalNodes) {<br \/>\n            for (int i &#061; 0; i &lt; virtualReplicas; i&#043;&#043;) {<br \/>\n                long hash &#061; hash(node &#043; &#034;-&#034; &#043; i);<br \/>\n                virtualNodes.put(hash, node);<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    public String getNode(String key) {<br \/>\n        long hash &#061; hash(key);<br \/>\n        \/\/ \u8fd4\u56de\u5927\u4e8e\u7b49\u4e8ehash\u7684\u6700\u5c0f\u8282\u70b9<br \/>\n        Map.Entry&lt;Long, String&gt; entry &#061; virtualNodes.ceilingEntry(hash);<br \/>\n        if (entry &#061;&#061; null) {<br \/>\n            entry &#061; virtualNodes.firstEntry();<br \/>\n        }<br \/>\n        return entry.getValue();<br \/>\n    }<\/p>\n<p>    private long hash(String key) {<br \/>\n        try {<br \/>\n            MessageDigest md5 &#061; MessageDigest.getInstance(&#034;MD5&#034;);<br \/>\n            byte[] digest &#061; md5.digest(key.getBytes());<br \/>\n            return ((long) (digest[3] &amp; 0xFF) &lt;&lt; 24) | ((long) (digest[2] &amp; 0xFF) &lt;&lt; 16)<br \/>\n                    | ((long) (digest[1] &amp; 0xFF) &lt;&lt; 8) | (digest[0] &amp; 0xFF);<br \/>\n        } catch (NoSuchAlgorithmException e) {<br \/>\n            throw new RuntimeException(e);<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<hr \/>\n<h4>3.4 \u65f6\u949f\u56de\u62e8<\/h4>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u5728\u5206\u5e03\u5f0fID\u751f\u6210\u5668&#xff08;\u5982Snowflake&#xff09;\u4e2d\u68c0\u6d4b\u65f6\u949f\u56de\u62e8&#xff0c;\u5982\u679c\u56de\u62e8\u65f6\u95f4\u8f83\u77ed\u5219\u7b49\u5f85&#xff0c;\u5982\u679c\u8fc7\u957f\u5219\u629b\u51fa\u5f02\u5e38\u6216\u4f7f\u7528\u5907\u7528\u65b9\u6848\u3002<\/p>\n<p>\u64cd\u4f5c\u6b65\u9aa4&#xff1a;<\/p>\n<li>\n<p>\u8bb0\u5f55\u4e0a\u6b21\u751f\u6210ID\u7684\u65f6\u95f4\u6233\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u5f53\u524d\u65f6\u95f4\u5c0f\u4e8e\u4e0a\u6b21\u65f6\u95f4\u6233&#xff0c;\u8bf4\u660e\u53d1\u751f\u4e86\u65f6\u949f\u56de\u62e8\u3002<\/p>\n<\/li>\n<li>\n<p>\u6839\u636e\u56de\u62e8\u5927\u5c0f\u51b3\u5b9a\u7b56\u7565&#xff1a;\u5c0f\u5e45\u5ea6\u56de\u62e8&#xff08;&lt;5ms&#xff09;\u5219\u7b49\u5f85\u65f6\u95f4\u8ffd\u4e0a&#xff1b;\u5927\u5e45\u5ea6\u56de\u62e8\u5219\u62a5\u8b66\u5e76\u62d2\u7edd\u670d\u52a1\u3002<\/p>\n<\/li>\n<p>Java\u4ee3\u7801\u793a\u4f8b&#xff08;Snowflake\u7b97\u6cd5\u5904\u7406\u65f6\u949f\u56de\u62e8&#xff09;&#xff1a;<\/p>\n<p>java<\/p>\n<p>public class SnowflakeIdWorker {<br \/>\n    private final long twepoch &#061; 1288834974657L;<br \/>\n    private final long workerIdBits &#061; 5L;<br \/>\n    private final long sequenceBits &#061; 12L;<br \/>\n    private final long workerId;<br \/>\n    private volatile long lastTimestamp &#061; -1L;<br \/>\n    private volatile long sequence &#061; 0L;<\/p>\n<p>    public SnowflakeIdWorker(long workerId) {<br \/>\n        if (workerId &lt; 0 || workerId &gt; (-1L ^ (-1L &lt;&lt; workerIdBits))) {<br \/>\n            throw new IllegalArgumentException(&#034;workerId out of range&#034;);<br \/>\n        }<br \/>\n        this.workerId &#061; workerId;<br \/>\n    }<\/p>\n<p>    public synchronized long nextId() {<br \/>\n        long timestamp &#061; timeGen();<br \/>\n        \/\/ \u5982\u679c\u5f53\u524d\u65f6\u95f4\u5c0f\u4e8e\u4e0a\u6b21\u751f\u6210ID\u7684\u65f6\u95f4\u6233&#xff0c;\u8bf4\u660e\u65f6\u949f\u56de\u62e8<br \/>\n        if (timestamp &lt; lastTimestamp) {<br \/>\n            long offset &#061; lastTimestamp &#8211; timestamp;<br \/>\n            if (offset &lt; 5) {<br \/>\n                \/\/ \u5982\u679c\u56de\u62e8\u5c0f\u4e8e5ms&#xff0c;\u7b49\u5f85\u65f6\u95f4\u8ffd\u4e0a<br \/>\n                try {<br \/>\n                    wait(lastTimestamp &#8211; timestamp);<br \/>\n                } catch (InterruptedException e) {<br \/>\n                    Thread.currentThread().interrupt();<br \/>\n                }<br \/>\n                timestamp &#061; timeGen();<br \/>\n                \/\/ \u518d\u6b21\u5224\u65ad<br \/>\n                if (timestamp &lt; lastTimestamp) {<br \/>\n                    throw new RuntimeException(&#034;Clock moved backwards, refuse generate id&#034;);<br \/>\n                }<br \/>\n            } else {<br \/>\n                throw new RuntimeException(&#034;Clock moved backwards, refuse generate id&#034;);<br \/>\n            }<br \/>\n        }<\/p>\n<p>        if (lastTimestamp &#061;&#061; timestamp) {<br \/>\n            sequence &#061; (sequence &#043; 1) &amp; ((1 &lt;&lt; sequenceBits) &#8211; 1);<br \/>\n            if (sequence &#061;&#061; 0) {<br \/>\n                timestamp &#061; tilNextMillis(lastTimestamp);<br \/>\n            }<br \/>\n        } else {<br \/>\n            sequence &#061; 0L;<br \/>\n        }<br \/>\n        lastTimestamp &#061; timestamp;<br \/>\n        return ((timestamp &#8211; twepoch) &lt;&lt; (workerIdBits &#043; sequenceBits)) | (workerId &lt;&lt; sequenceBits) | sequence;<br \/>\n    }<\/p>\n<p>    private long tilNextMillis(long lastTimestamp) {<br \/>\n        long timestamp &#061; timeGen();<br \/>\n        while (timestamp &lt;&#061; lastTimestamp) {<br \/>\n            timestamp &#061; timeGen();<br \/>\n        }<br \/>\n        return timestamp;<br \/>\n    }<\/p>\n<p>    private long timeGen() {<br \/>\n        return System.currentTimeMillis();<br \/>\n    }<br \/>\n}<\/p>\n<hr \/>\n<h4>3.5 \u62d2\u7edd\u8fde\u63a5<\/h4>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u4f7f\u7528\u9650\u6d41&#xff08;\u5982\u4ee4\u724c\u6876\u7b97\u6cd5&#xff09;\u4fdd\u62a4\u670d\u52a1&#xff0c;\u9632\u6b62\u8fc7\u8f7d\u62d2\u7edd\u8fde\u63a5\u3002<\/p>\n<p>\u64cd\u4f5c\u6b65\u9aa4&#xff1a;<\/p>\n<li>\n<p>\u5f15\u5165Guava\u7684RateLimiter\u6216Sentinel\u3002<\/p>\n<\/li>\n<li>\n<p>\u5728\u670d\u52a1\u5165\u53e3&#xff08;\u5982Controller&#xff09;\u8fdb\u884c\u9650\u6d41\u5224\u65ad\u3002<\/p>\n<\/li>\n<li>\n<p>\u5982\u679c\u8d85\u8fc7\u9608\u503c&#xff0c;\u8fd4\u56de\u201c\u670d\u52a1\u7e41\u5fd9\u201d\u6216\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<\/li>\n<p>Java\u4ee3\u7801\u793a\u4f8b&#xff08;Guava RateLimiter&#xff09;&#xff1a;<\/p>\n<p>java<\/p>\n<p>import com.google.common.util.concurrent.RateLimiter;<br \/>\nimport org.springframework.web.bind.annotation.GetMapping;<br \/>\nimport org.springframework.web.bind.annotation.RestController;<\/p>\n<p>&#064;RestController<br \/>\npublic class ApiController {<br \/>\n    \/\/ \u6bcf\u79d2\u6700\u591a\u5904\u740610\u4e2a\u8bf7\u6c42<br \/>\n    private final RateLimiter rateLimiter &#061; RateLimiter.create(10.0);<\/p>\n<p>    &#064;GetMapping(&#034;\/api\/data&#034;)<br \/>\n    public String getData() {<br \/>\n        if (!rateLimiter.tryAcquire()) {<br \/>\n            return &#034;\u7cfb\u7edf\u7e41\u5fd9&#xff0c;\u8bf7\u7a0d\u540e\u91cd\u8bd5&#034;;<br \/>\n        }<br \/>\n        \/\/ \u6267\u884c\u4e1a\u52a1\u903b\u8f91<br \/>\n        return &#034;success&#034;;<br \/>\n    }<br \/>\n}<\/p>\n<p>\u4f7f\u7528Sentinel\u7684\u793a\u4f8b&#xff08;Spring Cloud Alibaba&#xff09;&#xff1a;<\/p>\n<p>java<\/p>\n<p>&#064;RestController<br \/>\npublic class SentinelController {<br \/>\n    &#064;GetMapping(&#034;\/hello&#034;)<br \/>\n    &#064;SentinelResource(value &#061; &#034;hello&#034;, blockHandler &#061; &#034;handleBlock&#034;)<br \/>\n    public String hello() {<br \/>\n        return &#034;Hello Sentinel&#034;;<br \/>\n    }<\/p>\n<p>    public String handleBlock(BlockException ex) {<br \/>\n        return &#034;\u9650\u6d41\u4e86&#034;;<br \/>\n    }<br \/>\n}<\/p>\n<hr \/>\n<h3>\u56db\u3001\u7cfb\u7edf\u95ee\u9898<\/h3>\n<h4>4.1 \u5185\u5b58\u6cc4\u6f0f<\/h4>\n<p>\u95ee\u9898\u63cf\u8ff0&#xff1a;\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4e0d\u518d\u4f7f\u7528\u7684\u5bf9\u8c61\u65e0\u6cd5\u88ab\u5783\u573e\u56de\u6536&#xff0c;\u5bfc\u81f4\u5806\u5185\u5b58\u4f7f\u7528\u6301\u7eed\u589e\u957f&#xff0c;\u6700\u7ec8\u5f15\u53d1OutOfMemoryError\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u901a\u8fc7\u5185\u5b58\u5206\u6790\u5de5\u5177&#xff08;\u5982MAT\u3001JProfiler&#xff09;\u5b9a\u4f4d\u6cc4\u6f0f\u70b9&#xff0c;\u4fee\u590d\u4ee3\u7801\u4e2d\u7684\u4e0d\u5408\u7406\u5f15\u7528\u3002<\/p>\n<p>\u64cd\u4f5c\u6b65\u9aa4&#xff1a;<\/p>\n<li>\n<p>\u76d1\u63a7\u5185\u5b58\u4f7f\u7528&#xff1a;\u4f7f\u7528JVM\u76d1\u63a7\u5de5\u5177&#xff08;\u5982jstat\u3001VisualVM&#xff09;\u89c2\u5bdf\u5806\u5185\u5b58\u53d8\u5316\u8d8b\u52bf\u3002<\/p>\n<\/li>\n<li>\n<p>\u751f\u6210Heap Dump&#xff1a;\u5f53\u5185\u5b58\u5f02\u5e38\u65f6&#xff0c;\u4f7f\u7528jmap\u6216\u914d\u7f6eJVM\u53c2\u6570-XX:&#043;HeapDumpOnOutOfMemoryError\u81ea\u52a8\u5bfc\u51fadump\u6587\u4ef6\u3002<\/p>\n<\/li>\n<li>\n<p>\u5206\u6790Heap Dump&#xff1a;\u4f7f\u7528MAT&#xff08;Eclipse Memory Analyzer&#xff09;\u6253\u5f00dump\u6587\u4ef6&#xff0c;\u67e5\u770b\u53ef\u7591\u5bf9\u8c61\u3001GC Roots\u5f15\u7528\u94fe\u3002<\/p>\n<\/li>\n<li>\n<p>\u5b9a\u4f4d\u95ee\u9898\u4ee3\u7801&#xff1a;\u6839\u636e\u5206\u6790\u7ed3\u679c\u627e\u5230\u672a\u91ca\u653e\u7684\u5f15\u7528&#xff08;\u5982ThreadLocal\u672a\u6e05\u7406\u3001\u9759\u6001\u96c6\u5408\u4e0d\u65ad\u6dfb\u52a0\u3001\u672a\u5173\u95ed\u7684\u8d44\u6e90\u7b49&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u4fee\u590d\u4ee3\u7801&#xff1a;\u4fee\u6539\u4ee3\u7801&#xff0c;\u786e\u4fdd\u5bf9\u8c61\u4f7f\u7528\u5b8c\u6bd5\u540e\u7f6e\u7a7a\u5f15\u7528\u6216\u8c03\u7528\u6e05\u7406\u65b9\u6cd5\u3002<\/p>\n<\/li>\n<p>Java\u4ee3\u7801\u793a\u4f8b&#xff1a;\u6a21\u62dfThreadLocal\u5185\u5b58\u6cc4\u6f0f\u53ca\u4fee\u590d\u3002<\/p>\n<p>\u6cc4\u6f0f\u4ee3\u7801&#xff1a;<\/p>\n<p>java<\/p>\n<p>public class ThreadLocalLeakDemo {<br \/>\n    private static final ThreadLocal&lt;byte[]&gt; threadLocal &#061; new ThreadLocal&lt;&gt;();<\/p>\n<p>    public void process() {<br \/>\n        \/\/ \u5047\u8bbe\u6bcf\u4e2a\u8bf7\u6c42\u90fd\u8bbe\u7f6e\u4e00\u4e2a\u5927\u5bf9\u8c61<br \/>\n        threadLocal.set(new byte[10 * 1024 * 1024]); \/\/ 10MB<br \/>\n        \/\/ \u6267\u884c\u4e1a\u52a1\u903b\u8f91&#8230;<br \/>\n        \/\/ \u5fd8\u8bb0\u8c03\u7528 remove()<br \/>\n    }<br \/>\n}<\/p>\n<p>\u4fee\u590d\u4ee3\u7801&#xff1a;<\/p>\n<p>java<\/p>\n<p>public class ThreadLocalLeakDemo {<br \/>\n    private static final ThreadLocal&lt;byte[]&gt; threadLocal &#061; new ThreadLocal&lt;&gt;();<\/p>\n<p>    public void process() {<br \/>\n        try {<br \/>\n            threadLocal.set(new byte[10 * 1024 * 1024]);<br \/>\n            \/\/ \u6267\u884c\u4e1a\u52a1\u903b\u8f91&#8230;<br \/>\n        } finally {<br \/>\n            threadLocal.remove(); \/\/ \u786e\u4fdd\u6e05\u7406<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>\u64cd\u4f5c\u6307\u4ee4&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528jmap\u751f\u6210\u5806dump&#xff1a;jmap -dump:live,format&#061;b,file&#061;heap.hprof &lt;pid&gt;<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528jstat\u89c2\u5bdfGC\u60c5\u51b5&#xff1a;jstat -gcutil &lt;pid&gt; 1000 10<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h4>4.2 \u91cd\u590d\u63d0\u4ea4<\/h4>\n<p>\u95ee\u9898\u63cf\u8ff0&#xff1a;\u7528\u6237\u77ed\u65f6\u95f4\u5185\u591a\u6b21\u63d0\u4ea4\u540c\u4e00\u8868\u5355\u6216\u8bf7\u6c42&#xff0c;\u5bfc\u81f4\u6570\u636e\u91cd\u590d&#xff08;\u5982\u91cd\u590d\u4e0b\u5355\u3001\u91cd\u590d\u8bc4\u8bba&#xff09;\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u524d\u7aef\u6309\u94ae\u7f6e\u7070 &#043; \u540e\u7aefToken\u673a\u5236&#xff08;\u9632\u91cd\u4ee4\u724c&#xff09;\u3002<\/p>\n<p>\u64cd\u4f5c\u6b65\u9aa4&#xff1a;<\/p>\n<li>\n<p>\u524d\u7aef&#xff1a;\u5728\u63d0\u4ea4\u6309\u94ae\u70b9\u51fb\u540e\u7acb\u5373\u7981\u7528\u6309\u94ae&#xff0c;\u9632\u6b62\u7528\u6237\u91cd\u590d\u70b9\u51fb\u3002<\/p>\n<\/li>\n<li>\n<p>\u540e\u7aef&#xff1a;\u751f\u6210\u4e00\u4e2a\u552f\u4e00\u7684Token&#xff08;\u5982UUID&#xff09;&#xff0c;\u5b58\u5165Redis\u5e76\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4&#xff0c;\u8fd4\u56de\u7ed9\u524d\u7aef\u3002<\/p>\n<\/li>\n<li>\n<p>\u8bf7\u6c42\u643a\u5e26Token&#xff1a;\u524d\u7aef\u5728\u63d0\u4ea4\u8bf7\u6c42\u65f6\u5e26\u4e0a\u8be5Token\u3002<\/p>\n<\/li>\n<li>\n<p>\u540e\u7aef\u6821\u9a8c&#xff1a;\u5904\u7406\u8bf7\u6c42\u524d\u5148\u6821\u9a8cToken\u662f\u5426\u5b58\u5728\u4e14\u6709\u6548&#xff0c;\u82e5\u6709\u6548\u5219\u5220\u9664Token\u5e76\u7ee7\u7eed\u5904\u7406&#xff1b;\u82e5\u65e0\u6548\u5219\u8fd4\u56de\u91cd\u590d\u63d0\u4ea4\u9519\u8bef\u3002<\/p>\n<\/li>\n<p>Java\u4ee3\u7801\u793a\u4f8b&#xff1a;<\/p>\n<p>\u751f\u6210Token\u63a5\u53e3&#xff1a;<\/p>\n<p>java<\/p>\n<p>&#064;RestController<br \/>\n&#064;RequestMapping(&#034;\/token&#034;)<br \/>\npublic class TokenController {<br \/>\n    &#064;Autowired<br \/>\n    private RedisTemplate&lt;String, String&gt; redisTemplate;<\/p>\n<p>    &#064;GetMapping(&#034;\/generate&#034;)<br \/>\n    public String generateToken() {<br \/>\n        String token &#061; UUID.randomUUID().toString();<br \/>\n        redisTemplate.opsForValue().set(token, &#034;1&#034;, 30, TimeUnit.MINUTES); \/\/ 30\u5206\u949f\u6709\u6548<br \/>\n        return token;<br \/>\n    }<br \/>\n}<\/p>\n<p>\u81ea\u5b9a\u4e49\u6ce8\u89e3&#043;\u62e6\u622a\u5668\u5b9e\u73b0Token\u6821\u9a8c&#xff1a;<\/p>\n<p>java<\/p>\n<p>&#064;Target(ElementType.METHOD)<br \/>\n&#064;Retention(RetentionPolicy.RUNTIME)<br \/>\npublic &#064;interface PreventDuplicateSubmit {<br \/>\n}<\/p>\n<p>&#064;Component<br \/>\npublic class DuplicateSubmitInterceptor implements HandlerInterceptor {<br \/>\n    &#064;Autowired<br \/>\n    private RedisTemplate&lt;String, String&gt; redisTemplate;<\/p>\n<p>    &#064;Override<br \/>\n    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {<br \/>\n        if (handler instanceof HandlerMethod) {<br \/>\n            HandlerMethod method &#061; (HandlerMethod) handler;<br \/>\n            if (method.hasMethodAnnotation(PreventDuplicateSubmit.class)) {<br \/>\n                String token &#061; request.getHeader(&#034;Request-Token&#034;);<br \/>\n                if (StringUtils.isEmpty(token)) {<br \/>\n                    throw new RuntimeException(&#034;\u7f3a\u5c11\u9632\u91cdToken&#034;);<br \/>\n                }<br \/>\n                \/\/ \u539f\u5b50\u6027\u5220\u9664&#xff08;\u4f7f\u7528lua\u811a\u672c\u6216setnx&#xff09;<br \/>\n                Boolean result &#061; redisTemplate.delete(token);<br \/>\n                if (Boolean.TRUE.equals(result)) {<br \/>\n                    return true; \/\/ \u7b2c\u4e00\u6b21\u63d0\u4ea4&#xff0c;\u653e\u884c<br \/>\n                } else {<br \/>\n                    response.setStatus(400);<br \/>\n                    response.getWriter().write(&#034;\u8bf7\u52ff\u91cd\u590d\u63d0\u4ea4&#034;);<br \/>\n                    return false;<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n        return true;<br \/>\n    }<br \/>\n}<\/p>\n<p>Controller\u4f7f\u7528&#xff1a;<\/p>\n<p>java<\/p>\n<p>&#064;PostMapping(&#034;\/order&#034;)<br \/>\n&#064;PreventDuplicateSubmit<br \/>\npublic String createOrder(&#064;RequestBody OrderDTO orderDTO) {<br \/>\n    \/\/ \u4e1a\u52a1\u903b\u8f91<br \/>\n    return &#034;success&#034;;<br \/>\n}<\/p>\n<hr \/>\n<h4>4.3 \u6307\u4ee4\u91cd\u6392<\/h4>\n<p>\u95ee\u9898\u63cf\u8ff0&#xff1a;\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b&#xff0c;\u7f16\u8bd1\u5668\u548cCPU\u4e3a\u4e86\u4f18\u5316\u53ef\u80fd\u5bf9\u6307\u4ee4\u8fdb\u884c\u91cd\u6392&#xff0c;\u5bfc\u81f4\u610f\u6599\u4e4b\u5916\u7684\u5e76\u53d1\u95ee\u9898&#xff08;\u5982\u53cc\u91cd\u68c0\u67e5\u9501\u5931\u6548&#xff09;\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u4f7f\u7528volatile\u5173\u952e\u5b57\u7981\u6b62\u6307\u4ee4\u91cd\u6392&#xff0c;\u6216\u4f7f\u7528AtomicInteger\u3001synchronized\u7b49\u5e76\u53d1\u5de5\u5177\u3002<\/p>\n<p>\u64cd\u4f5c\u6b65\u9aa4&#xff1a;<\/p>\n<li>\n<p>\u8bc6\u522b\u9700\u8981\u7981\u6b62\u91cd\u6392\u7684\u53d8\u91cf&#xff08;\u901a\u5e38\u662f\u88ab\u591a\u4e2a\u7ebf\u7a0b\u5171\u4eab\u7684\u72b6\u6001\u6807\u5fd7&#xff09;\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528volatile\u4fee\u9970\u8be5\u53d8\u91cf\u3002<\/p>\n<\/li>\n<li>\n<p>\u5bf9\u4e8e\u590d\u5408\u64cd\u4f5c&#xff0c;\u4f7f\u7528java.util.concurrent.atomic\u5305\u4e2d\u7684\u539f\u5b50\u7c7b\u6216\u9501\u3002<\/p>\n<\/li>\n<p>Java\u4ee3\u7801\u793a\u4f8b&#xff1a;<\/p>\n<p>\u53cc\u91cd\u68c0\u67e5\u9501\u5355\u4f8b\u6a21\u5f0f&#xff08;\u6b63\u786e\u4f7f\u7528volatile&#xff09;&#xff1a;<\/p>\n<p>java<\/p>\n<p>public class Singleton {<br \/>\n    private static volatile Singleton instance; \/\/ volatile\u7981\u6b62\u91cd\u6392<\/p>\n<p>    private Singleton() {}<\/p>\n<p>    public static Singleton getInstance() {<br \/>\n        if (instance &#061;&#061; null) {<br \/>\n            synchronized (Singleton.class) {<br \/>\n                if (instance &#061;&#061; null) {<br \/>\n                    instance &#061; new Singleton(); \/\/ \u4e0d\u4f1a\u91cd\u6392<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n        return instance;<br \/>\n    }<br \/>\n}<\/p>\n<p>\u5982\u679c\u4e0d\u52a0volatile&#xff0c;instance &#061; new Singleton()\u00a0\u53ef\u80fd\u88ab\u91cd\u6392\u4e3a&#xff1a;\u5206\u914d\u5185\u5b58 -&gt; \u5c06\u5f15\u7528\u8d4b\u7ed9instance -&gt; \u521d\u59cb\u5316\u5bf9\u8c61\u3002\u5176\u4ed6\u7ebf\u7a0b\u53ef\u80fd\u62ff\u5230\u672a\u521d\u59cb\u5316\u7684\u5bf9\u8c61\u3002<\/p>\n<hr \/>\n<h4>4.4 \u7cfb\u7edf\u5047\u6b7b<\/h4>\n<p>\u95ee\u9898\u63cf\u8ff0&#xff1a;\u7cfb\u7edf\u65e0\u54cd\u5e94&#xff08;\u4e0d\u5904\u7406\u8bf7\u6c42&#xff09;&#xff0c;\u4f46\u8fdb\u7a0b\u4ecd\u5728&#xff0c;CPU\/\u5185\u5b58\u53ef\u80fd\u6b63\u5e38&#xff0c;\u901a\u5e38\u662f\u7ebf\u7a0b\u6c60\u8017\u5c3d\u3001\u6b7b\u9501\u3001\u7f51\u7edc\u963b\u585e\u7b49\u539f\u56e0\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u76d1\u63a7\u7ebf\u7a0b\u72b6\u6001\u3001\u6b7b\u9501\u68c0\u6d4b\u3001\u914d\u7f6e\u5065\u5eb7\u68c0\u67e5\u5e76\u81ea\u52a8\u91cd\u542f\u3002<\/p>\n<p>\u64cd\u4f5c\u6b65\u9aa4&#xff1a;<\/p>\n<li>\n<p>\u68c0\u6d4b\u7ebf\u7a0b\u6c60\u8017\u5c3d&#xff1a;\u901a\u8fc7jstack\u67e5\u770b\u7ebf\u7a0b\u5806\u6808&#xff0c;\u786e\u8ba4\u5927\u91cf\u7ebf\u7a0b\u5728\u7b49\u5f85\u961f\u5217\u6216\u963b\u585e\u3002<\/p>\n<\/li>\n<li>\n<p>\u68c0\u6d4b\u6b7b\u9501&#xff1a;\u4f7f\u7528jstack\u6216JConsole\u68c0\u6d4b\u6b7b\u9501\u3002<\/p>\n<\/li>\n<li>\n<p>\u589e\u52a0\u76d1\u63a7&#xff1a;\u5728\u5e94\u7528\u4e2d\u66b4\u9732\u5065\u5eb7\u68c0\u67e5\u7aef\u70b9&#xff08;\u5982Spring Boot Actuator&#xff09;&#xff0c;\u7531\u5916\u90e8\u76d1\u63a7\u7cfb\u7edf&#xff08;\u5982Kubernetes liveness probe&#xff09;\u5b9a\u671f\u68c0\u67e5&#xff0c;\u53d1\u73b0\u5047\u6b7b\u540e\u81ea\u52a8\u91cd\u542f\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f18\u5316\u4ee3\u7801&#xff1a;\u8bbe\u7f6e\u5408\u7406\u7684\u7ebf\u7a0b\u6c60\u53c2\u6570\u3001\u907f\u514d\u540c\u6b65\u5757\u8fc7\u5927\u3001\u4f7f\u7528\u8d85\u65f6\u673a\u5236\u3002<\/p>\n<\/li>\n<p>Java\u4ee3\u7801\u793a\u4f8b&#xff1a;<\/p>\n<p>Spring Boot Actuator\u5065\u5eb7\u68c0\u67e5\u7aef\u70b9&#xff1a;<\/p>\n<p>yaml<\/p>\n<p># application.yml<br \/>\nmanagement:<br \/>\n  endpoints:<br \/>\n    web:<br \/>\n      exposure:<br \/>\n        include: health,info<\/p>\n<p>\u81ea\u5b9a\u4e49\u5065\u5eb7\u68c0\u67e5&#xff08;\u68c0\u6d4b\u6570\u636e\u5e93\u8fde\u63a5\u7b49&#xff09;&#xff1a;<\/p>\n<p>java<\/p>\n<p>&#064;Component<br \/>\npublic class CustomHealthIndicator implements HealthIndicator {<br \/>\n    &#064;Autowired<br \/>\n    private DataSource dataSource;<\/p>\n<p>    &#064;Override<br \/>\n    public Health health() {<br \/>\n        try {<br \/>\n            dataSource.getConnection().close(); \/\/ \u7b80\u5355\u6d4b\u8bd5\u8fde\u63a5<br \/>\n            return Health.up().build();<br \/>\n        } catch (Exception e) {<br \/>\n            return Health.down(e).build();<br \/>\n        }<br \/>\n    }<br \/>\n}<\/p>\n<p>Kubernetes\u63a2\u9488\u914d\u7f6e&#xff1a;<\/p>\n<p>yaml<\/p>\n<p>livenessProbe:<br \/>\n  httpGet:<br \/>\n    path: \/actuator\/health\/liveness<br \/>\n    port: 8080<br \/>\n  initialDelaySeconds: 30<br \/>\n  periodSeconds: 10<\/p>\n<p>\u6b7b\u9501\u68c0\u6d4b\u4ee3\u7801&#xff08;\u53ef\u653e\u5728\u76d1\u63a7\u811a\u672c\u4e2d&#xff09;&#xff1a;<\/p>\n<p>bash<\/p>\n<p># \u4f7f\u7528jstack\u68c0\u6d4b\u6b7b\u9501<br \/>\njstack -l &lt;pid&gt; | grep -A 10 &#034;Found one Java-level deadlock&#034;<\/p>\n<hr \/>\n<h4>4.5 \u6570\u636e\u503e\u659c<\/h4>\n<p>\u95ee\u9898\u63cf\u8ff0&#xff1a;\u5728\u5206\u5e03\u5f0f\u8ba1\u7b97&#xff08;\u5982MapReduce\u3001Spark\u3001Flink&#xff09;\u6216\u6570\u636e\u5e93\u5206\u5e93\u5206\u8868\u4e2d&#xff0c;\u6570\u636e\u5206\u5e03\u4e0d\u5747&#xff0c;\u5bfc\u81f4\u90e8\u5206\u8282\u70b9\u8d1f\u8f7d\u8fc7\u9ad8&#xff0c;\u5f71\u54cd\u6574\u4f53\u6027\u80fd\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u91cd\u65b0\u8bbe\u8ba1\u5206\u533a\u952e\u3001\u52a0\u76d0&#xff08;\u968f\u673a\u524d\u7f00&#xff09;\u3001\u4e8c\u6b21\u805a\u5408\u3001\u6269\u5bb9\u3002<\/p>\n<p>\u64cd\u4f5c\u6b65\u9aa4&#xff1a;<\/p>\n<li>\n<p>\u5206\u6790\u6570\u636e\u5206\u5e03&#xff1a;\u67e5\u770b\u5404\u4e2a\u5206\u533a\u7684\u6570\u636e\u91cf\u6216\u5904\u7406\u65f6\u95f4\u3002<\/p>\n<\/li>\n<li>\n<p>\u5b9a\u4f4d\u70ed\u70b9Key&#xff1a;\u5982\u67d0\u4e2a\u7528\u6237ID\u7684\u6570\u636e\u91cf\u7279\u522b\u5927\u3002<\/p>\n<\/li>\n<li>\n<p>\u52a0\u76d0\u5904\u7406&#xff1a;\u5728Key\u4e0a\u52a0\u968f\u673a\u540e\u7f00&#xff0c;\u5c06\u6570\u636e\u6253\u6563\u5230\u591a\u4e2a\u5206\u533a\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e8c\u6b21\u805a\u5408&#xff1a;\u7b2c\u4e00\u9636\u6bb5\u52a0\u76d0\u5904\u7406&#xff0c;\u7b2c\u4e8c\u9636\u6bb5\u53bb\u6389\u540e\u7f00\u5408\u5e76\u7ed3\u679c\u3002<\/p>\n<\/li>\n<li>\n<p>\u8c03\u6574\u5206\u533a\u952e&#xff1a;\u9009\u62e9\u5206\u5e03\u66f4\u5747\u5300\u7684\u5b57\u6bb5\u4f5c\u4e3a\u5206\u533a\u952e\u3002<\/p>\n<\/li>\n<p>Java\u4ee3\u7801\u793a\u4f8b&#xff08;Flink\u4e2d\u5904\u7406\u6570\u636e\u503e\u659c&#xff09;&#xff1a;<\/p>\n<p>\u539f\u59cbKeyBy\u53ef\u80fd\u5bfc\u81f4\u503e\u659c&#xff1a;<\/p>\n<p>java<\/p>\n<p>DataStream&lt;Order&gt; stream &#061; &#8230;;<br \/>\nstream.keyBy(Order::getUserId).process(new MyProcessFunction());<\/p>\n<p>\u52a0\u76d0\u65b9\u6848&#xff1a;<\/p>\n<p>java<\/p>\n<p>int parallelism &#061; 10; \/\/ \u5e76\u884c\u5ea6<br \/>\nDataStream&lt;Order&gt; stream &#061; &#8230;;<br \/>\nstream<br \/>\n    .map(order -&gt; {<br \/>\n        \/\/ \u52a0\u968f\u673a\u540e\u7f00&#xff08;0~parallelism-1&#xff09;<br \/>\n        String saltedKey &#061; order.getUserId() &#043; &#034;_&#034; &#043; ThreadLocalRandom.current().nextInt(parallelism);<br \/>\n        return Tuple2.of(saltedKey, order);<br \/>\n    })<br \/>\n    .keyBy(t -&gt; t.f0)<br \/>\n    .process(new SaltedProcessFunction())<br \/>\n    .map(t -&gt; t.f1); \/\/ \u53bb\u9664\u76d0<\/p>\n<p>\u5728\u6570\u636e\u5e93\u5206\u5e93\u5206\u8868\u4e2d\u7684\u5e94\u7528&#xff1a;\u5982\u679c\u67d0\u4e2a\u5546\u6237ID\u6570\u636e\u91cf\u6781\u5927&#xff0c;\u53ef\u4ee5\u5bf9\u8be5\u5546\u6237ID\u8fdb\u884c\u53d6\u6a21&#043;\u968f\u673a\u540e\u7f00&#xff0c;\u5c06\u6570\u636e\u5206\u6563\u5230\u591a\u4e2a\u5e93\u8868\u4e2d&#xff0c;\u67e5\u8be2\u65f6\u9700\u805a\u5408\u3002<\/p>\n<hr \/>\n<h3>\u4e94\u3001\u6545\u969c\u7ba1\u7406<\/h3>\n<h4>5.1 \u6545\u969c\u76d1\u63a7\u53d1\u73b0<\/h4>\n<p>\u95ee\u9898\u63cf\u8ff0&#xff1a;\u5982\u4f55\u7b2c\u4e00\u65f6\u95f4\u53d1\u73b0\u7cfb\u7edf\u5f02\u5e38&#xff0c;\u907f\u514d\u6545\u969c\u6269\u5927\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u5efa\u7acb\u591a\u7ef4\u5ea6\u76d1\u63a7\u4f53\u7cfb&#xff0c;\u5305\u62ec\u57fa\u7840\u76d1\u63a7&#xff08;CPU\u3001\u5185\u5b58&#xff09;\u3001\u5e94\u7528\u76d1\u63a7&#xff08;QPS\u3001\u5ef6\u8fdf\u3001\u9519\u8bef\u7387&#xff09;\u3001\u4e1a\u52a1\u76d1\u63a7&#xff08;\u8ba2\u5355\u91cf\u3001\u6210\u529f\u7387&#xff09;\u3002<\/p>\n<p>\u64cd\u4f5c\u6b65\u9aa4&#xff1a;<\/p>\n<li>\n<p>\u90e8\u7f72\u76d1\u63a7\u5de5\u5177&#xff1a;Prometheus &#043; Grafana \u6536\u96c6\u6307\u6807&#xff0c;ELK\u6536\u96c6\u65e5\u5fd7&#xff0c;SkyWalking\u8fdb\u884c\u94fe\u8def\u8ffd\u8e2a\u3002<\/p>\n<\/li>\n<li>\n<p>\u66b4\u9732\u6307\u6807&#xff1a;\u5728\u5e94\u7528\u4e2d\u96c6\u6210Micrometer&#xff0c;\u66b4\u9732\u81ea\u5b9a\u4e49\u6307\u6807\u3002<\/p>\n<\/li>\n<li>\n<p>\u8bbe\u7f6e\u544a\u8b66\u89c4\u5219&#xff1a;\u5728Prometheus\u4e2d\u914d\u7f6eAlertmanager&#xff0c;\u901a\u8fc7\u90ae\u4ef6\u3001\u9489\u9489\u7b49\u901a\u77e5\u3002<\/p>\n<\/li>\n<li>\n<p>\u914d\u7f6e\u4eea\u8868\u76d8&#xff1a;\u5728Grafana\u4e2d\u53ef\u89c6\u5316\u5173\u952e\u6307\u6807&#xff0c;\u65b9\u4fbf\u5feb\u901f\u5b9a\u4f4d\u95ee\u9898\u3002<\/p>\n<\/li>\n<p>Java\u4ee3\u7801\u793a\u4f8b&#xff08;Micrometer\u66b4\u9732\u6307\u6807&#xff09;&#xff1a;<\/p>\n<p>java<\/p>\n<p>import io.micrometer.core.instrument.MeterRegistry;<br \/>\nimport org.springframework.web.bind.annotation.GetMapping;<br \/>\nimport org.springframework.web.bind.annotation.RestController;<\/p>\n<p>&#064;RestController<br \/>\npublic class MetricController {<br \/>\n    private final MeterRegistry meterRegistry;<\/p>\n<p>    public MetricController(MeterRegistry meterRegistry) {<br \/>\n        this.meterRegistry &#061; meterRegistry;<br \/>\n    }<\/p>\n<p>    &#064;GetMapping(&#034;\/api\/order&#034;)<br \/>\n    public String createOrder() {<br \/>\n        \/\/ \u8bb0\u5f55\u8bf7\u6c42\u6570<br \/>\n        meterRegistry.counter(&#034;order.create.count&#034;).increment();<br \/>\n        \/\/ \u8bb0\u5f55\u8017\u65f6<br \/>\n        long start &#061; System.currentTimeMillis();<br \/>\n        \/\/ \u4e1a\u52a1\u903b\u8f91&#8230;<br \/>\n        long duration &#061; System.currentTimeMillis() &#8211; start;<br \/>\n        meterRegistry.timer(&#034;order.create.duration&#034;).record(duration, TimeUnit.MILLISECONDS);<br \/>\n        return &#034;success&#034;;<br \/>\n    }<br \/>\n}<\/p>\n<p>Prometheus\u914d\u7f6e\u793a\u4f8b&#xff1a;<\/p>\n<p>yaml<\/p>\n<p># prometheus.yml<br \/>\nscrape_configs:<br \/>\n  &#8211; job_name: &#039;spring-boot-app&#039;<br \/>\n    metrics_path: &#039;\/actuator\/prometheus&#039;<br \/>\n    static_configs:<br \/>\n      &#8211; targets: [&#039;localhost:8080&#039;]<\/p>\n<p>\u544a\u8b66\u89c4\u5219\u793a\u4f8b&#xff1a;<\/p>\n<p>yaml<\/p>\n<p>groups:<br \/>\n  &#8211; name: example<br \/>\n    rules:<br \/>\n      &#8211; alert: HighErrorRate<br \/>\n        expr: rate(http_server_requests_seconds_count{status&#061;~&#034;5..&#034;}[1m]) &gt; 0.05<br \/>\n        for: 1m<br \/>\n        annotations:<br \/>\n          summary: &#034;High error rate detected&#034;<\/p>\n<hr \/>\n<h4>5.2 \u6545\u969c\u5206\u6790\u4e0e\u5b9a\u4f4d<\/h4>\n<p>\u95ee\u9898\u63cf\u8ff0&#xff1a;\u6545\u969c\u53d1\u751f\u540e&#xff0c;\u5982\u4f55\u5feb\u901f\u5b9a\u4f4d\u6839\u672c\u539f\u56e0\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u5229\u7528\u65e5\u5fd7\u3001\u94fe\u8def\u8ffd\u8e2a\u3001\u7ebf\u7a0b\u5806\u6808\u3001\u6570\u636e\u5e93\u6162\u67e5\u8be2\u7b49\u5de5\u5177\u8fdb\u884c\u591a\u7ef4\u5ea6\u5206\u6790\u3002<\/p>\n<p>\u64cd\u4f5c\u6b65\u9aa4&#xff1a;<\/p>\n<li>\n<p>\u67e5\u770b\u65e5\u5fd7&#xff1a;\u4f7f\u7528ELK\u6216Splunk\u641c\u7d22\u9519\u8bef\u5806\u6808&#xff0c;\u5173\u6ce8\u5f02\u5e38\u7c7b\u578b\u548c\u65f6\u95f4\u3002<\/p>\n<\/li>\n<li>\n<p>\u94fe\u8def\u8ffd\u8e2a&#xff1a;\u901a\u8fc7SkyWalking\u6216Zipkin\u67e5\u770b\u8bf7\u6c42\u8c03\u7528\u94fe&#xff0c;\u5b9a\u4f4d\u8017\u65f6\u957f\u7684\u670d\u52a1\u6216\u65b9\u6cd5\u3002<\/p>\n<\/li>\n<li>\n<p>\u7ebf\u7a0b\u5206\u6790&#xff1a;\u4f7f\u7528jstack dump\u7ebf\u7a0b&#xff0c;\u68c0\u67e5\u662f\u5426\u6709\u6b7b\u9501\u3001\u7ebf\u7a0b\u963b\u585e\u3002<\/p>\n<\/li>\n<li>\n<p>\u6570\u636e\u5e93\u5206\u6790&#xff1a;\u5f00\u542f\u6162\u67e5\u8be2\u65e5\u5fd7&#xff0c;\u627e\u51fa\u6267\u884c\u6162\u7684SQL&#xff0c;\u4f7f\u7528explain\u5206\u6790\u6267\u884c\u8ba1\u5212\u3002<\/p>\n<\/li>\n<li>\n<p>\u8d44\u6e90\u5206\u6790&#xff1a;\u67e5\u770bCPU\u3001\u5185\u5b58\u3001\u78c1\u76d8I\/O\u7b49\u6307\u6807&#xff0c;\u5224\u65ad\u662f\u5426\u8d44\u6e90\u8017\u5c3d\u3002<\/p>\n<\/li>\n<p>Java\u4ee3\u7801\u793a\u4f8b&#xff08;\u52a8\u6001\u83b7\u53d6\u7ebf\u7a0b\u5806\u6808&#xff09;&#xff1a;<\/p>\n<p>java<\/p>\n<p>import java.lang.management.ManagementFactory;<br \/>\nimport java.lang.management.ThreadInfo;<br \/>\nimport java.lang.management.ThreadMXBean;<\/p>\n<p>public class ThreadDumpUtil {<br \/>\n    public static String dumpThreads() {<br \/>\n        ThreadMXBean threadMXBean &#061; ManagementFactory.getThreadMXBean();<br \/>\n        ThreadInfo[] threadInfos &#061; threadMXBean.dumpAllThreads(true, true);<br \/>\n        StringBuilder sb &#061; new StringBuilder();<br \/>\n        for (ThreadInfo info : threadInfos) {<br \/>\n            sb.append(info.toString());<br \/>\n        }<br \/>\n        return sb.toString();<br \/>\n    }<br \/>\n}<\/p>\n<p>\u5e38\u7528\u547d\u4ee4&#xff1a;<\/p>\n<p>bash<\/p>\n<p># \u67e5\u770bJava\u8fdb\u7a0bCPU\u5360\u7528\u9ad8\u7684\u7ebf\u7a0b<br \/>\ntop -H -p &lt;pid&gt;<br \/>\nprintf &#034;%x\\\\n&#034; &lt;thread-id&gt;  # \u8f6c\u4e3a\u5341\u516d\u8fdb\u5236<br \/>\njstack &lt;pid&gt; | grep -A 10 &lt;thread-hex&gt;<\/p>\n<p># \u67e5\u770b\u6570\u636e\u5e93\u6162\u67e5\u8be2&#xff08;MySQL&#xff09;<br \/>\nSET GLOBAL slow_query_log &#061; ON;<br \/>\nSET GLOBAL long_query_time &#061; 2; # \u8d85\u8fc72\u79d2\u8bb0\u5f55<br \/>\ntail -f \/var\/log\/mysql\/mysql-slow.log<\/p>\n<hr \/>\n<h4>5.3 \u6545\u969c\u6062\u590d\u4e0e\u7ba1\u7406<\/h4>\n<p>\u95ee\u9898\u63cf\u8ff0&#xff1a;\u6545\u969c\u53d1\u751f\u540e&#xff0c;\u5982\u4f55\u5feb\u901f\u6062\u590d\u670d\u52a1&#xff0c;\u5e76\u7ba1\u7406\u6574\u4e2a\u6d41\u7a0b\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u5236\u5b9a\u5e94\u6025\u9884\u6848&#xff0c;\u5305\u62ec\u670d\u52a1\u91cd\u542f\u3001\u964d\u7ea7\u3001\u7194\u65ad\u3001\u6570\u636e\u4fee\u590d\u7b49&#xff1b;\u901a\u8fc7\u81ea\u52a8\u5316\u811a\u672c\u5feb\u901f\u6062\u590d&#xff1b;\u4e8b\u540e\u8fdb\u884c\u6545\u969c\u590d\u76d8\u3002<\/p>\n<p>\u64cd\u4f5c\u6b65\u9aa4&#xff1a;<\/p>\n<li>\n<p>\u7acb\u5373\u6b62\u635f&#xff1a;\u6839\u636e\u9884\u6848\u6267\u884c\u64cd\u4f5c&#xff0c;\u5982\u91cd\u542f\u5e94\u7528\u3001\u5207\u6d41\u3001\u964d\u7ea7\u975e\u6838\u5fc3\u529f\u80fd\u3002<\/p>\n<\/li>\n<li>\n<p>\u7194\u65ad\u964d\u7ea7&#xff1a;\u4f7f\u7528Sentinel\u6216Hystrix\u7194\u65ad\u5f02\u5e38\u670d\u52a1&#xff0c;\u9632\u6b62\u96ea\u5d29\u3002<\/p>\n<\/li>\n<li>\n<p>\u6570\u636e\u4fee\u590d&#xff1a;\u5982\u679c\u6570\u636e\u635f\u574f&#xff0c;\u4ece\u5907\u4efd\u6062\u590d\u6216\u6267\u884c\u8865\u507f\u811a\u672c\u3002<\/p>\n<\/li>\n<li>\n<p>\u6062\u590d\u540e\u9a8c\u8bc1&#xff1a;\u786e\u8ba4\u670d\u52a1\u6b63\u5e38&#xff0c;\u9010\u6b65\u6062\u590d\u6d41\u91cf\u3002<\/p>\n<\/li>\n<li>\n<p>\u6545\u969c\u590d\u76d8&#xff1a;\u8bb0\u5f55\u6545\u969c\u65f6\u95f4\u3001\u5f71\u54cd\u8303\u56f4\u3001\u6839\u672c\u539f\u56e0\u3001\u6539\u8fdb\u63aa\u65bd&#xff0c;\u5f62\u6210\u6587\u6863\u3002<\/p>\n<\/li>\n<p>Java\u4ee3\u7801\u793a\u4f8b&#xff08;Sentinel\u7194\u65ad\u964d\u7ea7\u914d\u7f6e&#xff09;&#xff1a;<\/p>\n<p>java<\/p>\n<p>import com.alibaba.csp.sentinel.annotation.SentinelResource;<br \/>\nimport com.alibaba.csp.sentinel.slots.block.BlockException;<\/p>\n<p>&#064;RestController<br \/>\npublic class ProductController {<br \/>\n    &#064;GetMapping(&#034;\/product\/{id}&#034;)<br \/>\n    &#064;SentinelResource(value &#061; &#034;getProduct&#034;, blockHandler &#061; &#034;handleBlock&#034;)<br \/>\n    public Product getProduct(&#064;PathVariable String id) {<br \/>\n        \/\/ \u53ef\u80fd\u51fa\u9519\u7684\u8fdc\u7a0b\u8c03\u7528<br \/>\n        return productService.getProduct(id);<br \/>\n    }<\/p>\n<p>    public Product handleBlock(String id, BlockException ex) {<br \/>\n        \/\/ \u964d\u7ea7\u903b\u8f91&#xff0c;\u8fd4\u56de\u515c\u5e95\u6570\u636e<br \/>\n        return new Product(id, &#034;\u9ed8\u8ba4\u5546\u54c1&#034;, 0.0);<br \/>\n    }<br \/>\n}<\/p>\n<p>\u81ea\u52a8\u5316\u91cd\u542f\u811a\u672c\u793a\u4f8b&#xff08;\u4f7f\u7528Shell&#xff09;&#xff1a;<\/p>\n<p>bash<\/p>\n<p>#!\/bin\/bash<br \/>\nAPP_NAME&#061;&#034;myapp.jar&#034;<br \/>\nPID&#061;$(ps -ef | grep $APP_NAME | grep -v grep | awk &#039;{print $2}&#039;)<br \/>\nif [ -n &#034;$PID&#034; ]; then<br \/>\n    kill -15 $PID<br \/>\n    sleep 10<br \/>\nfi<br \/>\nnohup java -jar $APP_NAME &gt; app.log 2&gt;&amp;1 &amp;<br \/>\necho &#034;Application restarted&#034;<\/p>\n<hr \/>\n<h4>5.4 \u6545\u969c\u9884\u9632<\/h4>\n<p>\u95ee\u9898\u63cf\u8ff0&#xff1a;\u5982\u4f55\u5728\u6545\u969c\u53d1\u751f\u524d\u91c7\u53d6\u63aa\u65bd&#xff0c;\u907f\u514d\u6216\u51cf\u5c11\u6545\u969c\u53d1\u751f\u7684\u6982\u7387\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848&#xff1a;\u538b\u6d4b\u3001\u4ee3\u7801\u5ba1\u67e5\u3001\u7070\u5ea6\u53d1\u5e03\u3001\u5bb9\u91cf\u89c4\u5212\u3001\u707e\u5907\u6f14\u7ec3\u7b49\u3002<\/p>\n<p>\u64cd\u4f5c\u6b65\u9aa4&#xff1a;<\/p>\n<li>\n<p>\u538b\u529b\u6d4b\u8bd5&#xff1a;\u4f7f\u7528JMeter\u6216LoadRunner\u6a21\u62df\u9ad8\u5e76\u53d1&#xff0c;\u53d1\u73b0\u6027\u80fd\u74f6\u9888\u3002<\/p>\n<\/li>\n<li>\n<p>\u4ee3\u7801\u5ba1\u67e5&#xff1a;\u5b9a\u671f\u5ba1\u67e5\u4ee3\u7801&#xff0c;\u5173\u6ce8\u5e76\u53d1\u3001\u8d44\u6e90\u91ca\u653e\u3001\u5f02\u5e38\u5904\u7406\u7b49\u3002<\/p>\n<\/li>\n<li>\n<p>\u7070\u5ea6\u53d1\u5e03&#xff1a;\u91c7\u7528\u91d1\u4e1d\u96c0\u53d1\u5e03&#xff0c;\u5148\u8ba9\u5c11\u91cf\u7528\u6237\u4f7f\u7528\u65b0\u7248\u672c&#xff0c;\u89c2\u5bdf\u65e0\u5f02\u5e38\u518d\u5168\u91cf\u3002<\/p>\n<\/li>\n<li>\n<p>\u5bb9\u91cf\u8bc4\u4f30&#xff1a;\u6839\u636e\u4e1a\u52a1\u589e\u957f\u9884\u4f30\u6d41\u91cf&#xff0c;\u63d0\u524d\u6269\u5bb9\u3002<\/p>\n<\/li>\n<li>\n<p>\u707e\u5907\u6f14\u7ec3&#xff1a;\u5b9a\u671f\u8fdb\u884c\u4e3b\u5907\u5207\u6362\u3001\u6570\u636e\u6062\u590d\u6f14\u7ec3&#xff0c;\u786e\u4fdd\u6d41\u7a0b\u53ef\u7528\u3002<\/p>\n<\/li>\n<p>Java\u4ee3\u7801\u793a\u4f8b&#xff08;\u7b80\u5355\u7684\u9650\u6d41\u9884\u9632&#xff09;&#xff1a;<\/p>\n<p>java<\/p>\n<p>import com.google.common.util.concurrent.RateLimiter;<br \/>\nimport org.springframework.web.bind.annotation.GetMapping;<br \/>\nimport org.springframework.web.bind.annotation.RestController;<\/p>\n<p>&#064;RestController<br \/>\npublic class LoginController {<br \/>\n    private final RateLimiter rateLimiter &#061; RateLimiter.create(100); \/\/ \u6bcf\u79d2100\u4e2a\u8bf7\u6c42<\/p>\n<p>    &#064;GetMapping(&#034;\/login&#034;)<br \/>\n    public String login() {<br \/>\n        if (!rateLimiter.tryAcquire()) {<br \/>\n            return &#034;\u7cfb\u7edf\u7e41\u5fd9&#xff0c;\u8bf7\u7a0d\u540e\u518d\u8bd5&#034;;<br \/>\n        }<br \/>\n        \/\/ \u767b\u5f55\u903b\u8f91<br \/>\n        return &#034;success&#034;;<br \/>\n    }<br \/>\n}<\/p>\n<p>\u538b\u6d4b\u811a\u672c\u793a\u4f8b&#xff08;JMeter&#xff09;&#xff1a;\u53ef\u4ee5\u901a\u8fc7JMeter GUI\u521b\u5efa\u6d4b\u8bd5\u8ba1\u5212&#xff0c;\u6216\u8005\u4f7f\u7528\u547d\u4ee4\u884c&#xff1a;<\/p>\n<p>bash<\/p>\n<p>jmeter -n -t testplan.jmx -l results.jtl -e -o report\/<\/p>\n<p>Kubernetes\u6eda\u52a8\u66f4\u65b0&#xff08;\u7070\u5ea6\u53d1\u5e03&#xff09;&#xff1a;<\/p>\n<p>yaml<\/p>\n<p>apiVersion: apps\/v1<br \/>\nkind: Deployment<br \/>\nmetadata:<br \/>\n  name: myapp<br \/>\nspec:<br \/>\n  replicas: 10<br \/>\n  strategy:<br \/>\n    type: RollingUpdate<br \/>\n    rollingUpdate:<br \/>\n      maxSurge: 1<br \/>\n      maxUnavailable: 0<br \/>\n  template:<br \/>\n    spec:<br \/>\n      containers:<br \/>\n      &#8211; name: myapp<br \/>\n        image: myapp:1.0.1<br \/>\n        readinessProbe:<br \/>\n          httpGet:<br \/>\n            path: \/actuator\/health\/readiness<br \/>\n            port: 8080<br \/>\n          initialDelaySeconds: 10<br \/>\n        livenessProbe:<br \/>\n          httpGet:<br \/>\n            path: \/actuator\/health\/liveness<br \/>\n            port: 8080<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u7f13\u5b58\u95ee\u98981.1 \u7f13\u5b58\u7a7f\u900f\u89e3\u51b3\u65b9\u6848&#xff1a;\u4f7f\u7528\u5e03\u9686\u8fc7\u6ee4\u5668&#xff08;Bloom Filter&#xff09;\u62e6\u622a\u4e0d\u5b58\u5728\u7684\u6570\u636e\u8bf7\u6c42\u3002\u64cd\u4f5c\u6b65\u9aa4&#xff1a;\u521d\u59cb\u5316\u5e03\u9686\u8fc7\u6ee4\u5668&#xff0c;\u5c06\u6570\u636e\u5e93\u4e2d\u5b58\u5728\u7684\u6570\u636eID\u5168\u90e8\u653e\u5165\u5e03\u9686\u8fc7\u6ee4\u5668\u3002\u67e5\u8be2\u8bf7\u6c42\u5230\u8fbe\u65f6&#xff0c;\u5148\u68c0\u67e5\u5e03\u9686\u8fc7\u6ee4\u5668\u662f\u5426\u5b58\u5728\u8be5key\u3002\u5982\u679c\u5e03\u9686\u8fc7\u6ee4\u5668\u5224\u65ad\u4e0d\u5b58\u5728&#xff0c;\u5219\u76f4\u63a5\u8fd4\u56de\u7a7a&#xff0c;\u907f\u514d\u67e5\u8be2\u6570\u636e\u5e93\u3002\u5982\u679c\u5b58\u5728&#xff0c;\u5219\u8d70\u7f13\u5b58\u3001\u6570\u636e\u5e93\u67e5\u8be2\u903b\u8f91\u3002Java\u4ee3\u7801\u793a\u4f8b&#xff08;\u4f7f\u7528G<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[664,3433],"topic":[],"class_list":["post-78725","post","type-post","status-publish","format-standard","hentry","category-server","tag-664","tag-3433"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u9ad8\u5e76\u53d1\u7cfb\u7edf\u4e2d\u5e38\u9047\u5230\u7684\u95ee\u9898 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/78725.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u9ad8\u5e76\u53d1\u7cfb\u7edf\u4e2d\u5e38\u9047\u5230\u7684\u95ee\u9898 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001\u7f13\u5b58\u95ee\u98981.1 \u7f13\u5b58\u7a7f\u900f\u89e3\u51b3\u65b9\u6848&#xff1a;\u4f7f\u7528\u5e03\u9686\u8fc7\u6ee4\u5668&#xff08;Bloom Filter&#xff09;\u62e6\u622a\u4e0d\u5b58\u5728\u7684\u6570\u636e\u8bf7\u6c42\u3002\u64cd\u4f5c\u6b65\u9aa4&#xff1a;\u521d\u59cb\u5316\u5e03\u9686\u8fc7\u6ee4\u5668&#xff0c;\u5c06\u6570\u636e\u5e93\u4e2d\u5b58\u5728\u7684\u6570\u636eID\u5168\u90e8\u653e\u5165\u5e03\u9686\u8fc7\u6ee4\u5668\u3002\u67e5\u8be2\u8bf7\u6c42\u5230\u8fbe\u65f6&#xff0c;\u5148\u68c0\u67e5\u5e03\u9686\u8fc7\u6ee4\u5668\u662f\u5426\u5b58\u5728\u8be5key\u3002\u5982\u679c\u5e03\u9686\u8fc7\u6ee4\u5668\u5224\u65ad\u4e0d\u5b58\u5728&#xff0c;\u5219\u76f4\u63a5\u8fd4\u56de\u7a7a&#xff0c;\u907f\u514d\u67e5\u8be2\u6570\u636e\u5e93\u3002\u5982\u679c\u5b58\u5728&#xff0c;\u5219\u8d70\u7f13\u5b58\u3001\u6570\u636e\u5e93\u67e5\u8be2\u903b\u8f91\u3002Java\u4ee3\u7801\u793a\u4f8b&#xff08;\u4f7f\u7528G\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/78725.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-27T16:19:06+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"17 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/78725.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/78725.html\",\"name\":\"\u9ad8\u5e76\u53d1\u7cfb\u7edf\u4e2d\u5e38\u9047\u5230\u7684\u95ee\u9898 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-27T16:19:06+00:00\",\"dateModified\":\"2026-02-27T16:19:06+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/78725.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/78725.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/78725.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u9ad8\u5e76\u53d1\u7cfb\u7edf\u4e2d\u5e38\u9047\u5230\u7684\u95ee\u9898\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u9ad8\u5e76\u53d1\u7cfb\u7edf\u4e2d\u5e38\u9047\u5230\u7684\u95ee\u9898 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/78725.html","og_locale":"zh_CN","og_type":"article","og_title":"\u9ad8\u5e76\u53d1\u7cfb\u7edf\u4e2d\u5e38\u9047\u5230\u7684\u95ee\u9898 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00\u3001\u7f13\u5b58\u95ee\u98981.1 \u7f13\u5b58\u7a7f\u900f\u89e3\u51b3\u65b9\u6848&#xff1a;\u4f7f\u7528\u5e03\u9686\u8fc7\u6ee4\u5668&#xff08;Bloom Filter&#xff09;\u62e6\u622a\u4e0d\u5b58\u5728\u7684\u6570\u636e\u8bf7\u6c42\u3002\u64cd\u4f5c\u6b65\u9aa4&#xff1a;\u521d\u59cb\u5316\u5e03\u9686\u8fc7\u6ee4\u5668&#xff0c;\u5c06\u6570\u636e\u5e93\u4e2d\u5b58\u5728\u7684\u6570\u636eID\u5168\u90e8\u653e\u5165\u5e03\u9686\u8fc7\u6ee4\u5668\u3002\u67e5\u8be2\u8bf7\u6c42\u5230\u8fbe\u65f6&#xff0c;\u5148\u68c0\u67e5\u5e03\u9686\u8fc7\u6ee4\u5668\u662f\u5426\u5b58\u5728\u8be5key\u3002\u5982\u679c\u5e03\u9686\u8fc7\u6ee4\u5668\u5224\u65ad\u4e0d\u5b58\u5728&#xff0c;\u5219\u76f4\u63a5\u8fd4\u56de\u7a7a&#xff0c;\u907f\u514d\u67e5\u8be2\u6570\u636e\u5e93\u3002\u5982\u679c\u5b58\u5728&#xff0c;\u5219\u8d70\u7f13\u5b58\u3001\u6570\u636e\u5e93\u67e5\u8be2\u903b\u8f91\u3002Java\u4ee3\u7801\u793a\u4f8b&#xff08;\u4f7f\u7528G","og_url":"https:\/\/www.wsisp.com\/helps\/78725.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-27T16:19:06+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"17 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/78725.html","url":"https:\/\/www.wsisp.com\/helps\/78725.html","name":"\u9ad8\u5e76\u53d1\u7cfb\u7edf\u4e2d\u5e38\u9047\u5230\u7684\u95ee\u9898 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-27T16:19:06+00:00","dateModified":"2026-02-27T16:19:06+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/78725.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/78725.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/78725.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u9ad8\u5e76\u53d1\u7cfb\u7edf\u4e2d\u5e38\u9047\u5230\u7684\u95ee\u9898"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/78725","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=78725"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/78725\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=78725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=78725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=78725"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=78725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}