SpringCloud learning (5) --- Zuul (a) the basic concepts, configuration

[TOC]

Spring Cloud

  • eureka: Registry
    • Server: providing registration
    • Client: register
  • ribbon: load balancing (cluster)
  • Hystrix: Fuse, the implementation alternatives
  • Feign: remote call
  • Zuul: gateway, unified entrance.

1.1, Kazuo When closed, Wan Fu Mo open --- Zuul Gateway

  • Gateway: Program unified entrance. The main functions: service delivery (dynamic routing), such as authentication services

    • Zuul is committed to Netflix hatched a "gateway" solutions open source components

    • Zuul dynamic routing, monitoring, flexibility, service management, security and other aspects play an important role.

    • Zuul bottom is Servlet, assembly is essentially composed of a series chain of responsibility Filter

Zuul have the function

  • Authentication, authorization
  • Pressure control
  • Gray release
  • Dynamic Routing
  • Load shedding
  • Static response processing
  • Active flow control

1.2, Zuul entry case - Route Forwarding

  • demand
//已有访问路径
http://localhost:8080/user
//通过网关希望访问路径
http://localhost:10010/service/user 

Service-oriented routing

Creating zuul_demo

pom

 <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!--添加eureka客户端--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> 

yml

server:
  port: 10010
spring:
  application: name: zuuldemo2 #网关配置:访问路径 和 服务路径 对应关系 zuul: preifx: /api routes: userservice: path: /userservice/** #访问路径 #url: http://localhost:8080 #服务路径 serviceId: userservice #服务名称时 #eureka配置 eureka: client: service-url: defaultZone: http://localhost:10086/eureka 

开启网关代理注解@EnableZuulProxy

package com.czxy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy; /** * @author 庭前云落 * @Date 2019/12/12 17:01 * @description */ @SpringBootApplication @EnableZuulProxy //开启zuul代替 @EnableEurekaClient //开启eureka客户端 public class ZuulApplication { public static void main(String[] args) { SpringApplication.run(ZuulApplication.class,args); } } 

1.3, routing prefix

  • Routing prefixes: maintains the version number or project name

  • Yml modify files, configuration

    zuul:
      prefix: /api
    

Routing wildcard

rule Explanation Examples
/** Matches any character and amount of data path /client/aa,/client/aa/bb/cc
/* Matches any number of characters /client/aa,/client/aaaaaaaaaaaaaa
/? Matches any single character /client/a,/client/b,/client/c

1.4, Zuul filter

1.4.1 Overview

  • Writing filters, inherited his father's class: ZuulFilter
  • Common methods:
    • filterType () filter type, value: "pre"
    • filterOrder () order filter, values: 1
    • shouldFilter () whether, values: true
    • run () execute business logic

Custom filter 1.4.2

  • The filter is achieved, spring added to the vessel, the filter is automatically enabled Zuul

    • Filter parent: ZuulFilter

    • Tools (request context object): RequestContext

    • The special request headers: Authorization

package com.czxy.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; /** * @author 庭前云落 * @Date 2019/12/12 18:07 * @description */ @Component public class LoginFilter extends ZuulFilter { @Override public String filterType() { return "pre"; //路由之前执行 } @Override public int filterOrder() { return 1; //排序 } @Override public boolean shouldFilter() { return true; //是否进行过滤,true将执行run()方法 } @Override public Object run() throws ZuulException { //获得一个特殊请求头,判断是否有值:有返回null(放行),没有 响应状态码401 //1 获得工具类 (获得上下文对象) RequestContext requestContext = RequestContext.getCurrentContext(); //2 通过工具类 获得请求对象 HttpServletRequest request = requestContext.getRequest(); //3 通过request对象获得特殊请求头 String token = request.getHeader("authorization"); //判断 if(token == null || "".equals(token)) { //没有token返回401,关闭响应 requestContext.setSendZuulResponse(false); requestContext.setResponseStatusCode(401); } //放行 return null; } } 
 Source: SEO Company

Guess you like

Origin www.cnblogs.com/1994jinnan/p/12037483.html