style: 优化部分代码及注释格式

This commit is contained in:
Charles7c 2024-01-31 00:35:53 +08:00
parent c03c082d2e
commit 8fe85e3a90
10 changed files with 60 additions and 64 deletions

View File

@ -108,7 +108,7 @@
<artifactId>continew-starter-json-jackson</artifactId> <artifactId>continew-starter-json-jackson</artifactId>
</dependency> </dependency>
<!--轻量级的分布式日志标记追踪神器--> <!-- TLog轻量级的分布式日志标记追踪神器 -->
<dependency> <dependency>
<groupId>com.yomahub</groupId> <groupId>com.yomahub</groupId>
<artifactId>tlog-web-spring-boot-starter</artifactId> <artifactId>tlog-web-spring-boot-starter</artifactId>

View File

@ -16,16 +16,11 @@
package top.charles7c.continew.admin.common.config.properties; package top.charles7c.continew.admin.common.config.properties;
import java.awt.*;
import lombok.Data; import lombok.Data;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.wf.captcha.*;
/** /**
* 验证码配置属性 * 验证码配置属性
* *

View File

@ -18,24 +18,38 @@ package top.charles7c.continew.admin.common.config.properties;
import lombok.Data; import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/** /**
* TLog 配置属性 * TLog 配置属性
* *
* <p>
* 重写 TLog 配置以适配 Spring Boot 3.x
* </p>
*
* @author Jasmine * @author Jasmine
* @since 2024/01/30 11:39 * @since 2024/1/30 11:39
*/ */
@Data @Data
@Component
@ConfigurationProperties(prefix = "tlog") @ConfigurationProperties(prefix = "tlog")
public class TLogProperties { public class TLogProperties {
/** 日志标签模板 */
/**
* 日志标签模板
*/
private String pattern; private String pattern;
/** 自动打印调用参数和时间 */
/**
* 自动打印调用参数和时间
*/
private Boolean enableInvokeTimePrint; private Boolean enableInvokeTimePrint;
/** 自定义TraceId生成器 */
/**
* 自定义 TraceId 生成器
*/
private String idGenerator; private String idGenerator;
/** MDC模式 */
/**
* MDC 模式
*/
private Boolean mdcEnable; private Boolean mdcEnable;
} }

View File

