<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>api on 又LAG隨性筆記</title><link>https://www.lagagain.com/tags/api/</link><description>Recent content in api on 又LAG隨性筆記</description><generator>Hugo -- gohugo.io</generator><language>zh-TW</language><lastBuildDate>Wed, 18 Nov 2026 06:30:00 +0800</lastBuildDate><atom:link href="https://www.lagagain.com/tags/api/index.xml" rel="self" type="application/rss+xml"/><item><title>Ch5 - APISIX 健康檢查：告別「白跑一趟」，讓你的服務聰明運作！</title><link>https://www.lagagain.com/post/2025-it%E9%90%B5%E4%BA%BA%E8%B3%BD-apisix%E4%BD%BF%E7%94%A8%E7%B6%93%E9%A9%97%E5%88%86%E4%BA%AB09/</link><pubDate>Wed, 18 Nov 2026 06:30:00 +0800</pubDate><guid>https://www.lagagain.com/post/2025-it%E9%90%B5%E4%BA%BA%E8%B3%BD-apisix%E4%BD%BF%E7%94%A8%E7%B6%93%E9%A9%97%E5%88%86%E4%BA%AB09/</guid><description>&lt;blockquote>
&lt;p>本系列文章於2025年，已在&lt;a href="https://ithelp.ithome.com.tw/users/20112470/ironman/8791">iT邦幫忙：【與雲原生精靈共舞：APISIX使用者的兩年旅程】&lt;/a>發布過。&lt;/p>
&lt;/blockquote>
&lt;p>就像是出去玩要先查店家有沒有開，可以透過直接打電話給店家、請朋友先協助確認等成本比較低的方式。在服務檢查部分，通常使用「Health Check」。&lt;/p>
&lt;p>&lt;img src="https://i.ibb.co/YBpD06vW/2011247087-Tw-Hpb-BFs.png" alt="圖片">&lt;/p>
&lt;p>APISIX提供「主動」和「被動」的檢查機制。&lt;/p>
&lt;ul>
&lt;li>主動：就是定期檢查上游服務節點是否健康。&lt;/li>
&lt;li>被動：只有有請求要轉發時，才檢查並記錄。&lt;/li>
&lt;/ul>
&lt;h1 id="建立健康檢查端點">建立健康檢查端點&lt;/h1>
&lt;p>爲了提供APISIX健康檢查端點，我們在「新竹百貨公司」服務節點下，再添加一個檔案充當健康檢查結果，那麼就直接取名這個檔案叫做&lt;code>health.json&lt;/code>：&lt;/p>
&lt;div class="highlight">&lt;pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-json" data-lang="json">{&lt;span style="color:#f92672">&amp;#34;status&amp;#34;&lt;/span>: &lt;span style="color:#e6db74">&amp;#34;health&amp;#34;&lt;/span>}
&lt;/code>&lt;/pre>&lt;/div>&lt;p>接著可以透過檢查 http://新竹百貨公司.com:9080/health.json 來檢查健康狀態。(可將端口改成特定節點監聽端口，以檢查特定節點的健康狀態)&lt;/p>
&lt;p>示範上設計簡單一點：只要有回傳檔案，HTTP回應狀態碼是200或201就是健康；反之找不到檔案，回應狀態碼是404就是不健康。&lt;/p>
&lt;h1 id="設定主動檢查">設定主動檢查&lt;/h1>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>欄位&lt;/th>
&lt;th>值&lt;/th>
&lt;th>備註&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>HTTP Path&lt;/td>
&lt;td>&lt;code>/health.json&lt;/code>&lt;/td>
&lt;td>健康檢查端點&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Interval&lt;/td>
&lt;td>1&lt;/td>
&lt;td>檢查間格(秒)&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Successes&lt;/td>
&lt;td>2&lt;/td>
&lt;td>連續成功2次數健康後，視爲節點健康。恢復轉發請求。&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>HTTP Status&lt;/td>
&lt;td>200&lt;/td>
&lt;td>回應狀態爲&lt;code>200&lt;/code>視爲健康&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table></description></item></channel></rss>