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-gateway eureka: client: service-url: defaultZone: http://localhost:9001/eureka/ server: port: 9501 zuul: routes: #把order-server服务映射到路径/my-order/** order-test-route: serviceId: order-server path: /my-order/** #过滤指定的url,即不路由指定的url ignored-patterns: - /*/order/list # 当hystrix的值小的时候,hystrix生效,当ribbon.ReadTimeout小的时候,ribbon生效 # 注意:只有zuul路由是url是才生效 # 如果使用的是serviceId 需要使用 ribbon.ReadTimeout 和 ribbon.SocketTimeout) hystrix: command: default: execution: timeout: enabled: true isolation: #命令的执行超时时间 超时将执行回退 thread: timeoutInMilliseconds: 8000 ribbon: ReadTimeout: 10000 #读取超时时间 ConnectTimeout: 10000
ignored-patterns 为忽略指定的API
自定义过滤器需要实现ZuulFilter接口:
/** * 用户访问时触发 * * @author 阮胜 * @date 2018/7/19 21:48 */ @Component public class TokenFilter extends ZuulFilter { private static final String TOKEN = "token"; /** * 过滤器类型 * * @return */ @Override public String filterType() { return FilterConstants.PRE_TYPE; } /** * 过滤器在过滤器链中的位置 * * @return */ @Override public int filterOrder() { return FilterConstants.PRE_DECORATION_FILTER_ORDER - 1; } /** * 返回值为true时执行过滤器,false则不执行当前过滤器 * * @return */ @Override public boolean shouldFilter() { return true; } /** * 用户访问时触发该过滤器 * * @return * @throws ZuulException */ @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; } }
/** * 用户访问完要返回的时候触发 * @author 阮胜 * @date 2018/7/19 21:57 */ @Component public 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; } }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。