日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

Spring Cloud Gateway提供的簡易網關實現方式,你使用過嗎?

來源: 責編: 時間:2023-09-18 21:41:29 294觀看
導讀環境:SpringBoot2.5.13Spring Cloud Gateway提供了一個名為ProxyExchange的實用程序對象。你可以在常規Spring web處理程序中使用它作為方法參數。它通過鏡像HTTP動詞的方法支持基本的下游HTTP交換。在MVC中,它還支持通

環境:SpringBoot2.5.13JYR28資訊網——每日最新資訊28at.com

Spring Cloud Gateway提供了一個名為ProxyExchange的實用程序對象。你可以在常規Spring web處理程序中使用它作為方法參數。它通過鏡像HTTP動詞的方法支持基本的下游HTTP交換。在MVC中,它還支持通過forward()方法轉發到本地處理程序。要使用ProxyExchange,需要在classpath中包含正確的模塊(spring-cloud-gateway-mvc(3.1.5)spring-cloud-gateway-webflux)。JYR28資訊網——每日最新資訊28at.com

下面的MVC示例將請求代理到/test下游到遠程服務器:JYR28資訊網——每日最新資訊28at.com

@RestController@SpringBootApplicationpublic class GatewaySampleApplication {  @Value("${remote.home}")  private URI home;  @GetMapping("/test")  public ResponseEntity<?> proxy(ProxyExchange<byte[]> proxy) throws Exception {    return proxy.uri(home.toString() + "/image/png").get();  }}

下面的例子對Webflux做了相同的事情:JYR28資訊網——每日最新資訊28at.com

@RestController@SpringBootApplicationpublic class GatewaySampleApplication {  @Value("${remote.home}")  private URI home;  @GetMapping("/test")  public Mono<ResponseEntity<?>> proxy(ProxyExchange<byte[]> proxy) throws Exception {    return proxy.uri(home.toString() + "/image/png").get();  }}

ProxyExchange上的便利方法使處理程序方法能夠發現并增強傳入請求的URI路徑。例如,你可能想提取路徑末尾的元素并將其傳遞到下游:JYR28資訊網——每日最新資訊28at.com

@GetMapping("/proxy/path/**")public ResponseEntity<?> proxyPath(ProxyExchange<byte[]> proxy) throws Exception {  // 如這里請求的/proxy/path/666,那么這里path = 666  String path = proxy.path("/proxy/path/");  return proxy.uri(home.toString() + "/foos/" + path).get();}

目標服務接口

@RestController@RequestMapping("/business")public class BusinessController {  @PostMapping("/index")  public Object index(@RequestBody Map<String ,Object> body) {    System.out.println("業務接口接收到的內容:" + body) ;    Map<String, Object> result = new HashMap<>() ;    result.put("code", 0) ;    result.put("data", "業務處理成功 - " + LocalDateTime.now().getNano()) ;    result.put("message", "success") ;    return result ;  }  }

網關服務接口

@RestController@RequestMapping("/proxy/api")public class GatewayController {  @GetMapping("")  public Object order(@RequestHeader("token") String token,       Integer id, ProxyExchange<Map<String, Object>> exchange) {    System.out.println("token = " + token + ", id = " + id) ;    Map<String, Object> body = new HashMap<>() ;    body.put("id", id) ;    body.put("token", token) ;    return exchange.uri("http://localhost:9000/business/index").body(body).post() ;  }  }

調用結果

圖片圖片JYR28資訊網——每日最新資訊28at.com

Postman請求JYR28資訊網——每日最新資訊28at.com

控制臺輸出控制臺輸出JYR28資訊網——每日最新資訊28at.com

你還可以使用ProxyExchange的header()方法向下游響應添加header。JYR28資訊網——每日最新資訊28at.com

exchange.uri("http://localhost:9000/business/index").header("key", "123123").body(body).post() ;

你還可以通過在get()方法(以及其他方法)中添加一個mapper來操作響應頭(以及響應中的其他任何內容)。mapper是一個Function,接收傳入的ResponseEntity并將其轉換為傳出的ResponseEntity,如下:JYR28資訊網——每日最新資訊28at.com

exchange.uri("http://localhost:9000/business/index").header("key", "123123").body(body).post(result -> {  System.out.println("Resposne Header: " + result.getHeaders()) ;  return ResponseEntity.ok("success") ;}) ;

對于“敏感”標頭(默認情況下為cookieauthorization)和“代理”(x-forward-*)頭,提供了非常好的支持,這些頭不會向下游傳遞。如:JYR28資訊網——每日最新資訊28at.com

當我們的請求中有Authorization 請求Header信息時,默認將不會向下游傳遞,這是默認行為還有cookie。我們可以通過修改配置文件覆蓋。JYR28資訊網——每日最新資訊28at.com

spring:  cloud:    gateway:      proxy:        sensitive:        - ''

完畢!!!JYR28資訊網——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-10458-0.htmlSpring Cloud Gateway提供的簡易網關實現方式,你使用過嗎?

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 2023 年前端 UI 組件庫概述,百花齊放!2023 年前端 UI 組件庫概述,百花齊放!

下一篇: 深度!HashMap的底層數據結構

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 阳城县| 米脂县| 六枝特区| 广昌县| 三门县| 海口市| 望奎县| 固安县| 河源市| 中阳县| 循化| 昭觉县| 兴和县| 正镶白旗| 峨山| 革吉县| 松阳县| 靖州| 神池县| 白玉县| 宁乡县| 宝山区| 四子王旗| 绥阳县| 邹平县| 马关县| 双城市| 那曲县| 晋宁县| 玛曲县| 孟津县| 隆安县| 曲阜市| 石嘴山市| 长垣县| 长春市| 鹤峰县| 洪泽县| 永登县| 新宁县| 彭州市|