• SpringCloud之Zuul


    使用SpringCloud Zuul实现网关代理。

    一、Maven依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
    </dependency>

    二、编写配置(application.yml)

    eureka:
      client:
        serviceUrl:
          defaultZone: http://localhost:8761/eureka/
    server:
      port: 8769
    spring:
      application:
        name: blog-zuul-client
    zuul:
      routes:
        api-a:
          path: /blog/**
          serviceId: blog-ribbon-client

    三、编写主类

    package com.springcloud.blog;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
    
    @SpringBootApplication
    @EnableZuulProxy
    @EnableEurekaClient
    @EnableDiscoveryClient
    public class BlogZuulClientApplication {
    
        public static void main(String[] args) {
            SpringApplication.run( BlogZuulClientApplication.class, args );
        }
    }

    四、编写过滤器

    package com.springcloud.blog;
    
    import javax.servlet.http.HttpServletRequest;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.stereotype.Component;
    
    import com.netflix.zuul.ZuulFilter;
    import com.netflix.zuul.context.RequestContext;
    
    @Component
    public class MyFilter extends ZuulFilter {
    
        private static Logger log = LoggerFactory.getLogger(MyFilter.class);
        @Override
        public String filterType() {
            return "pre";
        }
    
        @Override
        public int filterOrder() {
            return 0;
        }
    
        @Override
        public boolean shouldFilter() {
            return true;
        }
    
        @Override
        public Object run() {
            RequestContext ctx = RequestContext.getCurrentContext();
            HttpServletRequest request = ctx.getRequest();
            log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));
            Object accessToken = request.getHeader("Authorization");
            if(accessToken == null) {
                log.warn("token is empty");
                ctx.setSendZuulResponse(false);
                ctx.setResponseStatusCode(401);
                try {
                    ctx.getResponse().getWriter().write("token is empty");
                }catch (Exception e){}
    
                return null;
            }
            log.info("ok");
            return null;
        }
    
    }

    五、测试

    第一次请求,不携带token,如图:

    第二次请求,携带token,如图:

  • 相关阅读:
    5.5 数据库约束
    5.4 数据库数据类型
    5.3 数据 库,表 操作
    5.2 数据库引擎
    5.1 数据库安装
    4.6 并发编程/IO模型
    4.5 协程
    4.4 线程
    在线编辑器 引入方法
    MySQL查看版本号的五种方式介绍1111111
  • 原文地址:https://www.cnblogs.com/youcong/p/13066734.html
Copyright © 2020-2023  润新知