一、zuul简介
Zuul的主要功能是路由和过滤器。路由功能是微服务的一部分,比如/api/user映射到user服务,/api/shop映射到shop服务。zuul实现了负载均衡。
zuul有以下功能:
- Authentication
- Insights
- Stress Testing
- Canary Testing
- Dynamic Routing
- Service Migration
- Load Shedding
- Security
- Static Response handling
- Active/Active traffic management
二、jar包依赖
1<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 5 <groupId>com.pkfare</groupId> 6 <artifactId>zuul</artifactId> 7 <version>1.0.0</version> 8 <packaging>jar</packaging> 9 10 11 12 <properties> 13 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 14 </properties> 15 16 <parent> 17 <groupId>org.springframework.boot</groupId> 18 <artifactId>spring-boot-starter-parent</artifactId> 19 <version>1.5.9.RELEASE</version> 20 </parent> 21 <dependencyManagement> 22 <dependencies> 23 <dependency> 24 <groupId>org.springframework.cloud</groupId> 25 <artifactId>spring-cloud-dependencies</artifactId> 26 <version>Dalston.SR5</version> 27 <type>pom</type> 28 <scope>import</scope> 29 </dependency> 30 </dependencies> 31 </dependencyManagement> 32 <dependencies> 33 <dependency> 34 <groupId>org.springframework.cloud</groupId> 35 <artifactId>spring-cloud-starter-zuul</artifactId> 36 </dependency> 37 </dependencies> 38 <build> 39 <resources> 40 <resource> 41 <directory>src/main/java</directory> 42 </resource> 43 <resource> 44 <directory>src/main/resources</directory> 45 </resource> 46 </resources> 47 <plugins> 48 <plugin> 49 <groupId>org.springframework.boot</groupId> 50 <artifactId>spring-boot-maven-plugin</artifactId> 51 </plugin> 52 <plugin> 53 <groupId>org.apache.maven.plugins</groupId> 54 <artifactId>maven-compiler-plugin</artifactId> 55 <configuration> 56 <source>1.8</source> 57 <target>1.8</target> 58 <encoding>UTF-8</encoding> 59 </configuration> 60 </plugin> 61 </plugins> 62 </build> 63</project>
三、启动程序
1package com.pkfare.zuul; 2 3import org.springframework.boot.SpringApplication; 4import org.springframework.boot.autoconfigure.SpringBootApplication; 5import org.springframework.cloud.netflix.zuul.EnableZuulProxy; 6 7@EnableZuulProxy 8@SpringBootApplication 9public class Application 10{ 11 public static void main( String[] args ) 12 { 13 SpringApplication.run(Application.class, args); 14 } 15}
四、配置文件
1#配置忽略的请求头 2zuul.sensitive-headers= 3#配置对应的路由映射 4zuul.routes.usercenter-provider.stripPrefix=false 5zuul.routes.usercenter-provider.path=/user/** 6zuul.routes.usercenter-provider.serviceId=USERCENTER-PROVIDER 7#熔断时间 8hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=120000 9#熔断信号量 10zuul.semaphore.maxSemaphores=1000 11 12#服务超时间 13ribbon.ReadTimeout=180000 14ribbon.ConnectTimeout=18000