Zuul可以再陆游的时候对拦截的请求参数进行处理,确定如何响应。
application.yml
server:
port: 80
###网关名称
spring:
application:
name: service-zuul
###注册中心
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka/
register-with-eureka: true
fetch-registry: true
###配置网关反向代理
zuul:
routes:
###定义转发服务规则(随便起名)
meiszl-member:
###以/api-member/访问转发到会员服务
path: /api-member/**
###服务别名 zuul网关默认整合ribbon 自动实现负载均衡轮训效果
serviceId: APP-MEISZL-MEMBER
meiszl-order:
###以/api-order/访问转发到订单服务
path: /api-order/**
###订单服务别名
serviceId: TESTFEIGN
ribbon:
###指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间。
ReadTimeout: 5000
###指的是建立连接后从服务器读取到可用资源所用的时间。
ConnectTimeout: 5000
ZuulServiceMain
@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class ZuulServiceMain {
//@EnableZuulProxy开启网关服务
public static void main(String[] args){
SpringApplication.run(ZuulServiceMain.class,args);
}
}
ZuulFilter
package com.meiszl.filter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
@Component
public class TokenFilter extends ZuulFilter {
//网关的过滤器如何编写
//过滤类型 pre 表示在请求之前进行执行
@Override
public String filterType() {
/**
* pre:路由之前
* routing:路由之时
* post: 路由之后
* error:发送错误调用
*/
return FilterConstants.PRE_TYPE;
}
//过滤器执行顺序,当一个请求在同一个阶段存在多个过滤器的时候,多个过滤器执行顺序。
@Override
public int filterOrder() {
return 0;
}
//判断过滤器是否生效
@Override
public boolean shouldFilter() {
return true;
}
//编写过滤器拦截业务逻辑代码
@Override
public Object run() throws ZuulException {
//案例:拦截所有的服务接口,判断服务接口上是否有传递userToken参数。
//1.获取上下文
RequestContext currentContext = RequestContext.getCurrentContext();
//2.获取Request对象
HttpServletRequest request = currentContext.getRequest();
//3.获取token的时候从请求头中获取
String userToken = request.getParameter("userToken");
if (StringUtils.isEmpty(userToken)){
//不会继续执行....不会去调用服务接口,网关服务直接响应给客户端
currentContext.setSendZuulResponse(false);
currentContext.setResponseBody("userToken is null");
currentContext.setResponseStatusCode(401);
return null;
}
//正常执行调用其他服务接口...
return null;
}
}