<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.dan</groupId><artifactId>service-zuul</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>service-zuul</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.9.RELEASE</version><relativePath /> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Dalston.SR4</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-zuul</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
@EnableZuulProxy
@SpringBootApplication
public class ServiceZuulApplication {public static void main(String[] args) {SpringApplication.run(ServiceZuulApplication.class, args);}
}
接着配置applica.properties文件:
spring.application.name=service-zuul
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
server.port=8769zuul.routes.api-a.path=/api-a/**
zuul.routes.api-a.service-id=service-ribbonzuul.routes.api-b.path=/api-b/**
zuul.routes.api-b.service-id=service-feign
http://localhost:8769/api-a/hello?name=Jack http://localhost:8769/api-b/hello?name=Jack
Hello Jack. I am from port:8762
@Component
public class AccessFilter extends ZuulFilter {private static Logger log = Logger.getLogger(AccessFilter.class);/** filterType: 过滤器的类型,它决定过滤器在请求的哪个生命周期中执行。* pre:路由之前 routing:路由之时 post:路由之后 error:发送错误调用*/@Overridepublic String filterType() {return "pre";}/** filterOrder:过滤器的执行顺序。当请求在一个阶段中存在多个过滤器时,需要根据该方法返回的值来一次执行。*/@Overridepublic int filterOrder() {return 0;}/** shouldFilter:判断该过滤器是否需要被执行。* 这里可以写逻辑判断,是否要过滤,这里true, 永远过滤。*/@Overridepublic boolean shouldFilter() {return true;}/** run:过滤器的具体逻辑。可以很复杂,包括查sql,nosql去判断该请求到底有没有权限访问。*/@Overridepublic Object run() {RequestContext ctx = RequestContext.getCurrentContext();HttpServletRequest request = ctx.getRequest();log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));Object accessToken = request.getParameter("token");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;}}
http://localhost:8769/api-b/hello?name=Jack
token is empty
http://localhost:8769/api-a/hello?name=Jack&token=hhh
Hello Jack. I am from port:8762
https://github.com/lingd3/SpringCloudLearning/tree/master/chapter9