自動部署
實戰流程
下邊使用jenkins實現CI/CD的流程。
1、將代碼使用Git托管
2、在jenkins創建任務,從Git拉取代碼。
3、拉取代碼后進行自動構建:測試、打包、部署。
首先將代碼打成鏡像包上傳到docker私服。
自動創建容器、啟動容器。
4、當有代碼push到git實現自動構建。
代碼提交至Git
修改pom.xml文件
在pom.xml添加docker-maven-plugin插件實現將springboot工程創建鏡像,此pom.xml添加docker-maven-plugin插件用于生成鏡像。
分別修改system-api、content-api、media-api、gateway、auth、checkcode服務的pom.xml文件。
插件的坐標如下:
<dependency><groupId>com.spotify</groupId><artifactId>docker-maven-plugin</artifactId><version>1.2.2</version>
</dependency>
修改pom.xml文件,以xuecheng-plus-checkcode為例,如下:
<build><finalName>${project.artifactId}-${project.version}</finalName><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>${spring-boot.version}</version><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin><plugin><groupId>com.spotify</groupId><artifactId>docker-maven-plugin</artifactId><version>1.2.2</version><configuration><!--修改imageName節點的內容,改為私有倉庫地址和端口,再加上鏡像id和 TAG,我們要直接傳到私服--><!--配置最后生成的鏡像名,docker images里的,我們這邊取項目名:版本--><!--<imageName>${project.artifactId}:${project.version}</imageName>--><imageName>192.168.101.65:5000/${project.artifactId}:${project.version}</imageName><!--也可以通過以下方式定義image的tag信息。 --><!-- <imageTags><imageTag>${project.version}</imageTag><!–build 時強制覆蓋 tag,配合 imageTags 使用–><forceTags>true</forceTags><!–build 完成后,push 指定 tag 的鏡像,配合 imageTags 使用–><pushImageTag>true</pushImageTag></imageTags>--><baseImage>java:8u20</baseImage><maintainer>docker_maven docker_maven@email.com</maintainer><workdir>/root</workdir><cmd>["java", "-version"]</cmd><!--來指明Dockerfile文件的所在目錄,如果配置了dockerDirectory則忽略baseImage,maintainer等配置--><!--<dockerDirectory>./</dockerDirectory>--><!--2375是docker的遠程端口,插件生成鏡像時連接docker,這里需要指定docker遠程端口--><dockerHost>http://192.168.101.65:2375</dockerHost><!--入口點,project.build.finalName就是project標簽下的build標簽下 的filename標簽內容,testDocker--><!--相當于啟動容器后,會自動執行java -jar ...--><entryPoint>["java", "-Dfile.encoding=utf-8","-jar", "/root/${project.build.finalName}.jar"]</entryPoint><!--是否推送到docker私有倉庫,舊版本插件要配置maven的settings文件。 --><pushImage>true</pushImage><registryUrl>192.168.101.65:5000</registryUrl> <!-- 這里是復制 jar 包到 docker 容器指定目錄配置 --><resources><resource><targetPath>/root</targetPath><directory>${project.build.directory}</directory><!--把哪個文件上傳到docker,相當于Dockerfile里的add app.jar /--><include>${project.build.finalName}.jar</include></resource></resources></configuration></plugin></plugins>
</build>
其中system-api服務的bootstrap.yml修改如下:
server:servlet:context-path: /systemport: 63110
#微服務配置
spring:application:name: system-apidatasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://192.168.101.65:3306/xcplus_system?serverTimezone=UTC&userUnicode=true&useSSL=false&username: rootpassword: mysqlcloud:nacos:server-addr: 192.168.101.65:8848discovery:namespace: dev166group: xuecheng-plus-project
# 日志文件配置路徑
logging:config: classpath:log4j2-dev.xml# swagger 文檔配置
swagger:title: "學成在線系統管理"description: "系統管理接口"base-package: com.xuecheng.systemenabled: trueversion: 1.0.0
在system-api工程添加nacos的依賴:
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
刪除system-service工程下的配置文件。
以上內容修改完畢再次提交Git.
自動構建測試
找到jenkins_02任務,配置源碼管理
配置完畢,開始構建
通過控制臺輸出日志觀察構建情況
如果控制臺有報錯,根據錯誤信息進行調試。
部署成功后,進入服務器查看docker容器是否啟動成功
部署前端門戶
在虛擬機的docker中已經部署了nginx,修改nginx.conf的配置文件
#user nobody;
worker_processes 1;#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;#pid logs/nginx.pid;events {worker_connections 1024;
}http {server_names_hash_bucket_size 64;client_max_body_size 100M; # 設置客戶端請求體最大值client_body_buffer_size 128k; # 設置請求體緩存區大小include mime.types;default_type application/octet-stream;#log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';#access_log logs/access.log main;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#文件服務upstream fileserver{server 192.168.101.65:9000 weight=10;} #后臺網關upstream gatewayserver{server 192.168.101.65:63010 weight=10;} #gzip on;server {listen 80;server_name www.51xuecheng.cn localhost;#rewrite ^(.*) https://$server_name$1 permanent;#charset koi8-r;ssi on;ssi_silent_errors on;#access_log logs/host.access.log main;location / {alias /etc/nginx/html/;index index.html index.htm;}#apilocation /api/ {proxy_pass http://gatewayserver/;} #靜態資源location /static/img/ { alias /etc/nginx/html/img/;} location /static/css/ { alias /etc/nginx/html/css/;} location /static/js/ { alias /etc/nginx/html/js/;} location /static/plugins/ { alias /etc/nginx/html/plugins/;add_header Access-Control-Allow-Origin http://ucenter.51xuecheng.cn; add_header Access-Control-Allow-Credentials true; add_header Access-Control-Allow-Methods GET;} location /plugins/ { alias /etc/nginx/html/plugins/;} location /course/preview/learning.html {alias /etc/nginx/html/course/learning.html;} location /course/search.html { root /etc/nginx/html;} location /course/learning.html { root /etc/nginx/html;} location /course/ { proxy_pass http://fileserver/mediafiles/course/;} #openapilocation /open/content/ {proxy_pass http://gatewayserver/content/open/;} location /open/media/ {proxy_pass http://gatewayserver/media/open/;} #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;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;# include fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#}}server {listen 80;server_name file.51xuecheng.cn;#charset koi8-r;ssi on;ssi_silent_errors on;#access_log logs/host.access.log main;location /video {proxy_pass http://fileserver;}location /mediafiles {proxy_pass http://fileserver;}}server {listen 80;server_name teacher.51xuecheng.cn;#charset koi8-r;ssi on;ssi_silent_errors on;#access_log logs/host.access.log main;location / {alias /etc/nginx/html/dist/;index index.html index.htm;}#location / {# proxy_pass http://uidevserver;#}location /api/ {proxy_pass http://gatewayserver/;} }server {listen 80;server_name ucenter.51xuecheng.cn;#charset koi8-r;ssi on;ssi_silent_errors on;#access_log logs/host.access.log main;location / {alias /etc/nginx/html/ucenter/;index index.html index.htm;}location /include {proxy_pass http://192.168.101.65;}location /img/ {proxy_pass http://192.168.101.65/static/img/;}location /api/ {proxy_pass http://gatewayserver/;} }# another virtual host using mix of IP-, name-, and port-based configuration##server {# listen 8000;# listen somename:8080;# server_name somename alias another.alias;# location / {# root html;# index index.html index.htm;# }#}# HTTPS server##server {# listen 443 ssl;# server_name localhost;# ssl_certificate cert.pem;# ssl_certificate_key cert.key;# ssl_session_cache shared:SSL:1m;# ssl_session_timeout 5m;# ssl_ciphers HIGH:!aNULL:!MD5;# ssl_prefer_server_ciphers on;# location / {# root html;# index index.html index.htm;# }#}}
將前端門戶的靜態頁面拷貝到 /data/soft/nginx/xuecheng_portal_static
啟動nginx容器:
docker start nginx
修改本機hosts文件:
192.168.101.65 www.51xuecheng.cn 51xuecheng.cn ucenter.51xuecheng.cn teacher.51xuecheng.cn file.51xuecheng.cn
將本機的nginx服務停掉,訪問www.51xuecheng.cn。
部署機構端前端
將機構端的前端工程打包,運行yarn build
打包成功在工程目錄生成dist目錄
將此目錄的內容拷貝到虛擬機的/data/soft/nginx/xuecheng_portal_static/dist
配置觸發器
當向gogs提交代碼時進行自動構建
在gogs配置鉤子
推送地址設置jenkins的接口:
http://192.168.101.65:8888/gogs-webhook/?job=jenkins_02
配置好可以測試一下:
測試后觀察jenkina是否重新構建任務。
提交代碼測試:
修改代碼提交到gogs,觀察jenkins是否自動構建任務
功能測試
測試認證功能
部署成功后對功能進行測試。
1、首先測試認證功能
進入www.51xuecheng.cn,點擊登錄,輸入賬號和密碼進行登錄。
賬號和密碼:t1/111111
測試內容管理
1、測試課程列表
出現 Request failed with status code 503 錯誤
通過nacos排查,進入服務列表
缺少system-api
排查system-api工程的bootstrap.yml配置文件、依賴包等內容。
修改代碼后重新提交git
再次進行jenkins構建。
2、測試上傳課程圖片
首先測試修改課程,上傳一個新圖片。
3、測試課程發布
首先觀察xxl-job調度中心是否成功注冊執行器
啟動課程發布任務
發布一門課程,觀察content-api容器的日志
錯誤日志:
java.io.FileNotFoundException: file:/root/xuecheng-plus-content-api-0.0.1-SNAPSHOT.jar!/BOOT-INF/classes!/templates does not exist.
無法找到靜態化的模板
屏蔽原來的方式,改如下方式
// String classpath = this.getClass().getResource("/").getPath(); //打包jar無法獲取模板
// configuration.setDirectoryForTemplateLoading(new File(classpath + "/templates/"));//更改為如下方式configuration.setTemplateLoader(new ClassTemplateLoader(this.getClass().getClassLoader(),"/templates"));
測試媒資管理
1、配置ffmpeg的目錄
將linux版本的ffmpeg拷貝到 /data/soft/service 下,在nacos配置ffmpeg 的地址:
Ffmpeg linux版本下載地址:https://johnvansickle.com/ffmpeg/
videoprocess:ffmpegpath: /root/soft/ffmpeg
2、測試上傳視頻