2021SC@SDUSC
本次报告着重于application.properties类和config文件夹,从中了解springboot的各种配置
目录
一、springboot的多环境配置
profile
二、application.properties中的有关其他配置
三、config配置
在本开发项目中,开发环境、测试环境、补丁环境都是有不同的配置的。各个环境可能用不同的数据库,为了便于开发,spring提供了多环境配置文件切换功能。比如:application-dev.yml表示测试环境配置、application-test.yml表示测试环境配置、application-prod.yml表示补丁环境配置、application.yml则是默认环境。如此,我们让那个配置生效,只需要指定对应的配置文件即可
Profile是Spring对不同环境提供不同配置功能的支持, 可以通过激活 , 制定参数等方式快速切换环境
本项目通过application.properties实现多环境配置
server.port = ${KK_SERVER_PORT:8012}
server.servlet.context-path= ${KK_CONTEXT_PATH:/}
1、有关路径
#文件资源路径(默认为打包根路径下的file目录下) file.dir = ${KK_FILE_DIR:default} #openoffice home路径 office.home = ${KK_OFFICE_HOME:default}
2、缓存配置,其中实现了缓存定时自动清理的功能
#缓存实现类型,不配默认为内嵌RocksDB(type = default)实现,可配置为redis(type = redis)实现(需要配置spring.redisson.address等参数)和 JDK 内置对象实现(type = jdk), cache.type = ${KK_CACHE_TYPE:jdk} #redis连接,只有当cache.type = redis时才有用 spring.redisson.address = ${KK_SPRING_REDISSON_ADDRESS:127.0.0.1:6379} spring.redisson.password = ${KK_SPRING_REDISSON_PASSWORD:} #缓存是否自动清理 true 为开启,注释掉或其他值都为关闭 cache.clean.enabled = ${KK_CACHE_CLEAN_ENABLED:true} #缓存自动清理时间,cache.clean.enabled = true时才有用,cron表达式,基于Quartz cron cache.clean.cron = ${KK_CACHE_CLEAN_CRON:0 0 3 * * ?}
3、文件类型
#文本类型,默认如下,可自定义添加 simText = ${KK_SIMTEXT:txt,html,htm,asp,jsp,xml,json,properties,md,gitignore,log,java,py,c,cpp,sql,sh,bat,m,bas,prg,cmd} #多媒体类型,默认如下,可自定义添加 media = ${KK_MEDIA:mp3,wav,mp4,flv} #是否开启多媒体类型转视频格式转换,目前可转换视频格式有:avi,mov,wmv,3gp,rm #请谨慎开启此功能,建议异步调用添加到处理队列,并且增加任务队列处理线程,防止视频转换占用完线程资源,转换比较耗费时间,并且控制了只能串行处理转换任务 media.convert.disable = ${KK_MEDIA_CONVERT_DISABLE:false} #支持转换的视频类型 convertMedias = ${KK_CONVERTMEDIAS:avi,mov,wmv,mkv,3gp,rm} #office类型文档(word ppt)样式,默认为图片(image),可配置为pdf(预览时也有按钮切换) office.preview.type = ${KK_OFFICE_PREVIEW_TYPE:image}
4、水印配置
#水印内容 #例:watermark.txt = ${WATERMARK_TXT:凯京科技内部文件,严禁外泄} #如需取消水印,内容设置为空即可,例:watermark.txt = ${WATERMARK_TXT:} watermark.txt = ${WATERMARK_TXT:} #水印x轴间隔 watermark.x.space = ${WATERMARK_X_SPACE:10} #水印y轴间隔 watermark.y.space = ${WATERMARK_Y_SPACE:10} #水印字体 watermark.font = ${WATERMARK_FONT:微软雅黑} #水印字体大小 watermark.fontsize = ${WATERMARK_FONTSIZE:18px} #水印字体颜色 watermark.color = ${WATERMARK_COLOR:black} #水印透明度,要求设置在大于等于0.005,小于1 watermark.alpha = ${WATERMARK_ALPHA:0.2} #水印宽度 watermark.width = ${WATERMARK_WIDTH:180} #水印高度 watermark.height = ${WATERMARK_HEIGHT:80} #水印倾斜度数,要求设置在大于等于0,小于90 watermark.angle = ${WATERMARK_ANGLE:10}
config的配置信息大致由上面几个类所构成
ConfigConstants:基本信息配置,包括ftp账号密码,以及文件的基本路径等常用内容
private static Boolean cacheEnabled;
private static String[] simTexts = {};
private static String[] medias = {};
private static String[] convertMedias = {};
private static String mediaConvertDisable;
private static String officePreviewType;
private static String officePreviewSwitchDisabled;
private static String ftpUsername;
private static String ftpPassword;
private static String ftpControlEncoding;
private static String baseUrl;
private static String fileDir = ConfigUtils.getHomePath() + File.separator + "file" + File.separator;
private static CopyOnWriteArraySet trustHostSet;
private static String pdfDownloadDisable;
private static Boolean fileUploadDisable;
public static final String DEFAULT_CACHE_ENABLED = "true";
public static final String DEFAULT_TXT_TYPE = "txt,html,htm,asp,jsp,xml,json,properties,md,gitignore,log,java,py,c,cpp,sql,sh,bat,m,bas,prg,cmd";
public static final String DEFAULT_MEDIA_TYPE = "mp3,wav,mp4,flv";
public static final String DEFAULT_OFFICE_PREVIEW_TYPE = "image";
public static final String DEFAULT_OFFICE_PREVIEW_SWITCH_DISABLED = "false";
public static final String DEFAULT_FTP_USERNAME = null;
public static final String DEFAULT_FTP_PASSWORD = null;
public static final String DEFAULT_FTP_CONTROL_ENCODING = "UTF-8";
public static final String DEFAULT_BASE_URL = "default";
public static final String DEFAULT_FILE_DIR_VALUE = "default";
public static final String DEFAULT_TRUST_HOST = "default";
public static final String DEFAULT_PDF_DOWNLOAD_DISABLE = "true";
public static final String DEFAULT_FILE_UPLOAD_DISABLE = "false";
Reddisson Config:客户端配置,管理客户端的线程,顺序等
ScheduleClean Config:定期清理缓存配置,这是我认为该项目的一个亮点,默认的是每晚3点清理一次缓存
@Scheduled(cron = "${cache.clean.cron:0 0 3 * * ?}")
public void clean() {
logger.info("Cache clean start");
cacheService.cleanCache();
KkFileUtils.deleteDirectory(fileDir);
logger.info("Cache clean end");
}
WaterMarkConfigConstants:水印配置管理,例如水印字体大小,添加位置等内容
WebConfig:访问外部文件配置
public void addResourceHandlers(ResourceHandlerRegistry registry) {
String filePath = ConfigConstants.getFileDir();
LOGGER.info("Add resource locations: {}", filePath);
registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/","classpath:/resources/","classpath:/static/","classpath:/public/","file:" + filePath);
}
}