WIKI: 傳送門
站在用戶的角度思考問(wèn)題,與客戶深入溝通,找到中原網(wǎng)站設(shè)計(jì)與中原網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都做網(wǎng)站、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、空間域名、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋中原地區(qū)。
關(guān)鍵配置:
The configuration property
zuul.host.maxTotalConnections
andzuul.host.maxPerRouteConnections
, which default to 200 and 20 respectively.
mscx-ad-zuul
三步曲創(chuàng)建法:
org.springframework.cloud
spring-cloud-starter-netflix-zuul
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
@SpringCloudApplication
@EnableZuulProxy //啟用網(wǎng)關(guān)
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
spring:
application:
name: ad-gateway-zuul
server:
port: 1111
eureka:
client:
service-url:
defaultZone: http://server1:7777/eureka/,http://server2:8888/eureka/,http://server3:9999/eureka/
instance:
hostname: ad-gateway-zuul
zuul:
ignored-services: '*' # 過(guò)濾所有請(qǐng)求,除了下面routes中聲明過(guò)的服務(wù)
routes:
sponsor: #在路由中自定義服務(wù)路由名稱
path: /ad-sponsor/**
serviceId: mscx-ad-sponsor #微服務(wù)name
strip-prefix: false
search: #在路由中自定義服務(wù)路由名稱
path: /ad-search/**
serviceId: mscx-ad-search #微服務(wù)name
strip-prefix: false
prefix: /gateway/api
strip-prefix: false #不對(duì) prefix: /gateway/api 設(shè)置的路徑進(jìn)行截取,默認(rèn)轉(zhuǎn)發(fā)會(huì)截取掉配置的前綴
我們來(lái)編寫一個(gè)記錄請(qǐng)求時(shí)間周期的過(guò)濾器,根據(jù)Filter的三種類型:Pre filters
,routing filters
和Post filters
,我們需要定義2個(gè)filter,用來(lái)記錄開始和結(jié)束時(shí)間,很明顯,我們需要實(shí)現(xiàn)Pre
& Post
2個(gè)過(guò)濾器。
@Slf4j
@Component
public class PreRequestFilter extends ZuulFilter {
@Override
public String filterType() {
// pre filter
return FilterConstants.PRE_TYPE;
}
@Override
public int filterOrder() {
return 0;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() throws ZuulException {
//獲取當(dāng)前請(qǐng)求的請(qǐng)求上下文
RequestContext requestContext = RequestContext.getCurrentContext();
//記錄請(qǐng)求進(jìn)入時(shí)間
requestContext.set("api_request_time", System.currentTimeMillis());
return null;
}
}
---
@Slf4j
@Component
public class AccessLogFilter extends ZuulFilter {
@Override
public String filterType() {
return FilterConstants.POST_TYPE;
}
@Override
public int filterOrder() {
//需要最后一個(gè)執(zhí)行的filter
return FilterConstants.SEND_RESPONSE_FILTER_ORDER - 1;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() throws ZuulException {
RequestContext requestContext = RequestContext.getCurrentContext();
HttpServletRequest request = requestContext.getRequest();
log.info("Request \"{}\" spent : {} seconds.", request.getRequestURI(),
(System.currentTimeMillis() - Long.valueOf(requestContext.get("api_request_time").toString())) / 1000);
return null;
}
}
后續(xù)更新