spring cloud微服务分布式云架构Spring Cloud Zuul
短信预约 -IT技能 免费直播动态提醒
Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码请加企鹅求求: 三五三六二四七二五九
POM依赖
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> </dependencies>
启动类加上: @EnableZuulProxy 注解
配置:
spring: application: name: zuul-gatewayeureka: client: service-url: defaultZone: http://localhost:9001/eureka/server: port: 9501zuul: routes: #把order-server服务映射到路径/my-orderorder/list # 当hystrix的值小的时候,hystrix生效,当ribbon.ReadTimeout小的时候,ribbon生效 # 注意:只有zuul路由是url是才生效# 如果使用的是serviceId 需要使用 ribbon.ReadTimeout 和 ribbon.SocketTimeout)hystrix: command: default: execution: timeout: enabled: true isolation: #命令的执行超时时间 超时将执行回退 thread: timeoutInMilliseconds: 8000ribbon: ReadTimeout: 10000 #读取超时时间 ConnectTimeout: 10000
ignored-patterns 为忽略指定的API
自定义过滤器需要实现ZuulFilter接口:
@Componentpublic class TokenFilter extends ZuulFilter { private static final String TOKEN = "token"; @Override public String filterType() { return FilterConstants.PRE_TYPE; } @Override public int filterOrder() { return FilterConstants.PRE_DECORATION_FILTER_ORDER - 1; } @Override public boolean shouldFilter() { return true; } @Override public Object run() throws ZuulException { RequestContext requestContext = RequestContext.getCurrentContext(); HttpServletRequest request = requestContext.getRequest(); String token = request.getParameter(TOKEN); if (StringUtils.isEmpty(token)) { requestContext.setSendZuulResponse(false); requestContext.setResponseStatusCode(HttpStatus.SC_UNAUTHORIZED); } return null; }}
@Componentpublic class PostFilter extends ZuulFilter { @Override public String filterType() { return FilterConstants.POST_TYPE; } @Override public int filterOrder() { return FilterConstants.SEND_RESPONSE_FILTER_ORDER - 1; } @Override public boolean shouldFilter() { return true; } @Override public Object run() throws ZuulException { RequestContext requestContext = RequestContext.getCurrentContext(); HttpServletResponse resp = requestContext.getResponse(); resp.setHeader("auth", "test auth"); return null; }}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341