使用RestTemplate遇到线上的问题

在线上环境使用RestTemplate时发现运行一阵后会出现服务不可用。通过观察日志发现有这么一个警告一直在打印

2018-12-03 09:59:58.772 [hystrix-UnionService-2961] WARN o.s.cloud.netflix.metrics.servo.ServoMonitorCache [57]- timerCache is above the warning threshold of 1000 with size 511486.
2018-12-03 09:59:58.953 [hystrix-UnionService-2961] WARN o.s.cloud.netflix.metrics.servo.ServoMonitorCache [57]- timerCache is above the warning threshold of 1000 with size 511487.

通过查看源码

public class ServoMonitorCache {

    private static final Log log = LogFactory.getLog(ServoMonitorCache.class);

    private final Map timerCache = new HashMap<>();
    private final MonitorRegistry monitorRegistry;
    private final ServoMetricsConfigBean config;

    public ServoMonitorCache(MonitorRegistry monitorRegistry, ServoMetricsConfigBean config) {
        this.monitorRegistry = monitorRegistry;
        this.config = config;
    }

    /**
     * @param config contains the name and tags that uniquely identify a timer
     * @return an already registered timer if it exists, otherwise create/register one and
     * return it.
     */
    public synchronized BasicTimer getTimer(MonitorConfig config) {
        BasicTimer t = this.timerCache.get(config);
        if (t != null)
            return t;

        t = new BasicTimer(config);
        this.timerCache.put(config, t);

        if (this.timerCache.size() > this.config.getCacheWarningThreshold()) {
            log.warn("timerCache is above the warning threshold of " + this.config.getCacheWarningThreshold() + " with size " + this.timerCache.size() + ".");
        }

        this.monitorRegistry.register(t);
        return t;
    }
}

发现this.timerCache.put(config, t);这行一直在往map里边put,导致map过大触发了下边的警告,所以这部分的内存一直得不到释放,可能导致内存泄漏,通过在github上和作者团队沟通。


使用RestTemplate遇到线上的问题_第1张图片
image.png

升级版本或者关闭spring.metrics.servo.enabled=false就可以了

你可能感兴趣的:(使用RestTemplate遇到线上的问题)