Jenkins+Docker+SpringCloud微服務持續整合
Jenkins+Docker+SpringCloud微服務持續整合
目錄- 一:Jenkins+Docker+SpringCloud續整合流程說明
- 二: 本地部署SpringCloud微服務部署
- 三:windows本地安裝mysql 資料庫,匯入庫
- 四:修改微服務的配置檔案,啟動微服務
- 五:安裝postman,使用postman進行測試
- 六:將微服務打包,本地執行
- 七:安裝Nodejs和python2
- 八,安裝執行VSCode
一:Jenkins+Docker+SpringCloud續整合流程說明
大致流程說明:
1) 開發人員每天把程式碼提交到Gitlab程式碼倉庫
2) Jenkins從Gitlab中拉取專案原始碼,編譯並打成jar包,然後構建成Docker映象,將映象上傳到Harbor私有倉庫。
3) Jenkins傳送SSH遠端命令,讓生產部署伺服器到Harbor私有倉庫拉取映象到本地,然後建立容器。
4) 最後,使用者可以訪問到容器
微服務原始碼概述
專案架構:前後端分離
後端技術棧:SpringBoot+SpringCloud+SpringDataJpa(Spring全家桶)
微服務專案結構:
- tensquare_parent: 父 工 程 , 存 放 基 礎 配 置
- tensquare_common:通用工程,存放工具類
- tensquare_eureka_server:Eureka註冊中心
- tensquare_zuul:SpringCloud 的 網 關 服 務
- tensquare_admin_service:基礎許可權認證中心,負責使用者認證(WT認證)
- tensquare_gathering:一個簡單的業務模組,負責微服務相關邏輯
資料庫結構
- l tensquare_user:使用者認證資料庫,存放使用者賬戶資料(登入)。對應tensquare_admin_service微服務
- l tensquare_gathering:活動微服務資料庫。對應tensquare_gathering微服務(用於展示出來)
微服務配置分析:
- tensquare_eureka (註冊中心)
- tensquare_zuul (閘道器)
- tensquare_admin_service (許可權管理)
- tensquare_gathering (業務)
二: 本地部署SpringCloud微服務部署
載入新專案,配置jdk
逐一啟動微服務
連結:https://pan.baidu.com/s/1FEuOhlnbKpG-Wabydo4cqQ?pwd=s7b0
提取碼:s7b0
微服務專案包解壓,將tensquare_parent 專案目錄放到idea的專案目錄idea project中
然後在idea中點選open,選擇專案,將專案在idea中開啟,idea會自動下載相關元件外掛(等待時間較長)
右下角彈窗,點選Always Add
配置jdk
SpringBoot啟動類報錯can not resolve method 'run(java.lang.class,String [])',則是因為jdk配置問題
每一個都要檢查jdk配置
安裝配置Maven
連結:https://pan.baidu.com/s/1FlnSgBKpt0AkLveVId4iUQ?pwd=bhde
提取碼:bhde
Maven官網:https://maven.apache.org/download.cgi
解壓maven
配置環境變數
新增兩個新環境變數
新增maven命令到path路徑
格式如C:\Users\wangxiang\apache-maven-3.8.4\bin;..............
win+r,輸入cmd開啟命令列介面,輸入mvn -v 可以看到maven版本
修改maven 倉庫,配置下載源
新建資料夾repo
開啟maven的工作目錄下的conf目錄,開啟settins.xml檔案
修改元件地址為本地
<localRepository>C:/Users/wangxiang/repo</localRepository>
配置下載源
<mirror>
<id>aliyunmaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/repository/public </url>
</mirror>
儲存退出
在idea裡配置maven設定
自動更新
maven 安裝完畢後,每一個都不能有紅色波浪線。有紅色波浪線,表示有必要的元件沒有下載完成。此時,最好刪除專案檔案 ,重新匯入專案,刪除快取,然後,然後maven倉庫,重新建立倉庫,最後,再重新配置maven,下載元件
三:windows本地安裝mysql 資料庫,匯入庫
連結:https://pan.baidu.com/s/1v3T8tudglq_ikJpudbh_Lg?pwd=43uw
提取碼:43uw
本地安裝mysql
匯入資料庫
配置mysql的bin目錄到系統環境變數
windows命令列登入進入資料庫
mysql -uroot -p密碼
建立兩個資料庫tensquare_gathering, tensquare_user
create database tensquare_gathering;
create database tensquare_user;
show databases;
匯入資料庫
use tensquare_user;
source c:/Users/wangxiang/tensquare_user.sql;
use tensquare_gathering;
source c:/Users/wangxiang/tensquare_gathering.sql;
四:修改微服務的配置檔案,啟動微服務
eureka_server
修改配置檔案
在eureka_server 專案----->src---->main--->resources---->application.yml
# 單機版
server:
port: 10086
#基本伺服器資訊
spring:
application:
name: eureka-server #服務ID
#enreka伺服器配置
eureka:
client:
fetch-registry: false #單機版關閉enreka相互註冊
register-with-eureka: false
service-url:
defaultZone: http://localhost:${server.port}/eureka #暴露eureka服務訪問地址
server:
enable-self-preservation: false #關閉自我保護
啟動服務,瀏覽器訪問
在eureka_server 專案----->src0---->main--->java---com.tensquare.eureka-->EurekaServerApplication--->右擊,點選run....
配置都正確後,啟動後會啟動10086埠,使用瀏覽器訪問localhost:10086
啟動zuul 閘道器服務
修改zuul 服務的配置檔案aaplication.yaml
server:
port: 10020 # 埠
# 基本服務資訊
spring:
application:
name: tensquare-zuul # 服務ID
# Eureka配置
eureka:
client:
service-url:
defaultZone: http://localhost:10086/eureka # Eureka訪問地址
instance:
prefer-ip-address: true
# 修改ribbon的超時時間
ribbon:
ConnectTimeout: 1500 # 連線超時時間,預設500ms
ReadTimeout: 3000 # 請求超時時間,預設1000ms
# 修改hystrix的熔斷超時時間
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMillisecond: 2000 # 熔斷超時時長,預設1000ms
# 閘道器路由配置
zuul:
routes:
admin:
path: /admin/**
serviceId: tensquare-admin-service
gathering:
path: /gathering/**
serviceId: tensquare-gathering
# jwt引數
jwt:
config:
key: itcast
ttl: 1800000
啟動服務
啟動 ZuulApplication
在eureka介面,可以看到zuul 已經註冊了
啟動許可權中心admin_service
修改配置檔案
server:
port: 9001
spring:
application:
name: tensquare-admin-service #指定服務名
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/tensquare_user?characterEncoding=UTF8 #jdbc連線資料庫
username: root #資料庫使用者名稱和密碼
password: abc123
jpa:
database: mysql
show-sql: true
#Eureka配置
eureka:
client:
service-url:
defaultZone: http://localhost:10086/eureka,http://192.168.66.104:10086/eureka
instance:
lease-renewal-interval-in-seconds: 5 # 每隔5秒傳送一次心跳
lease-expiration-duration-in-seconds: 10 # 10秒不傳送就過期
prefer-ip-address: true
# jwt引數
jwt:
config:
key: itcast
ttl: 1800000
啟動AdminApplication
在eureka介面,可以看到admin-service 已經註冊
開啟業務微服務gathering
修該配置檔案
server:
port: 9002
spring:
application:
name: tensquare-gathering #指定服務名
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/tensquare_gathering?characterEncoding=UTF8
username: root
password: abc123
jpa:
database: mysql
show-sql: true
#Eureka客戶端配置
eureka:
client:
service-url:
defaultZone: http://localhost:10086/eureka
instance:
lease-renewal-interval-in-seconds: 5 # 每隔5秒傳送一次心跳
lease-expiration-duration-in-seconds: 10 # 10秒不傳送就過期
prefer-ip-address: true
啟動GatheringApplication
五:安裝postman,使用postman進行測試
連結:https://pan.baidu.com/s/1rI97F8RHnOs-OoslBmN_GA?pwd=duyp
提取碼:duyp
登入使用者名稱,存放在匯入的資料庫中,登入微服務時,微服務去資料庫中驗證,只用資料庫中的使用者名稱和密碼,才允許登入
安裝postman
使用post 方式提交, 訪問localhost:10020 ,對閘道器zuul 進行訪問
http://localhost:10020/admin/admin/login
在body中,使用json 方式,以raw 行為新增,新增使用者名稱密碼
{
"loginname":"admin",
"password":"123456"
}
eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiIxMTk0MjgxNTMzMjMwNDE5OTY4Iiwic3ViIjoiYWRtaW4iLCJpYXQiOjE2NDYwMTY3MzQsInJvbGVzIjoiYWRtaW4iLCJleHAiOjE2NDYwMTg1MzR9.HOA1SkO1se23nx0FqudRQGz5kAbL-K8fDD8-4NOQPsY
六:將微服務打包,本地執行
在整個專案的配置pom.xml 檔案中,新增配置
<build>
<plugins>
<plugin>
<!--提供打包(將應用打包成可執行的jar包)-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<!-- 指定maven編譯的jdk版本 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<!--jdk地址-->
<executable>C:/Program Files/Java/jdk1.8.0_152/bin/javac</executable>
</configuration>
</plugin>
</plugins>
</build>
在idea裡先停止EurekaServerApplication的執行
在彈出的命令列介面,輸入打包命令mvn clean package
打包成功後,生成了jar包,放在專案eureka的target目錄中
進入到target 目錄
在彈出的命令列, 使用java -jar tensquare_eureka_server-1.0-SNAPSHOT.jar 進行執行(idea裡的eureka 需要先關閉)
執行成功後,使用瀏覽器訪問localhost:10086埠
至此,微服務後端部分連線資料資料庫配置都完成。後續將配置前端方面NodeJS+VueJS+ElementUI
七:安裝Nodejs和python2
連結:https://pan.baidu.com/s/16ohy8f4hx0whWNkOSJinMA?pwd=dgw0
提取碼:dgw0
安裝完畢後,將nodejs安裝目錄將npm命令,加入到系統path 環境變數中在命令列使用npm -v 命令
進入前端專案目錄,啟動cmd
前端專案:
連結:https://pan.baidu.com/s/1z58VWbMHSS6VWZmcqC4hPg?pwd=0xzn
提取碼:0xzn
使用npm run dev 啟動,此時會報錯,原因是沒有安裝 python2
npm 啟動報錯
error in ./src/views/dashboard/index.vue
Module build failed: Error: Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime (93)
安裝python2.7,全部預設即可
連結:https://pan.baidu.com/s/1AtI8-rIJnOfyKKS_kTdJ7g?pwd=7ldo
提取碼:7ldo
安裝好Python2.7後,將python2.7安裝目錄,加入到path環境變數中
來到前端專案目錄,輸入cmd,在當前前端專案目錄開啟命令列
使用命令安裝cnpm 命令。 由於npm 的伺服器在國外,所以在使用npm執行時,可能會因為網的問題報錯
npm install -g cnpm --registry=https://registry.npm.taobao.org
在tensquareAdmin目錄下執行cnpm uninstall node-sass
執行cnpm install node-sass 命令
此時,在執行cnpm run dev 命令
啟動後,出現這個登入頁面。使用者名稱admin 密碼123456
此時還無法登入,因為前面的幾個微服務關閉了。按照順序,依次啟動eureka,zuul,admin_service ,gathering 服務
八,安裝執行VSCode
安裝
在安裝完畢,啟動進入後,可以選擇安裝中文包
使用VSConde 開啟前端專案
開啟專案後,在config資料夾下,index.js就是專案的首頁檔案
開啟專案的終端。在頂部選擇終端--->新建終端
此時可以在終端,vsconde的終端,使用npm run dev 命令,也可以執行前端專案
注:vscode無法直接執行cnpm命令,需要以管理員身份開啟才行(選擇vscode圖示,右擊,以管理員身份執行)
在vscode 中直接執行 cnpm run dev 命報錯如下:
cnpm : 無法載入檔案 C:\Users\wangxiang\AppData\Roaming\npm\cnpm.ps1,因為在此係統上禁止執行指令碼。有關詳細資訊,請參閱 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
此時需要開啟許可權
此時再執行cnpm run dev 命令成功