Spring Cloud service gateway Zuul

Elements of service gateway

  • stability
  • Safety
  • Performance, concurrency
  • Expansibility
    Spring Cloud Zuul

     -Route + filter
     -The core is a series of filters
     

Zuul routing configuration

management:
    security:
        enabled: false // Permission settings
zuul: 
    routes:
        # myProduct: / / this name can be filled in at will
        #     path: /myProduct/**
        #     serviceId: product
        #     sensitiveHeader: //Sensitive head filtration
        # Concise writing
        product: /myProduct/**
    ignored-patterns: 
        - /**/product/listForOrder // No external access (- for set set set)
View all routing rules: localhost:port/application/routes
{
    /myProduct/**: "product",
    /config/**: "config",
    /product/**: "product",
}
   

Dynamic injection of Zuul configuration (can also be written to startup class)

@Compoent
public class ZuulConfig{
    @ConfigurationProperties("zuul")
    @RefreshScope
    public ZuulProperties zuulProperties(){
        return new ZuulProperties();
    }
}

Typical application scenarios

Prefilter

 Current limiting
 - Authentication
 -Parameter verification and adjustment

back filter

- Statistics
 - log

To implement Filter, you need the following steps:

1. Inherit ZuulFilter class. In order to verify the characteristics of Filter, we create three filters here.

Filter by user name

package com.chhliu.springcloud.zuul;  
  
import javax.servlet.http.HttpServletRequest;  
  
import com.netflix.zuul.ZuulFilter;  
import com.netflix.zuul.context.RequestContext;  
  
public class AccessUserNameFilter extends ZuulFilter {  
    @Override  
    public Object run() {  
        RequestContext ctx = RequestContext.getCurrentContext();  
        HttpServletRequest request = ctx.getRequest();  
  
        System.out.println(String.format("%s AccessUserNameFilter request to %s", request.getMethod(), request.getRequestURL().toString()));  
  
        String username = request.getParameter("username");// Get requested parameters  
        if(null != username && username.equals("chhliu")) {// If the requested parameter is not empty and the value is chhliu, the  
            ctx.setSendZuulResponse(true);// Route the request  
            ctx.setResponseStatusCode(200);  
            ctx.set("isSuccess", true);// Set the value so that the next Filter can see the status of the previous Filter  
            return null;  
        }else{  
            ctx.setSendZuulResponse(false);// Filter the request without routing it  
            ctx.setResponseStatusCode(401);// Return error code  
            ctx.setResponseBody("{\"result\":\"username is not correct!\"}");// Return error content  
            ctx.set("isSuccess", false);  
            return null;  
        }  
    }  
  
    @Override  
    public boolean shouldFilter() {  
        return true;// Whether to execute the filter? This is true, indicating that filtering is needed.  
    }  
  
    @Override  
    public int filterOrder() {  
        return 0;// Priority is 0, the larger the number, the lower the priority  
    }  
  
    @Override  
    public String filterType() {  
        return "pre";// Prefilter  
    }  
} 

Reference resources: https://www.cnblogs.com/a8457...

Keywords: Java

Added by guyfromfl on Sat, 02 Nov 2019 03:05:45 +0200