@ -18,6 +18,8 @@ package top.charles7c.continew.admin.common.config.tlog;
import com.yomahub.tlog.id.TLogIdGeneratorLoader; import com.yomahub.tlog.id.TLogIdGeneratorLoader;
import com.yomahub.tlog.spring.TLogPropertyInit; import com.yomahub.tlog.spring.TLogPropertyInit;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; import org.springframework.context.annotation.Primary;
@ -26,19 +28,21 @@ import top.charles7c.continew.admin.common.config.properties.TLogProperties;
/** /**
* TLog 配置 * TLog 配置
* *
* <p>
* 重写 TLog 配置以适配 Spring Boot 3.x
* </p>
*
* @see TLogConfiguration * @see TLogConfiguration
* @author Jasmine * @author Jasmine
* @since 2024/01/30 11:39 * @since 2024/1/30 11:39
*/ */
@Configuration @Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties(TLogProperties.class)
public class TLogConfiguration { public class TLogConfiguration {
private final TLogProperties tLogProperties; private final TLogProperties tLogProperties;
public TLogConfiguration(TLogProperties tLogProperties) {
this.tLogProperties = tLogProperties;
}
@Bean @Bean
@Primary @Primary
public TLogPropertyInit tLogPropertyInit() { public TLogPropertyInit tLogPropertyInit() {
@ -46,8 +50,7 @@ public class TLogConfiguration {
tLogPropertyInit.setPattern(tLogProperties.getPattern()); tLogPropertyInit.setPattern(tLogProperties.getPattern());
tLogPropertyInit.setEnableInvokeTimePrint(tLogProperties.getEnableInvokeTimePrint()); tLogPropertyInit.setEnableInvokeTimePrint(tLogProperties.getEnableInvokeTimePrint());
tLogPropertyInit.setMdcEnable(tLogProperties.getMdcEnable()); tLogPropertyInit.setMdcEnable(tLogProperties.getMdcEnable());
// 设置自定义 TraceId 生成器
// 设置自定义TraceId生成器
TLogIdGeneratorLoader.setIdGenerator(new TraceIdGenerator()); TLogIdGeneratorLoader.setIdGenerator(new TraceIdGenerator());
return tLogPropertyInit; return tLogPropertyInit;
} }

View File

@ -28,26 +28,26 @@ import java.io.IOException;
/** /**
* TLog 过滤器 * TLog 过滤器
* *
* <p>
* 重写 TLog 配置以适配 Spring Boot 3.x
* </p>
*
* @see TLogConfiguration * @see TLogConfiguration
* @author Jasmine * @author Jasmine
* @since 2024/01/30 11:39 * @since 2024/1/30 11:39
*/ */
@Component @Component
public class TLogServletFilter implements Filter { public class TLogServletFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override @Override
public void doFilter(ServletRequest request, public void doFilter(ServletRequest request,
ServletResponse response, ServletResponse response,
FilterChain chain) throws IOException, ServletException { FilterChain chain) throws IOException, ServletException {
if (request instanceof HttpServletRequest && response instanceof HttpServletResponse) { if (request instanceof HttpServletRequest httpServletRequest && response instanceof HttpServletResponse httpServletResponse) {
try { try {
TLogWebCommon.loadInstance().preHandle((HttpServletRequest)request); TLogWebCommon.loadInstance().preHandle(httpServletRequest);
// traceId放入response的header为了方便有些人有这样的需求从前端拿整条链路的traceId // traceId 放入 response header为了方便有些人有这样的需求从前端拿整条链路的 traceId
((HttpServletResponse)response).addHeader(TLogConstants.TLOG_TRACE_KEY, TLogContext.getTraceId()); httpServletResponse.addHeader(TLogConstants.TLOG_TRACE_KEY, TLogContext.getTraceId());
chain.doFilter(request, response); chain.doFilter(request, response);
return; return;
} finally { } finally {
@ -56,9 +56,4 @@ public class TLogServletFilter implements Filter {
} }
chain.doFilter(request, response); chain.doFilter(request, response);
} }
@Override
public void destroy() {
}
} }

View File

@ -20,20 +20,20 @@ import com.yomahub.tlog.constant.TLogConstants;
import com.yomahub.tlog.core.rpc.TLogLabelBean; import com.yomahub.tlog.core.rpc.TLogLabelBean;
import com.yomahub.tlog.core.rpc.TLogRPCHandler; import com.yomahub.tlog.core.rpc.TLogRPCHandler;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* TLog * TLog Web 通用拦截器
*
* <p>
* 重写 TLog 配置以适配 Spring Boot 3.x
* </p>
* *
* @see TLogWebCommon * @see TLogWebCommon
* @author Jasmine * @author Jasmine
* @since 2024/01/30 11:39 * @since 2024/1/30 11:39
*/ */
public class TLogWebCommon extends TLogRPCHandler { public class TLogWebCommon extends TLogRPCHandler {
private final static Logger log = LoggerFactory.getLogger(TLogWebCommon.class);
private static volatile TLogWebCommon tLogWebCommon; private static volatile TLogWebCommon tLogWebCommon;
public static TLogWebCommon loadInstance() { public static TLogWebCommon loadInstance() {
@ -53,9 +53,7 @@ public class TLogWebCommon extends TLogRPCHandler {
String preIvkApp = request.getHeader(TLogConstants.PRE_IVK_APP_KEY); String preIvkApp = request.getHeader(TLogConstants.PRE_IVK_APP_KEY);
String preIvkHost = request.getHeader(TLogConstants.PRE_IVK_APP_HOST); String preIvkHost = request.getHeader(TLogConstants.PRE_IVK_APP_HOST);
String preIp = request.getHeader(TLogConstants.PRE_IP_KEY); String preIp = request.getHeader(TLogConstants.PRE_IP_KEY);
TLogLabelBean labelBean = new TLogLabelBean(preIvkApp, preIvkHost, preIp, traceId, spanId); TLogLabelBean labelBean = new TLogLabelBean(preIvkApp, preIvkHost, preIp, traceId, spanId);
processProviderSide(labelBean); processProviderSide(labelBean);
} }

View File

@ -19,16 +19,15 @@ package top.charles7c.continew.admin.common.config.tlog;
import com.yomahub.tlog.id.TLogIdGenerator; import com.yomahub.tlog.id.TLogIdGenerator;
/** /**
* TLog 配置 * TLog ID 自定义生成器
* *
* @see TraceIdGenerator * @see TraceIdGenerator
* @author Jasmine * @author Jasmine
* @since 2024/01/30 11:39 * @since 2024/1/30 11:39
*/ */
public class TraceIdGenerator extends TLogIdGenerator { public class TraceIdGenerator extends TLogIdGenerator {
@Override @Override
public String generateTraceId() { public String generateTraceId() {
String traceId = String.valueOf(System.nanoTime()); return String.valueOf(System.nanoTime());
return traceId;
} }
} }

View File

@ -32,6 +32,8 @@ import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import top.charles7c.continew.starter.core.autoconfigure.project.ProjectProperties; import top.charles7c.continew.starter.core.autoconfigure.project.ProjectProperties;
import top.charles7c.continew.starter.core.constant.PropertiesConstants;
import top.charles7c.continew.starter.core.constant.StringConstants;
import top.charles7c.continew.starter.extension.crud.annotation.EnableCrudRestController; import top.charles7c.continew.starter.extension.crud.annotation.EnableCrudRestController;
import top.charles7c.continew.starter.web.annotation.EnableGlobalExceptionHandler; import top.charles7c.continew.starter.web.annotation.EnableGlobalExceptionHandler;
@ -81,7 +83,8 @@ public class ContiNewAdminApplication implements ApplicationRunner {
log.info("----------------------------------------------"); log.info("----------------------------------------------");
log.info("{} service started successfully.", projectProperties.getName()); log.info("{} service started successfully.", projectProperties.getName());
log.info("API 地址:{}", baseUrl); log.info("API 地址:{}", baseUrl);
if (SpringUtil.getProperty("springdoc.swagger-ui.enabled", boolean.class, false)) { String docEnabledProperty = PropertiesConstants.SPRINGDOC_SWAGGER_UI + StringConstants.DOT + PropertiesConstants.ENABLED;
if (Boolean.TRUE.equals(SpringUtil.getProperty(docEnabledProperty, boolean.class, false))) {
log.info("API 文档:{}/doc.html", baseUrl); log.info("API 文档:{}/doc.html", baseUrl);
} }
log.info("----------------------------------------------"); log.info("----------------------------------------------");

View File

@ -37,6 +37,11 @@ continew-starter.log:
## 项目日志配置 ## 项目日志配置
logging: logging:
config: classpath:logback-spring.xml config: classpath:logback-spring.xml
## TLog 链路追踪配置
tlog:
enable-invoke-time-print: false
pattern: '[$spanId][$traceId]'
mdc-enable: false
--- ### 线程池配置 --- ### 线程池配置
continew-starter.thread-pool: continew-starter.thread-pool:
@ -247,10 +252,4 @@ generator:
packageName: service.impl packageName: service.impl
Controller: Controller:
templatePath: generator/Controller.ftl templatePath: generator/Controller.ftl
packageName: controller packageName: controller
--- ### TLog
tlog:
enable-invoke-time-print: true
pattern: '[$spanId][$traceId]'
mdc-enable: false

10
pom.xml
View File

@ -33,8 +33,6 @@
<properties> <properties>
<!-- 项目版本号 --> <!-- 项目版本号 -->
<revision>2.4.0-SNAPSHOT</revision> <revision>2.4.0-SNAPSHOT</revision>
<tlog.version>1.5.1</tlog.version>
<sonar.version>3.9.1.2184</sonar.version>
</properties> </properties>
<!-- 全局依赖版本管理 --> <!-- 全局依赖版本管理 -->
@ -74,13 +72,6 @@
<artifactId>continew-admin-common</artifactId> <artifactId>continew-admin-common</artifactId>
<version>${revision}</version> <version>${revision}</version>
</dependency> </dependency>
<!--轻量级的分布式日志标记追踪神器-->
<dependency>
<groupId>com.yomahub</groupId>
<artifactId>tlog-web-spring-boot-starter</artifactId>
<version>${tlog.version}</version>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
@ -188,7 +179,6 @@
<plugin> <plugin>
<groupId>org.sonarsource.scanner.maven</groupId> <groupId>org.sonarsource.scanner.maven</groupId>
<artifactId>sonar-maven-plugin</artifactId> <artifactId>sonar-maven-plugin</artifactId>
<version>${sonar.version}</version>
<executions> <executions>
<execution> <execution>
<phase>verify</phase> <phase>verify</phase>