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
Jan 7, 2020
1 parent
cdef8e8
commit f93680d
Showing
29 changed files
with
950 additions
and
19 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
3 changes: 1 addition & 2 deletions
3
lab-40/lab-40-demo/src/main/java/cn/iocoder/springboot/lab40/zipkindemo/Application2.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
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,61 @@ | ||
<?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-40-logback</artifactId> | ||
|
||
<dependencies> | ||
<!-- 实现对 SpringMVC 的自动化配置 --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<!-- Brave 核心库 --> | ||
<!-- The below are needed to report traces to http://localhost:9411/api/v2/spans --> | ||
<dependency> | ||
<groupId>io.zipkin.brave</groupId> | ||
<artifactId>brave</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.zipkin.reporter2</groupId> | ||
<artifactId>zipkin-sender-okhttp3</artifactId> | ||
</dependency> | ||
|
||
<!-- Adds the MVC class and method names to server spans --> | ||
<!-- Brave 对 Spring MVC 的支持 --> | ||
<dependency> | ||
<groupId>io.zipkin.brave</groupId> | ||
<artifactId>brave-instrumentation-spring-webmvc</artifactId> | ||
</dependency> | ||
<!-- Integrates so you can use log patterns like %X{traceId}/%X{spanId} --> | ||
<!-- Brave 对 SLF4J 的支持 --> | ||
<dependency> | ||
<groupId>io.zipkin.brave</groupId> | ||
<artifactId>brave-context-slf4j</artifactId> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<dependencyManagement> | ||
<!-- Brave Bom 文件 --> | ||
<dependencies> | ||
<dependency> | ||
<groupId>io.zipkin.brave</groupId> | ||
<artifactId>brave-bom</artifactId> | ||
<version>5.9.1</version> | ||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
</project> |
13 changes: 13 additions & 0 deletions
13
...b-40-logback/src/main/java/cn/iocoder/springboot/lab40/zipkindemo/LogbackApplication.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.lab40.zipkindemo; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class LogbackApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(LogbackApplication.class, args); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
...k/src/main/java/cn/iocoder/springboot/lab40/zipkindemo/config/SpringMvcConfiguration.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 cn.iocoder.springboot.lab40.zipkindemo.config; | ||
|
||
import brave.spring.webmvc.SpanCustomizingAsyncHandlerInterceptor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
@Import(SpanCustomizingAsyncHandlerInterceptor.class) | ||
public class SpringMvcConfiguration implements WebMvcConfigurer { | ||
|
||
@Autowired | ||
public SpanCustomizingAsyncHandlerInterceptor webMvcTracingCustomizer; | ||
|
||
/** | ||
* Decorates server spans with application-defined web tags | ||
*/ | ||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
registry.addInterceptor(webMvcTracingCustomizer); | ||
} | ||
|
||
} |
84 changes: 84 additions & 0 deletions
84
...back/src/main/java/cn/iocoder/springboot/lab40/zipkindemo/config/ZipkinConfiguration.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,84 @@ | ||
package cn.iocoder.springboot.lab40.zipkindemo.config; | ||
|
||
import brave.CurrentSpanCustomizer; | ||
import brave.SpanCustomizer; | ||
import brave.Tracing; | ||
import brave.context.slf4j.MDCScopeDecorator; | ||
import brave.http.HttpTracing; | ||
import brave.propagation.ThreadLocalCurrentTraceContext; | ||
import brave.servlet.TracingFilter; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import zipkin2.Span; | ||
import zipkin2.reporter.AsyncReporter; | ||
import zipkin2.reporter.Sender; | ||
import zipkin2.reporter.okhttp3.OkHttpSender; | ||
|
||
import javax.servlet.Filter; | ||
|
||
@Configuration | ||
public class ZipkinConfiguration { | ||
|
||
// ==================== 通用配置 ==================== | ||
|
||
/** | ||
* Configuration for how to send spans to Zipkin | ||
*/ | ||
@Bean | ||
public Sender sender() { | ||
return OkHttpSender.create("http://127.0.0.1:9411/api/v2/spans"); | ||
} | ||
|
||
/** | ||
* Configuration for how to buffer spans into messages for Zipkin | ||
*/ | ||
@Bean | ||
public AsyncReporter<Span> spanReporter() { | ||
return AsyncReporter.create(sender()); | ||
} | ||
|
||
/** | ||
* Controls aspects of tracing such as the service name that shows up in the UI | ||
*/ | ||
@Bean | ||
public Tracing tracing(@Value("${spring.application.name}") String serviceName) { | ||
return Tracing.newBuilder() | ||
.localServiceName(serviceName) | ||
.currentTraceContext(ThreadLocalCurrentTraceContext.newBuilder() | ||
.addScopeDecorator(MDCScopeDecorator.create()) // puts trace IDs into logs | ||
.build() | ||
) | ||
.spanReporter(spanReporter()).build(); | ||
} | ||
|
||
/** | ||
* Allows someone to add tags to a span if a trace is in progress | ||
*/ | ||
@Bean | ||
public SpanCustomizer spanCustomizer(Tracing tracing) { | ||
return CurrentSpanCustomizer.create(tracing); | ||
} | ||
|
||
// ==================== HTTP 相关 ==================== | ||
|
||
/** | ||
* Decides how to name and tag spans. By default they are named the same as the http method | ||
*/ | ||
@Bean | ||
public HttpTracing httpTracing(Tracing tracing) { | ||
return HttpTracing.create(tracing); | ||
} | ||
|
||
/** | ||
* Creates server spans for http requests | ||
*/ | ||
@Bean | ||
public Filter tracingFilter(HttpTracing httpTracing) { | ||
return TracingFilter.create(httpTracing); | ||
} | ||
|
||
// ==================== SpringMVC 相关 ==================== | ||
// @see SpringMvcConfiguration 类上的,@Import(SpanCustomizingAsyncHandlerInterceptor.class) | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
...gback/src/main/java/cn/iocoder/springboot/lab40/zipkindemo/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,21 @@ | ||
package cn.iocoder.springboot.lab40.zipkindemo.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("/logback") | ||
public String echo() { | ||
logger.info("测试日志"); | ||
return "logback"; | ||
} | ||
|
||
} |
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 @@ | ||
spring: | ||
application: | ||
name: demo-application-springmvc | ||
|
||
logging: | ||
pattern: | ||
console: "%clr(%d{${LOG_DATEFORMAT_PATTERN:yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %X{traceId}/%X{spanId} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:%wEx}" | ||
file: "%d{${LOG_DATEFORMAT_PATTERN:yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } %X{traceId}/%X{spanId} --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:%wEx}" |
Oops, something went wrong.