1 添加依赖
<?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">
<parent>
<artifactId>leyou</artifactId>
<groupId>com.leyou.parent</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.leyou.common</groupId>
<artifactId>ly-gateway</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-zuul</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-zuul</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.leyou.service</groupId>
<artifactId>ly-auth-common</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.leyou.common</groupId>
<artifactId>ly-common</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2 编写启动类
@SpringCloudApplication
@EnableZuulProxy
public class LyGateway {
public static void main(String[] args) {
SpringApplication.run(LyGateway.class);
}
}
zuul的注解@EnableZuulServer和@EnableZuulProxy
@EnableZuulProxy
简单理解为@EnableZuulServer
的增强版,当Zuul与Eureka、Ribbon等组件配合使用时,我们使用@EnableZuulProxy
。- 使用
@EnableZuulProxy
注解会增加额外的几种过滤器 。
注解@SpringCloudApplication和@SpringBootApplication的区别
注解@SpringCloudApplication
包括:
@SpringBootApplication
:SpringBoot注解@EnableDiscoveryClient
:注册服务中心Eureka注解@EnableCircuitBreaker
,断路器注解。
对于SpringCloud来说,这是每一微服务必须应有的三个注解,所以才推出了@SpringCloudApplication
这一注解集合。
因此,使用@SpringCloudApplication需要引入Springboot依赖外,还需要引入以下的两个依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
但是,spring-cloud-starter-netflix-zuul
中已经引入了上述hystrix
,因此可以简化配置。
3 配置文件
server:
port: 10010
spring:
application:
name: api-gateway
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
zuul:
prefix: /api # 添加路由前缀
routes:
item-service: /item/**
search-service: /search/**
user-service: /user/**
auth-service: /auth/**
cart-service: /cart/**
order-service: /order/**
ignored-services:
- upload-service # 忽略upload-service服务
add-host-header: true
sensitive-headers:
# upload-service: #/upload/**:不能这样写,网关捕获到upload之后页面的url只剩下/image,但是controller里面是/upload/image,所以不能去除upload的前缀
# path: /upload/**
# serviceId: upload-service
# strip-prefix: false
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMillisecond: 5000 # 熔断超时时长:10000ms
ribbon:
ConnectTimeout: 1000 # 连接超时时间(ms)
ReadTimeout: 3500 # 通信超时时间(ms)
MaxAutoRetriesNextServer: 0 # 同一服务不同实例的重试次数
MaxAutoRetries: 0 # 同一实例的重试次数