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; }}