本身動手寫一個服務網關-java

本身動手寫一個服務網關

原文連接:https://www.cnblogs.com/bigben0123/p/9252444.htmlcss

引言

什麼是網關?爲何須要使用網關?


如圖所示,在不使用網關的狀況下,咱們的服務是直接暴露給服務調用方。當調用方增多,勢必須要添加定製化訪問權限、校驗等邏輯。當添加API網關後,再第三方調用端和服務提供方之間就建立了一面牆,這面牆直接與調用方通訊進行權限控制。
本文所實現的網關源碼抄襲了---Oh,不對,是借鑑。借鑑了Zuul網關的源碼,提煉出其核心思路,實現了一套簡單的網關源碼,博主將其更名爲Eatuul。html

題外話

本文是業內能搜到的第一篇本身動手實現網關的文章。博主寫的手把手系列的文章,目的是在以最簡單的方式,揭露出中間件的核心原理,讓讀者可以迅速瞭解實現的核心。須要說明的是,這不是源碼分析系列的文章,所以寫出來的代碼,省去了一些複雜的內容,畢竟你們能理解到該中間件的核心原理便可。若是想看源碼分析系列的,請關注博主,後期會將spring、spring boot、dubbo、mybatis等開源框架一一揭示。java

正文

設計思路

先大體說一下,就是定義一個Servlet接收請求。而後通過preFilter(封裝請求參數),routeFilter(轉發請求),postFilter(輸出內容)。三個過濾器之間,共享request、response以及其餘的一些全局變量。以下圖所示

和真正的Zuul的區別?
主要區別有以下幾點
(1)Zuul中在異常處理模塊,有一個ErrorFilter來處理,博主在實現的時候偷懶了,略去。
(2)Zuul中PreFilters,RoutingFilters,PostFilters默認都實現了一組,具體以下表所示:web




博主總不可能每個都給大家實現一遍吧。因此偷懶了,每種只實現一個。可是調用順序仍是不變,按照PreFilters->RoutingFilters->PostFilters的順序調用。spring


(3)在routeFilters確實有轉發請求的Filter,然而博主偷天換日了,改用RestTemplate實現。springboot

 

代碼結構

你們去spring官網上搭建一套springboot的項目,博主就不展現pom的代碼了。直接將項目結構展現一下,以下圖所示

EatuulServlet.java
這個是網關的入口,邏輯也十分簡單,分爲三步
(1)將request,response放入threadlocal中
(2)執行三組過濾器
(3)清除threadlocal中的的環境變量
源碼以下mybatis

