Skip to content

Commit

Permalink
增加 spring boot x sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed Mar 26, 2020
1 parent ec7cb88 commit 1e5d414
Show file tree
Hide file tree
Showing 22 changed files with 366 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@

* [《芋道 Spring Boot 日志集成 Logging 入门》](http://www.iocoder.cn/Spring-Boot/Logging/?github) 对应 [lab-37](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-37)
* [《芋道 Spring Boot 日志平台 ELK + Filebeat 入门》](http://www.iocoder.cn/Spring-Boot/ELK/?github) 对应 [lab-38](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-38)
* [《芋道 Spring Boot 异常管理平台 Sentry 入门》](http://www.iocoder.cn/Spring-Boot/Sentry/?github) 对应 [lab-51](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-51)

## 链路追踪

Expand Down Expand Up @@ -267,3 +268,6 @@

Spring Boot 优雅关闭示例。

# lab-50

Email 示例
2 changes: 1 addition & 1 deletion lab-34/lab-34-actuator-demo-info/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>lab-34-acturator-demo-info</artifactId>
<artifactId>lab-34-actuator-demo-info</artifactId>

<dependencies>
<!-- 实现对 Spring MVC 的自动化配置 -->
Expand Down
2 changes: 1 addition & 1 deletion lab-34/lab-34-actuator-demo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>lab-34-acturator-demo</artifactId>
<artifactId>lab-34-actuator-demo</artifactId>

<dependencies>
<!-- 实现对 Spring MVC 的自动化配置 -->
Expand Down
2 changes: 1 addition & 1 deletion lab-34/lab-34-actuator-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>lab-34-acturator-test</artifactId>
<artifactId>lab-34-actuator-test</artifactId>

<dependencies>
<!-- 实现对 Spring MVC 的自动化配置 -->
Expand Down
30 changes: 30 additions & 0 deletions lab-50/lab-50-demo/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>lab-50-demo</artifactId>

<dependencies>
<!-- 实现对 Java Mail 的自动化配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

<!-- 方便等会写单元测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cn.iocoder.springboot.lab50.maildemo;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
}
30 changes: 30 additions & 0 deletions lab-50/lab-50-demo/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#spring:
# mail: # 配置发送告警的邮箱
# host: smtp.163.com
# port: 465
# protocol: smtps
# username: [email protected]
# password: '***'
# default-encoding: UTF-8
## properties:
## smtp:
## auth: true
## starttls:
## enable: true
## required: true


spring:
mail: # 配置发送告警的邮箱
host: smtp.exmail.qq.com
port: 465
protocol: smtps
username: [email protected]
password: '***'
default-encoding: UTF-8
properties:
smtp:
auth: true
starttls:
enable: true
required: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cn.iocoder.springboot.lab50.maildemo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class ApplicationTests {

@Autowired
private JavaMailSender mailSender;

@Value("${spring.mail.username}")
private String username;

@Test
public void testSend() {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(username);
message.setTo("[email protected]");
message.setSubject("我是测试主题");
message.setText("我是测试内容");

mailSender.send(message);
}

}
19 changes: 19 additions & 0 deletions lab-50/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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">
<parent>
<artifactId>labs-parent</artifactId>
<groupId>cn.iocoder.springboot.labs</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>lab-50</artifactId>
<packaging>pom</packaging>
<modules>
<module>lab-50-demo</module>
</modules>


</project>
30 changes: 30 additions & 0 deletions lab-51/lab-51-sentry-logback/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>lab-51-sentry-logback</artifactId>

<dependencies>
<!-- 实现对 Spring MVC 的自动化配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- Sentry 对 Logback 的拓展,实现通过打日志的方式,来上传日志到 Sentry 服务 -->
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry-logback</artifactId>
<version>1.7.30</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cn.iocoder.springboot.lab51.sentrydemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cn.iocoder.springboot.lab51.sentrydemo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/demo")
public class DemoController {

private Logger logger = LoggerFactory.getLogger(getClass());

@GetMapping("/sentry")
public String sentry() {
logger.error("[main][我就是展示下异常!]");

return "success";
}

@GetMapping("/exception")
public String exception() {
throw new RuntimeException("直接抛出异常");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package cn.iocoder.springboot.lab51.sentrydemo.core;
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cn.iocoder.springboot.lab51.sentrydemo.core.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice(basePackages = "cn.iocoder.springboot.lab51.sentrydemo.controller")
public class GlobalExceptionHandler {

private Logger logger = LoggerFactory.getLogger(getClass());

// ... 省略其它类型异常的处理

/**
* 处理其它 Exception 异常
*/
@ResponseBody
@ExceptionHandler(value = Exception.class)
public String exceptionHandler(HttpServletRequest req, Exception e) {
// 记录异常日志
logger.error("[exceptionHandler]", e);
// 返回 ERROR CommonResult
return "系统异常";
}

}
29 changes: 29 additions & 0 deletions lab-51/lab-51-sentry-logback/src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>

<configuration>

<!-- 参考 -->
<include resource="org/springframework/boot/logging/logback/defaults.xml" />
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />

<!-- 定义 Sentry Appender -->
<appender name="SentryAppender" class="io.sentry.logback.SentryAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARN</level>
</filter>
<!-- 使用默认的 SentryAppender encoder 格式 -->
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<!-- 日志输出级别 -->
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
<appender-ref ref="SentryAppender" />
</root>

</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Sentry 配置文件。每个配置 key,从 DefaultSentryClientFactory 类中寻找
# DSN
dsn=http://[email protected]:9000/3
# HTTP 请求建立连接超时时间
timeout=60000
# HTTP 请求等待结果超时时间
readtimeout=60000
24 changes: 24 additions & 0 deletions lab-51/lab-51-sentry-spring/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>lab-51-sentry-spring</artifactId>

<dependencies>
<!-- sentry-spring 的 Spring Boot Starter 库,实现它的自动配置 -->
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry-spring-boot-starter</artifactId>
<version>1.7.30</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package cn.iocoder.springboot.lab51.sentrydemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

public static void main(String[] args) throws InterruptedException {
// 启动 Spring Boot 应用
SpringApplication.run(DemoApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cn.iocoder.springboot.lab51.sentrydemo.controller;

import io.sentry.SentryClient;
import io.sentry.event.EventBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/demo")
public class DemoController {

@Autowired
private SentryClient sentryClient;

@GetMapping("/sentry")
public String sentry() {
// 上传消息到 Sentry 中
sentryClient.sendMessage("示例消息");

// 上传异常到 Sentry 中
sentryClient.sendException(new RuntimeException("测试异常"));

// 上传事件到 Sentry 中
sentryClient.sendEvent(new EventBuilder().withMessage("示例事件").build());

return "success";
}

@GetMapping("/exception")
public String exception() {
throw new RuntimeException("直接抛出异常");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Sentry 配置项,对应 SentryProperties 配置类
sentry:
dsn: http://[email protected]:9000/3 # DSN
timeout: 60000 # HTTP 请求建立连接超时时间

server:
port: 18080
Loading

0 comments on commit 1e5d414

Please sign in to comment.