| <!doctype html> |
| <html lang="en" dir="ltr"> |
| <head> |
| <meta charset="UTF-8"> |
| <meta name="viewport" content="width=device-width,initial-scale=1"> |
| <meta name="generator" content="Docusaurus v2.0.0-beta.4"> |
| <link rel="alternate" type="application/rss+xml" href="/blog/rss.xml" title="Apache ShenYu Blog RSS Feed"> |
| <link rel="alternate" type="application/atom+xml" href="/blog/atom.xml" title="Apache ShenYu Blog Atom Feed"> |
| <link rel="search" type="application/opensearchdescription+xml" title="Apache ShenYu" href="/opensearch.xml"> |
| <link rel="alternate" type="application/rss+xml" href="/news/rss.xml" title="Apache ShenYu Blog RSS Feed"> |
| <link rel="alternate" type="application/atom+xml" href="/news/atom.xml" title="Apache ShenYu Blog Atom Feed"><title data-react-helmet="true">index | Apache ShenYu</title><meta data-react-helmet="true" property="og:url" content="https://shenyu.apache.org//shenyuNginx/index"><meta data-react-helmet="true" name="docsearch:language" content="en"><meta data-react-helmet="true" name="docsearch:version" content="current"><meta data-react-helmet="true" name="docsearch:docusaurus_tag" content="docs-shenyuNginx-current"><meta data-react-helmet="true" property="og:title" content="index | Apache ShenYu"><meta data-react-helmet="true" name="description" content="Apache ShenYu Nginx Module"><meta data-react-helmet="true" property="og:description" content="Apache ShenYu Nginx Module"><link data-react-helmet="true" rel="shortcut icon" href="/img/favicon.svg"><link data-react-helmet="true" rel="canonical" href="https://shenyu.apache.org//shenyuNginx/index"><link data-react-helmet="true" rel="alternate" href="https://shenyu.apache.org//shenyuNginx/index" hreflang="en"><link data-react-helmet="true" rel="alternate" href="https://shenyu.apache.org//zh/shenyuNginx/index" hreflang="zh"><link data-react-helmet="true" rel="alternate" href="https://shenyu.apache.org//shenyuNginx/index" hreflang="x-default"><link data-react-helmet="true" rel="preconnect" href="https://BH4D9OD16A-dsn.algolia.net" crossorigin="anonymous"><link rel="stylesheet" href="/assets/css/styles.b6f9a199.css"> |
| <link rel="preload" href="/assets/js/runtime~main.490de106.js" as="script"> |
| <link rel="preload" href="/assets/js/main.cb386500.js" as="script"> |
| </head> |
| <body> |
| <script>!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();t(null!==e?e:"light")}()</script><div id="__docusaurus"> |
| <div><a href="#" class="skipToContent_OuoZ">Skip to main content</a></div><nav class="navbar navbar--fixed-top"><div class="navbar__inner"><div class="navbar__items"><button aria-label="Navigation bar toggle" class="navbar__toggle clean-btn" type="button" tabindex="0"><svg width="30" height="30" viewBox="0 0 30 30" aria-hidden="true"><path stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="2" d="M4 7h22M4 15h22M4 23h22"></path></svg></button><a class="navbar__brand" href="/"><img src="/img/logo.svg" alt="Apache ShenYu Logo" class="themedImage_TMUO themedImage--light_4Vu1 navbar__logo"><img src="/img/logo-light.svg" alt="Apache ShenYu Logo" class="themedImage_TMUO themedImage--dark_uzRr navbar__logo"></a></div><div class="navbar__items navbar__items--right"><a class="navbar__item navbar__link" href="/download">Download</a><a class="navbar__item navbar__link" href="/document">Docs</a><a class="navbar__item navbar__link" href="/community/contributor-guide">Community</a><a class="navbar__item navbar__link" href="/team">Team</a><a class="navbar__item navbar__link" href="/event">Event</a><a class="navbar__item navbar__link" href="/news">News</a><a class="navbar__item navbar__link" href="/blog">Blog</a><a class="navbar__item navbar__link" href="/users">Users</a><div class="navbar__item dropdown dropdown--hoverable dropdown--right"><a class="navbar__item navbar__link">ASF</a><ul class="dropdown__menu"><li><a href="https://www.apache.org/" target="_blank" rel="noopener noreferrer" class="dropdown__link">Foundation</a></li><li><a href="https://www.apache.org/licenses/" target="_blank" rel="noopener noreferrer" class="dropdown__link">License</a></li><li><a href="https://www.apache.org/events/current-event" target="_blank" rel="noopener noreferrer" class="dropdown__link">Events</a></li><li><a href="https://www.apache.org/security/" target="_blank" rel="noopener noreferrer" class="dropdown__link">Security</a></li><li><a href="https://www.apache.org/foundation/sponsorship.html" target="_blank" rel="noopener noreferrer" class="dropdown__link">Sponsorship</a></li><li><a href="https://www.apache.org/foundation/policies/privacy.html" target="_blank" rel="noopener noreferrer" class="dropdown__link">Privacy</a></li><li><a href="https://www.apache.org/foundation/thanks.html" target="_blank" rel="noopener noreferrer" class="dropdown__link">Thanks</a></li></ul></div><a href="https://github.com/apache/shenyu" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link"><span>GitHub<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_wgqa"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></span></a><div class="navbar__item dropdown dropdown--hoverable dropdown--right"><a href="#" class="navbar__item navbar__link"><span><svg t="1631348384596" class="icon" viewBox="0 0 1024 1024" version="1.1" style="vertical-align:text-bottom;margin-right:5px" p-id="557" width="20" height="20"><path d="M547.797333 638.208l-104.405333-103.168 1.237333-1.28a720.170667 720.170667 0 0 0 152.490667-268.373333h120.448V183.082667h-287.744V100.906667H347.605333v82.218666H59.818667V265.386667h459.178666a648.234667 648.234667 0 0 1-130.304 219.946666 643.242667 643.242667 0 0 1-94.976-137.728H211.541333a722.048 722.048 0 0 0 122.453334 187.434667l-209.194667 206.378667 58.368 58.368 205.525333-205.525334 127.872 127.829334 31.232-83.84m231.424-208.426667h-82.218666l-184.96 493.312h82.218666l46.037334-123.306667h195.242666l46.464 123.306667h82.218667l-185.002667-493.312m-107.690666 287.744l66.56-178.005333 66.602666 178.005333z" fill="currentColor" p-id="558"></path></svg><span>English</span></span></a><ul class="dropdown__menu"><li><a href="/shenyuNginx/index" target="_self" rel="noopener noreferrer" class="dropdown__link dropdown__link--active" style="text-transform:capitalize">English</a></li><li><a href="/zh/shenyuNginx/index" target="_self" rel="noopener noreferrer" class="dropdown__link" style="text-transform:capitalize">įŽäŊä¸æ</a></li></ul></div><div class="react-toggle toggle_2i4l react-toggle--disabled"><div class="react-toggle-track" role="button" tabindex="-1"><div class="react-toggle-track-check"><span class="toggle_iYfV">đ</span></div><div class="react-toggle-track-x"><span class="toggle_iYfV">đ</span></div><div class="react-toggle-thumb"></div></div><input type="checkbox" class="react-toggle-screenreader-only" aria-label="Switch between dark and light mode"></div><div class="searchBox_Bc3W"><button type="button" class="DocSearch DocSearch-Button" aria-label="Search"><span class="DocSearch-Button-Container"><svg width="20" height="20" class="DocSearch-Search-Icon" viewBox="0 0 20 20" aria-hidden="true"><path d="M14.386 14.386l4.0877 4.0877-4.0877-4.0877c-2.9418 2.9419-7.7115 2.9419-10.6533 0-2.9419-2.9418-2.9419-7.7115 0-10.6533 2.9418-2.9419 7.7115-2.9419 10.6533 0 2.9419 2.9418 2.9419 7.7115 0 10.6533z" stroke="currentColor" fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round"></path></svg><span class="DocSearch-Button-Placeholder">Search</span></span><span class="DocSearch-Button-Keys"></span></button></div></div></div><div role="presentation" class="navbar-sidebar__backdrop"></div></nav><div class="main-wrapper docs-wrapper doc-page"><div class="docPage_lDyR"><button class="clean-btn backToTopButton_i9tI" type="button" title="Scroll to top"><svg viewBox="0 0 24 24" width="28"><path d="M7.41 15.41L12 10.83l4.59 4.58L18 14l-6-6-6 6z" fill="currentColor"></path></svg></button><aside class="docSidebarContainer_0YBq"><div class="sidebar_a3j0"><nav class="menu thin-scrollbar menu_cyFh menuWithAnnouncementBar_+O1J"><ul class="menu__list"><li class="menu__list-item"><a aria-current="page" class="menu__link menu__link--active active" href="/shenyuNginx/index">index</a></li></ul></nav></div></aside><main class="docMainContainer_r8cw"><div class="container padding-top--md padding-bottom--lg"><div class="row"><div class="col docItemCol_zHA2"><div class="docItemContainer_oiyr"><article><div class="tocCollapsible_aw-L tocMobile_Tx6Y"><button type="button" class="clean-btn tocCollapsibleButton_zr6a">On this page</button></div><div class="markdown"><header><h1 class="h1Heading_dC7a">index</h1></header><h2><a aria-hidden="true" tabindex="-1" class="anchor enhancedAnchor_WiXH" id="apache-shenyu-nginx-module"></a>Apache ShenYu Nginx Module<a class="hash-link" href="#apache-shenyu-nginx-module" title="Direct link to heading">#</a></h2><p>This module provided SDK to watch available ShenYu instance list as upstream nodes by Service Register Center for OpenResty.</p><ol><li><a href="#greeting-etcd">ETCD</a> (Supported)</li><li><a href="#greeting-nacos">Nacos</a> (Supported)</li><li><a href="#greeting-zookeeper">Zookeeper</a> (Supported)</li><li><a href="#greeting-consul">Consul</a> (Supported)</li></ol><p>In the cluster mode, Apache ShenYu supports the deployment of multiple ShenYu instances, which may have new instances joining or leaving at any time. |
| Hence, Apache ShenYu introduces Service Discovery modules to help client to detect the available instances. |
| Currently, Apache ShenYu Bootstrap already supports Apache Zookeeper, Nacos, Etcd, and consul. Client or LoadBalancer can get the available ShenYu instances by those Service register center.</p><p>Here provides a completed <a href="https://github.com/apache/shenyu-nginx/tree/main/example" target="_blank" rel="noopener noreferrer">examples</a>.</p><p>=======</p><p>Here is a completed <a href="https://github.com/apache/shenyu-nginx/blob/main/example/etcd/nginx.conf" target="_blank" rel="noopener noreferrer">example</a> working with ETCD.</p><p>Here is a completed <a href="https://github.com/apache/shenyu-nginx/blob/main/example/nacos/nginx.conf" target="_blank" rel="noopener noreferrer">example</a> working with Nacos.</p><p>Here is a completed <a href="https://github.com/apache/shenyu-nginx/blob/main/example/consul/nginx.conf" target="_blank" rel="noopener noreferrer">example</a> working with Consul.</p><p>Here is a completed <a href="https://github.com/apache/shenyu-nginx/blob/main/example/zookeeper/nginx.conf" target="_blank" rel="noopener noreferrer">example</a> working with Zookeeper.</p><h2><a aria-hidden="true" tabindex="-1" class="anchor enhancedAnchor_WiXH" id="getting-started"></a>Getting Started<a class="hash-link" href="#getting-started" title="Direct link to heading">#</a></h2><ul><li>Prerequisite:</li></ul><ol><li>Luarocks</li><li>OpenResty</li></ol><h3><a aria-hidden="true" tabindex="-1" class="anchor enhancedAnchor_WiXH" id="build-from-source"></a>Build from source<a class="hash-link" href="#build-from-source" title="Direct link to heading">#</a></h3><p>The first, clone the source from GitHub.</p><div class="codeBlockContainer_J+bg"><div class="codeBlockContent_csEI shell"><pre tabindex="0" class="prism-code language-shell codeBlock_rtdJ thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_1zSZ"><span class="token-line" style="color:#393A34"><span class="token function" style="color:#d73a49">git</span><span class="token plain"> clone https://github.com/apache/shenyu-nginx</span></span></code></pre><button type="button" aria-label="Copy code to clipboard" class="copyButton_M3SB clean-btn">Copy</button></div></div><p>Then, build from source and install.</p><div class="codeBlockContainer_J+bg"><div class="codeBlockContent_csEI shell"><pre tabindex="0" class="prism-code language-shell codeBlock_rtdJ thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_1zSZ"><span class="token-line" style="color:#393A34"><span class="token builtin class-name">cd</span><span class="token plain"> shenyu-nginx</span></span><span class="token-line" style="color:#393A34"><span class="token plain">luarocks </span><span class="token function" style="color:#d73a49">make</span><span class="token plain"> rockspec/shenyu-nginx-1.0.0-1.rockspec</span></span></code></pre><button type="button" aria-label="Copy code to clipboard" class="copyButton_M3SB clean-btn">Copy</button></div></div><h3><a aria-hidden="true" tabindex="-1" class="anchor enhancedAnchor_WiXH" id="greeting-etcd"></a>Greeting ETCD<a class="hash-link" href="#greeting-etcd" title="Direct link to heading">#</a></h3><p>Modify the Nginx configure, create and initialize the ShenYu Register to connect to the target register center. |
| The module will fetch the all of ShenYu instances which are registered to Etcd in the same cluster. |
| It works like Etcd client to watch(based on long polling) ShenYu instance lists.</p><p>Here is an example for Etcd.</p><div class="codeBlockContainer_J+bg"><div class="codeBlockContent_csEI"><pre tabindex="0" class="prism-code language-undefined codeBlock_rtdJ thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_1zSZ"><span class="token-line" style="color:#393A34"><span class="token plain">init_worker_by_lua_block {</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> local register = require("shenyu.register.etcd")</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> register.init({</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> balancer_type = "chash",</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> etcd_base_url = "http://127.0.0.1:2379",</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> })</span></span><span class="token-line" style="color:#393A34"><span class="token plain">}</span></span></code></pre><button type="button" aria-label="Copy code to clipboard" class="copyButton_M3SB clean-btn">Copy</button></div></div><ol><li><code>balancer_type</code> specify the balancer. It has supported <code>chash</code> and <code>round robin</code>.</li><li><code>etcd_base_url</code> specify the Etcd server.(Currently, authentication is not supported.)</li></ol><p>Add an <code>upstream block</code> for ShenYu and enable to update upstream servers dynamically. This case will synchronize the ShenYu instance list with register center. |
| And then pick one up for handling the request.</p><div class="codeBlockContainer_J+bg"><div class="codeBlockContent_csEI"><pre tabindex="0" class="prism-code language-undefined codeBlock_rtdJ thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_1zSZ"><span class="token-line" style="color:#393A34"><span class="token plain">upstream shenyu {</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> server 0.0.0.1; -- bad </span></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span></span><span class="token-line" style="color:#393A34"><span class="token plain"> balancer_by_lua_block {</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> require("shenyu.register.etcd").pick_and_set_peer()</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> }</span></span><span class="token-line" style="color:#393A34"><span class="token plain">}</span></span></code></pre><button type="button" aria-label="Copy code to clipboard" class="copyButton_M3SB clean-btn">Copy</button></div></div><h3><a aria-hidden="true" tabindex="-1" class="anchor enhancedAnchor_WiXH" id="greeting-nacos"></a>Greeting Nacos<a class="hash-link" href="#greeting-nacos" title="Direct link to heading">#</a></h3><p>Modify the Nginx configure, create and initialize the ShenYu Register to connect to target register center. Here is an example for Nacos.</p><div class="codeBlockContainer_J+bg"><div class="codeBlockContent_csEI"><pre tabindex="0" class="prism-code language-undefined codeBlock_rtdJ thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_1zSZ"><span class="token-line" style="color:#393A34"><span class="token plain">init_worker_by_lua_block {</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> local register = require("shenyu.register.nacos")</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> register.init({</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> shenyu_storage = ngx.shared.shenyu_storage,</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> balancer_type = "chash",</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> nacos_base_url = "http://127.0.0.1:8848",</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> username = "nacos",</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> password = "naocs",</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> })</span></span><span class="token-line" style="color:#393A34"><span class="token plain">}</span></span></code></pre><button type="button" aria-label="Copy code to clipboard" class="copyButton_M3SB clean-btn">Copy</button></div></div><ol><li><code>balancer_type</code> specify the balancer. It has supported <code>chash</code> and <code>round robin</code>.</li><li><code>nacos_base_url</code> specify the Nacos server address.</li><li><code>username</code> specify the username to log in Nacos. (it is only required when Nacos auth enable)</li><li><code>password</code> specify the password to log in Nacos.</li></ol><p>Modify the <code>upstream</code> to enable to update upstream servers dynamically. This case will synchronize the ShenYu instance list with register center. |
| And then pick one up for handling the request.</p><div class="codeBlockContainer_J+bg"><div class="codeBlockContent_csEI"><pre tabindex="0" class="prism-code language-undefined codeBlock_rtdJ thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_1zSZ"><span class="token-line" style="color:#393A34"><span class="token plain">upstream shenyu {</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> server 0.0.0.1; -- bad </span></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span></span><span class="token-line" style="color:#393A34"><span class="token plain"> balancer_by_lua_block {</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> require("shenyu.register.nacos").pick_and_set_peer()</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> }</span></span><span class="token-line" style="color:#393A34"><span class="token plain">}</span></span></code></pre><button type="button" aria-label="Copy code to clipboard" class="copyButton_M3SB clean-btn">Copy</button></div></div><h2><a aria-hidden="true" tabindex="-1" class="anchor enhancedAnchor_WiXH" id="greeting-zookeeper"></a>Greeting Zookeeper<a class="hash-link" href="#greeting-zookeeper" title="Direct link to heading">#</a></h2><p>Modify the Nginx configure, create and initialize the ShenYu register to connect to target register center. |
| Listen for changes to the node via the zookeeper watch event. Here is an example of the zookeeper configuration.</p><div class="codeBlockContainer_J+bg"><div class="codeBlockContent_csEI shell"><pre tabindex="0" class="prism-code language-shell codeBlock_rtdJ thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_1zSZ"><span class="token-line" style="color:#393A34"><span class="token plain">init_worker_by_lua_block </span><span class="token punctuation" style="color:#393A34">{</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token builtin class-name">local</span><span class="token plain"> register </span><span class="token operator" style="color:#393A34">=</span><span class="token plain"> require</span><span class="token punctuation" style="color:#393A34">(</span><span class="token string" style="color:#e3116c">"shenyu.register.zookeeper"</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> register.init</span><span class="token punctuation" style="color:#393A34">(</span><span class="token punctuation" style="color:#393A34">{</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> servers </span><span class="token operator" style="color:#393A34">=</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">{</span><span class="token string" style="color:#e3116c">"127.0.0.1:2181"</span><span class="token plain">,</span><span class="token string" style="color:#e3116c">"127.0.0.1:2182"</span><span class="token punctuation" style="color:#393A34">}</span><span class="token plain">,</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> shenyu_storage </span><span class="token operator" style="color:#393A34">=</span><span class="token plain"> ngx.shared.shenyu_storage,</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> balancer_type </span><span class="token operator" style="color:#393A34">=</span><span class="token plain"> </span><span class="token string" style="color:#e3116c">"roundrobin"</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">}</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">}</span></span></code></pre><button type="button" aria-label="Copy code to clipboard" class="copyButton_M3SB clean-btn">Copy</button></div></div><ol><li><code>servers</code> zookeeper cluster address.</li><li><code>balancer_type</code> specify the balancer. It has supported <code>chash</code> and <code>round robin</code>.</li></ol><p>Modify the upstream to enable to update upstream servers dynamically. This case will synchronize the ShenYu instance list with register center. And then pick one up for handling the request.</p><div class="codeBlockContainer_J+bg"><div class="codeBlockContent_csEI shell"><pre tabindex="0" class="prism-code language-shell codeBlock_rtdJ thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_1zSZ"><span class="token-line" style="color:#393A34"><span class="token plain"> upstream shenyu </span><span class="token punctuation" style="color:#393A34">{</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> server </span><span class="token number" style="color:#36acaa">0.0</span><span class="token plain">.0.1</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> balancer_by_lua_block </span><span class="token punctuation" style="color:#393A34">{</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> require</span><span class="token punctuation" style="color:#393A34">(</span><span class="token string" style="color:#e3116c">"shenyu.register.zookeeper"</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain">.pick_and_set_peer</span><span class="token punctuation" style="color:#393A34">(</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">}</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">}</span></span></code></pre><button type="button" aria-label="Copy code to clipboard" class="copyButton_M3SB clean-btn">Copy</button></div></div><h3><a aria-hidden="true" tabindex="-1" class="anchor enhancedAnchor_WiXH" id="greeting-consul"></a>Greeting Consul<a class="hash-link" href="#greeting-consul" title="Direct link to heading">#</a></h3><p>Modify the Nginx configure, create and initialize the ShenYu register to connect to target register center. |
| Listen for changes to the node via the consul watch event. Here is an example of the consul configuration.</p><div class="codeBlockContainer_J+bg"><div class="codeBlockContent_csEI shell"><pre tabindex="0" class="prism-code language-shell codeBlock_rtdJ thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_1zSZ"><span class="token-line" style="color:#393A34"><span class="token plain">init_worker_by_lua_block </span><span class="token punctuation" style="color:#393A34">{</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token builtin class-name">local</span><span class="token plain"> register </span><span class="token operator" style="color:#393A34">=</span><span class="token plain"> require </span><span class="token string" style="color:#e3116c">"shenyu.register.consul"</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> register.init</span><span class="token punctuation" style="color:#393A34">(</span><span class="token punctuation" style="color:#393A34">{</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> uri </span><span class="token operator" style="color:#393A34">=</span><span class="token plain"> </span><span class="token string" style="color:#e3116c">"http://127.0.0.1:8500"</span><span class="token plain">,</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> path </span><span class="token operator" style="color:#393A34">=</span><span class="token plain"> </span><span class="token string" style="color:#e3116c">"/v1/catalog/service/demo"</span><span class="token plain">,</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> shenyu_storage </span><span class="token operator" style="color:#393A34">=</span><span class="token plain"> ngx.shared.shenyu_storage,</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> balancer_type </span><span class="token operator" style="color:#393A34">=</span><span class="token plain"> </span><span class="token string" style="color:#e3116c">"chash"</span><span class="token plain">,</span></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">}</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">}</span></span></code></pre><button type="button" aria-label="Copy code to clipboard" class="copyButton_M3SB clean-btn">Copy</button></div></div><ol><li><code>balancer_type</code> specify the balancer. It has supported <code>chash</code> and <code>round robin</code>.</li><li><code>uri</code> consul server address.</li><li><code>path</code> path of service.</li></ol><p>Modify the upstream to enable to update upstream servers dynamically. This case will synchronize the ShenYu instance list with register center. And then pick one up for handling the request.</p><div class="codeBlockContainer_J+bg"><div class="codeBlockContent_csEI shell"><pre tabindex="0" class="prism-code language-shell codeBlock_rtdJ thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_1zSZ"><span class="token-line" style="color:#393A34"><span class="token plain" style="display:inline-block"> |
| </span></span><span class="token-line" style="color:#393A34"><span class="token plain"> upstream shenyu </span><span class="token punctuation" style="color:#393A34">{</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> server </span><span class="token number" style="color:#36acaa">0.0</span><span class="token plain">.0.1</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> balancer_by_lua_block </span><span class="token punctuation" style="color:#393A34">{</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> require</span><span class="token punctuation" style="color:#393A34">(</span><span class="token string" style="color:#e3116c">"shenyu.register.consul"</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain">.pick_and_set_peer</span><span class="token punctuation" style="color:#393A34">(</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">}</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">}</span></span></code></pre><button type="button" aria-label="Copy code to clipboard" class="copyButton_M3SB clean-btn">Copy</button></div></div><h2><a aria-hidden="true" tabindex="-1" class="anchor enhancedAnchor_WiXH" id="finally"></a>Finally<a class="hash-link" href="#finally" title="Direct link to heading">#</a></h2><p>Finally, restart OpenResty.</p><div class="codeBlockContainer_J+bg"><div class="codeBlockContent_csEI shell"><pre tabindex="0" class="prism-code language-shell codeBlock_rtdJ thin-scrollbar" style="color:#393A34;background-color:#f6f8fa"><code class="codeBlockLines_1zSZ"><span class="token-line" style="color:#393A34"><span class="token plain">openresty -s reload</span></span></code></pre><button type="button" aria-label="Copy code to clipboard" class="copyButton_M3SB clean-btn">Copy</button></div></div><h2><a aria-hidden="true" tabindex="-1" class="anchor enhancedAnchor_WiXH" id="contributor-and-support"></a>Contributor and Support<a class="hash-link" href="#contributor-and-support" title="Direct link to heading">#</a></h2><ul><li><a href="https://shenyu.apache.org/community/contributor-guide" target="_blank" rel="noopener noreferrer">How to Contributor</a></li><li><a href="mailto:dev@shenyu.apache.org" target="_blank" rel="noopener noreferrer">Mailing Lists</a></li></ul><h2><a aria-hidden="true" tabindex="-1" class="anchor enhancedAnchor_WiXH" id="license"></a>License<a class="hash-link" href="#license" title="Direct link to heading">#</a></h2><p><a href="https://apache.org/licenses/LICENSE-2.0" target="_blank" rel="noopener noreferrer">Apache License 2.0</a></p></div><footer class="row docusaurus-mt-lg"><div class="col"></div><div class="col lastUpdated_wj+Z">Last updated on <b><time datetime="2024-04-20T01:09:36.000Z">4/20/2024</time></b> by <b>Kerwin Bryant</b></div></footer></article><nav class="pagination-nav docusaurus-mt-lg" aria-label="Docs pages navigation"><div class="pagination-nav__item"></div><div class="pagination-nav__item pagination-nav__item--next"></div></nav></div></div><div class="col col--3"><div class="tableOfContents_vrFS thin-scrollbar"><ul class="table-of-contents table-of-contents__left-border"><li><a href="#apache-shenyu-nginx-module" class="table-of-contents__link">Apache ShenYu Nginx Module</a></li><li><a href="#getting-started" class="table-of-contents__link">Getting Started</a><ul><li><a href="#build-from-source" class="table-of-contents__link">Build from source</a></li><li><a href="#greeting-etcd" class="table-of-contents__link">Greeting ETCD</a></li><li><a href="#greeting-nacos" class="table-of-contents__link">Greeting Nacos</a></li></ul></li><li><a href="#greeting-zookeeper" class="table-of-contents__link">Greeting Zookeeper</a><ul><li><a href="#greeting-consul" class="table-of-contents__link">Greeting Consul</a></li></ul></li><li><a href="#finally" class="table-of-contents__link">Finally</a></li><li><a href="#contributor-and-support" class="table-of-contents__link">Contributor and Support</a></li><li><a href="#license" class="table-of-contents__link">License</a></li></ul></div></div></div></div></main></div></div></div> |
| <script src="/assets/js/runtime~main.490de106.js"></script> |
| <script src="/assets/js/main.cb386500.js"></script> |
| </body> |
| </html> |