本文介绍了订阅Spring Metrics频道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,根据Spring的文档,它将在REST端点和消息通道上发布指标。

So according to the documents of Spring it will publish metrics on a REST endpoint and a message channel.

REST端点工作正常,因为我得到了预期的结果。但是,我想处理指标中的每个更改。所以它说它会默认将消息发布到一个名为metricsChannel的频道

The REST endpoint works fine as I get the expected result. However I would like to handle each change in the metrics. So it says it will by default publish messages to a channel called "metricsChannel"

我试图创建以下类来听这个频道,但似乎没有开火。其他所有内容都是Spring Boot应用程序的默认设置。

I tried to create the following class which would listen to this channel, but it does not seem to fire. Everything else has been kept default for the Spring Boot application.

package services.core;

import org.springframework.stereotype.Service;
import org.springframework.integration.annotation.ServiceActivator;

@Service
public class MetricService {
    @ServiceActivator(inputChannel = "metricsChannel")
    public void handleMessage(org.springframework.messaging.Message<?> message) {
        System.out.println("Message [" + message.toString() + "] is received");
    }
}


推荐答案

我我刚测试过并运行良好:

I've just tested that and works well:

@Bean
@ServiceActivator(inputChannel = "metricsChannel")
public MessageHandler metricsHandler() {
    return System.out::println;
}

我已经在我们的网络套接字中完成了 示例服务器上部分。
添加:

I've done that in our web-sockets sample on the server part.Added this:

compile 'org.springframework.boot:spring-boot-starter-actuator'

到该项目Gradle配置。

to that project Gradle config.

我看到这在控制台中,当我启动客户端应用程序时:

And I see this in console, when I started the client app:

GenericMessage [payload=Metric [name=gauge.response.time.star-star, value=26.0, timestamp=Tue Apr 14 16:03:53 EEST 2015], headers={metricName=gauge.response.time.star-star, id=08697a97-83c1-5000-f031-65f6797c0cd8, timestamp=1429016633672}]
GenericMessage [payload=Metric [name=counter.status.101.time.star-star, value=1, timestamp=Tue Apr 14 16:03:53 EEST 2015], headers={metricName=counter.status.101.time.star-star, id=8d070cb4-88e8-f5a7-6b83-6b27edf75bfc, timestamp=1429016633674}]

但是,是的:您的代码也很好。

But, yes: your code is good as well.

这篇关于订阅Spring Metrics频道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 06:03