Spring Cloud 如何利用zuul实现网关

Posted 指尖,写不尽

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Cloud 如何利用zuul实现网关相关的知识,希望对你有一定的参考价值。

可以利用zuul做哪些事情:

  路由转发,限流熔断,日志监控,安全认证,单点入口

1.新建项目 spring-zuul

2.引入pom

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

3.配置

spring:
  application:
    name: spring-zuul
  http:
    encoding:
      charset: UTF-8 #设置请求返回UTF-8编码
      force: true
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
server:
  port: 8082

4.启动类注解

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;

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class SpringZuulApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringZuulApplication.class, args);
    }

}

 

路由转发

通过zuul中转访问各个业务微服务的url格式: {basePath}/{spring.application.name}/url

{basePath} : 网关zuul项目启动后的路径
{spring.application.name} 各个具体子系统的微服务名称,即配置文件中配置的spring.application.name
url : 各个应用自定的的http接口。

 

测试:

 

安全认证

  zuul作为系统的入口,可以再其上面统一对请求做处理,判断请求的安全型,并且能够改写返回的报文内容。

       无论作为前置过滤器(处理刚进系统的预处理),还是作为后置过滤器,修改返回的报文内容。都要继承ZuulFilter类

    1.前置过滤器

  

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class PreAuthFilter extends ZuulFilter{

    //是什么类型的过滤器
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    /**
     * 判断执行的顺序,顺序越小,越靠前拦截
     * @return
     */
    @Override
    public int filterOrder() {
        return FilterConstants.PRE_DECORATION_FILTER_ORDER-1;
    }

    //判断是否应该被拦截
    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {

        RequestContext requestContext = RequestContext.getCurrentContext();

        String auth = requestContext.getRequest().getHeader("auth");
        if(StringUtils.isBlank(auth) || !"halou".equals(auth)){
            requestContext.setSendZuulResponse(false);
            requestContext.setResponseStatusCode(HttpStatus.UNAUTHORIZED.value());
        }
        return null;
    }
}
  其中:filterType() 定义过滤器类型
     
filterOrder() 定义执行顺序
shouldFilter() 是否应该被拦截,可以编写是否拦截的详细逻辑
  2.后置过滤器
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;

@Component
public class PostFilter extends ZuulFilter{

    /**
     * 处理器类型
     * @return {@link FilterConstants#POST_TYPE}
     */
    @Override
    public String filterType() {
        return FilterConstants.POST_TYPE;
    }

    /**
     * 执行顺序
     * @return {@link FilterConstants#SEND_RESPONSE_FILTER_ORDER} -1
     */
    @Override
    public int filterOrder() {
        return FilterConstants.SEND_RESPONSE_FILTER_ORDER -1;
    }

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

    /**
     * 返回内容设置
     * @return
     * @throws ZuulException
     */
    @Override
    public Object run() throws ZuulException {


        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletResponse response = requestContext.getResponse();
        response.setHeader("hello","hello");
        return null;

    }
}

  

 
 完整代码访问: https://github.com/halouprogramer/spring-cloud-demo


  

以上是关于Spring Cloud 如何利用zuul实现网关的主要内容,如果未能解决你的问题,请参考以下文章

最全面的改造Zuul网关为Spring Cloud Gateway(包含Zuul核心实现和Spring Cloud Gateway核心实现)

Spring Cloud Zuul微服务网关的API限流

Spring Cloud微服务中网关服务是如何实现的?(Zuul篇)

Spring Cloud:API网关服务——Spring Cloud Zuul

Spring Cloud(Dalston.SR5)--Zuul 网关-路由配置

Spring Cloud:服务网关zuul过滤器