forked from xuwujing/springBoot-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
239 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<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>1.0.0</groupId> | ||
<artifactId>springboot-prometheus</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>springboot-prometheus</name> | ||
<url>http://maven.apache.org</url> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
<java.version>1.8</java.version> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
</properties> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.1.3.RELEASE</version> | ||
<relativePath/> | ||
</parent> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-actuator</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.micrometer</groupId> | ||
<artifactId>micrometer-registry-prometheus</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-devtools</artifactId> | ||
<optional>true</optional> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<defaultGoal>compile</defaultGoal> | ||
<sourceDirectory>src</sourceDirectory> | ||
<finalName>springboot-prometheus</finalName> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin </artifactId> | ||
<configuration> | ||
<fork>true</fork> | ||
<mainClass>com.pancm.App</mainClass> | ||
</configuration> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>repackage</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
42 changes: 42 additions & 0 deletions
42
springboot-prometheus/src/main/java/com/pancm/ActuatorApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.pancm; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
|
||
/** | ||
* | ||
* @Title: ActuatorApp | ||
* @Description: | ||
启动程序之后,在浏览器输入: http://localhost:8284/actuator 加上 | ||
1. /autoconfig 可以得到配置生效信息 | ||
2. /configprops 可以得到属性的内容和默认值 | ||
3. /beans 可 以得到bean的别名、类型、是否单例、类的地址、依赖等信息 | ||
4. /dump 可 以得到线程名、线程ID、线程的状态、是否等待锁资源等信息 | ||
5. /env 可以得到环境变量、JVM 属性、命令行参数、项目使用的jar包等信息 | ||
5.1 /sun.boot.library.path 可以得到JDK安装路径 | ||
6. /health 可以得到磁盘检测和数据库检测等信息 | ||
7. /mappings 可以得到全部的URI路径,以及它们和控制器的映射关系 | ||
8. /metrics 可以得到JVM内容使用、GC情况、类加载信息 | ||
8.1 /gc.* 可以得到GC相关信息 | ||
8.2 /mem.* 可以得到内存信息 | ||
... | ||
9. /info 可以得到自定义的配置信息 | ||
10. /shutdown 可以进行关闭程序 post请求 | ||
11. /trace 可以得到所Web请求的详细信息 | ||
12./prometheus 可以通过Prometheus查看信息 | ||
* @Version:1.0.0 | ||
* @author pancm | ||
* @date 2019年1月17日 | ||
*/ | ||
@SpringBootApplication | ||
public class ActuatorApp | ||
{ | ||
public static void main( String[] args ) | ||
{ | ||
SpringApplication.run(ActuatorApp.class, args); | ||
System.out.println("Actuator启动成功!"); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
springboot-prometheus/src/main/java/com/pancm/config/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.pancm.config; | ||
|
||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
import javax.annotation.Resource; | ||
|
||
/** | ||
* @Author pancm | ||
* @Description 全局异常捕获 | ||
* @Date 2020/8/18 | ||
* @Param | ||
* @return | ||
**/ | ||
@ControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
@Resource | ||
private PrometheusCustomMonitor monitor; | ||
|
||
@ResponseBody | ||
@ExceptionHandler(value = Exception.class) | ||
public String handle(Exception e) { | ||
monitor.getRequestErrorCount().increment(); | ||
return "error, count:"+monitor.getRequestErrorCount().count()+" message: " + e.getMessage(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
springboot-prometheus/src/main/java/com/pancm/config/PrometheusCustomMonitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.pancm.config; | ||
|
||
import io.micrometer.core.instrument.Counter; | ||
import io.micrometer.core.instrument.DistributionSummary; | ||
import io.micrometer.core.instrument.MeterRegistry; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.annotation.PostConstruct; | ||
|
||
|
||
/** | ||
* @Author pancm | ||
* @Description | ||
* @Date 2020/8/18 | ||
* @Param | ||
* @return | ||
**/ | ||
@Component | ||
public class PrometheusCustomMonitor { | ||
|
||
/** | ||
* 记录请求出错次数 | ||
*/ | ||
private Counter requestErrorCount; | ||
private final MeterRegistry registry; | ||
@Autowired | ||
public PrometheusCustomMonitor(MeterRegistry registry) { | ||
this.registry = registry; | ||
} | ||
@PostConstruct | ||
private void init() { | ||
requestErrorCount = registry.counter("requests_error_total", "status", "error"); | ||
} | ||
public Counter getRequestErrorCount() { | ||
return requestErrorCount; | ||
} | ||
|
||
|
||
} |
25 changes: 25 additions & 0 deletions
25
springboot-prometheus/src/main/java/com/pancm/web/HelloWorldController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.pancm.web; | ||
|
||
import com.pancm.config.PrometheusCustomMonitor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.util.StringUtils; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
public class HelloWorldController { | ||
|
||
@Autowired | ||
PrometheusCustomMonitor prometheusCustomMonitor; | ||
|
||
|
||
@RequestMapping("/hello/{name}") | ||
public String index(@PathVariable String name) throws Exception { | ||
if(!"pancm".equals(name)){ | ||
throw new Exception("出错了"); | ||
} | ||
return "Hello " + name; | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
springboot-prometheus/src/main/java/com/pancm/web/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* @Title: package-info | ||
* @Description: | ||
* @Version:1.0.0 | ||
* @author pancm | ||
* @date 2019年1月17日 | ||
*/ | ||
package com.pancm.web; |
19 changes: 19 additions & 0 deletions
19
springboot-prometheus/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
spring.port = 8284 | ||
spring.application.name=@project.name@ | ||
# /health端点 暴露详细信息 | ||
management.endpoint.health.show-details=always | ||
# "*" 代表暴露所有的端点 如果指定多个端点,用","分开 | ||
management.endpoints.web.exposure.include=* | ||
# 赋值规则同上 | ||
management.endpoints.web.exposure.exclude= | ||
# 正式应用 慎重启用 | ||
management.endpoint.shutdown.enabled=true | ||
# 为指标设置一个名为的Tag,Tag是Prometheus提供的一种能力,从而实现更加灵活的筛选。 | ||
management.metrics.tags.application=${spring.application.name} | ||
|
||
info.app.encoding=@project.build.sourceEncoding@ | ||
info.app.java.source=@java.version@ | ||
info.app.java.target=@java.version@ | ||
info.app.name=@project.name@ | ||
info.app.description=@project.description@ | ||
info.app.version=@project.version@ |