spring boot實現超輕量級網關(反向代理、轉發)的示例
作者: JadePeng
出處:https://www.cnblogs.com/xiaoqi/p/spring-boot-route.html
在我們的rest服務中,需要暴露一個中間件的接口給用戶,但是需要經過rest服務的認證,這是典型的網關使用場景。可以引入網關組件來搞定,但是引入zuul等中間件會增加系統復雜性,這里實現一個超輕量級的網關,只實現請求轉發,認證等由rest服務的spring security來搞定。
如何進行請求轉發呢? 熟悉網絡請求的同學應該很清楚,請求無非就是請求方式、HTTP header,以及請求body,我們將這些信息取出來,透傳給轉發的url即可。
舉例:
/graphdb/** 轉發到 Graph_Server/**
獲取轉發目的地址:
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,后續可以用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; }
透明轉發
最后用RestTemplate來實現請求:
private ResponseEntity<String> route(RequestEntity requestEntity) { RestTemplate restTemplate = new RestTemplate(); return restTemplate.exchange(requestEntity, String.class); }
全部代碼
以下是輕量級轉發全部代碼:
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;@Servicepublic 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 POSTPUTDELETE 支持的請求帶上,就能實現轉發了。
@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實現超輕量級網關(反向代理、轉發)的示例的詳細內容,更多關于spring boot實現網關的資料請關注好吧啦網其它相關文章!
相關文章:
