springboot集成zuul

pom.xml


<dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-zuul</artifactId>
           </dependency>
           <!-- 路由重试 -->
           <dependency>
                <groupId>org.springframework.retry</groupId>
                <artifactId>spring-retry</artifactId>
           </dependency>


application.yml

spring:
  application:
    name: spring-cloud-zuul
server:
  port: 8888
  
eureka:
  client:
    serviceUrl:
      defaultZone:http://localhost:8999/eureka/
  instance:
    leaseRenewalIntervalInSeconds: 1
    leaseExpirationDurationInSeconds: 2
    
zuul:
  routes:
    api-a:
      path: /producer/**
      serviceId: spring-cloud-producer
    api-b:
      path: /consumer/**
      serviceId: spring-cloud-consumer
      
      
zuul:
  #是否开启重试功能
  retryable: false #true
ribbon:
  #对当前服务的重试次数
  MaxAutoRetries: 2
  #切换相同Server的次数
  MaxAutoRetriesNextServer: 0


Application.java

package com.daimeng.zuul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
import com.daimeng.zuul.filter.TokenFilter;
@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class ZuulApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZuulApplication.class, args);
    }
    
    @Bean
    public TokenFilter tokenFilter() {
        return new TokenFilter();
    }
}

Filter.java

package com.daimeng.zuul.filter;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
public class TokenFilter extends ZuulFilter{
    
    private static final Logger logger = LoggerFactory.getLogger(TokenFilter.class);
    
    //定义filter的类型,有pre、route、post、error四种
    @Override
    public String filterType() {
        return "pre";
    }
    
    //定义filter的顺序,数字越小表示顺序越高,越先执行
    @Override
    public int filterOrder() {
        return 0;
    }
    
    //表示是否需要执行该filter,true表示执行,false表示不执行
    @Override
    public boolean shouldFilter() {
        return true;
    }
        
    //filter需要执行的具体操作
    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        logger.info("--->>> TokenFilter {},{}", request.getMethod(), request.getRequestURL().toString());
        String token = request.getParameter("token");// 获取请求的参数
        if (token !=null && !"".equals(token)) {
            ctx.setSendZuulResponse(true); //对请求进行路由
            ctx.setResponseStatusCode(200);
            ctx.set("isSuccess", true);
            return null;
        } else {
            ctx.setSendZuulResponse(false); //不对其进行路由
            ctx.setResponseStatusCode(400);
            ctx.setResponseBody("token is empty");
            ctx.set("isSuccess", false);
            return null;
        }
    }
}


{context}