Zuul(Router and Filter)
WIKI: Portal
Effect
- Authentication/Security
- Insights
- Stress Testing
- Canary Testing
- Dynamic Routing
- Service Migration
- Load Shedding
- Static Response handling
- Active/Active traffic management
Key configurations:
The configuration property zuul.host.maxTotalConnections and zuul.host.maxPerRouteConnections, which default to 200 and 20 respectively.
Create mscx-ad-zuul
The method of creating trilogy:
Add dependency
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies>
append notes to
@SpringCloudApplication @EnableZuulProxy //enable gateway public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } }
Modified configuration
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: '*' # Filter all requests except the services declared in routes below routes: sponsor: #Customize service routing name in routing path: /ad-sponsor/** serviceId: mscx-ad-sponsor #Microservice name strip-prefix: false search: #Customize service routing name in routing path: /ad-search/** serviceId: mscx-ad-search #Microservice name strip-prefix: false prefix: /gateway/api strip-prefix: false #Instead of intercepting the path set by prefix: /gateway/api, default forwarding intercepts the prefix of the configuration
Filter compilation
Let's write a filter to record the request time period. According to the three types of filters: Pre filters,routing filters and Post filters, we need to define two filters to record the start and end times. Obviously, we need to implement Pre & Post two filters.
@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 { //Get the request context of the current request RequestContext requestContext = RequestContext.getCurrentContext(); //Record request entry time 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() { //filter that needs the last execution 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; } }
Gateway
Follow-up updates
Be a good person.