1、先创建一个springboot项目
2、添加zuul的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
3、在启动类上添加网关的注解
@EnableZuulProxy
public class GatewayApplication {
4、在主配置文件中配置网关的路由规则:
#实现路由规则
zuul.routes.book.url=http://127.0.0.1:8070
zuul.routes.sale.url=http://127.0.0.1:8090
ps:book、sale是其它微服务的名称
spring.application.name=book
spring.application.name=sale
#网关的访问端口号
server.port=8000
ps:
#127.0.0.1:8080 相当于访问网关
#127.0.0.1:8080/book 相当于访问book微服务
5、创建过滤器,拦截请求
一:创建前置过滤器:pre
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
@Component
public class PreFilter extends ZuulFilter {
Logger log = LoggerFactory.getLogger(PreFilter.class);
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 1;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() throws ZuulException {
RequestContext context = RequestContext.getCurrentContext();
HttpServletRequest request = context.getRequest();
log.info(String.format("请求的uri地址:%s----请求方式:%s", request.getRequestURI(), request.getMethod()));
String token = request.getParameter("token");
if(token == null){
context.setSendZuulResponse(false);
context.setResponseStatusCode(500);
}
return null;
}
}
二:创建后置过滤器:post
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.exception.ZuulException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
public class PostFilter extends ZuulFilter {
Logger log = LoggerFactory.getLogger(PostFilter.class);
@Override
public String filterType() {
return "post";
}
@Override
public int filterOrder() {
return 1;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() throws ZuulException {
log.info("=========PostFilter=========");
return null;
}
}
三:创建路由过滤器:route
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.exception.ZuulException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
public class RouteFilter extends ZuulFilter {
Logger log = LoggerFactory.getLogger(RouteFilter.class);
@Override
public String filterType() {
return "route";
}
@Override
public int filterOrder() {
return 1;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() throws ZuulException {
log.info("=======routeFilter=====");
return null;
}
}
四:创建error过滤器
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.exception.ZuulException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
public class ErrorFilter extends ZuulFilter {
Logger log = LoggerFactory.getLogger(ErrorFilter.class);
@Override
public String filterType() {
return "error";
}
@Override
public int filterOrder() {
return 1;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() throws ZuulException {
log.info("==========error==========");
return null;
}
}