Skip to content

Commit 81ebc84

Browse files
committed
add java doc
1 parent 3a0bd75 commit 81ebc84

2 files changed

Lines changed: 422 additions & 0 deletions

File tree

content/en/docs/practice/java.md

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
---
2+
title: "Java Programs"
3+
description: "How to monitor Java programs? There are usually the following methods: Actuator, Micrometer, JMX, and components exposing metrics themselves. This article introduces the configuration methods and usage scenarios of these methods."
4+
date: 2025-07-30T20:14:23+08:00
5+
lastmod: 2025-07-30T20:14:23+08:00
6+
draft: false
7+
images: []
8+
menu:
9+
docs:
10+
parent: "practice"
11+
weight: 5500
12+
toc: true
13+
---
14+
15+
> For monitoring systems, the strength of basic functions is indeed crucial, but how to implement them in different scenarios is even more critical. In the "Monitoring Practice" chapter, we collect various monitoring practice experiences, which will be categorized by different components. If you have good practical experience with a certain component, you are welcome to submit a PR and attach the link of your article to the corresponding component directory.
16+
17+
How to monitor Java programs? There are usually the following methods:
18+
19+
- Actuator
20+
- Micrometer
21+
- JMX
22+
- Components exposing metrics themselves
23+
24+
## Actuator
25+
26+
If you have a Spring Boot app, you can directly use Actuator to expose metrics. You can use the following prompt to ask GPT:
27+
28+
```
29+
How to enable actuator and expose monitoring metrics in Prometheus format?
30+
```
31+
32+
I have pasted the AI's reply here, which is probably correct. You can try it. If you find any problems, please feel free to submit an issue or PR.
33+
34+
To enable Actuator and expose Prometheus monitoring metrics in a Spring Boot project, you need to complete the following steps:
35+
36+
### 1. Add dependencies
37+
38+
Add the following dependencies to your `pom.xml`:
39+
40+
```xml
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-actuator</artifactId>
44+
</dependency>
45+
<dependency>
46+
<groupId>io.micrometer</groupId>
47+
<artifactId>micrometer-registry-prometheus</artifactId>
48+
</dependency>
49+
```
50+
51+
If you are using Gradle:
52+
53+
```groovy
54+
implementation 'org.springframework.boot:spring-boot-starter-actuator'
55+
implementation 'io.micrometer:micrometer-registry-prometheus'
56+
```
57+
58+
### 2. Configure application.properties or application.yml
59+
60+
Ensure that Actuator endpoints and the Prometheus metrics endpoint are exposed. For example:
61+
62+
```properties
63+
management.endpoints.web.exposure.include=*
64+
management.endpoint.prometheus.enabled=true
65+
management.metrics.export.prometheus.enabled=true
66+
```
67+
68+
Or in YAML format:
69+
70+
```yaml
71+
management:
72+
endpoints:
73+
web:
74+
exposure:
75+
include: "*"
76+
endpoint:
77+
prometheus:
78+
enabled: true
79+
metrics:
80+
export:
81+
prometheus:
82+
enabled: true
83+
```
84+
85+
### 3. Start the application
86+
87+
After starting your Spring Boot application, Actuator's Prometheus endpoint will be exposed at `/actuator/prometheus` by default.
88+
89+
### 4. Verify the metrics endpoint
90+
91+
Access it using a browser or curl:
92+
93+
```
94+
http://localhost:8080/actuator/prometheus
95+
```
96+
97+
You will see monitoring metrics in Prometheus format, which can be scraped using Prometheus or Categraf.
98+
99+
**Note:**
100+
101+
- If you want to expose only some endpoints, such as `prometheus` and `health`, you can adjust the configuration to `management.endpoints.web.exposure.include=prometheus,health`
102+
- For more advanced configurations (such as custom metrics), you can inject `MeterRegistry` in the code and register custom metrics.
103+
104+
## Micrometer
105+
106+
If you are not using a Spring Boot app, you can also use Micrometer to expose monitoring metrics in Prometheus format. Micrometer is an independent library that can be integrated with any Java application. Actuator is actually based on Micrometer. You can use the following prompt to ask GPT:
107+
108+
```
109+
If actuator is not used and only micrometer is used, how to export metrics to Prometheus?
110+
```
111+
112+
I have pasted the AI's reply here, which is probably correct. You can try it. If you find any problems, please feel free to submit an issue or PR.
113+
114+
### 1. Add Micrometer and Prometheus dependencies
115+
116+
Taking Maven as an example:
117+
118+
```xml
119+
<dependency>
120+
<groupId>io.micrometer</groupId>
121+
<artifactId>micrometer-core</artifactId>
122+
</dependency>
123+
<dependency>
124+
<groupId>io.micrometer</groupId>
125+
<artifactId>micrometer-registry-prometheus</artifactId>
126+
</dependency>
127+
```
128+
129+
### 2. Register PrometheusMeterRegistry
130+
131+
In your application code, manually create and register `PrometheusMeterRegistry`, for example:
132+
133+
```java
134+
import io.micrometer.prometheus.PrometheusMeterRegistry;
135+
import io.micrometer.prometheus.PrometheusConfig;
136+
import io.micrometer.core.instrument.MeterRegistry;
137+
138+
PrometheusMeterRegistry prometheusRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
139+
140+
// You can register custom metrics through MeterRegistry
141+
// prometheusRegistry.counter("my_custom_counter").increment();
142+
```
143+
144+
### 3. Expose Prometheus metrics HTTP interface
145+
146+
Micrometer does not automatically expose an HTTP interface. You need to **implement an HTTP endpoint yourself**, such as using Spring MVC or other web frameworks:
147+
148+
```java
149+
import org.springframework.web.bind.annotation.GetMapping;
150+
import org.springframework.web.bind.annotation.RestController;
151+
152+
@RestController
153+
public class PrometheusController {
154+
private final PrometheusMeterRegistry prometheusRegistry;
155+
156+
public PrometheusController(PrometheusMeterRegistry prometheusRegistry) {
157+
this.prometheusRegistry = prometheusRegistry;
158+
}
159+
160+
@GetMapping("/prometheus")
161+
public String scrape() {
162+
return prometheusRegistry.scrape();
163+
}
164+
}
165+
```
166+
167+
Alternatively, if it is not a Spring project, you can use a web server like Jetty, Undertow, or Netty to directly expose the `/prometheus` path and use the content of `prometheusRegistry.scrape()` as the response. Finally, use Prometheus or Categraf to scrape this endpoint.
168+
169+
170+
## JMX
171+
172+
If the Java program you want to monitor is not a self-developed program but an open-source component, such as Tomcat, Kafka, Zookeeper, etc., these components usually expose monitoring metrics through JMX. You can use the following prompt to ask GPT:
173+
174+
```
175+
How to collect monitoring metrics for ordinary Java middleware such as Tomcat and Kafka?
176+
```
177+
178+
1. It is recommended to use [JMX Exporter](https://github.com/prometheus/jmx_exporter), which is a jar package that runs as a javaagent.
179+
2. Download the jmx_exporter jar package.
180+
3. Find the component's startup command and add javaagent-related parameters to the startup parameters, such as `-javaagent:/path/to/jmx_prometheus_javaagent-<version>.jar=PORT:/path/to/config.yaml` to specify the path of the jmx_exporter jar, the port to expose metrics, and the path of the configuration file.
181+
182+
This will expose Prometheus metrics on the specified port (e.g., `http://localhost:PORT/metrics`). Then use Prometheus or Categraf to scrape this endpoint.
183+
184+
However, note that different components require different configuration files. JMX Exporter provides many examples at the specific address: [https://github.com/prometheus/jmx_exporter/tree/main/examples](https://github.com/prometheus/jmx_exporter/tree/main/examples). You can continue to ask AI:
185+
186+
- What do the configuration items in config.yaml mean?
187+
- What is a Java MBean?
188+
- How to configure JMX Exporter to collect a specific MBean?
189+
- Throw the sample configuration provided by jmx_exporter to AI and let AI help you analyze the specific meaning of this configuration.
190+
191+
## Components exposing metrics themselves
192+
193+
Some components have built-in ways to expose monitoring metrics. For example, Tomcat can display various monitoring metrics on the HTTP endpoint `/manager/status/all`. The Tomcat collection plugin provided by Categraf collects monitoring metrics based on this endpoint. The configuration method is:
194+
195+
1. Modify tomcat-users.xml and add the following content, which is equivalent to creating a user to access the `/manager/status/all` endpoint:
196+
197+
```xml
198+
<role rolename="admin-gui" />
199+
<user username="tomcat" password="s3cret" roles="manager-gui" />
200+
```
201+
202+
2. Comment out the following content in the file `webapps/manager/META-INF/context.xml`:
203+
204+
```xml
205+
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
206+
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
207+
```
208+
209+
3. Configure the Tomcat collection address and authentication information in Categraf's `conf/input.tomcat/tomcat.toml`.
210+
211+
> Note: The JMX method is universal, but the way each component exposes metrics varies. The above is just an example with Tomcat. For other components, you need to refer to their respective documents.

0 commit comments

Comments
 (0)