亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

spring boot實現(xiàn)超輕量級網(wǎng)關的方法(反向代理、轉發(fā))

 更新時間:2020年11月18日 10:41:41   作者:jqpeng的技術記事本  
這篇文章主要介紹了spring boot實現(xiàn)超輕量級網(wǎng)關(反向代理、轉發(fā))的相關知識,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

在我們的rest服務中,需要暴露一個中間件的接口給用戶,但是需要經(jīng)過rest服務的認證,這是典型的網(wǎng)關使用場景??梢砸刖W(wǎng)關組件來搞定,但是引入zuul等中間件會增加系統(tǒng)復雜性,這里實現(xiàn)一個超輕量級的網(wǎng)關,只實現(xiàn)請求轉發(fā),認證等由rest服務的spring security來搞定。

如何進行請求轉發(fā)呢? 熟悉網(wǎng)絡請求的同學應該很清楚,請求無非就是請求方式、HTTP header,以及請求body,我們將這些信息取出來,透傳給轉發(fā)的url即可。

舉例:

/graphdb/** 轉發(fā)到 Graph_Server/**

獲取轉發(fā)目的地址:

private String createRedictUrl(HttpServletRequest request, String routeUrl, String prefix) {
  String queryString = request.getQueryString();
  return routeUrl + request.getRequestURI().replace(prefix, "") +
    (queryString != null ? "?" + queryString : "");
 }

解析請求頭和內容

然后從request中提取出header、body等內容,構造一個RequestEntity,后續(xù)可以用RestTemplate來請求。

private RequestEntity createRequestEntity(HttpServletRequest request, String url) throws URISyntaxException, IOException {
  String method = request.getMethod();
  HttpMethod httpMethod = HttpMethod.resolve(method);
  MultiValueMap<String, String> headers = parseRequestHeader(request);
  byte[] body = parseRequestBody(request);
  return new RequestEntity<>(body, headers, httpMethod, new URI(url));
 }

 private byte[] parseRequestBody(HttpServletRequest request) throws IOException {
  InputStream inputStream = request.getInputStream();
  return StreamUtils.copyToByteArray(inputStream);
 }

 private MultiValueMap<String, String> parseRequestHeader(HttpServletRequest request) {
  HttpHeaders headers = new HttpHeaders();
  List<String> headerNames = Collections.list(request.getHeaderNames());
  for (String headerName : headerNames) {
   List<String> headerValues = Collections.list(request.getHeaders(headerName));
   for (String headerValue : headerValues) {
    headers.add(headerName, headerValue);
   }
  }
  return headers;
 }

透明轉發(fā)

最后用RestTemplate來實現(xiàn)請求:

 private ResponseEntity<String> route(RequestEntity requestEntity) {
  RestTemplate restTemplate = new RestTemplate();
  return restTemplate.exchange(requestEntity, String.class);
 }

全部代碼

以下是輕量級轉發(fā)全部代碼:

import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StreamUtils;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.List;

@Service
public class RoutingDelegate {


 public ResponseEntity<String> redirect(HttpServletRequest request, HttpServletResponse response,String routeUrl, String prefix) {
  try {
   // build up the redirect URL
   String redirectUrl = createRedictUrl(request,routeUrl, prefix);
   RequestEntity requestEntity = createRequestEntity(request, redirectUrl);
   return route(requestEntity);
  } catch (Exception e) {
   return new ResponseEntity("REDIRECT ERROR", HttpStatus.INTERNAL_SERVER_ERROR);
  }
 }

 private String createRedictUrl(HttpServletRequest request, String routeUrl, String prefix) {
  String queryString = request.getQueryString();
  return routeUrl + request.getRequestURI().replace(prefix, "") +
    (queryString != null ? "?" + queryString : "");
 }


 private RequestEntity createRequestEntity(HttpServletRequest request, String url) throws URISyntaxException, IOException {
  String method = request.getMethod();
  HttpMethod httpMethod = HttpMethod.resolve(method);
  MultiValueMap<String, String> headers = parseRequestHeader(request);
  byte[] body = parseRequestBody(request);
  return new RequestEntity<>(body, headers, httpMethod, new URI(url));
 }
	
 private ResponseEntity<String> route(RequestEntity requestEntity) {
  RestTemplate restTemplate = new RestTemplate();
  return restTemplate.exchange(requestEntity, String.class);
 }


 private byte[] parseRequestBody(HttpServletRequest request) throws IOException {
  InputStream inputStream = request.getInputStream();
  return StreamUtils.copyToByteArray(inputStream);
 }

 private MultiValueMap<String, String> parseRequestHeader(HttpServletRequest request) {
  HttpHeaders headers = new HttpHeaders();
  List<String> headerNames = Collections.list(request.getHeaderNames());
  for (String headerName : headerNames) {
   List<String> headerValues = Collections.list(request.getHeaders(headerName));
   for (String headerValue : headerValues) {
    headers.add(headerName, headerValue);
   }
  }
  return headers;
 }
}

Spring 集成

Spring Controller,RequestMapping里把GET \ POST\PUT\DELETE 支持的請求帶上,就能實現(xiàn)轉發(fā)了。

@RestController
@RequestMapping(GraphDBController.DELEGATE_PREFIX)
@Api(value = "GraphDB", tags = {
  "graphdb-Api"
})
public class GraphDBController {

 @Autowired
 GraphProperties graphProperties;

 public final static String DELEGATE_PREFIX = "/graphdb";

 @Autowired
 private RoutingDelegate routingDelegate;

 @RequestMapping(value = "/**", method = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE}, produces = MediaType.TEXT_PLAIN_VALUE)
 public ResponseEntity catchAll(HttpServletRequest request, HttpServletResponse response) {
  return routingDelegate.redirect(request, response, graphProperties.getGraphServer(), DELEGATE_PREFIX);
 }
}

到此這篇關于spring boot實現(xiàn)超輕量級網(wǎng)關(反向代理、轉發(fā))的文章就介紹到這了,更多相關spring boot輕量級網(wǎng)關內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 設計模式之構建(Builder)模式 建造房子實例分析

    設計模式之構建(Builder)模式 建造房子實例分析

    構建模式主要用來針對復雜產品生產,分離部件構建細節(jié),以達到良好的伸縮性,考慮到設計模式來源于建筑學,因此舉一個建造房子的例子,需要的朋友可以參考下
    2012-12-12
  • 最新評論