首页
学习
活动
专区
圈层
工具
发布

SpringCloud-Zuul服务网关[性能优化]

  zuul的一些默认设置如果我们不调整下,那么对整体的服务的性能还是有很大影响的,比如如果网关调用的服务处理时间比较长,那么就会出现timeout异常。

性能优化

1.创建项目

2.添加依赖

代码语言:javascript
复制
<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-eureka</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-zuul</artifactId>
	</dependency>
</dependencies>

3.配置文件

代码语言:javascript
复制
spring.application.name=zuul-gateway-timeout
server.port=9020
#\u8BBE\u7F6E\u670D\u52A1\u6CE8\u518C\u4E2D\u5FC3\u5730\u5740\uFF0C\u6307\u5411\u53E6\u4E00\u4E2A\u6CE8\u518C\u4E2D\u5FC3
eureka.client.serviceUrl.defaultZone=http://dpb:123456@eureka1:8761/eureka/,http://dpb:123456@eureka2:8761/eureka/

4.超时效果

  设置休眠时间,启动product服务

通过网关访问服务

5.优化处理

  在application.properties中添加如下配置

代码语言:javascript
复制
#第一层 hystrix 超时时间设置
#默认情况下是线程池隔离,超时时间 1000ms
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=8000
#第二层 ribbon 超时时间设置:设置比第一层小
# 请求连接的超时时间: 默认 5s
ribbon.ConnectTimeout=5000
# 请求处理的超时时间: 默认 5s
ribbon.ReadTimeout=5000

然后访问:虽然卡顿了2秒,但是之后还是访问到了请求

6.超时调优图解

下一篇
举报
领券