gpt4 book ai didi

java - 卡门与 Spring 和 Akka

转载 作者:行者123 更新时间:2023-12-01 18:03:17 33 4
gpt4 key购买 nike

我将 akka (java) 与 spring boot 结合使用。我想使用 kamon 监控指标并将其显示在 grafana 仪表板上。我已将 kamon core kamon statsd 添加到依赖项中,并为 statsd< 创建了一个具有正确端口和主机名的 application.conf/。没有明确的示例或教程来显示我正在使用的堆栈。是否可以使用 spring boot、javaakkakamon< 来测量 akka 指标tomcat 中部署为 war?

最佳答案

这是一个简单的例子

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>kamon.annotation</groupId>
<artifactId>kamon-spring-boot</artifactId>
<version>0.1.0</version>


<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.kamon</groupId>
<artifactId>kamon-core_2.11</artifactId>
<version>0.6.2</version>
</dependency>
<dependency>
<groupId>io.kamon</groupId>
<artifactId>kamon-annotation_2.11</artifactId>
<version>0.6.2</version>
</dependency>
<dependency>
<groupId>io.kamon</groupId>
<artifactId>kamon-akka_2.11</artifactId>
<version>0.6.2</version>
</dependency>
<dependency>
<groupId>io.kamon</groupId>
<artifactId>kamon-log-reporter_2.11</artifactId>
<version>0.6.2</version>
</dependency>
</dependencies>

<properties>
<java.version>1.8</java.version>
</properties>


<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>io.kamon</id>
<url>http://snapshots.kamon.io</url>
</repository>
</repositories>

两个 Actor 打乒乓球

import akka.actor.UntypedActor;

class Pinger extends UntypedActor {
static final class PingMessage {}

public void onReceive(Object message) throws Exception {
if (message instanceof Ponger.PongMessage) getSender().tell(new PingMessage(), getSelf());
else unhandled(message);
}

}

import akka.actor.UntypedActor;

class Ponger extends UntypedActor {
static final class PongMessage {}

public void onReceive(Object message) throws Exception {
if (message instanceof Pinger.PingMessage) getSender().tell(new PongMessage(), getSelf());
else unhandled(message);
}

}

Spring 组件

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;

@Component
public class PingPong {

@PostConstruct
public void initialize() {
final ActorSystem system = ActorSystem.create("kamon-spring-boot-actor-system");

final ActorRef pinger = system.actorOf(Props.create(Pinger.class), "pinger");
final ActorRef ponger = system.actorOf(Props.create(Ponger.class), "ponger");

pinger.tell(new Ponger.PongMessage(), ponger);
}
}

还制作一个简单的 Controller ,以展示使用 kamon-annotation 是多么简单。收集其他一些指标的模块

import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@Controller
@EnableAutoConfiguration
@RequestMapping("/kamon")
@EnableKamon
public class KamonController {

@RequestMapping("/counter")
@ResponseBody
@Count(name = "awesomeCounter")
public String counter() { return "count!!!"; }
}

应用程序主

import kamon.akka.pingpong.PingPong;
import kamon.annotation.KamonController;
import org.springframework.boot.SpringApplication;

public class KamonSpringApplication {
public static void main(String... args) {
Kamon.start();
SpringApplication.run(KamonController.class, args);
}
}

为了测试目的而简化的application.conf

kamon {
metric {
filters {
trace.includes = [ "**" ]
akka-actor.includes = [ "**" ]
akka-actor.excludes = ["*/system/**", "*/user/IO-**" ]
akka-dispatcher.includes = [ "**" ]
akka-dispatcher.excludes = [ ]
}
}
}

构建应用程序并运行

mvn package && java -javaagent:/path/to/aspectjweaver.jar -jar target/kamon-spring-boot-0.1.0.jar

我们应该得到这样的东西

+--------------------------------------------------------------------------------------------------+
| |
| Actor: kamon-spring-boot-actor-system/user/pinger |
| |
| Processing Time (nanoseconds) Time in Mailbox (nanoseconds) Mailbox Size |
| Msg Count: 3393358 Msg Count: 3393405 Min: 0 |
| Min: 237 Min: 178 Avg.: 0.0 |
| 50th Perc: 672 50th Perc: 756 Max: 2 |
| 90th Perc: 988 90th Perc: 1264 |
| 95th Perc: 1088 95th Perc: 1368 |
| 99th Perc: 1520 99th Perc: 1848 Error Count: 0 |
| 99.9th Perc: 20480 99.9th Perc: 17920 |
| Max: 16646144 Max: 34865152 |
| |
+--------------------------------------------------------------------------------------------------+

+--------------------------------------------------------------------------------------------------+
| |
| Actor: kamon-spring-boot-actor-system/user/ponger |
| |
| Processing Time (nanoseconds) Time in Mailbox (nanoseconds) Mailbox Size |
| Msg Count: 3739208 Msg Count: 3739161 Min: 0 |
| Min: 272 Min: 172 Avg.: 0.0 |
| 50th Perc: 672 50th Perc: 732 Max: 2 |
| 90th Perc: 976 90th Perc: 1232 |
| 95th Perc: 1064 95th Perc: 1344 |
| 99th Perc: 1360 99th Perc: 1656 Error Count: 0 |
| 99.9th Perc: 10496 99.9th Perc: 14272 |
| Max: 7766016 Max: 30277632 |
| |
+--------------------------------------------------------------------------------------------------+

如果我们做2个卷发

http://localhost:8080/kamon/counter
http://localhost:8080/kamon/counter

+--------------------------------------------------------------------------------------------------+
| |
| Counters |
| ------------- |
| awesomeCounter => 2 |
| |
| |
| Histograms |
| -------------- |
| |
| MinMaxCounters |
| ----------------- |
| |
| Gauges |
| ---------- |
| |
+--------------------------------------------------------------------------------------------------+

link到完整的例子。

希望这可以帮助你。

关于java - 卡门与 Spring 和 Akka ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38983025/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com