The main function of zuul is routing forwarding and filtering. For example, all requests from /api-a/* are forwarded to server a, and all requests from /api-b/* are forwarded to server b. By default, zuul is implemented in combination with ribbon. Load balancing function.
1 zuul's routing forwarding
1.1 Create a springboot project and introduce related dependencies
<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.beifeng.hadoop</groupId> <artifactId>beifeng-spring-cloud-zuul</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>beifeng-spring-cloud-zuul</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath /> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- 声明为web项目 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 配置eureka --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- 配置zuul --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
1.2 Configure the routing forwarding policy in the configuration file
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ #Register server address
server:
port: 8766
spring:
application:
name: cloud-zuul
zuul:
routes:
api-a:
path: /api-a/** #All requests starting with /api-a are forwarded to the cloud-consumer-ribbon server
serviceId: cloud-consumer-ribbon
api-b:
path: /api-b/** #All requests starting with /api-b are forwarded to the cloud-consumer-feign server
serviceId: cloud-consumer-feign
1.3 Declare enabling zuul in the startup class
@SpringBootApplication @EnableZuulProxy @EnableEurekaClient public class CloudZuul { public static void main(String[] args) { SpringApplication.run(CloudZuul.class, args); } }
1.4 Start to view the results
Start the eureka-server, eureka-client, cloud-consumer-ribbon, cloud-consumer-feign, cloud-zuul projects in sequence
2 Route filtering
2.1 Create a custom route filter, specify which requests to filter and how to filter
@Component public class MyZuulFilter extends ZuulFilter { Logger logger=LoggerFactory.getLogger(MyZuulFilter.class); // The specific logic of the filter public Object run() { RequestContext context=RequestContext.getCurrentContext(); HttpServletRequest request=context.getRequest(); logger.info("url:"+request.getRequestURL().toString()); Object token=request.getParameter("token"); if (token==null||StringUtils.isEmpty(token.toString())) { logger.info("token is empty"); context.setSendZuulResponse ( false ); context.setResponseStatusCode(401); try { context.getResponse().getWriter().write("token is empty"); } catch (IOException e) { e.printStackTrace (); } return null; } logger.info("ok"); return null; } // Whether to filter, you can write logical judgment public boolean shouldFilter() { RequestContext context=RequestContext.getCurrentContext(); HttpServletRequest request=context.getRequest(); String url = request.getRequestURL().toString(); return url.contains("/api-a/"); // Only verify the request of api-a } // Filter order @Override public int filterOrder() { return 0 ; } // Return the type of filter, pre: before routing, routing: during routing, post: after routing, error: call @Override when an error occurs public String filterType() { return "pre" ; } }
2.2 Start to view the effect