好耶!终于可以部署服务器了!很开心?确实,但是结果并非一帆风顺,遇到了各种各样的问题。下面来一个一个描述与给出解决方案。
vue路由history模式后端配置
之前对于vue的history模式路由的配置方式是xml修改404错误页指向。对于springboot相关的配置类是WebServerFactoryCustomizer也就是web服务器的定制器。
参考代码如下:
代码语言:javascript复制package top.dreamcenter.dreamcenter.config;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import java.util.HashSet;
import java.util.Set;
@Configuration
public class WebServerFactoryCustomerConfig {
@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){
return factory -> {
ErrorPage errorPage = new ErrorPage(HttpStatus.NOT_FOUND, "/index.html");
Set<ErrorPage> errorPages = new HashSet<>();
errorPages.add(errorPage);
factory.setErrorPages(errorPages);
};
}
}
数据的备份和恢复
对于小的数据库数据的备份利用如下语法进行备份:
代码语言:javascript复制mysqldump -uxxx -p dbname > xxx.sql
该语法导出来的sql文件是dbname数据库的所有表,所以导入时要先创建好对应的库。之后使用如下的语法进行恢复,注意如果出现'n'未定义之类的错误,说明与编码不一致,会发生问题,需要添加参数:
代码语言:javascript复制mysql -uroot -h xxx.xxx.xxx.xxx --default-character-set=utf8 -p -Ddbname < xxx.sql
maven的clean报错
倘若出现 fail to clean target 的错误,说明当前项目target目录有被进程占用,注意关闭相关的命令行或者引用。
服务器启动失败
依赖缺失
这个问题倒是修了有一活儿,产生的原因也很简单,先贴出来错误的日志记录:
代码语言:javascript复制2022-04-17 22:00:25.889 ERROR 1316 --- [main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'imageController' defined in URL [jar:file:/C:/Users/Administrator/Desktop/dreamcenter/dreamcenter-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/top/dreamcenter/dreamcenter/controller/ImageController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'imageServiceImpl': Lookup method resolution failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [top.dreamcenter.dreamcenter.service.impl.ImageServiceImpl] from ClassLoader [org.springframework.boot.loader.LaunchedURLClassLoader@7e32c033]
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) ~[spring-beans-5.3.18.jar!/:5.3.18]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229) ~[spring-beans-5.3.18.jar!/:5.3.18]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372) ~[spring-beans-5.3.18.jar!/:5.3.18]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222) ~[spring-beans-5.3.18.jar!/:5.3.18]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.18.jar!/:5.3.18]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.18.jar!/:5.3.18]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.18.jar!/:5.3.18]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.18.jar!/:5.3.18]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.18.jar!/:5.3.18]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.18.jar!/:5.3.18]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953) ~[spring-beans-5.3.18.jar!/:5.3.18]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.18.jar!/:5.3.18]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.18.jar!/:5.3.18]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.6.jar!/:2.6.6]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:740) ~[spring-boot-2.6.6.jar!/:2.6.6]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:415) ~[spring-boot-2.6.6.jar!/:2.6.6]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) ~[spring-boot-2.6.6.jar!/:2.6.6]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312) ~[spring-boot-2.6.6.jar!/:2.6.6]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) ~[spring-boot-2.6.6.jar!/:2.6.6]
at top.dreamcenter.dreamcenter.DreamcenterApplication.main(DreamcenterApplication.java:11) ~[classes!/:0.0.1-SNAPSHOT]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na]
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49) ~[dreamcenter-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:108) ~[dreamcenter-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:58) ~[dreamcenter-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88) ~[dreamcenter-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'imageServiceImpl': Lookup method resolution failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [top.dreamcenter.dreamcenter.service.impl.ImageServiceImpl] from ClassLoader [org.springframework.boot.loader.LaunchedURLClassLoader@7e32c033]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:289) ~[spring-beans-5.3.18.jar!/:5.3.18]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1302) ~[spring-beans-5.3.18.jar!/:5.3.18]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1219) ~[spring-beans-5.3.18.jar!/:5.3.18]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.18.jar!/:5.3.18]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.18.jar!/:5.3.18]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.18.jar!/:5.3.18]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.18.jar!/:5.3.18]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.18.jar!/:5.3.18]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.18.jar!/:5.3.18]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.18.jar!/:5.3.18]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1389) ~[spring-beans-5.3.18.jar!/:5.3.18]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1309) ~[spring-beans-5.3.18.jar!/:5.3.18]
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887) ~[spring-beans-5.3.18.jar!/:5.3.18]
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791) ~[spring-beans-5.3.18.jar!/:5.3.18]
... 27 common frames omitted
Caused by: java.lang.IllegalStateException: Failed to introspect Class [top.dreamcenter.dreamcenter.service.impl.ImageServiceImpl] from ClassLoader [org.springframework.boot.loader.LaunchedURLClassLoader@7e32c033]
at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:485) ~[spring-core-5.3.18.jar!/:5.3.18]
at org.springframework.util.ReflectionUtils.doWithLocalMethods(ReflectionUtils.java:321) ~[spring-core-5.3.18.jar!/:5.3.18]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:267) ~[spring-beans-5.3.18.jar!/:5.3.18]
... 40 common frames omitted
Caused by: java.lang.NoClassDefFoundError: org/springframework/boot/configurationprocessor/json/JSONException
at java.base/java.lang.Class.getDeclaredMethods0(Native Method) ~[na:na]
at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3167) ~[na:na]
at java.base/java.lang.Class.getDeclaredMethods(Class.java:2310) ~[na:na]
at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:467) ~[spring-core-5.3.18.jar!/:5.3.18]
... 42 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.configurationprocessor.json.JSONException
at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:436) ~[na:na]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588) ~[na:na]
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:151) ~[dreamcenter-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[na:na]
... 46 common frames omitted
也许对于聪明的你很快就已经知道为啥了,由于遇到的次数太少,对于这个才有了初体验。
首先,我们要把springboot导出为jar包的话,需要配置packaging标签:<packaging>jar</packaging>
其次,从报错的最后两个错误,可以发现json的类没有找到,好家伙,居然导的是processor的包,最后会被自动移除,而我们对接图床的接口需求的json包是org.json的jar包,所以导致最后maven装包的时候,缺少了必要的依赖!所以我们假如org.json的maven配置并且修改相关的import语句即可。
所以前辈总结的,70%的部署错误是来源于依赖问题,也是有道理的。
配置切换
学时不知她的好,用时方知奶香奶香。当出在学习的时候,对于profile配置并没有在意,而今天也算是真真正正的明白了其重要地位。
首先,我们知道打包的前提是本地代码编译能够通过,假如不能通过就不存在打包的可能性。但是,我们生产环境和开发环境有众多的配置不同,比如redis的服务地址和密码,mysql的账户密码,日志记录级别,以及其它相关环境的不同,如果每次部署,都改原来的配置,效率并不是还高。因此有了profile修改策略,当然还有很多修改的方式,不过感觉最好的是这种。
首先对application.yml配置分出两个子配置,一个是application-test.yml,一个是application-prod.yml,将application.yml中的分歧点抽离到两个配置文件中,最后在application.yml或者清晰点在application.properties中设置 spring.profiles.active=test 这样就可以默认对接测试环境的配置文件了,当然线上时,运行jar包也只需要如下的指令即可快速切换环境:
代码语言:javascript复制java -jar --spring.profiles.active=prod
当然在服务器端写上各种配置文件也是可行方法之一。
静态资源的定位
两种需求,一个是本地备份图片服务,一个是前端静态网页资源的服务,进行如下配置:
代码语言:javascript复制spring.web.resources.static-locations:[file:C:/data/dreamcenter/,classpath:/static/]
前端错误及解决
前端出错是没想到的,而且也从来没注意,先来看看报错内容:
Error: Loading CSS chunk chunk-5973b69d failed.(/css/chunk-5973b69d.0e433876.css)
经过查询,得知是vue的<style ...></>如果内容为空的话,那么会导出一个字节数为0的css文件,在读取时,没有内容,返回了404.所以,去除掉没有意义的空标签。
一些已知的bug和需求
1. 图片和ttf字体文件相对较大,对于我的渣渣服务器来说,带宽实在跟不上,也许可以走图床dns加速。
2. 可以增加加载动画来让页面更加自然顺滑。
3. 升级服务器各项密码安全等级。
4. 部署到服务器的博客内容中code代码未染色,暂且不清楚是什么原因,虽然本地跑服务能够染色。