From 1e5d4145feb5ece9c79b7fa89ecf472d750a4148 Mon Sep 17 00:00:00 2001
From: YunaiV <>
Date: Thu, 26 Mar 2020 22:13:16 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=20spring=20boot=20x=20sentry?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 4 +++
lab-34/lab-34-actuator-demo-info/pom.xml | 2 +-
lab-34/lab-34-actuator-demo/pom.xml | 2 +-
lab-34/lab-34-actuator-test/pom.xml | 2 +-
lab-50/lab-50-demo/pom.xml | 30 ++++++++++++++++
.../lab50/maildemo/Application.java | 7 ++++
.../src/main/resources/application.yml | 30 ++++++++++++++++
.../lab50/maildemo/ApplicationTests.java | 33 +++++++++++++++++
lab-50/pom.xml | 19 ++++++++++
lab-51/lab-51-sentry-logback/pom.xml | 30 ++++++++++++++++
.../lab51/sentrydemo/DemoApplication.java | 13 +++++++
.../sentrydemo/controller/DemoController.java | 27 ++++++++++++++
.../lab51/sentrydemo/core/package-info.java | 1 +
.../core/web/GlobalExceptionHandler.java | 30 ++++++++++++++++
.../src/main/resources/logback-spring.xml | 29 +++++++++++++++
.../src/main/resources/sentry.properties | 7 ++++
lab-51/lab-51-sentry-spring/pom.xml | 24 +++++++++++++
.../lab51/sentrydemo/DemoApplication.java | 14 ++++++++
.../sentrydemo/controller/DemoController.java | 36 +++++++++++++++++++
.../src/main/resources/application.yml | 7 ++++
lab-51/pom.xml | 20 +++++++++++
pom.xml | 2 ++
22 files changed, 366 insertions(+), 3 deletions(-)
create mode 100644 lab-50/lab-50-demo/pom.xml
create mode 100644 lab-50/lab-50-demo/src/main/java/cn/iocoder/springboot/lab50/maildemo/Application.java
create mode 100644 lab-50/lab-50-demo/src/main/resources/application.yml
create mode 100644 lab-50/lab-50-demo/src/test/java/cn/iocoder/springboot/lab50/maildemo/ApplicationTests.java
create mode 100644 lab-50/pom.xml
create mode 100644 lab-51/lab-51-sentry-logback/pom.xml
create mode 100644 lab-51/lab-51-sentry-logback/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/DemoApplication.java
create mode 100644 lab-51/lab-51-sentry-logback/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/controller/DemoController.java
create mode 100644 lab-51/lab-51-sentry-logback/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/core/package-info.java
create mode 100644 lab-51/lab-51-sentry-logback/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/core/web/GlobalExceptionHandler.java
create mode 100644 lab-51/lab-51-sentry-logback/src/main/resources/logback-spring.xml
create mode 100644 lab-51/lab-51-sentry-logback/src/main/resources/sentry.properties
create mode 100644 lab-51/lab-51-sentry-spring/pom.xml
create mode 100644 lab-51/lab-51-sentry-spring/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/DemoApplication.java
create mode 100644 lab-51/lab-51-sentry-spring/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/controller/DemoController.java
create mode 100644 lab-51/lab-51-sentry-spring/src/main/resources/application.yml
create mode 100644 lab-51/pom.xml
diff --git a/README.md b/README.md
index dab96240..e5f8a57a 100644
--- a/README.md
+++ b/README.md
@@ -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)
## 链路追踪
@@ -267,3 +268,6 @@
Spring Boot 优雅关闭示例。
+# lab-50
+
+Email 示例
diff --git a/lab-34/lab-34-actuator-demo-info/pom.xml b/lab-34/lab-34-actuator-demo-info/pom.xml
index a5834d77..7583d9d9 100644
--- a/lab-34/lab-34-actuator-demo-info/pom.xml
+++ b/lab-34/lab-34-actuator-demo-info/pom.xml
@@ -10,7 +10,7 @@
4.0.0
- lab-34-acturator-demo-info
+ lab-34-actuator-demo-info
diff --git a/lab-34/lab-34-actuator-demo/pom.xml b/lab-34/lab-34-actuator-demo/pom.xml
index 887ee24a..5fa9fe2d 100644
--- a/lab-34/lab-34-actuator-demo/pom.xml
+++ b/lab-34/lab-34-actuator-demo/pom.xml
@@ -10,7 +10,7 @@
4.0.0
- lab-34-acturator-demo
+ lab-34-actuator-demo
diff --git a/lab-34/lab-34-actuator-test/pom.xml b/lab-34/lab-34-actuator-test/pom.xml
index 84f2b53f..0b87ad69 100644
--- a/lab-34/lab-34-actuator-test/pom.xml
+++ b/lab-34/lab-34-actuator-test/pom.xml
@@ -10,7 +10,7 @@
4.0.0
- lab-34-acturator-test
+ lab-34-actuator-test
diff --git a/lab-50/lab-50-demo/pom.xml b/lab-50/lab-50-demo/pom.xml
new file mode 100644
index 00000000..a9d20f91
--- /dev/null
+++ b/lab-50/lab-50-demo/pom.xml
@@ -0,0 +1,30 @@
+
+
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 2.2.2.RELEASE
+
+
+ 4.0.0
+
+ lab-50-demo
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-mail
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+
+
diff --git a/lab-50/lab-50-demo/src/main/java/cn/iocoder/springboot/lab50/maildemo/Application.java b/lab-50/lab-50-demo/src/main/java/cn/iocoder/springboot/lab50/maildemo/Application.java
new file mode 100644
index 00000000..7c0b88db
--- /dev/null
+++ b/lab-50/lab-50-demo/src/main/java/cn/iocoder/springboot/lab50/maildemo/Application.java
@@ -0,0 +1,7 @@
+package cn.iocoder.springboot.lab50.maildemo;
+
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+public class Application {
+}
diff --git a/lab-50/lab-50-demo/src/main/resources/application.yml b/lab-50/lab-50-demo/src/main/resources/application.yml
new file mode 100644
index 00000000..bcbcb2d8
--- /dev/null
+++ b/lab-50/lab-50-demo/src/main/resources/application.yml
@@ -0,0 +1,30 @@
+#spring:
+# mail: # 配置发送告警的邮箱
+# host: smtp.163.com
+# port: 465
+# protocol: smtps
+# username: yudaoyuanma@163.com
+# 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: yunai.wang@medcloud.cn
+ password: '***'
+ default-encoding: UTF-8
+ properties:
+ smtp:
+ auth: true
+ starttls:
+ enable: true
+ required: true
diff --git a/lab-50/lab-50-demo/src/test/java/cn/iocoder/springboot/lab50/maildemo/ApplicationTests.java b/lab-50/lab-50-demo/src/test/java/cn/iocoder/springboot/lab50/maildemo/ApplicationTests.java
new file mode 100644
index 00000000..4b24a04d
--- /dev/null
+++ b/lab-50/lab-50-demo/src/test/java/cn/iocoder/springboot/lab50/maildemo/ApplicationTests.java
@@ -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("7685413@qq.com");
+ message.setSubject("我是测试主题");
+ message.setText("我是测试内容");
+
+ mailSender.send(message);
+ }
+
+}
diff --git a/lab-50/pom.xml b/lab-50/pom.xml
new file mode 100644
index 00000000..71e8efc7
--- /dev/null
+++ b/lab-50/pom.xml
@@ -0,0 +1,19 @@
+
+
+
+ labs-parent
+ cn.iocoder.springboot.labs
+ 1.0-SNAPSHOT
+
+ 4.0.0
+
+ lab-50
+ pom
+
+ lab-50-demo
+
+
+
+
diff --git a/lab-51/lab-51-sentry-logback/pom.xml b/lab-51/lab-51-sentry-logback/pom.xml
new file mode 100644
index 00000000..8f4af3a0
--- /dev/null
+++ b/lab-51/lab-51-sentry-logback/pom.xml
@@ -0,0 +1,30 @@
+
+
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 2.2.2.RELEASE
+
+
+ 4.0.0
+
+ lab-51-sentry-logback
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+
+
+ io.sentry
+ sentry-logback
+ 1.7.30
+
+
+
+
diff --git a/lab-51/lab-51-sentry-logback/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/DemoApplication.java b/lab-51/lab-51-sentry-logback/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/DemoApplication.java
new file mode 100644
index 00000000..0c31346f
--- /dev/null
+++ b/lab-51/lab-51-sentry-logback/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/DemoApplication.java
@@ -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);
+ }
+
+}
diff --git a/lab-51/lab-51-sentry-logback/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/controller/DemoController.java b/lab-51/lab-51-sentry-logback/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/controller/DemoController.java
new file mode 100644
index 00000000..2c2efe2b
--- /dev/null
+++ b/lab-51/lab-51-sentry-logback/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/controller/DemoController.java
@@ -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("直接抛出异常");
+ }
+
+}
diff --git a/lab-51/lab-51-sentry-logback/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/core/package-info.java b/lab-51/lab-51-sentry-logback/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/core/package-info.java
new file mode 100644
index 00000000..a0de6bb9
--- /dev/null
+++ b/lab-51/lab-51-sentry-logback/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/core/package-info.java
@@ -0,0 +1 @@
+package cn.iocoder.springboot.lab51.sentrydemo.core;
diff --git a/lab-51/lab-51-sentry-logback/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/core/web/GlobalExceptionHandler.java b/lab-51/lab-51-sentry-logback/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/core/web/GlobalExceptionHandler.java
new file mode 100644
index 00000000..8b800d85
--- /dev/null
+++ b/lab-51/lab-51-sentry-logback/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/core/web/GlobalExceptionHandler.java
@@ -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 "系统异常";
+ }
+
+}
diff --git a/lab-51/lab-51-sentry-logback/src/main/resources/logback-spring.xml b/lab-51/lab-51-sentry-logback/src/main/resources/logback-spring.xml
new file mode 100644
index 00000000..0b9ef7c9
--- /dev/null
+++ b/lab-51/lab-51-sentry-logback/src/main/resources/logback-spring.xml
@@ -0,0 +1,29 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+ WARN
+
+
+
+ %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
+
+
+
+
+
+
+
+
+
+
+
diff --git a/lab-51/lab-51-sentry-logback/src/main/resources/sentry.properties b/lab-51/lab-51-sentry-logback/src/main/resources/sentry.properties
new file mode 100644
index 00000000..cc010474
--- /dev/null
+++ b/lab-51/lab-51-sentry-logback/src/main/resources/sentry.properties
@@ -0,0 +1,7 @@
+# Sentry 配置文件。每个配置 key,从 DefaultSentryClientFactory 类中寻找
+# DSN
+dsn=http://b10468b71cd945358ff88ed975287194@47.56.150.64:9000/3
+# HTTP 请求建立连接超时时间
+timeout=60000
+# HTTP 请求等待结果超时时间
+readtimeout=60000
diff --git a/lab-51/lab-51-sentry-spring/pom.xml b/lab-51/lab-51-sentry-spring/pom.xml
new file mode 100644
index 00000000..2764df1c
--- /dev/null
+++ b/lab-51/lab-51-sentry-spring/pom.xml
@@ -0,0 +1,24 @@
+
+
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 2.2.2.RELEASE
+
+
+ 4.0.0
+
+ lab-51-sentry-spring
+
+
+
+
+ io.sentry
+ sentry-spring-boot-starter
+ 1.7.30
+
+
+
+
diff --git a/lab-51/lab-51-sentry-spring/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/DemoApplication.java b/lab-51/lab-51-sentry-spring/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/DemoApplication.java
new file mode 100644
index 00000000..5dde280e
--- /dev/null
+++ b/lab-51/lab-51-sentry-spring/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/DemoApplication.java
@@ -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);
+ }
+
+}
diff --git a/lab-51/lab-51-sentry-spring/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/controller/DemoController.java b/lab-51/lab-51-sentry-spring/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/controller/DemoController.java
new file mode 100644
index 00000000..c132be3b
--- /dev/null
+++ b/lab-51/lab-51-sentry-spring/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/controller/DemoController.java
@@ -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("直接抛出异常");
+ }
+
+}
diff --git a/lab-51/lab-51-sentry-spring/src/main/resources/application.yml b/lab-51/lab-51-sentry-spring/src/main/resources/application.yml
new file mode 100644
index 00000000..a92789af
--- /dev/null
+++ b/lab-51/lab-51-sentry-spring/src/main/resources/application.yml
@@ -0,0 +1,7 @@
+# Sentry 配置项,对应 SentryProperties 配置类
+sentry:
+ dsn: http://b10468b71cd945358ff88ed975287194@47.56.150.64:9000/3 # DSN
+ timeout: 60000 # HTTP 请求建立连接超时时间
+
+server:
+ port: 18080
diff --git a/lab-51/pom.xml b/lab-51/pom.xml
new file mode 100644
index 00000000..b187569a
--- /dev/null
+++ b/lab-51/pom.xml
@@ -0,0 +1,20 @@
+
+
+
+ labs-parent
+ cn.iocoder.springboot.labs
+ 1.0-SNAPSHOT
+
+ 4.0.0
+
+ lab-51
+ pom
+
+ lab-51-sentry-logback
+ lab-51-sentry-spring
+
+
+
+
diff --git a/pom.xml b/pom.xml
index 45e89ee3..9ceb946e 100644
--- a/pom.xml
+++ b/pom.xml
@@ -75,6 +75,8 @@
labx-12
labx-13
labx-14
+ lab-50
+ lab-51