我们希望捕获由Zuul代理服务器处理的每个请求的响应时间,以用于性能监视。 Zuul过滤器似乎是这样做的合理方式,但考虑到Zuul过滤器的结构,它似乎不是捕获经过时间的简单方法。 任何人都可以阐明我们如何实现这一目标吗?
答案 0 :(得分:2)
我还努力使用Zuul过滤器来获得基本指标。
Spring Actuator trace提供了很多信息。只需使用“/ trace”查看:
这对Zuul来说是完美的
[
{
"timestamp": 1499429507097,
"info": {
"method": "GET",
"path": "/index.html",
"headers": {
"request": {
"host": "localhost:8765",
"connection": "keep-alive",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/58.0.3029.110 Chrome/58.0.3029.110 Safari/537.36",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"accept-encoding": "gzip, deflate, sdch, br",
"accept-language": "en-US,en;q=0.8",
"cookie": "zbx_sessionid=02ac46186474dbd668d7abe0f78705c9"
},
"response": {
"X-Application-Context": "application:8765",
"Last-Modified": "Fri, 07 Jul 2017 10:52:42 GMT",
"Cache-Control": "no-store",
"Accept-Ranges": "bytes",
"Content-Type": "text/html",
"Content-Length": "446",
"Date": "Fri, 07 Jul 2017 12:11:47 GMT",
"status": "200"
}
},
"timeTaken": "3"
}
},
但是,标准InMemoryTraceRepository给了我关于“/ health”甚至“/ trace”的统计信息,但我只对 POSTS 去Zuul。所以我只是扩展InMemoryTraceRepository并将其作为@Bean提供。
import org.springframework.boot.actuate.trace.InMemoryTraceRepository;
import org.springframework.boot.actuate.trace.Trace;
public class ZuulInMemoryTraceRepository extends InMemoryTraceRepository {
public ZuulInMemoryTraceRepository(int maxSize) {
setCapacity(maxSize);
}
public Collection<DateTimeTaken> getStats(){
return fifo;
}
public void setReverse(boolean reverse) {
super.setReverse(reverse);
}
@Override
public void setCapacity(int capacity) {
super.setCapacity(capacity);
}
@Override
public List<Trace> findAll() {
return super.findAll();
}
@Override
public void add(Map<String, Object> map) {
Object method = map.get("method");
if (!"POST".equals(method)) {
return;
}
Object timeTaken = map.get("timeTaken");//timeTaken is a String
//log timeTaken
super.add(map);
}
}
在我的@Configuration中,我添加了:
@Bean
public ZuulInMemoryTraceRepository myZuulInMemoryTraceRepository() {
return new ZuulInMemoryTraceRepository(10000);
}
或者,您可以在ZuulInMemoryTraceRepository中将自定义对象添加到固定大小的EvictingQueue。最后公开一个宁静的Web服务来暴露像我的getStats()这样的EvictingQueue。然后可以使用此json绘制漂亮的响应时间图。
@Override
public void add(Map<String, Object> map) {
super.add(map);
Object method = map.get("method");
if (!"POST".equals(method)) {
return;
}
Object timeTaken = map.get("timeTaken");//timeTaken is a String
if (timeTaken != null) {
synchronized (fifo) {
//Make your own DateTimeTaken
fifo.add(new DateTimeTaken(new Date(), timeTaken.toString()));
}
}
}
在我的统计数据@RestController中:
@Autowired
ZuulInMemoryTraceRepository zuulInMemoryTraceRepository;
@RequestMapping("/stats")
public Collection<DateTimeTaken> getAll() {
return zuulInMemoryTraceRepository.getStats();
}