您好,登錄后才能下訂單哦!
Spring Cloud大型企業(yè)分布式微服務(wù)云構(gòu)建的B2B2C電子商務(wù)平臺(tái)源碼請(qǐng)加企鵝求求: 三五三六二四七二五九
POM依賴(lài)
<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>
啟動(dòng)類(lèi)加上: @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/** #過(guò)濾指定的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
自定義過(guò)濾器需要實(shí)現(xiàn)ZuulFilter接口:
/** * 用戶(hù)訪問(wèn)時(shí)觸發(fā) * * @author 阮勝 * @date 2018/7/19 21:48 */ @Component public class TokenFilter extends ZuulFilter { private static final String TOKEN = "token"; /** * 過(guò)濾器類(lèi)型 * * @return */ @Override public String filterType() { return FilterConstants.PRE_TYPE; } /** * 過(guò)濾器在過(guò)濾器鏈中的位置 * * @return */ @Override public int filterOrder() { return FilterConstants.PRE_DECORATION_FILTER_ORDER - 1; } /** * 返回值為true時(shí)執(zhí)行過(guò)濾器,false則不執(zhí)行當(dāng)前過(guò)濾器 * * @return */ @Override public boolean shouldFilter() { return true; } /** * 用戶(hù)訪問(wèn)時(shí)觸發(fā)該過(guò)濾器 * * @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; } }
/** * 用戶(hù)訪問(wèn)完要返回的時(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; } }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。