lettuce redis SDK监控

227 阅读1分钟

前提介绍

如果已经使用了redis sdk雪球的SDK组件,已经集成实现了

 

实现原理

英文原声文档,简单易懂,而且这种metric模式,是趋势

github.com/lettuce-io/…

 

代码实现

添加监控

注意

commandLatencyCollector方法

private void buildRedisClusterClient() {``    ``if (redisClusterClient == ``null``) {``        ``ClientResources res = DefaultClientResources.builder()``                ``.ioThreadPoolSize(LETTUCE_IO_THREAD_COUNT)``                ``.commandLatencyPublisherOptions(``                        ``DefaultEventPublisherOptions.builder()``                                ``.eventEmitInterval(Duration.ofSeconds(``5``))``                                ``.build()``                ``)``                ``.commandLatencyCollector(``new LettuceCommandLatencyCollector(metrics))``                ``.build();``        ``redisClusterClient = RedisClusterClient.create(res, REDIS_CLUSTER_URI);``        ``int MAX_REDIRECTS = ``99``;``        ``redisClusterClient.setOptions(``                ``ClusterClientOptions.builder()``                        ``.maxRedirects(MAX_REDIRECTS)``                        ``.socketOptions(SocketOptions.builder().keepAlive(``true``).build())``                        ``.topologyRefreshOptions(``                                ``ClusterTopologyRefreshOptions.builder()``                                        ``.enableAllAdaptiveRefreshTriggers()``                                        ``.build())``                        ``.build());         ``redisClusterClient.getResources()``                ``.eventBus()``                ``.get()``                ``.subscribe(``new LettuceMetricsSubscriber(buildEventVisitors(metrics)));``    ``}``}

实现监控接口,其中的

firstResponseLatency和completionLatency概念要清楚,不要混淆

LettuceCommandLatencyCollector

public class LettuceCommandLatencyCollector ``implements CommandLatencyCollector {``    ``private static final Logger log = LoggerFactory.getLogger(LettuceCommandLatencyCollector.``class``);     ``private Metrics metrics;     ``public LettuceCommandLatencyCollector(Metrics metrics) {``        ``this``.metrics = metrics;``    ``}     ``@Override``    ``public void recordCommandLatency(SocketAddress local, SocketAddress remote, ProtocolKeyword commandType, ``long firstResponseLatency, ``long completionLatency) {``        ``metrics.timer(commandType.name() + ``".firstResponseLatency"``).update(firstResponseLatency, TimeUnit.NANOSECONDS);``        ``metrics.timer(commandType.name() + ``".completionLatency"``).update(completionLatency - firstResponseLatency, TimeUnit.NANOSECONDS);``        ``if (firstResponseLatency > ``10000000 || completionLatency > ``10000000``) {``            ``log.warn(``"REDIS4 slowlog | " + local.toString() + ``" -> " + remote.toString() + ``" | " + commandType.name() + ``" | firstResponseLatency:" + firstResponseLatency + ``" ns | completionLatency:" + completionLatency + ``" ns"``);``        ``}``    ``}     ``@Override``    ``public void shutdown() {``    ``}     ``@Override``    ``public Map<CommandLatencyId, CommandMetrics> retrieveMetrics() {``        ``return ImmutableMap.of();``    ``}     ``@Override``    ``public boolean isEnabled() {``        ``return true``;``    ``}``}