package com.rjzheng.eatuul.http;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "eatuul", urlPatterns = "/*")
public class EatuulServlet extends HttpServlet {    
   private EatRunner eatRunner = new EatRunner();    
   @Override   public void service(HttpServletRequest req, HttpServletResponse resp)            throws ServletException, IOException {        
       //將request,和response放入上下文對象中        eatRunner.init(req, resp);        
       try {            //執行前置過濾           eatRunner.preRoute();            //執行過濾           eatRunner.route();            //執行後置過濾           eatRunner.postRoute();        } catch (Throwable e) {                    
       RequestContext.getCurrentContext().getResponse() .sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage());        } finally {            
       //清除變量            RequestContext.getCurrentContext().unset();        }    } }

EatuulRunner.java
這個是具體的執行器。須要說明一下,在Zuul中,ZuulRunner在獲取具體有哪些過濾器的時候,有一個FileLoader能夠動態讀取配置加載。博主在實現咱們本身的EatuulRunner時候,略去動態讀取的過程,直接靜態寫死。
源碼以下app

package com.rjzheng.eatuul.http;
import java.util.ArrayList;import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.rjzheng.eatuul.filter.EatuulFilter;
import com.rjzheng.eatuul.filter.post.SendResponseFilter;
import com.rjzheng.eatuul.filter.pre.RequestWrapperFilter;
import com.rjzheng.eatuul.filter.route.RoutingFilter;

public class EatRunner {    
   //靜態寫死過濾器    private ConcurrentHashMap<String, List<EatuulFilter>> hashFiltersByType = new ConcurrentHashMap<String, List<EatuulFilter>>(){{          put("pre",new ArrayList<EatuulFilter>(){{            add(new RequestWrapperFilter());        }});        put("route",new ArrayList<EatuulFilter>(){{            add(new RoutingFilter());        }});        put("post",new ArrayList<EatuulFilter>(){{            add(new SendResponseFilter());        }});     }};    
   public void init(HttpServletRequest req, HttpServletResponse resp) {        RequestContext ctx = RequestContext.getCurrentContext();        ctx.setRequest(req);        ctx.setResponse(resp);    }    public void preRoute() throws Throwable {        runFilters("pre");      }    public void route() throws Throwable{        runFilters("route");        }    public void postRoute() throws Throwable{        runFilters("post");    }    
        public void runFilters(String sType) throws Throwable {            List<EatuulFilter> list = this.hashFiltersByType.get(sType);            if (list != null) {                
for (int i = 0; i < list.size(); i++) {                    EatuulFilter zuulFilter = list.get(i);                    zuulFilter.run();                }            }     } }

EatuulFilter.java
接下來就是一系列Filter的代碼了,先上父類EatuulFilter的源碼框架

package com.rjzheng.eatuul.filter;
public abstract class EatuulFilter {    
   abstract public String filterType();  
    abstract public int filterOrder();  
    abstract public void run(); }

RequestWrapperFilter.java
這個是PreFilter,前置執行過濾器,負責封裝請求。步驟以下所示
(1)封裝請求頭
(2)封裝請求體
(3)構造出RestTemplate能識別的RequestEntity
(4)將RequestEntity放入全局threadlocal之中ide


代碼以下所示

package com.rjzheng.eatuul.filter.pre;
import java.io.IOException;import java.io.InputStream;
import java.net.URI;import java.net.URISyntaxException;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StreamUtils;
import com.rjzheng.eatuul.filter.EatuulFilter;
import com.rjzheng.eatuul.http.RequestContext;

public class RequestWrapperFilter extends EatuulFilter{    
   @Override    public String filterType() {        
     // TODO Auto-generated method stub        return "pre";    }    
   @Override    public int filterOrder() {        
     // TODO Auto-generated method stub        return -1;    }    
   @Override    public void run() {        String rootURL = "http://localhost:9090";        RequestContext ctx =RequestContext.getCurrentContext();        HttpServletRequest servletRequest = ctx.getRequest();        String targetURL = rootURL + servletRequest.getRequestURI();        RequestEntity<byte[]> requestEntity = null;        try {            requestEntity = createRequestEntity(servletRequest, targetURL);        } catch (Exception e) {            e.printStackTrace();        }        //四、將requestEntity放入全局threadlocal之中        ctx.setRequestEntity(requestEntity);    }    
       private RequestEntity createRequestEntity(HttpServletRequest request,String url) throws URISyntaxException, IOException {        String method = request.getMethod();        HttpMethod httpMethod = HttpMethod.resolve(method);        //一、封裝請求頭        MultiValueMap<String, String> headers =createRequestHeaders(request);        //二、封裝請求體        byte[] body = createRequestBody(request);        //三、構造出RestTemplate能識別的RequestEntity        RequestEntity requestEntity = new RequestEntity<byte[]>(body,headers,httpMethod, new URI(url));        return requestEntity;    }        private byte[] createRequestBody(HttpServletRequest request) throws IOException {        InputStream inputStream = request.getInputStream();        return StreamUtils.copyToByteArray(inputStream);    }    
       private MultiValueMap<String, String> createRequestHeaders(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;    } }

RoutingFilter.java
這個是routeFilter,這裏我偷懶了,直接作轉發請求,而且將返回值ResponseEntity放入全局threadlocal中

package com.rjzheng.eatuul.filter.route;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import com.rjzheng.eatuul.filter.EatuulFilter;
import com.rjzheng.eatuul.http.RequestContext; public class RoutingFilter extends EatuulFilter{    @Override    public String filterType() {        
// TODO Auto-generated method stub        return "route";    }    @Override    public int filterOrder() {        
// TODO Auto-generated method stub        return 0;    }        @Override    public void run(){        
       RequestContext ctx = RequestContext.getCurrentContext();
       RequestEntity requestEntity = ctx.getRequestEntity();        
       RestTemplate restTemplate = new RestTemplate();
       ResponseEntity responseEntity = restTemplate.exchange(requestEntity,byte[].class);        ctx.setResponseEntity(responseEntity);    }     }

SendResponseFilter.java
這個是postFilters,將ResponseEntity輸出便可

package com.rjzheng.eatuul.filter.post;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import com.rjzheng.eatuul.filter.EatuulFilter;
import com.rjzheng.eatuul.http.RequestContext;

public class SendResponseFilter extends EatuulFilter{    
    @Override    public String filterType() {        
         return "post";    }    
   @Override    public int filterOrder() {      
         return 1000;    }  
   @Override    public void run() {      
     try {          addResponseHeaders();          writeResponse();        } catch (Exception e) {            e.printStackTrace();        }    }    
   private void addResponseHeaders() {        RequestContext ctx = RequestContext.getCurrentContext();        HttpServletResponse servletResponse = ctx.getResponse();        ResponseEntity responseEntity = ctx.getResponseEntity();        HttpHeaders httpHeaders = responseEntity.getHeaders();
      for(Map.Entry<String, List<String>> entry:httpHeaders.entrySet()){            String headerName = entry.getKey();            List<String> headerValues = entry.getValue();                       for(String headerValue:headerValues) {                servletResponse.addHeader(headerName, headerValue);              }        }    }  
   
   private void writeResponse()throws Exception {        RequestContext ctx = RequestContext.getCurrentContext();        HttpServletResponse servletResponse = ctx.getResponse();      
       if (servletResponse.getCharacterEncoding() == null) {
       // only set if not set            servletResponse.setCharacterEncoding("UTF-8");        }        ResponseEntity responseEntity = ctx.getResponseEntity();  
       if(responseEntity.hasBody()) {          
            byte[] body = (byte[]) responseEntity.getBody();             ServletOutputStream outputStream =       servletResponse.getOutputStream();            outputStream.write(body);            outputStream.flush();        }    } }

RequestContext.java
最後是一直在說的全局threadlocal變量

package com.rjzheng.eatuul.http;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;

public class RequestContext extends ConcurrentHashMap<String, Object> {        protected static Class<? extends RequestContext> contextClass = RequestContext.class;    
   protected static final ThreadLocal<? extends RequestContext> threadLocal = new ThreadLocal<RequestContext>() {        
   @Override    protected RequestContext initialValue() {            
       try {                
           return contextClass.newInstance();
        } catch (Throwable e) {              
             throw new RuntimeException(e);         }       }    };    
             
   public static RequestContext getCurrentContext() {       RequestContext context = threadLocal.get();      
      return context;    }    
      
   public HttpServletRequest getRequest() {        
      return (HttpServletRequest) get("request");    }    
      
   public void setRequest(HttpServletRequest request) {        put("request", request);    }    
   
   public HttpServletResponse getResponse() {        
       return (HttpServletResponse) get("response");    }    
   
   public void setResponse(HttpServletResponse response) {        set("response", response);    }  
        public void setRequestEntity(RequestEntity requestEntity){        set("requestEntity",requestEntity);    }  
             public RequestEntity getRequestEntity() {        
       return (RequestEntity) get("requestEntity");    }    
           public void setResponseEntity(ResponseEntity responseEntity){        set("responseEntity",responseEntity);    }    
               public ResponseEntity getResponseEntity() {        
       return (ResponseEntity) get("responseEntity");    }    
           public void set(String key, Object value) {        
       if (value != null)           put(key, value);        
       else           remove(key);    }    
       
   public void unset() {        threadLocal.remove();    } }

如何測試?

本身另外起一個server端口爲9090以下所示

package com.rjzheng.eatservice;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import com.rjzheng.eatservice.controller.IndexController;
@SpringBootApplication@ServletComponentScan(basePackageClasses = IndexController.class)

public class Application {    public static void main(String[] args) {      
     new SpringApplicationBuilder(Application.class).
         properties("server.port=9090").run(args);    } }

再來一個controller

package com.rjzheng.eatservice.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class IndexController {   @RequestMapping("/index")   public String index() {      
    return "hello!world";   } }

而後,你就發現能夠從localhost:8080/index進行跳轉訪問了。

引言

什麼是網關?爲何須要使用網關?


如圖所示,在不使用網關的狀況下,咱們的服務是直接暴露給服務調用方。當調用方增多,勢必須要添加定製化訪問權限、校驗等邏輯。當添加API網關後,再第三方調用端和服務提供方之間就建立了一面牆,這面牆直接與調用方通訊進行權限控制。
本文所實現的網關源碼抄襲了---Oh,不對,是借鑑。借鑑了Zuul網關的源碼,提煉出其核心思路,實現了一套簡單的網關源碼,博主將其更名爲Eatuul。

題外話

本文是業內能搜到的第一篇本身動手實現網關的文章。博主寫的手把手系列的文章,目的是在以最簡單的方式,揭露出中間件的核心原理,讓讀者可以迅速瞭解實現的核心。須要說明的是,這不是源碼分析系列的文章,所以寫出來的代碼,省去了一些複雜的內容,畢竟你們能理解到該中間件的核心原理便可。若是想看源碼分析系列的,請關注博主,後期會將spring、spring boot、dubbo、mybatis等開源框架一一揭示。

正文

設計思路

先大體說一下,就是定義一個Servlet接收請求。而後通過preFilter(封裝請求參數),routeFilter(轉發請求),postFilter(輸出內容)。三個過濾器之間,共享request、response以及其餘的一些全局變量。以下圖所示

和真正的Zuul的區別?
主要區別有以下幾點
(1)Zuul中在異常處理模塊,有一個ErrorFilter來處理,博主在實現的時候偷懶了,略去。
(2)Zuul中PreFilters,RoutingFilters,PostFilters默認都實現了一組,具體以下表所示:




博主總不可能每個都給大家實現一遍吧。因此偷懶了,每種只實現一個。可是調用順序仍是不變,按照PreFilters->RoutingFilters->PostFilters的順序調用。


(3)在routeFilters確實有轉發請求的Filter,然而博主偷天換日了,改用RestTemplate實現。

 

代碼結構

你們去spring官網上搭建一套springboot的項目,博主就不展現pom的代碼了。直接將項目結構展現一下,以下圖所示

EatuulServlet.java
這個是網關的入口,邏輯也十分簡單,分爲三步
(1)將request,response放入threadlocal中
(2)執行三組過濾器
(3)清除threadlocal中的的環境變量
源碼以下

package com.rjzheng.eatuul.http;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "eatuul", urlPatterns = "/*")
public class EatuulServlet extends HttpServlet {    
   private EatRunner eatRunner = new EatRunner();    
   @Override   public void service(HttpServletRequest req, HttpServletResponse resp)            throws ServletException, IOException {        
       //將request,和response放入上下文對象中        eatRunner.init(req, resp);        
       try {            //執行前置過濾           eatRunner.preRoute();            //執行過濾           eatRunner.route();            //執行後置過濾           eatRunner.postRoute();        } catch (Throwable e) {                    
       RequestContext.getCurrentContext().getResponse() .sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage());        } finally {            
       //清除變量            RequestContext.getCurrentContext().unset();        }    } }

EatuulRunner.java
這個是具體的執行器。須要說明一下,在Zuul中,ZuulRunner在獲取具體有哪些過濾器的時候,有一個FileLoader能夠動態讀取配置加載。博主在實現咱們本身的EatuulRunner時候,略去動態讀取的過程,直接靜態寫死。
源碼以下

package com.rjzheng.eatuul.http;
import java.util.ArrayList;import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.rjzheng.eatuul.filter.EatuulFilter;
import com.rjzheng.eatuul.filter.post.SendResponseFilter;
import com.rjzheng.eatuul.filter.pre.RequestWrapperFilter;
import com.rjzheng.eatuul.filter.route.RoutingFilter;

public class EatRunner {    
   //靜態寫死過濾器    private ConcurrentHashMap<String, List<EatuulFilter>> hashFiltersByType = new ConcurrentHashMap<String, List<EatuulFilter>>(){{          put("pre",new ArrayList<EatuulFilter>(){{            add(new RequestWrapperFilter());        }});        put("route",new ArrayList<EatuulFilter>(){{            add(new RoutingFilter());        }});        put("post",new ArrayList<EatuulFilter>(){{            add(new SendResponseFilter());        }});     }};    
   public void init(HttpServletRequest req, HttpServletResponse resp) {        RequestContext ctx = RequestContext.getCurrentContext();        ctx.setRequest(req);        ctx.setResponse(resp);    }    public void preRoute() throws Throwable {        runFilters("pre");      }    public void route() throws Throwable{        runFilters("route");        }    public void postRoute() throws Throwable{        runFilters("post");    }    
        public void runFilters(String sType) throws Throwable {            List<EatuulFilter> list = this.hashFiltersByType.get(sType);            if (list != null) {                
for (int i = 0; i < list.size(); i++) {                    EatuulFilter zuulFilter = list.get(i);                    zuulFilter.run();                }            }     } }

EatuulFilter.java
接下來就是一系列Filter的代碼了,先上父類EatuulFilter的源碼

package com.rjzheng.eatuul.filter;
public abstract class EatuulFilter {    
   abstract public String filterType();  
    abstract public int filterOrder();  
    abstract public void run(); }

RequestWrapperFilter.java
這個是PreFilter,前置執行過濾器,負責封裝請求。步驟以下所示
(1)封裝請求頭
(2)封裝請求體
(3)構造出RestTemplate能識別的RequestEntity
(4)將RequestEntity放入全局threadlocal之中


代碼以下所示

package com.rjzheng.eatuul.filter.pre;
import java.io.IOException;import java.io.InputStream;
import java.net.URI;import java.net.URISyntaxException;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StreamUtils;
import com.rjzheng.eatuul.filter.EatuulFilter;
import com.rjzheng.eatuul.http.RequestContext;

public class RequestWrapperFilter extends EatuulFilter{    
   @Override    public String filterType() {        
     // TODO Auto-generated method stub        return "pre";    }    
   @Override    public int filterOrder() {        
     // TODO Auto-generated method stub        return -1;    }    
   @Override    public void run() {        String rootURL = "http://localhost:9090";        RequestContext ctx =RequestContext.getCurrentContext();        HttpServletRequest servletRequest = ctx.getRequest();        String targetURL = rootURL + servletRequest.getRequestURI();        RequestEntity<byte[]> requestEntity = null;        try {            requestEntity = createRequestEntity(servletRequest, targetURL);        } catch (Exception e) {            e.printStackTrace();        }        //四、將requestEntity放入全局threadlocal之中        ctx.setRequestEntity(requestEntity);    }    
       private RequestEntity createRequestEntity(HttpServletRequest request,String url) throws URISyntaxException, IOException {        String method = request.getMethod();        HttpMethod httpMethod = HttpMethod.resolve(method);        //一、封裝請求頭        MultiValueMap<String, String> headers =createRequestHeaders(request);        //二、封裝請求體        byte[] body = createRequestBody(request);        //三、構造出RestTemplate能識別的RequestEntity        RequestEntity requestEntity = new RequestEntity<byte[]>(body,headers,httpMethod, new URI(url));        return requestEntity;    }        private byte[] createRequestBody(HttpServletRequest request) throws IOException {        InputStream inputStream = request.getInputStream();        return StreamUtils.copyToByteArray(inputStream);    }    
       private MultiValueMap<String, String> createRequestHeaders(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;    } }

RoutingFilter.java
這個是routeFilter,這裏我偷懶了,直接作轉發請求,而且將返回值ResponseEntity放入全局threadlocal中

package com.rjzheng.eatuul.filter.route;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import com.rjzheng.eatuul.filter.EatuulFilter;
import com.rjzheng.eatuul.http.RequestContext; public class RoutingFilter extends EatuulFilter{    @Override    public String filterType() {        
// TODO Auto-generated method stub        return "route";    }    @Override    public int filterOrder() {        
// TODO Auto-generated method stub        return 0;    }        @Override    public void run(){        
       RequestContext ctx = RequestContext.getCurrentContext();
       RequestEntity requestEntity = ctx.getRequestEntity();        
       RestTemplate restTemplate = new RestTemplate();
       ResponseEntity responseEntity = restTemplate.exchange(requestEntity,byte[].class);        ctx.setResponseEntity(responseEntity);    }     }

SendResponseFilter.java
這個是postFilters,將ResponseEntity輸出便可

package com.rjzheng.eatuul.filter.post;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import com.rjzheng.eatuul.filter.EatuulFilter;
import com.rjzheng.eatuul.http.RequestContext;

public class SendResponseFilter extends EatuulFilter{    
    @Override    public String filterType() {        
         return "post";    }    
   @Override    public int filterOrder() {      
         return 1000;    }  
   @Override    public void run() {      
     try {          addResponseHeaders();          writeResponse();        } catch (Exception e) {            e.printStackTrace();        }    }    
   private void addResponseHeaders() {        RequestContext ctx = RequestContext.getCurrentContext();        HttpServletResponse servletResponse = ctx.getResponse();        ResponseEntity responseEntity = ctx.getResponseEntity();        HttpHeaders httpHeaders = responseEntity.getHeaders();
      for(Map.Entry<String, List<String>> entry:httpHeaders.entrySet()){            String headerName = entry.getKey();            List<String> headerValues = entry.getValue();                       for(String headerValue:headerValues) {                servletResponse.addHeader(headerName, headerValue);              }        }    }  
   
   private void writeResponse()throws Exception {        RequestContext ctx = RequestContext.getCurrentContext();        HttpServletResponse servletResponse = ctx.getResponse();      
       if (servletResponse.getCharacterEncoding() == null) {
       // only set if not set            servletResponse.setCharacterEncoding("UTF-8");        }        ResponseEntity responseEntity = ctx.getResponseEntity();  
       if(responseEntity.hasBody()) {          
            byte[] body = (byte[]) responseEntity.getBody();             ServletOutputStream outputStream =       servletResponse.getOutputStream();            outputStream.write(body);            outputStream.flush();        }    } }

RequestContext.java
最後是一直在說的全局threadlocal變量

package com.rjzheng.eatuul.http;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;

public class RequestContext extends ConcurrentHashMap<String, Object> {        protected static Class<? extends RequestContext> contextClass = RequestContext.class;    
   protected static final ThreadLocal<? extends RequestContext> threadLocal = new ThreadLocal<RequestContext>() {        
   @Override    protected RequestContext initialValue() {            
       try {                
           return contextClass.newInstance();
        } catch (Throwable e) {              
             throw new RuntimeException(e);         }       }    };    
             
   public static RequestContext getCurrentContext() {       RequestContext context = threadLocal.get();      
      return context;    }    
      
   public HttpServletRequest getRequest() {        
      return (HttpServletRequest) get("request");    }    
      
   public void setRequest(HttpServletRequest request) {        put("request", request);    }    
   
   public HttpServletResponse getResponse() {        
       return (HttpServletResponse) get("response");    }    
   
   public void setResponse(HttpServletResponse response) {        set("response", response);    }  
        public void setRequestEntity(RequestEntity requestEntity){        set("requestEntity",requestEntity);    }  
             public RequestEntity getRequestEntity() {        
       return (RequestEntity) get("requestEntity");    }    
           public void setResponseEntity(ResponseEntity responseEntity){        set("responseEntity",responseEntity);    }    
               public ResponseEntity getResponseEntity() {        
       return (ResponseEntity) get("responseEntity");    }    
           public void set(String key, Object value) {        
       if (value != null)           put(key, value);        
       else           remove(key);    }    
       
   public void unset() {        threadLocal.remove();    } }

如何測試?

本身另外起一個server端口爲9090以下所示

package com.rjzheng.eatservice;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import com.rjzheng.eatservice.controller.IndexController;
@SpringBootApplication@ServletComponentScan(basePackageClasses = IndexController.class)

public class Application {    public static void main(String[] args) {      
     new SpringApplicationBuilder(Application.class).
         properties("server.port=9090").run(args);    } }

再來一個controller

package com.rjzheng.eatservice.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class IndexController {   @RequestMapping("/index")   public String index() {      
    return "hello!world";   } }

而後,你就發現能夠從localhost:8080/index進行跳轉訪問了。

相關文章
相關標籤/搜索