SpringCloud 版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.RELEASE</version>
<relativePath/>
</parent>
zuul 网关所需依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
zuul 网关 yml 配置
spring:
application:
name: cherish-gateway
server:
port: 30002
# 设置的eureka端口号
eureka:
client:
# 表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,故而设置为false
fetchRegistry: true
# 表示是否将自己注册到Eureka Server,默认为true。由于当前应用就是Eureka Server,故而设置为false
registerWithEureka: true
# Eureka server地址,查询服务和注册服务都需要依赖这个地址,多个地址可用逗号(英文的)分割
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:30001/eureka/
# 设置eureka的主机地址
instance:
hostname: 127.0.0.1
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: always
logging:
level:
root: info
com.cloud: debug
file: logs/${spring.application.name}.log
zuul:
ignored-services: '*'
sensitiveHeaders:
routes:
api-u:
path: /api-u/**
serviceId: cherish-user
host:
connect-timeout-millis: 10000
socket-timeout-millis: 60000
add-proxy-headers: true
ribbon:
eager-load:
enabled: true
ribbon:
ReadTimeout: 10000
ConnectTimeout: 10000
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 60000
需要注意的地方
# 表示是否从Eureka Server获取注册信息,默认为true。因为这是一个单点的Eureka Server,不需要同步其他的Eureka Server节点的数据,故而设置为false
fetchRegistry: true
上面这个配置,默认是为 true,不能将其设置为 false,否则网关无法获取到 eureka 上其他服务的注册信息,就无法通过网关访问。
查看日志
通过查询日志,第一条的配置如下,我们可以看到,在里面没有获取到 eureka 上面注册的信息,比如 ip、port,因此这样会导致我们无法通过 zuul 网关访问到其他服务。
fetchRegistry: false
第二条日志的 yml 配置如下,我们可以看到日志里面已经打印出了 eureka 上其他服务的配置信息,例如:ip、port,zuul 从 eureka 上获取到这些信息,就可以访问到 eureka 上的其他服务了。
fetchRegistry: true