zoukankan      html  css  js  c++  java
  • 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,如图:

  • 相关阅读:
    JS开发技巧
    Git push 常见用法
    关闭浏览器标签
    vue 样式穿透 watch深度监听
    Git Commit Template 提交模板
    常用git stash命令:
    Windows下安装及使用NVM
    JS正则表达式
    js转码
    多行文本溢出显示省略号(…)全攻略
  • 原文地址:https://www.cnblogs.com/youcong/p/13066734.html
Copyright © 2011-2022 走看看