AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當(dāng)前位置:首頁(yè) > 科技  > 軟件

Spring Cloud Gateway提供的簡(jiǎn)易網(wǎng)關(guān)實(shí)現(xiàn)方式,你使用過(guò)嗎?

來(lái)源: 責(zé)編: 時(shí)間:2023-09-18 21:41:29 321觀看
導(dǎo)讀環(huán)境:SpringBoot2.5.13Spring Cloud Gateway提供了一個(gè)名為ProxyExchange的實(shí)用程序?qū)ο蟆D憧梢栽诔R?guī)Spring web處理程序中使用它作為方法參數(shù)。它通過(guò)鏡像HTTP動(dòng)詞的方法支持基本的下游HTTP交換。在MVC中,它還支持通

環(huán)境:SpringBoot2.5.13u9y28資訊網(wǎng)——每日最新資訊28at.com

Spring Cloud Gateway提供了一個(gè)名為ProxyExchange的實(shí)用程序?qū)ο蟆D憧梢栽诔R?guī)Spring web處理程序中使用它作為方法參數(shù)。它通過(guò)鏡像HTTP動(dòng)詞的方法支持基本的下游HTTP交換。在MVC中,它還支持通過(guò)forward()方法轉(zhuǎn)發(fā)到本地處理程序。要使用ProxyExchange,需要在classpath中包含正確的模塊(spring-cloud-gateway-mvc(3.1.5)spring-cloud-gateway-webflux)。u9y28資訊網(wǎng)——每日最新資訊28at.com

下面的MVC示例將請(qǐng)求代理到/test下游到遠(yuǎn)程服務(wù)器:u9y28資訊網(wǎng)——每日最新資訊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();  }}

下面的例子對(duì)Webflux做了相同的事情:u9y28資訊網(wǎng)——每日最新資訊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上的便利方法使處理程序方法能夠發(fā)現(xiàn)并增強(qiáng)傳入請(qǐng)求的URI路徑。例如,你可能想提取路徑末尾的元素并將其傳遞到下游:u9y28資訊網(wǎng)——每日最新資訊28at.com

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

目標(biāo)服務(wù)接口

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

網(wǎng)關(guān)服務(wù)接口

@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() ;  }  }

調(diào)用結(jié)果

圖片圖片u9y28資訊網(wǎng)——每日最新資訊28at.com

Postman請(qǐng)求u9y28資訊網(wǎng)——每日最新資訊28at.com

控制臺(tái)輸出控制臺(tái)輸出u9y28資訊網(wǎng)——每日最新資訊28at.com

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

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

你還可以通過(guò)在get()方法(以及其他方法)中添加一個(gè)mapper來(lái)操作響應(yīng)頭(以及響應(yīng)中的其他任何內(nèi)容)。mapper是一個(gè)Function,接收傳入的ResponseEntity并將其轉(zhuǎn)換為傳出的ResponseEntity,如下:u9y28資訊網(wǎng)——每日最新資訊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") ;}) ;

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

當(dāng)我們的請(qǐng)求中有Authorization 請(qǐng)求Header信息時(shí),默認(rèn)將不會(huì)向下游傳遞,這是默認(rèn)行為還有cookie。我們可以通過(guò)修改配置文件覆蓋。u9y28資訊網(wǎng)——每日最新資訊28at.com

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

完畢!!!u9y28資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-10458-0.htmlSpring Cloud Gateway提供的簡(jiǎn)易網(wǎng)關(guān)實(shí)現(xiàn)方式,你使用過(guò)嗎?

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com

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

下一篇: 深度!HashMap的底層數(shù)據(jù)結(jié)構(gòu)

標(biāo)簽:
  • 熱門(mén)焦點(diǎn)
Top