6

我们可以通过Sentry来收集线上错误日志并进行告警、监控及任务分配处理

初始化项目

通过Spring initializr初始化项目。

依赖:

  • Spring Web

集成Sentry

pom.xml中dependencies节点下添加如下配置

<dependency>
    <groupId>io.sentry</groupId>
    <artifactId>sentry-spring</artifactId>
    <version>1.7.30</version>
</dependency>

添加配置文件SentryConfig.java

@Configuration
public class SentryConfig {

    @Bean
    public HandlerExceptionResolver sentryExceptionResolver() {
        return new io.sentry.spring.SentryExceptionResolver();
    }

    @Bean
    public ServletContextInitializer sentryServletContextInitializer() {
        return new io.sentry.spring.SentryServletContextInitializer();
    }

}

添加测试的DefaultController.java

package com.bohanzhang.sentry;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DefaultController {

    @GetMapping
    public void index() {
        throw new NullPointerException();
    }
}

打包Spring-boot程序后启动

$ ./mvnw package
$ java -jar target/sentry-0.0.1-SNAPSHOT.jar
 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.6.RELEASE)

2020-04-08 09:36:53.611  INFO 55482 --- [           main] com.bohanzhang.sentry.SentryApplication  : Starting SentryApplication v0.0.1-SNAPSHOT on zhangbohans-MacBook-Pro.local with PID 55482 (/Users/bohan/Downloads/sentry/target/sentry-0.0.1-SNAPSHOT.jar started by bohan in /Users/bohan/Downloads/sentry)
2020-04-08 09:36:53.613  INFO 55482 --- [           main] com.bohanzhang.sentry.SentryApplication  : No active profile set, falling back to default profiles: default
2020-04-08 09:36:54.350  INFO 55482 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-04-08 09:36:54.361  INFO 55482 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-04-08 09:36:54.361  INFO 55482 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.33]
2020-04-08 09:36:54.418  INFO 55482 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-04-08 09:36:54.418  INFO 55482 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 765 ms
2020-04-08 09:36:54.549  INFO 55482 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-04-08 09:36:54.680  INFO 55482 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-04-08 09:36:54.683  INFO 55482 --- [           main] com.bohanzhang.sentry.SentryApplication  : Started SentryApplication in 1.34 seconds (JVM running for 1.678)

启动后访问:http://127.0.0.1:8080报错,我们就完成了Sentry的基本集成,查看日志发现由于我们没有配置DSN,Sentry不会做任何操作

*** Couldn't find a suitable DSN, Sentry operations will do nothing! See documentation: https://docs.sentry.io/clients/java/ ***

我们在官网sentry.io中注册登录后创建我们的Sentry项目

创建成功后在配置中找到DNS,并复制

配置DSN有以下种方法

sentry.properties中设置

dsn=https://public:private@host:port/1

在Java属性中配置

$ java -Dsentry.dsn=https://public:private@host:port/1 -jar target/sentry-0.0.1-SNAPSHOT.jar

在环境变量中配置

SENTRY_DSN=https://public:private@host:port/1 java -jar target/sentry-0.0.1-SNAPSHOT.jar

在代码中配置

import io.sentry.Sentry;

Sentry.init("https://public:private@host:port/1");

我们成功收集到了第一条错误

源码

该项目源码已同步至github,有感兴趣的小伙伴可以自取SpringBootSentryDemo


张伯函
4.3k 声望164 粉丝

我本楚狂人,凤歌笑孔丘