您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。