spring-cloud-gateway-stateful-route.html 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <meta name="generator" content="Asciidoctor 2.0.15">
  8. <meta name="author" content="pxzxj, pudge.zxj@gmail.com, 2023/03/08">
  9. <title>Spring Cloud Gateway有状态路由</title>
  10. <link rel="stylesheet" href="css/site.css">
  11. <link href="css/custom.css" rel="stylesheet">
  12. <script src="js/setup.js"></script><script defer src="js/site.js"></script>
  13. </head>
  14. <body class="article toc2 toc-left"><div id="banner-container" class="container" role="banner">
  15. <div id="banner" class="contained" role="banner">
  16. <div id="switch-theme">
  17. <input type="checkbox" id="switch-theme-checkbox" />
  18. <label for="switch-theme-checkbox">Dark Theme</label>
  19. </div>
  20. </div>
  21. </div>
  22. <div id="tocbar-container" class="container" role="navigation">
  23. <div id="tocbar" class="contained" role="navigation">
  24. <button id="toggle-toc"></button>
  25. </div>
  26. </div>
  27. <div id="main-container" class="container">
  28. <div id="main" class="contained">
  29. <div id="doc" class="doc">
  30. <div id="header">
  31. <h1>Spring Cloud Gateway有状态路由</h1>
  32. <div class="details">
  33. <span id="author" class="author">pxzxj</span><br>
  34. <span id="author2" class="author">pudge.zxj@gmail.com</span><br>
  35. <span id="author3" class="author">2023/03/08</span><br>
  36. </div>
  37. <div id="toc" class="toc2">
  38. <div id="toctitle">Table of Contents</div>
  39. <span id="back-to-index"><a href="index.html">Back to index</a></span><ul class="sectlevel1">
  40. <li><a href="#_项目准备">1. 项目准备</a></li>
  41. <li><a href="#_配置验证">2. 配置验证</a></li>
  42. </ul>
  43. </div>
  44. </div>
  45. <div id="content">
  46. <div id="preamble">
  47. <div class="sectionbody">
  48. <div class="paragraph">
  49. <p>Spring Cloud Gateway是目前最流行的网关实现之一,通常与Spring Cloud中其它框架一起用于构建微服务项目,Spring Cloud Gateway默认是按照负载均衡的方式将请求路由到后端服务实例的,但在某些特殊场景下则需要有状态路由,例如服务有两个接口其中一个用于生成文件存储在本地,下一个接口用于下载生成的文件。本文介绍有状态路由的实现方法</p>
  50. </div>
  51. </div>
  52. </div>
  53. <div class="sect1">
  54. <h2 id="_项目准备"><a class="anchor" href="#_项目准备"></a>1. 项目准备</h2>
  55. <div class="sectionbody">
  56. <div class="paragraph">
  57. <p>新建一个网关项目 <code>gateway</code> 和后端服务项目 <code>backend</code>,并在 <code>backend</code> 中定义两个有状态的接口</p>
  58. </div>
  59. <div class="listingblock">
  60. <div class="content">
  61. <pre class="highlight"><code class="language-java" data-lang="java">@RestController
  62. public class MyController {
  63. private final Map&lt;String, String&gt; map = new HashMap&lt;&gt;();
  64. @RequestMapping("/get")
  65. public String get() {
  66. return map.get("key");
  67. }
  68. @RequestMapping("/set/{value}")
  69. public void set(@PathVariable String value) {
  70. map.put("key", value);
  71. }
  72. }
  73. </code></pre>
  74. </div>
  75. </div>
  76. <div class="paragraph">
  77. <p><code>backend</code> 启动两个实例并在 <code>gateway</code> 中配置两个实例的地址,此处为 <code>localhost:8082</code> 和 <code>localhost:8083</code>,对应的实例Id是 <code>ins2</code> 和 <code>ins3</code></p>
  78. </div>
  79. <div class="listingblock">
  80. <div class="content">
  81. <pre class="highlight"><code class="language-yaml" data-lang="yaml">server:
  82. port: 8081
  83. spring:
  84. cloud:
  85. discovery:
  86. client:
  87. simple:
  88. instances:
  89. backend:
  90. - uri: http://localhost:8082
  91. instance-id: ins2
  92. - uri: http://localhost:8083
  93. instance-id: ins3
  94. gateway:
  95. routes:
  96. - id: backend
  97. uri: lb://backend
  98. predicates:
  99. - Path=/**</code></pre>
  100. </div>
  101. </div>
  102. <div class="admonitionblock tip">
  103. <table>
  104. <tr>
  105. <td class="icon">
  106. <i class="fa icon-tip" title="Tip"></i>
  107. </td>
  108. <td class="content">
  109. 完整项目源码参考 <a href="https://github.com/pxzxj/spring-cloud-gateway-stateful-route">GitHub</a>
  110. </td>
  111. </tr>
  112. </table>
  113. </div>
  114. <div class="paragraph">
  115. <p>此时通过网关先调用 <code>/set/vv</code> 接口再调用 <code>/get</code> 接口是显然无法获取到上一步保存的 <code>vv</code> 的,因为按照默认的负载均衡算法,两次请求被路由到了不同的 <code>backend</code> 实例上</p>
  116. </div>
  117. <div class="listingblock">
  118. <div class="content">
  119. <pre>C:\Users\pxzxj1&gt;curl "http://localhost:8081/set/vv"
  120. C:\Users\pxzxj1&gt;curl "http://localhost:8081/get"
  121. C:\Users\pxzxj1&gt;</pre>
  122. </div>
  123. </div>
  124. </div>
  125. </div>
  126. <div class="sect1">
  127. <h2 id="_配置验证"><a class="anchor" href="#_配置验证"></a>2. 配置验证</h2>
  128. <div class="sectionbody">
  129. <div class="paragraph">
  130. <p>Spring Cloud LoadBalancer提供了 <a href="https://docs.spring.io/spring-cloud-commons/docs/3.1.6/reference/html/#request-based-sticky-session-for-loadbalancer">Request-based Sticky Session</a>配置可以解决有状态路由问题,Spring Cloud Cloud也使用了Spring Cloud LoadBalancer实现路由选择</p>
  131. </div>
  132. <div class="paragraph">
  133. <p>首先在网关的配置文件中添加如下内容</p>
  134. </div>
  135. <div class="listingblock">
  136. <div class="content">
  137. <pre class="highlight"><code class="language-yaml" data-lang="yaml">spring:
  138. cloud:
  139. loadbalancer:
  140. configurations: request-based-sticky-session <i class="conum" data-value="1"></i><b>(1)</b>
  141. sticky-session:
  142. add-service-instance-cookie: true <i class="conum" data-value="2"></i><b>(2)</b></code></pre>
  143. </div>
  144. </div>
  145. <div class="olist arabic">
  146. <ol class="arabic">
  147. <li>
  148. <p>基于请求中的Cookie路由,默认Cookie名称是 <code>sc-lb-instance-id</code></p>
  149. </li>
  150. <li>
  151. <p>请求转发到后端时将选中的后端实例信息添加到请求的Cookie中</p>
  152. </li>
  153. </ol>
  154. </div>
  155. <div class="paragraph">
  156. <p>为了将此次选中的实例信息返回客户端,后端接口也需要修改,将网关添加到请求中的Cookie添加到响应中</p>
  157. </div>
  158. <div class="listingblock">
  159. <div class="content">
  160. <pre class="highlight"><code class="language-java" data-lang="java">@RestController
  161. public class MyController {
  162. private final Map&lt;String, String&gt; map = new HashMap&lt;&gt;();
  163. @RequestMapping("/get")
  164. public String get() {
  165. return map.get("key");
  166. }
  167. @RequestMapping("/set/{value}")
  168. public void set(@PathVariable String value, HttpServletRequest request, HttpServletResponse response) {
  169. Cookie[] cookies = request.getCookies();
  170. if(cookies != null) {
  171. for(Cookie cookie : cookies) {
  172. if("sc-lb-instance-id".equals(cookie.getName())) {
  173. response.addCookie(cookie);
  174. break;
  175. }
  176. }
  177. }
  178. map.put("key", value);
  179. }
  180. }
  181. </code></pre>
  182. </div>
  183. </div>
  184. <div class="paragraph">
  185. <p>此时再次调用 <code>/set/vv</code> 接口可以在它的响应的Cookie中看到本次网关选中的后端实例为 <code>ins2</code>,那么调用 <code>/get</code> 时也附带上此Cookie就可以使网关把请求也路由到 <code>ins2</code>,从而获取到 <code>vv</code></p>
  186. </div>
  187. <div class="listingblock">
  188. <div class="content">
  189. <pre>C:\Users\pxzxj1&gt;curl "http://localhost:8081/set/vv" -v
  190. * Trying 127.0.0.1:8081...
  191. * Connected to localhost (127.0.0.1) port 8081 (#0)
  192. &gt; GET /set/vv HTTP/1.1
  193. &gt; Host: localhost:8081
  194. &gt; User-Agent: curl/7.83.1
  195. &gt; Accept: */*
  196. &gt;
  197. * Mark bundle as not supporting multiuse
  198. &lt; HTTP/1.1 200 OK
  199. &lt; Set-Cookie: sc-lb-instance-id=ins2
  200. &lt; Content-Length: 0
  201. &lt; Date: Wed, 08 Mar 2023 08:44:57 GMT
  202. &lt;
  203. * Connection #0 to host localhost left intact
  204. C:\Users\pxzxj1&gt;curl -H "Cookie: sc-lb-instance-id=ins2" "http://localhost:8081/get"
  205. vv
  206. C:\Users\pxzxj1&gt;</pre>
  207. </div>
  208. </div>
  209. </div>
  210. </div>
  211. </div>
  212. <div id="footer">
  213. <div id="footer-text">
  214. Last updated 2024-03-18 05:44:42 UTC
  215. </div>
  216. </div>
  217. <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.18.3/highlight.min.js"></script>
  218. <script>
  219. if (!hljs.initHighlighting.called) {
  220. hljs.initHighlighting.called = true
  221. ;[].slice.call(document.querySelectorAll('pre.highlight > code')).forEach(function (el) { hljs.highlightBlock(el) })
  222. }
  223. </script>
  224. <script src="https://utteranc.es/client.js"
  225. repo="pxzxj/articles"
  226. issue-term="title"
  227. label="utteranc"
  228. theme="github-light"
  229. crossorigin="anonymous"
  230. async>
  231. </script>
  232. </div>
  233. </div>
  234. </div>
  235. </body>
  236. </html>