Micro-micro-service gateway --- call other services

 


First, a write filter inherit ZuulFilter, and wherein the rewriting run () method, an access to information request object:
CTX = RequestContext.getCurrentContext the RequestContext (); 
the HttpServletRequest request ctx.getRequest = (); 
String servletPath request.getServletPath = (); 
information can be obtained after the get request object request:
String accessSys = request.getParameter("accessSys");
String paramData = getRequestBody(request);

private String getRequestBody(HttpServletRequest request) {
try {
InputStream v_inputstream = request.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int x = 0;
while ((x = v_inputstream.read()) != -1) {
baos.write(x);
}
baos.flush();
return new String(baos.toByteArray(), UTF_8);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return "";
}

 2, the routing information in the routing routes traversing configuration information is as follows:

@ConfigurationProperties(prefix="application.myroutes")
public class MyRoutesConfig {

	
	private Map<String, String> routes;

	public Map<String, String> getRoutes() {
		return routes;
	}

	public void setRoutes(Map<String, String> routes) {
		this.routes = routes;
	}
}
Configuration files are as follows: 

# custom filter routes Reference: RestFaceFilter.java
file application:
myroutes:
routes: {
-Service: 'ePassport-Platform',
ProxyService: 'ePassport-exservice'
}
 if (routes != null) {
            Set<String> keySet = routes.getRoutes().keySet();
            for (String str : keySet) {
//                logger.info("get route key: {}", str);
                if (servletPath.startsWith("/" + str + "/")) {
                    restface_forward = routes.getRoutes().get(str);
                    break;
                }
            }
        }

  

 

Guess you like

Origin www.cnblogs.com/otways/p/11411598.html