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

        enabled: false // Permission settings
        # myProduct: / / this name can be filled in at will
        #     path: /myProduct/**
        #     serviceId: product
        #     sensitiveHeader: //Sensitive head filtration
        # Concise writing
        product: /myProduct/**
        - /**/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)

public class ZuulConfig{
    public ZuulProperties zuulProperties(){
        return new ZuulProperties();

Typical application scenarios


 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 {  
    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.set("isSuccess", true);// Set the value so that the next Filter can see the status of the previous Filter  
            return null;  
            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;  
    public boolean shouldFilter() {  
        return true;// Whether to execute the filter? This is true, indicating that filtering is needed.  
    public int filterOrder() {  
        return 0;// Priority is 0, the larger the number, the lower the priority  
    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