1、zuul簡介java
Zuul的主要功能是路由和過濾器。路由功能是微服務的一部分,好比/api/user映射到user服務,/api/shop映射到shop服務。zuul實現了負載均衡。spring
zuul有如下功能:apache
2、jar包依賴api
<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.pkfare</groupId> <artifactId>zuul</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
3、啓動程序負載均衡
package com.pkfare.zuul; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @EnableZuulProxy @SpringBootApplication public class Application { public static void main( String[] args ) { SpringApplication.run(Application.class, args); } }
4、配置文件maven
#配置忽略的請求頭 zuul.sensitive-headers= #配置對應的路由映射 zuul.routes.usercenter-provider.stripPrefix=false zuul.routes.usercenter-provider.path=/user/** zuul.routes.usercenter-provider.serviceId=USERCENTER-PROVIDER #熔斷時間 hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=120000 #熔斷信號量 zuul.semaphore.maxSemaphores=1000 #服務超時間 ribbon.ReadTimeout=180000 ribbon.ConnectTimeout=18000