forked from yudaocode/SpringBoot-Labs
-
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
YunaiV
committed
Mar 26, 2020
1 parent
ec7cb88
commit 1e5d414
Showing
22 changed files
with
366 additions
and
3 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
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
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
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
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,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> |
7 changes: 7 additions & 0 deletions
7
lab-50/lab-50-demo/src/main/java/cn/iocoder/springboot/lab50/maildemo/Application.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,7 @@ | ||
package cn.iocoder.springboot.lab50.maildemo; | ||
|
||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class Application { | ||
} |
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,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 |
33 changes: 33 additions & 0 deletions
33
lab-50/lab-50-demo/src/test/java/cn/iocoder/springboot/lab50/maildemo/ApplicationTests.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,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); | ||
} | ||
|
||
} |
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 @@ | ||
<?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> |
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,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> |
13 changes: 13 additions & 0 deletions
13
...-sentry-logback/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/DemoApplication.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,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); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
...gback/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/controller/DemoController.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,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("直接抛出异常"); | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
...entry-logback/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/core/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 @@ | ||
package cn.iocoder.springboot.lab51.sentrydemo.core; |
30 changes: 30 additions & 0 deletions
30
...src/main/java/cn/iocoder/springboot/lab51/sentrydemo/core/web/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,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
29
lab-51/lab-51-sentry-logback/src/main/resources/logback-spring.xml
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,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> |
7 changes: 7 additions & 0 deletions
7
lab-51/lab-51-sentry-logback/src/main/resources/sentry.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,7 @@ | ||
# Sentry 配置文件。每个配置 key,从 DefaultSentryClientFactory 类中寻找 | ||
# DSN | ||
dsn=http://[email protected]:9000/3 | ||
# HTTP 请求建立连接超时时间 | ||
timeout=60000 | ||
# HTTP 请求等待结果超时时间 | ||
readtimeout=60000 |
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,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> |
14 changes: 14 additions & 0 deletions
14
...1-sentry-spring/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/DemoApplication.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,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); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
...pring/src/main/java/cn/iocoder/springboot/lab51/sentrydemo/controller/DemoController.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,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("直接抛出异常"); | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
lab-51/lab-51-sentry-spring/src/main/resources/application.yml
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,7 @@ | ||
# Sentry 配置项,对应 SentryProperties 配置类 | ||
sentry: | ||
dsn: http://[email protected]:9000/3 # DSN | ||
timeout: 60000 # HTTP 请求建立连接超时时间 | ||
|
||
server: | ||
port: 18080 |
Oops, something went wrong.