Zuul basic understanding and use

Why Micro Services Gateway

Different micro-services in general have different network addresses, and external clients may need to call multiple services interfaces only
to complete a business needs. For example, a movie ticket collection APP, micro film classification may call back services, users
micro-service, micro-payment services. If the client and micro service to communicate directly, there will be questions about:
# client repeatedly requested different micro-services, increase the complexity of the client
# cross-domain requests, processing under certain scenarios relative complexity
# authentication complex, each services require independent certification
# is difficult to reconstruct, with the iteration of the project may need to be re-divided micro service, if the client services directly and through micro
letter, it will be difficult to implement reconstruction
# certain micro services may use other protocols, direct it will be difficult to access
the above-mentioned problems, it can be solved by means of micro-services gateway. Micro service gateway is interposed between the client and the server-side
interlayer, all external requests will go through the micro serving gateway.

What is Zuul

Zuul is open source Netflix micro-services gateway, and he could Eureka, Ribbon, Hystrix and other components with the
use. Zuul core component is a series of filters, these filters can perform the following functions:
verification requirements to identify each resource, and reject those that do not match the request: # identity authentication and security
# review and monitoring:
## Dynamic Routing: Dynamic route requests to different backend cluster
# stress test: increasing traffic to the cluster, in order to understand the performance
# load distribution: corresponding to the load capacity for each type of distribution, and discards the request exceeds the limit value
# static response process: edge position to respond, to avoid forwarded to the internal cluster
# multi-regional flexibility: Cross-domain routing request AWS region designed to achieve ELB (ElasticLoad Balancing) the
use diversification
of Zuul was Spring Cloud integration and enhancement.
After using Zuul, architecture diagram the evolution of the form

1.1 micro-management background service gateway

1. dependence

 <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>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
    </dependencies>

2. Start class

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class ManagerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ManagerApplication.class);
    }

   
}

  

3. Profiles

 

 

4. Filter

@Component 
public class ManagerFilter the extends ZuulFilter { 

    @Autowired 
    Private JwtUtil jwtUtil; 
    / ** 
     * After pre or post request before execution 
     * @return 
     * / 
    @Override 
    public String filterType () { 
        return "pre"; 
    } 

    / ** 
     * Multi execution order filters, the smaller the number, the more the first implementation 
     * @return 
     * / 
    @Override 
    public int filterOrder () { 
        return 0; 
    } 

    / ** 
     * current filter is turned true to turn 
     * @return 
     * / 
    @ override 
    public Boolean shouldFilter () { 
        return to true; 
    }

    / ** 
     * return any value ojbect operations performed within the filter expressed proceed 
     * setsendzullRespponse (false) that is no longer continue 
     * @return 
     * @throws ZuulException 
     * / 
    @Override 
    public RUN Object () {ZuulException throws 
        the System. out.println ( "back through the filter!"); 
        the requestContext requestContext RequestContext.getCurrentContext = (); 
        // Request field 
        the HttpServletRequest requestContext.getRequest Request = (); 

        . IF (request.getMethod () the equals ( "the OPTIONS") ) { 
            return null; 
        } 

        IF (Request.getRequestURI () the indexOf ( "Login")> 0) {. 
            return null; 
        } 

        // get header
        String header = request.getHeader("Authorization");
        if(header!=null && !"".equals(header)){
            if(header.startsWith("Bearer ")){
                String token = header.substring(7);
                try {
                    Claims claims = jwtUtil.parseJWT(token);
                    String roles = (String) claims.get("roles");
                    if(roles.equals("admin")){
                        //把头信息转发下去,并且放行
                        requestContext.addZuulRequestHeader("Authorization", header);
                        return null;
                    }
                }catch (Exception e){
                    e.printStackTrace();
                    requestContext.setSendZuulResponse (false); // terminate 
                } 
            } 
        } 
        requestContext.setSendZuulResponse (false); // terminate 
        requestContext.setResponseStatusCode (403); 
        requestContext.setResponseBody ( "lack of authority"); 
        . requestContext.getResponse () setContentType ( "text / HTML; charset = UTF-. 8"); 
        return null; 
    } 
}

 

filterType: Returns a string representative of the type of filter, defined over the life cycle of four different zuul in the
filter type, as follows:
pre: may be invoked before the route request
route: when the route request is called
post: in is called after the route and the error filter
error: is called when an error occurred while processing the request
filterOrder: to define a filter by an int execution order
shouldFilter: returns a boolean to determine whether the filter is to be performed, so that by this function
filter switching achieved. In the above example, we directly returns true, so the filter is always active
run: the specified logical filter.

2.1 micro-management background service gateway

1. dependence

<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>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
    </dependencies>

  

 

2. Start class

 

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class WebApplication {
    public static void main(String[] args) {
        SpringApplication.run(WebApplication.class);
    }
}

  

3. Profiles

 

4. Filter

@Component
public class WebFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        //得到request上下文
        RequestContext currentContext = RequestContext.getCurrentContext();
        //得到request域
        HttpServletRequest request = currentContext.getRequest();
        //得到头信息
        Request.getHeader header = String ( "the Authorization");
        // determines whether header information 
        IF (!! = Null && header "." The equals (header)) { 
            // continue to pass information down head 
            currentContext.addZuulRequestHeader ( "the Authorization", header); 
        } 
        return null; 
    } 
}

  

Guess you like

Origin www.cnblogs.com/liushisaonian/p/11260563.html