元数据
spring:
cloud:
gateway:
routes:
- id: route_with_metadata
uri: https://example.org
metadata:
optionName: "OptionValue"
compositeObject:
name: "value"
iAmNumber: 1
可通过ServerWebExchange获取:
Route route = exchange.getAttribute(GATEWAY_ROUTE_ATTR);
route.getMetadata();
route.getMetadata(someKey);
超时
要配置全局http超时:
connect-timeout必须以毫秒为单位指定。
response-timeout必须指定为java.time.Duration。
spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: lb://producer
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- Path=/producer/{segment}
filters:
- StripPrefix=1
metrics:
tags:
path:
enabled: true
enabled: true
httpclient:
connect-timeout: 1000
response-timeout: 1s
修改Producer模块的Controller:
@RequestMapping("/hello")
public String hello(String name, HttpServletRequest request, HttpServletResponse response) {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return "hello," + name + "," + port;
}
访问http://localhost:8500/producer/hello,看到:
要配置每个路由超时:
connect-timeout必须以毫秒为单位指定。
response-timeout必须以毫秒为单位指定。
spring:
cloud:
gateway:
enabled: true
routes:
- id: Goods-Server # 路由 id,唯一标识
uri: lb://producer
predicates:
# - Path=/** # 断言,路由匹配条件,匹配 /product 开头的所有 api
- Path=/producer/{segment}
filters:
- StripPrefix=1
metadata:
response-timeout: 200
connect-timeout: 200
修改Producer模块的Controller:
@RequestMapping("/hello")
public String hello(String name, HttpServletRequest request, HttpServletResponse response) {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return "hello," + name + "," + port;
}
访问http://localhost:8500/producer/hello,看到:
Netty Access Logs
要启用Reactor Netty访问日志,请设置-Dreaactor.Netty.http.server.accessLogEnabled=true。必须是Java System Property, 不能是Spring Boot property。
在Resources加入 logback.xml文件:
<configuration debug="false">
<!-- 一、日志文件存放的地址,可以更改 -->
<property name="LOG_HOME" value="/home/shigp/文档"/>
<!-- 这里更改 -->
<!-- 二、日志输出到控制台的代码 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- 刷新时间 -->
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n</pattern>
</encoder>
</appender>
<!-- 三、日志输出到本地文件的代码 -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 当文件到达多大就自动新建一个文件继续存放数据 -->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/log.%d{yyyy-MM-dd}.%i.log </fileNamePattern>
<maxFileSize>1000MB</maxFileSize>
<MaxHistory>60</MaxHistory>
</rollingPolicy>
<!-- 刷新时间 -->
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} - [%thread] %-5level %logger{50} -%msg%n</pattern>
</encoder>
</appender>
<!-- 四、总控制开关的代码 -->
<root level="INFO">
<!-- 输出级别(多少),默认就是DEBUG。TRACE<DEBUG<INFO<WARN<ERROR -->
<appender-ref ref="STDOUT"/>
<!-- 输出到控制台,不想输出到控制台可以删掉 -->
<appender-ref ref="FILE"/>
<!-- 输出本地磁盘文件夹中,不想输出到文件夹中可以删掉 -->
</root>
</configuration>
启动GatewayServer是加入JVM属性:-Dreactor.netty.http.server.accessLogEnabled=true。访问http://localhost:8500/producer/hello,从控制台看到:
2023-05-07 13:35:41.625 INFO 43318 --- [or-http-epoll-2] reactor.netty.http.server.AccessLog : 0:0:0:0:0:0:0:1%0 - - [07/5月/2023:13:35:41 +0800] "GET /producer/hello HTTP/1.1" 200 15 257
标签:Netty,http,Logs,producer,SpringCloud,response,timeout,true,hello
From: https://www.cnblogs.com/shigongp/p/17379161.html