SpringCloudGateWay修改请求路径,从注册中心获得服务

Posted saozhou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringCloudGateWay修改请求路径,从注册中心获得服务相关的知识,希望对你有一定的参考价值。

SpringCloudGateWay修改请求路径,从注册中心获得服务

    @Resource
    private DiscoveryClient disClient;
    @Resource
    private LoadBalancerClient loadBlance;
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) String contentType; ServiceInstance serviceInstance = null; boolean formdataType = false; String postBody = ""; String ip = ""; String tokenID = ""; ServerHttpRequest req = exchange.getRequest(); ServerHttpResponse rep = exchange.getResponse(); String path = req.getURI().getPath(); String port = Integer.toString(req.getURI().getPort()); URI requestUri = req.getURI(); String method = req.getMethod().name(); String schema = requestUri.getScheme(); if ((!"http".equals(schema) && !"https".equals(schema))) return chain.filter(exchange); if (method.equals("GET") && port.equals(localPort)) return chain.filter(exchange); else if(method.equals("POST") && (path.equals("/token")||path.equals("/callMESFunctionNoAuth"))) chain.filter(exchange); else if (method.equals("POST") && port.equals(localPort)) AccessRecord accessRecord = new AccessRecord(); accessRecord.setPath(requestUri.getPath()); accessRecord.setQueryString(req.getQueryParams()); exchange.getAttributes().put("startTime", System.currentTimeMillis()); ip = exchange.getRequest().getRemoteAddress().getAddress().getHostAddress(); try postBody = resolveBodyFromRequest(req);if (true) contentType = exchange.getRequest().getHeaders().getFirst("Content-Type"); if (!StringUtil.isEmpty(contentType)) formdataType = contentType.startsWith("multipart/form-data"); if (!formdataType) URL uri = service.getURI(auth.getFunCode()); String url = null; if (uri != null) try serviceInstance = loadBlance.choose(uri.getApplicationName()); catch (Exception e) e.printStackTrace(); url = schema + "://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + uri.getUrl(); else url = schema + "://" + mesUrl + requestUri.getPath(); log.info("post redirect url ", url); System.out.println("post redirect url " + url); // postBody = exchange.getAttribute("cachedRequestBodyObject"); // 下面将请求体再次封装写回到 request 里,传到下一级. URI eTx = URI.create(url);//修改了请求的路径 ServerHttpRequest newRequest = req.mutate().uri(eTx).build(); System.out.println(newRequest.getHeaders()); DataBuffer bodyDataBuffer = stringBuffer(postBody); Flux<DataBuffer> bodyFlux = Flux.just(bodyDataBuffer); newRequest = new ServerHttpRequestDecorator(newRequest) @Override public Flux<DataBuffer> getBody() return bodyFlux; ; accessRecord.setBody(formatStr(postBody)); ServerWebExchange newExchange = exchange.mutate().request(newRequest).build(); exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR, newRequest.getURI()); exchange = newExchange; return returnMono(chain, exchange, accessRecord); else rep.setStatusCode(HttpStatus.FORBIDDEN); return rep.setComplete(); catch (Exception e) log.error("IP:,URI:,TokenID:,Exception:", ip, path, tokenID, e); rep.setStatusCode(HttpStatus.FORBIDDEN); return rep.setComplete(); return chain.filter(exchange);

 

以上是关于SpringCloudGateWay修改请求路径,从注册中心获得服务的主要内容,如果未能解决你的问题,请参考以下文章

SpringCloudGateway 学习笔记 - 使用内置过滤器添加请求头响应头

SpringCloudGateway 学习笔记 - 使用内置过滤器添加请求头响应头

Spring Cloud Gateway:基于ServerWebExchange修改请求或者响应内容

Spring Cloud Gateway:基于ServerWebExchange修改请求或者响应内容

Spring Cloud Gateway修改请求和响应body的内容

Spring Cloud Gateway修改请求和响应body的内容