使用maven打jar包
1.pom.xml 使用jar包方式打包
<groupId>com.summer</groupId>
<artifactId>gk</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
2.配置資源過濾:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<!-- maven項(xiàng)目中src源代碼下的xml等資源文件編譯進(jìn)classes文件夾芽唇,
注意:如果沒有這個姻报,它會自動搜索resources下是否有mapper.xml文件居触,
如果沒有就會報org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.pet.mapper.PetMapper.selectByPrimaryKey-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!--將resources目錄下的配置文件編譯進(jìn)classes文件 -->
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>utf-8</encoding>
<useDefaultDelimiters>true</useDefaultDelimiters>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>woff</nonFilteredFileExtension>
<nonFilteredFileExtension>woff2</nonFilteredFileExtension>
<nonFilteredFileExtension>eot</nonFilteredFileExtension>
<nonFilteredFileExtension>ttf</nonFilteredFileExtension>
<nonFilteredFileExtension>svg</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
3.使用IDEA的maven工具進(jìn)行 clear 和 package
先 clear 再 package 出現(xiàn)BUILD SUCCESS就是打包成功了掌猛。
Building jar 是jar包的路徑
4.我們進(jìn)入cmd使用指令運(yùn)行我們的jar包蔼两,檢查是否能成功訪問沃测。
java -jar gk-0.0.1-SNAPSHOT.jar
頁面訪問成功截亦!
拿到j(luò)ar包訪問成功我們第一階段完成宴胧。
服務(wù)器環(huán)境準(zhǔn)備 準(zhǔn)備jdk,Nginx童番,MySQL
1.建議小白安裝寶塔
Centos安裝命令:
yum install -y wget && wget -O install.sh http://download.bt.cn/install/install_6.0.sh && sh install.sh
2.使用寶塔安裝tomcat8(tomcat8會自帶jdk8的環(huán)境)
3.安裝Nginx(待會配置域名)
4.安裝MySQL(服務(wù)使用線上數(shù)據(jù)庫青灼,當(dāng)然你在打jar包之前就要配置好數(shù)據(jù)庫了)
運(yùn)行jar包
1.將jar包上傳到服務(wù)器上(哪里都行暴心,你能找到就好)
2.使用命令云jar包
nohup java -jar yqjk-0.0.1-SNAPSHOT.jar
3.如果想查看運(yùn)行日志就可以使用如下的命令
nohup java -jar yqjk-0.0.1-SNAPSHOT.jar >temp.txt &
查看運(yùn)行日志
cat temp.txt
這樣我們就可以使用 IP地址+端口號來訪問我們的服務(wù)啦<嗣ぁT硬Α!
如何關(guān)閉服務(wù)悯衬?
1.查看進(jìn)程號
netstat -anp|grep 8888
2.殺死進(jìn)程
kill -s 9 104373
配置域名
1.阿里或騰訊云里面把域名解析到服務(wù)器上
2.打開寶塔的Nginx
<meta charset="utf-8">
3.在Nginx的配置文件里面再加入一個server
主要修改如下幾個參數(shù):
- server_name :域名
- proxy_pass http://localhost:8888 要轉(zhuǎn)發(fā)到的端口(這個端口號就是你springboot項(xiàng)目里面配置的端口號)
server {
listen 80; #首先是nginx的監(jiān)聽端口默認(rèn)為80
server_name xxxxx; #這里是你需要訪問的域名地址
#add_header 'Access-Control-Allow-Origin' '*';#這里是http 域名跨域弹沽,報錯時候添加的請求頭,這樣寫所有請求都會進(jìn)來筋粗,會很不安全策橘。
#charset koi8-r;
#access_log logs/host.access.log main;#這里是 日志文件的生成路徑
#詳細(xì)介紹location
location / {
#是監(jiān)聽的端口默認(rèn)訪問的地址,這里如果沒有做tomcat的轉(zhuǎn)發(fā)則會進(jìn)入nginx的html目錄下的index.html
root html;
#這里是編寫監(jiān)聽到的請求所轉(zhuǎn)發(fā)的端口號娜亿,即tomcat端口
proxy_pass http://localhost:8888;
#Proxy Settings;
#proxy_redirect off;
#proxy_set_header Host $host;
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#設(shè)置nginx 的默認(rèn)顯示頁
index index.html index.htm;
#設(shè)置http請求的請求頭丽已,使其在跨域訪問上不會被瀏覽器阻止。ps:這里設(shè)置我發(fā)現(xiàn)沒有用买决,后來還是在ajax過濾器中添加的 請求頭沛婴,如果大家有知道這里怎么修改的,請留言大家一起學(xué)習(xí)督赤。
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
這樣咱就可使用域名訪問我們的服務(wù)啦
問題:
1.使用thymeleaf摸版引擎的th:replace在服務(wù)器上面出現(xiàn)的一種報錯嘁灯。
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Jul 10 14:09:04 CST 2020
There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template [/commons/public], template might not exist or might not be accessible by any of the configured Template Resolvers (template: "public/public-hot-news" - line 10, col 10)
在window系統(tǒng)下沒問題。部署到linux下面出現(xiàn)這個問題
需要注意配置
spring.thymeleaf.prefix=classpath:/templates/
這里已經(jīng)有斜線了 引入模板的時候不需要再加了
錯誤寫法6闵唷3笮觥!没卸!
<head th:replace="/common/head::head"></head>
正確寫法8睢!约计!
<head th:replace="common/head::head"></head>
common為目錄templates下面的