perf: 优化配置

This commit is contained in:
xiongxiaoyang 2022-08-19 19:36:08 +08:00
parent 7c0ff5e9ce
commit 3e89d2a363
10 changed files with 147 additions and 60 deletions

View File

@ -3,9 +3,7 @@ package io.github.xxyopen.novel.core.aspect;
import io.github.xxyopen.novel.core.annotation.Key;
import io.github.xxyopen.novel.core.annotation.Lock;
import io.github.xxyopen.novel.core.common.exception.BusinessException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.concurrent.TimeUnit;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
@ -20,6 +18,10 @@ import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.concurrent.TimeUnit;
/**
* 分布式锁 切面
*
@ -28,7 +30,10 @@ import org.springframework.util.StringUtils;
*/
@Aspect
@Component
public record LockAspect(RedissonClient redissonClient) {
@RequiredArgsConstructor
public class LockAspect {
private final RedissonClient redissonClient;
private static final String KEY_PREFIX = "Lock";

View File

@ -17,7 +17,7 @@ import org.springframework.context.annotation.Configuration;
* @date 2022/5/23
*/
@Configuration
@ConditionalOnProperty(prefix = "spring.elasticsearch", name = "enable", havingValue = "true")
@ConditionalOnProperty(prefix = "spring.elasticsearch", name = "enabled", havingValue = "true")
@RequiredArgsConstructor
public class EsConfig {

View File

@ -2,8 +2,6 @@ package io.github.xxyopen.novel.core.config;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
@ -16,7 +14,7 @@ import org.springframework.context.annotation.Configuration;
* @date 2022/5/31
*/
@Configuration
@ConditionalOnProperty(prefix = "xxl.job", name = "enable", havingValue = "true")
@ConditionalOnProperty(prefix = "xxl.job", name = "enabled", havingValue = "true")
@Slf4j
public class XxlJobConfig {

View File

@ -21,8 +21,8 @@ import org.springframework.stereotype.Component;
* @date 2022/5/25
*/
@Component
@ConditionalOnProperty(prefix = "spring", name = {"elasticsearch.enable",
"amqp.enable"}, havingValue = "true")
@ConditionalOnProperty(prefix = "spring", name = {"elasticsearch.enabled",
"amqp.enabled"}, havingValue = "true")
@RequiredArgsConstructor
@Slf4j
public class RabbitQueueListener {

View File

@ -13,20 +13,21 @@ import io.github.xxyopen.novel.core.constant.EsConsts;
import io.github.xxyopen.novel.dao.entity.BookInfo;
import io.github.xxyopen.novel.dao.mapper.BookInfoMapper;
import io.github.xxyopen.novel.dto.es.EsBookDto;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* 小说数据同步到 elasticsearch 任务
*
* @author xiongxiaoyang
* @date 2022/5/23
*/
@ConditionalOnProperty(prefix = "spring.elasticsearch", name = "enable", havingValue = "true")
@ConditionalOnProperty(prefix = "spring.elasticsearch", name = "enabled", havingValue = "true")
@Component
@RequiredArgsConstructor
@Slf4j

View File

@ -1,8 +1,6 @@
package io.github.xxyopen.novel.manager.mq;
import io.github.xxyopen.novel.core.common.constant.CommonConsts;
import io.github.xxyopen.novel.core.constant.AmqpConsts;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Value;
@ -22,14 +20,14 @@ public class AmqpMsgManager {
private final AmqpTemplate amqpTemplate;
@Value("${spring.amqp.enable}")
private String enableAmqp;
@Value("${spring.amqp.enabled:false}")
private boolean amqpEnabled;
/**
* 发送小说信息改变消息
*/
public void sendBookChangeMsg(Long bookId) {
if (Objects.equals(enableAmqp, CommonConsts.TRUE)) {
if (amqpEnabled) {
sendAmqpMessage(amqpTemplate, AmqpConsts.BookChangeMq.EXCHANGE_NAME, null, bookId);
}
}

View File

@ -8,19 +8,20 @@ import io.github.xxyopen.novel.dao.mapper.BookInfoMapper;
import io.github.xxyopen.novel.dto.req.BookSearchReqDto;
import io.github.xxyopen.novel.dto.resp.BookInfoRespDto;
import io.github.xxyopen.novel.service.SearchService;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 数据库搜索 服务实现类
*
* @author xiongxiaoyang
* @date 2022/5/23
*/
@ConditionalOnProperty(prefix = "spring.elasticsearch", name = "enable", havingValue = "false")
@ConditionalOnProperty(prefix = "spring.elasticsearch", name = "enabled", havingValue = "false")
@Service
@RequiredArgsConstructor
@Slf4j

View File

@ -19,22 +19,23 @@ import io.github.xxyopen.novel.dto.es.EsBookDto;
import io.github.xxyopen.novel.dto.req.BookSearchReqDto;
import io.github.xxyopen.novel.dto.resp.BookInfoRespDto;
import io.github.xxyopen.novel.service.SearchService;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/**
* Elasticsearch 搜索 服务实现类
*
* @author xiongxiaoyang
* @date 2022/5/23
*/
@ConditionalOnProperty(prefix = "spring.elasticsearch", name = "enable", havingValue = "true")
@ConditionalOnProperty(prefix = "spring.elasticsearch", name = "enabled", havingValue = "true")
@Service
@RequiredArgsConstructor
@Slf4j

View File

@ -0,0 +1,65 @@
{
"properties": [
{
"name": "spring.elasticsearch.enabled",
"description": "Whether enable elasticsearch or not.",
"type": "java.lang.Boolean"
},
{
"defaultValue": false,
"name": "spring.amqp.enabled",
"description": "Whether enable amqp or not.",
"type": "java.lang.Boolean"
},
{
"name": "xxl.job.enabled",
"description": "Whether enable xxl-job or not.",
"type": "java.lang.Boolean"
},
{
"name": "novel.jwt.secret",
"type": "java.lang.String",
"description": "JWT 密钥."
},
{
"name": "novel.xss.enabled",
"type": "java.lang.Boolean",
"description": "是否开启 XSS 过滤."
},
{
"name": "novel.xss.excludes",
"type": "java.util.List<java.lang.String>",
"description": "XSS 过滤排除链接."
},
{
"name": "novel.file.upload.path",
"type": "java.lang.String",
"description": "上传文件目录."
},
{
"name": "novel.cors.allow-origins",
"type": "java.util.List<java.lang.String>",
"description": "允许跨域的域名."
},
{
"name": "xxl.job.admin.addresses",
"type": "java.lang.String",
"description": "调度中心部署根地址."
},
{
"name": "xxl.job.executor.appname",
"type": "java.lang.String",
"description": "执行器 AppName."
},
{
"name": "xxl.job.executor.logpath",
"type": "java.lang.String",
"description": "执行器运行日志文件存储磁盘路径."
},
{
"name": "xxl.job.accessToken",
"type": "java.lang.String",
"description": "xxl-job accessToken."
}
]
}

View File

@ -1,20 +1,26 @@
#---------------------通用配置-------------------------
spring:
application:
# 应用名
name: novel
profiles:
# 激活特定配置
active: dev
# 将所有数字转为 String 类型返回,避免前端数据精度丢失的问题
jackson:
generator:
# JSON 序列化时,将所有 Number 类型的属性都转为 String 类型返回,避免前端数据精度丢失的问题。
# 由于 Javascript 标准规定所有数字处理都应使用 64 位 IEEE 754 浮点值完成,
# 结果是某些 64 位整数值无法准确表示(尾数只有 51 位宽)
write-numbers-as-strings: true
servlet:
# 上传文件最大大小
multipart:
# 上传文件最大大小
max-file-size: 5MB
application:
name: novel
server:
# 端口号
port: 8888
#---------------------数据库配置-------------------------
---
spring:
datasource:
@ -75,31 +81,29 @@ spring:
# 分片算法的行表达式
algorithm-expression: book_content$->{chapter_id % 10}
config:
activate:
on-profile: dev
#---------------------中间件配置-------------------------
---
spring:
# Redis 配置
redis:
host: 127.0.0.1
port: 6379
password: 123456
config:
activate:
on-profile: dev
data:
# Redis 配置
redis:
host: 127.0.0.1
port: 6379
password: 123456
# Elasticsearch 配置
elasticsearch:
# 是否开启 elasticsearch 搜索引擎功能true-开启 false-不开启
enable: false
# 是否开启 Elasticsearch 搜索引擎功能true-开启 false-不开启
enabled: false
uris:
- https://my-deployment-ce7ca3.es.us-central1.gcp.cloud.es.io:9243
username: elastic
password: qTjgYVKSuExX6tWAsDuvuvwl
# Spring AMQP 配置
amqp:
# 是否开启 Spring AMQPtrue-开启 false-不开启
enable: false
enabled: false
# RabbitMQ 配置
rabbitmq:
addresses: "amqp://guest:guest@47.106.243.172"
@ -117,7 +121,7 @@ spring:
xxl:
job:
# 是否开启 XXL-JOBtrue-开启 false-不开启
enable: false
enabled: false
admin:
### 调度中心部署根地址 [选填]:如调度中心集群部署存在多个地址则用逗号分隔。执行器将会使用该地址进行"执行器心跳注册"和"任务结果回调";为空则关闭自动注册;
addresses: http://127.0.0.1:8080/xxl-job-admin
@ -129,13 +133,10 @@ xxl:
### xxl-job, access token
accessToken: 123
#---------------------安全配置-------------------------
---
spring:
config:
activate:
on-profile: dev
# Spring Boot 应用管理和监控
# Spring Boot 应用管理和监控
boot:
admin:
client:
@ -167,7 +168,6 @@ management:
exposure:
# 公开所有的 Web 端点
include: "*"
# 端点启用配置
endpoint:
logfile:
@ -175,7 +175,6 @@ management:
enabled: true
# 外部日志文件路径
external-file: logs/novel.log
info:
env:
# 公开所有以 info. 开头的环境属性
@ -188,15 +187,7 @@ management:
# 关闭 elasticsearch 的健康检查
enabled: false
---
spring:
config:
activate:
on-profile: dev
# 项目配置
#---------------------自定义配置-------------------------
novel:
# 跨域配置
cors:
@ -221,4 +212,31 @@ novel:
path: /Users/xiongxiaoyang/upload
#----------------------- dev 特定配置-----------------------------
---
spring:
config:
activate:
on-profile: dev
#----------------------- test 特定配置-----------------------------
---
spring:
config:
activate:
on-profile: test
#----------------------- prod 特定配置-----------------------------
---
spring:
config:
activate:
on-profile: prod
data:
# Redis 配置
redis:
host: 127.0.0.1
port: 6379
password: