blob: 797d13131a7a6c9fb53f789b2e6613112ba8ac80 [file] [log] [blame]
<!doctype html>
<html lang="zh" 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="/zh/blog/rss.xml" title="Apache ShenYu Blog RSS Feed">
<link rel="alternate" type="application/atom+xml" href="/zh/blog/atom.xml" title="Apache ShenYu Blog Atom Feed">
<link rel="search" type="application/opensearchdescription+xml" title="Apache ShenYu" href="/zh/opensearch.xml">
<link rel="alternate" type="application/rss+xml" href="/zh/news/rss.xml" title="Apache ShenYu Blog RSS Feed">
<link rel="alternate" type="application/atom+xml" href="/zh/news/atom.xml" title="Apache ShenYu Blog Atom Feed"><title data-react-helmet="true">Redirect插件 | Apache ShenYu</title><meta data-react-helmet="true" property="og:url" content="https://shenyu.apache.org//zh/docs/plugin-center/http-process/redirect-plugin"><meta data-react-helmet="true" name="docsearch:language" content="zh"><meta data-react-helmet="true" name="docsearch:version" content="2.6.1"><meta data-react-helmet="true" name="docsearch:docusaurus_tag" content="docs-default-2.6.1"><meta data-react-helmet="true" property="og:title" content="Redirect插件 | Apache ShenYu"><meta data-react-helmet="true" name="description" content="redirect插件"><meta data-react-helmet="true" property="og:description" content="redirect插件"><meta data-react-helmet="true" name="keywords" content="redirect"><link data-react-helmet="true" rel="shortcut icon" href="/zh/img/favicon.svg"><link data-react-helmet="true" rel="canonical" href="https://shenyu.apache.org//zh/docs/plugin-center/http-process/redirect-plugin"><link data-react-helmet="true" rel="alternate" href="https://shenyu.apache.org//docs/plugin-center/http-process/redirect-plugin" hreflang="en"><link data-react-helmet="true" rel="alternate" href="https://shenyu.apache.org//zh/docs/plugin-center/http-process/redirect-plugin" hreflang="zh"><link data-react-helmet="true" rel="alternate" href="https://shenyu.apache.org//docs/plugin-center/http-process/redirect-plugin" hreflang="x-default"><link data-react-helmet="true" rel="preconnect" href="https://BH4D9OD16A-dsn.algolia.net" crossorigin="anonymous"><link rel="stylesheet" href="/zh/assets/css/styles.b6f9a199.css">
<link rel="preload" href="/zh/assets/js/runtime~main.ba3d74b1.js" as="script">
<link rel="preload" href="/zh/assets/js/main.404c2b61.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="/zh/"><img src="/zh/img/logo.svg" alt="Apache ShenYu Logo" class="themedImage_TMUO themedImage--light_4Vu1 navbar__logo"><img src="/zh/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="/zh/download">下载</a><a class="navbar__item navbar__link" href="/zh/document">文档</a><a class="navbar__item navbar__link" href="/zh/community/contributor-guide">社区</a><a class="navbar__item navbar__link" href="/zh/team">团队</a><a class="navbar__item navbar__link" href="/zh/event">事件</a><a class="navbar__item navbar__link" href="/zh/news">新闻</a><a class="navbar__item navbar__link" href="/zh/blog">博客</a><a class="navbar__item navbar__link" href="/zh/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>简体中文</span></span></a><ul class="dropdown__menu"><li><a href="/docs/plugin-center/http-process/redirect-plugin" target="_self" rel="noopener noreferrer" class="dropdown__link" style="text-transform:capitalize">English</a></li><li><a href="/zh/docs/plugin-center/http-process/redirect-plugin" target="_self" rel="noopener noreferrer" class="dropdown__link dropdown__link--active" 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="搜索"><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">搜索</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 class="menu__link" href="/zh/docs/index">Apache ShenYu 介绍</a></li><li class="menu__list-item menu__list-item--collapsed"><a class="menu__link menu__link--sublist" href="#">设计文档</a></li><li class="menu__list-item menu__list-item--collapsed"><a class="menu__link menu__link--sublist" href="#">运维部署</a></li><li class="menu__list-item menu__list-item--collapsed"><a class="menu__link menu__link--sublist" href="#">快速开始</a></li><li class="menu__list-item menu__list-item--collapsed"><a class="menu__link menu__link--sublist" href="#">用户指南</a></li><li class="menu__list-item"><a class="menu__link menu__link--sublist menu__link--active" href="#">插件集合</a><ul style="display:block;overflow:visible;height:auto" class="menu__list"><li class="menu__list-item"><a class="menu__link menu__link--sublist menu__link--active" href="#" tabindex="0">Http Process</a><ul style="display:block;overflow:visible;height:auto" class="menu__list"><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/zh/docs/plugin-center/http-process/contextpath-plugin">Context Path插件</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/zh/docs/plugin-center/http-process/modifyresponse-plugin">ModifyResponse插件</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/zh/docs/plugin-center/http-process/parammapping-plugin">ParamMapping插件</a></li><li class="menu__list-item"><a aria-current="page" class="menu__link menu__link--active active" tabindex="0" href="/zh/docs/plugin-center/http-process/redirect-plugin">Redirect插件</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/zh/docs/plugin-center/http-process/request-plugin">Request插件</a></li><li class="menu__list-item"><a class="menu__link" tabindex="0" href="/zh/docs/plugin-center/http-process/rewrite-plugin">Rewrite插件</a></li></ul></li><li class="menu__list-item menu__list-item--collapsed"><a class="menu__link menu__link--sublist" href="#" tabindex="0">Proxy</a></li><li class="menu__list-item menu__list-item--collapsed"><a class="menu__link menu__link--sublist" href="#" tabindex="0">熔断限流</a></li><li class="menu__list-item menu__list-item--collapsed"><a class="menu__link menu__link--sublist" href="#" tabindex="0">安全性</a></li><li class="menu__list-item menu__list-item--collapsed"><a class="menu__link menu__link--sublist" href="#" tabindex="0">可观测性</a></li><li class="menu__list-item menu__list-item--collapsed"><a class="menu__link menu__link--sublist" href="#" tabindex="0">通用组件</a></li><li class="menu__list-item menu__list-item--collapsed"><a class="menu__link menu__link--sublist" href="#" tabindex="0">Cache</a></li><li class="menu__list-item menu__list-item--collapsed"><a class="menu__link menu__link--sublist" href="#" tabindex="0">Mock</a></li></ul></li><li class="menu__list-item menu__list-item--collapsed"><a class="menu__link menu__link--sublist" href="#">开发者文档</a></li><li class="menu__list-item menu__list-item--collapsed"><a class="menu__link menu__link--sublist" href="#">基准测试</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><span class="badge badge--secondary">Version: 2.6.1</span><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">Redirect插件</h1></header><h2><a aria-hidden="true" tabindex="-1" class="anchor enhancedAnchor_WiXH" id="说明"></a>说明<a class="hash-link" href="#说明" title="Direct link to heading">#</a></h2><ul><li>Apache ShenYu 网关在对目标服务进行代理调用的时候,允许用户使用 <code>redirect</code> 插件来重定向请求。</li></ul><h2><a aria-hidden="true" tabindex="-1" class="anchor enhancedAnchor_WiXH" id="插件设置"></a>插件设置<a class="hash-link" href="#插件设置" title="Direct link to heading">#</a></h2><ul><li><code>shenyu-admin</code> --&gt; 基础配置 --&gt; 插件管理 --&gt; <code>redirect</code>,设置为开启。</li><li>如果用户不需要,可以把插件禁用。</li></ul><img src="/img/shenyu/plugin/redirect/redirect-plugin-enable-zh.png" width="70%" height="60%"><h2><a aria-hidden="true" tabindex="-1" class="anchor enhancedAnchor_WiXH" id="插件使用"></a>插件使用<a class="hash-link" href="#插件使用" title="Direct link to heading">#</a></h2><ul><li>在网关的 <code>pom.xml</code> 文件中添加 <code>redirect</code> 的支持。</li></ul><div class="codeBlockContainer_J+bg"><div class="codeBlockContent_csEI xml"><pre tabindex="0" class="prism-code language-xml 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"> </span><span class="token comment" style="color:#999988;font-style:italic">&lt;!-- apache shenyu redirect plugin start--&gt;</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token tag punctuation" style="color:#393A34">&lt;</span><span class="token tag" style="color:#00009f">dependency</span><span class="token tag punctuation" style="color:#393A34">&gt;</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token tag punctuation" style="color:#393A34">&lt;</span><span class="token tag" style="color:#00009f">groupId</span><span class="token tag punctuation" style="color:#393A34">&gt;</span><span class="token plain">org.apache.shenyu</span><span class="token tag punctuation" style="color:#393A34">&lt;/</span><span class="token tag" style="color:#00009f">groupId</span><span class="token tag punctuation" style="color:#393A34">&gt;</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token tag punctuation" style="color:#393A34">&lt;</span><span class="token tag" style="color:#00009f">artifactId</span><span class="token tag punctuation" style="color:#393A34">&gt;</span><span class="token plain">shenyu-spring-boot-starter-plugin-redirect</span><span class="token tag punctuation" style="color:#393A34">&lt;/</span><span class="token tag" style="color:#00009f">artifactId</span><span class="token tag punctuation" style="color:#393A34">&gt;</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token tag punctuation" style="color:#393A34">&lt;</span><span class="token tag" style="color:#00009f">version</span><span class="token tag punctuation" style="color:#393A34">&gt;</span><span class="token plain">${project.version}</span><span class="token tag punctuation" style="color:#393A34">&lt;/</span><span class="token tag" style="color:#00009f">version</span><span class="token tag punctuation" style="color:#393A34">&gt;</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token tag punctuation" style="color:#393A34">&lt;/</span><span class="token tag" style="color:#00009f">dependency</span><span class="token tag punctuation" style="color:#393A34">&gt;</span><span class="token plain"></span></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">&lt;!-- apache shenyu redirect plugin end--&gt;</span></span></code></pre><button type="button" aria-label="Copy code to clipboard" class="copyButton_M3SB clean-btn">Copy</button></div></div><ul><li>选择器和规则,只有匹配的请求,才会进行转发和重定向,请参考:<a href="/zh/docs/user-guide/admin-usage/selector-and-rule">选择器规则管理</a></li></ul><h2><a aria-hidden="true" tabindex="-1" class="anchor enhancedAnchor_WiXH" id="场景"></a>场景<a class="hash-link" href="#场景" title="Direct link to heading">#</a></h2><blockquote><p>顾名思义,<code>redirect</code> 插件就是对 <code>uri</code> 的重新转发和重定向。</p></blockquote><h4><a aria-hidden="true" tabindex="-1" class="anchor enhancedAnchor_WiXH" id="重定向"></a>重定向<a class="hash-link" href="#重定向" title="Direct link to heading">#</a></h4><ul><li>我们在 <code>Rule</code> 配置自定义路径时,应该为一个可达的服务路径。</li><li>当匹配到请求后,根据自定义的路径,<code>Apache ShenYu</code>网关会进行 <code>308</code> 服务跳转。</li></ul><img src="/img/shenyu/plugin/redirect/redirect-plugin-rule-zh.png" width="70%" height="60%"><h4><a aria-hidden="true" tabindex="-1" class="anchor enhancedAnchor_WiXH" id="网关自身接口转发"></a>网关自身接口转发<a class="hash-link" href="#网关自身接口转发" title="Direct link to heading">#</a></h4><ul><li>当满足匹配规则时,服务内部会使用 <code>DispatcherHandler</code> 内部接口转发。</li><li>要实现网关自身接口转发,我们需要在配置路径使用 <code>/</code> 作为前缀开始,具体配置如下图。</li></ul><img src="/img/shenyu/plugin/redirect/redirect-plugin-forward-rule-zh.png" width="70%" height="60%"></div><footer class="row docusaurus-mt-lg"><div class="col"><a href="https://github.com/apache/shenyu-website/edit/main/i18n/zh/docusaurus-plugin-content-docs/version-2.6.1/plugin-center/http-process/redirect-plugin.md" target="_blank" rel="noreferrer noopener"><svg fill="currentColor" height="20" width="20" viewBox="0 0 40 40" class="iconEdit_mS5F" aria-hidden="true"><g><path d="m34.5 11.7l-3 3.1-6.3-6.3 3.1-3q0.5-0.5 1.2-0.5t1.1 0.5l3.9 3.9q0.5 0.4 0.5 1.1t-0.5 1.2z m-29.5 17.1l18.4-18.5 6.3 6.3-18.4 18.4h-6.3v-6.2z"></path></g></svg>编辑本文档</a></div><div class="col lastUpdated_wj+Z"></div></footer></article><nav class="pagination-nav docusaurus-mt-lg" aria-label="Docs pages navigation"><div class="pagination-nav__item"><a class="pagination-nav__link" href="/zh/docs/plugin-center/http-process/parammapping-plugin"><div class="pagination-nav__sublabel">上一篇</div><div class="pagination-nav__label">« ParamMapping插件</div></a></div><div class="pagination-nav__item pagination-nav__item--next"><a class="pagination-nav__link" href="/zh/docs/plugin-center/http-process/request-plugin"><div class="pagination-nav__sublabel">下一篇</div><div class="pagination-nav__label">Request插件 »</div></a></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="#说明" class="table-of-contents__link">说明</a></li><li><a href="#插件设置" class="table-of-contents__link">插件设置</a></li><li><a href="#插件使用" class="table-of-contents__link">插件使用</a></li><li><a href="#场景" class="table-of-contents__link">场景</a></li></ul></div></div></div></div></main></div></div></div>
<script src="/zh/assets/js/runtime~main.ba3d74b1.js"></script>
<script src="/zh/assets/js/main.404c2b61.js"></script>
</body>
</html>