Spring Cloud大型企業(yè)分布式微服務(wù)云構(gòu)建的B2B2C電子商務(wù)平臺(tái)源碼請(qǐng)加企鵝求求: 三五三六二四七二五九
創(chuàng)新互聯(lián)是一家專業(yè)提供大同企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站建設(shè)、成都做網(wǎng)站、H5頁(yè)面制作、小程序制作等業(yè)務(wù)。10年已為大同眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。
POM依賴
org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud-starter-netflix-zuul
啟動(dòng)類加上: @EnableZuulProxy 注解
配置:
spring: application: name: zuul-gateway eureka: client: service-url: defaultZone: http://localhost:9001/eureka/ server: port: 9501 zuul: routes: #把order-server服務(wù)映射到路徑/my-order/** order-test-route: serviceId: order-server path: /my-order/** #過濾指定的url,即不路由指定的url ignored-patterns: - /*/order/list # 當(dāng)hystrix的值小的時(shí)候,hystrix生效,當(dāng)ribbon.ReadTimeout小的時(shí)候,ribbon生效 # 注意:只有zuul路由是url是才生效 # 如果使用的是serviceId 需要使用 ribbon.ReadTimeout 和 ribbon.SocketTimeout) hystrix: command: default: execution: timeout: enabled: true isolation: #命令的執(zhí)行超時(shí)時(shí)間 超時(shí)將執(zhí)行回退 thread: timeoutInMilliseconds: 8000 ribbon: ReadTimeout: 10000 #讀取超時(shí)時(shí)間 ConnectTimeout: 10000
ignored-patterns 為忽略指定的API
自定義過濾器需要實(shí)現(xiàn)ZuulFilter接口:
/** * 用戶訪問時(shí)觸發(fā) * * @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時(shí)執(zhí)行過濾器,false則不執(zhí)行當(dāng)前過濾器 * * @return */ @Override public boolean shouldFilter() { return true; } /** * 用戶訪問時(shí)觸發(fā)該過濾器 * * @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; } }
/** * 用戶訪問完要返回的時(shí)候觸發(fā) * @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; } }