springcloud系列—Zuul—第5章-2: Spring Cloud Zuul 路由详解

论坛 期权论坛     
选择匿名的用户   2021-5-28 02:11   29   0
<p><em>资料参考:《Spring Cloud 微服务实战》</em></p>
<p id="main-toc"><strong>目录</strong></p>
<p id="%E8%B7%AF%E7%94%B1%E8%AF%A6%E8%A7%A3-toc" style="margin-left:0px;"><a href="https://blog.csdn.net/weixin_40663800/article/details/85247594#%E8%B7%AF%E7%94%B1%E8%AF%A6%E8%A7%A3">路由详解</a></p>
<p id="%E4%BC%A0%E7%BB%9F%E8%B7%AF%E7%94%B1%E9%85%8D%E7%BD%AE-toc" style="margin-left:40px;"><a href="https://blog.csdn.net/weixin_40663800/article/details/85247594#%E4%BC%A0%E7%BB%9F%E8%B7%AF%E7%94%B1%E9%85%8D%E7%BD%AE">传统路由配置</a></p>
<p id="服务路由配置-toc" style="margin-left:40px;"><a href="https://blog.csdn.net/weixin_40663800/article/details/85247594#%E6%9C%8D%E5%8A%A1%E8%B7%AF%E7%94%B1%E9%85%8D%E7%BD%AE">服务路由配置</a></p>
<p id="%E6%9C%8D%E5%8A%A1%E8%B7%AF%E7%94%B1%E7%9A%84%E9%BB%98%E8%AE%A4%E8%A7%84%E5%88%99-toc" style="margin-left:40px;"><a href="https://blog.csdn.net/weixin_40663800/article/details/85247594#%E6%9C%8D%E5%8A%A1%E8%B7%AF%E7%94%B1%E7%9A%84%E9%BB%98%E8%AE%A4%E8%A7%84%E5%88%99">服务路由的默认规则</a></p>
<p id="%E8%87%AA%E5%AE%9A%E4%B9%89%E8%B7%AF%E7%94%B1%E6%98%A0%E5%B0%84%E5%85%B3%E7%B3%BB-toc" style="margin-left:40px;"><a href="https://blog.csdn.net/weixin_40663800/article/details/85247594#%E8%87%AA%E5%AE%9A%E4%B9%89%E8%B7%AF%E7%94%B1%E6%98%A0%E5%B0%84%E5%85%B3%E7%B3%BB">自定义路由映射关系</a></p>
<p id="%E8%B7%AF%E5%BE%84%E5%8C%B9%E9%85%8D-toc" style="margin-left:40px;"><a href="https://blog.csdn.net/weixin_40663800/article/details/85247594#%E8%B7%AF%E5%BE%84%E5%8C%B9%E9%85%8D">路径匹配</a></p>
<p id="%E5%BF%BD%E7%95%A5%E8%A1%A8%E8%BE%BE%E5%BC%8F-toc" style="margin-left:40px;"><a href="https://blog.csdn.net/weixin_40663800/article/details/85247594#%E5%BF%BD%E7%95%A5%E8%A1%A8%E8%BE%BE%E5%BC%8F">忽略表达式</a></p>
<p id="%E8%B7%AF%E7%94%B1%E5%89%8D%E7%BC%80-toc" style="margin-left:40px;"><a href="https://blog.csdn.net/weixin_40663800/article/details/85247594#%E8%B7%AF%E7%94%B1%E5%89%8D%E7%BC%80">路由前缀</a></p>
<p id="%E6%9C%AC%E5%9C%B0%E8%B7%B3%E8%BD%AC-toc" style="margin-left:40px;"><a href="https://blog.csdn.net/weixin_40663800/article/details/85247594#%E6%9C%AC%E5%9C%B0%E8%B7%B3%E8%BD%AC">本地跳转</a></p>
<p id="cookie%E4%B8%8E%E5%A4%B4%E4%BF%A1%E6%81%AF-toc" style="margin-left:40px;"><a href="https://blog.csdn.net/weixin_40663800/article/details/85247594#cookie%E4%B8%8E%E5%A4%B4%E4%BF%A1%E6%81%AF">cookie与头信息</a></p>
<p id="%E9%87%8D%E5%AE%9A%E5%90%91%E9%97%AE%E9%A2%98-toc" style="margin-left:40px;"><a href="https://blog.csdn.net/weixin_40663800/article/details/85247594#%E9%87%8D%E5%AE%9A%E5%90%91%E9%97%AE%E9%A2%98">重定向问题</a></p>
<hr id="hr-toc">
<h1 id="%E8%B7%AF%E7%94%B1%E8%AF%A6%E8%A7%A3">路由详解</h1>
<h2 id="%E4%BC%A0%E7%BB%9F%E8%B7%AF%E7%94%B1%E9%85%8D%E7%BD%AE">传统路由配置</h2>
<p style="text-indent:0;">    所谓的传统路由配置方式就是在不依赖于服务发现机制的情况下,通过在配置文件中具体指定每个路由表达式与服务实例的映射关系来实现API网关对外部请求的路由。</p>
<p style="text-indent:0;">    没有Eureka和Consul的服务治理框架帮助的时候,我们需要根据服务实例的数量采用不同方式的配置来实现路由规则:</p>
<ul><li><span style="color:#3399ea;"><strong>单实例配置:通过一组<code>zuul.routes.&lt;route&gt;.path</code>与<code>zuul.routes.&lt;route&gt;.url</code>参数对的方式配置,比如:</strong></span></li></ul>
<pre class="blockcode"><code>zuul.routes.user-service.path&#61;/user-service/**
zuul.routes.user-service.url&#61;http://localhost:8080/</code></pre>
<p>    该配置实现了对符合<code>/user-service/**</code>规则的请求路径转发到<code>http://localhost:8080/</code>地址的路由规则,比如,当有一个请求<code>http://localhost:1101/user-service/hello</code>被发送到API网关上,由于<code>/user-service/hello</code>能够被上述配置的<code>path</code>规则匹配,所以API网关会转发请求到<code>http://localhost:8080/hello</code>地址。</p>
<ul><li><span style="color:#3399ea;"><strong>多实例配置:通过一组<code>zuul.routes.&lt;route&gt;.path</code>与<code>zuul.routes.&lt;route&gt;.serviceId</code>参数对的方式配置,比如:</strong></span></li></ul>
<pre class="blockcode"><code>zuul.routes.user-service.path&#61;/user-service/**
zuul.routes.user-service.serviceId&#61;user-service

ribbon.eureka.enabled&#61;false
user-service.ribbon.listOfServers&#61;http://localhost:8080/,http://localhost:8081/</code></pre>
<p>该配置实现了对符合<code>/user-service/**</code>规则的请求路径转发到<code>http://localhost:8080/</code>和<code>http://localhost:8081/</code>两个实例地址的路由规则。它的配置方式与服务路由的配置方式一样,都采用了<code>zuul.routes.&lt;route&gt;.path</code>与<code>zuul.routes.&lt;route&gt;.serviceId</code>参数对的映射方式,只是这里的<code>serviceId</code>是由用户手工命名的服务名称,配合<code>&lt;serviceId&gt;.ribbon.listOfServers</code>参数实现服务与实例的维护。<strong><span style="color:#86ca5e;">由于存在多个实例,API网关在进行路由转发时需要实现负载均衡策略,于是这里还需要Spr
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP