gateway filters Zuul Filter springcloud

Why gateway filters?

  Micro-service architecture systems, a business usually have a lot of micro-system services,

  比如:OrderServiceProductServiceUserService...

  To make calling easier, usually a layer of repackaging these services in front-end,

  

 

 

 

  Similar to the following:

  

  This front layer known as a "gateway layer", meaning that it is present, the "a pair of N" Problems converted into "a pair of 1 " issues ( route ),

  At the same time before reaching a real micro service request, you can do some preprocessing ( filtering ),

  For example: login authentication, log printing ...

 

Filter life cycle

  Filter life cycle has four, namely,

     "The PRE (pre-filter)": This type of filters in Request routing performed prior to the source web-service. For implementing Authentication, select the source address of the service;

    "The ROUTING (routing)": This type of filters used in the Request routing to a web-service source, the source is a web-service business logic services. As used herein, HttpClient requested web-service;

    "The POST (post-filter)": The type of the filters is performed ROUTING return Response. Response to achieve the results to be modified, as well as the collection of statistical data will be transmitted Response clients.

    "ERROR (error filter)": any one of the above three processes are referred ERROR error type filters for processing.

  The entire life cycle can be represented by the following FIG.

 

 

 

 

 

 "Coustom" is a custom filter.

 

Set up filters

1. Create a micro gateway service. zuul-server-20001 (2001 is the port number easy to see)

 

2. import prm.xml zuul dependencies

 

<!--zuuljar包-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>

 

 

3. add annotations @EnableZuulProxy class configuration;

@EnableZuulProxy this annotation is open gateway
@SpringBootApplication
 // add gateway comment 
@RestController
@EnableZuulProxy
public class ZuulServerApplication {

    @RequestMapping("/")
    public String show(){
        return "你好网关";
    }
    public static void main(String[] args) {
        SpringApplication.run(ZuulServerApplication.class, args);
    }
}

4.在yml里添加配置,注意网关也要指定注册中心地址

#自定义路由映射
zuul:
routes:
#你微服务的名字:/随便来个别名/你的微服务api
orders-server1: /order/**
orders-server: /order/**
user-server: /user/**
prefix: "/"
#禁止所有服务使用服务名访问
ignored-services: "*"
 

 

启动网关服务,就行了。

 

如果要自定义过滤器在网关微服务中建个类继承zuulfilter

 

/**
 * 这是网关的自定义过滤器
 */
@Component
    public class LoginZuulFilter extends ZuulFilter {
    @Override
    public String filterType() {
    //这个过滤器设置为前置过滤器
return "pre"; }    @Override public int filterOrder() { return 1; } /** * 次方法返回true,就执行下面的run方法 * @return */ @Override public boolean shouldFilter() { //获取请求上下文 RequestContext context = RequestContext.getCurrentContext(); //获取当前请求对象 HttpServletRequest request = context.getRequest(); //获取请求路径 String requestURI = request.getRequestURI(); //判断是否含有login路径,有就return turn if(requestURI.toUpperCase().contains("LOGIN")){ return false; } return true; } /** * 在run方法中添加业务逻辑 * @return * @throws ZuulException */ @Override public Object run() throws ZuulException { //获取请求上下文 RequestContext context = RequestContext.getCurrentContext(); //获取当前请求对象 HttpServletRequest request = context.getRequest(); //获取当前响应对象 HttpServletResponse response = context.getResponse(); //获取头信息 String tokin = request.getHeader("x-tokin"); //判断tokin是否存在 if(tokin==null){ HashMap<Object, Object> map = new HashMap<>(); map.put("booter",false); map.put("massage","亲登录"); String s = JSON.toJSONString(map); try { response.getWriter().write(s); } catch (IOException e) { e.printStackTrace(); } //阻止放行 context.setSendZuulResponse(false); } return null; }

 

 

 

配了就行了。重启服务。

Guess you like

Origin www.cnblogs.com/bigbigxiao/p/12122052.html