首页 > 其他 > 详细

微服务网关---调用其他微服务

时间:2019-08-26 14:06:41      阅读:96      评论:0      收藏:0      [点我收藏+]
 


首先写一个过滤器继承ZuulFilter ,然后重写其中的run()方法, 1、获取request对象的信息:
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
String servletPath = request.getServletPath();
拿到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、遍历routes路由信息   路由的配置信息如下:

@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;
	}
}
配置文件的信息如下:

#自定义过滤器 路由,参考:RestFaceFilter.java
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;
                }
            }
        }

  

 

微服务网关---调用其他微服务

原文:https://www.cnblogs.com/otways/p/11411598.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!