微服務框架saf-2:容器化部署allinone-demo

目錄

(1).關於saf

(2).saf-allinone-demo說明

(3).容器化部署環境準備

1.硬件要求

2.K8S單節點集群搭建

3.容器化相關基礎組件

3.1.基礎中間件容器化準備

3.2.zookeeper容器化部署

3.3.mysql容器化部署

3.4.redis-cluster容器化部署

3.5.rocketmq-cluster容器化部署

3.6.apollo容器化部署

3.7.ingress容器化

3.8.配置相關的ingress代理

4.數據準備

(4).編譯saf

1.工程下載與編譯順序

2.編譯saf

3.編譯saf-boot-starter

4.編譯saf-boot-starter-sample-allinone

(5).進行apollo配置

(6).容器化部署saf-boot-starter-sample-allinone-service

1.製作saf-boot-starter-sample-allinone鏡像

2.容器化saf-boot-starter-sample-allinone

(7).容器化部署saf-boot-starter-sample-allinone-web

1.製作saf-boot-starter-sample-allinoe-web鏡像

2.容器化saf-boot-starter-sample-allinoe-web

(8).驗證服務

1.驗證spring boot actuator組件

2.驗證業務接口(redis-cluster,motanRPC)

3.驗證jvmCache(guava)

4.驗證rocketmq使用

5.通過saf-boot-starter-sample-allinone-service的日誌打印驗證

(9).相關文檔


說明:

本文全部以容器化方式部署為例(主要是因為部署高效),非容器化類似。

注:

之前本文發過一次,但是由於近期對saf框架做了拆分,拆分為4個工程:saf, saf-sample, saf-boot-starter, saf-boot-starter-sample,所以需要重新修改部分內容。

詳情參見:


(1).關於saf


項目地址:

https://github.com/saf-group


1.一個微服務框架,完全基於註解的方式開發。

2.適用於雲原生(K8S)下的微服務體系搭建,為技術中臺提供底層支撐。

3.解放業務,使業務方專注於業務邏輯本身:通過註解以搭積木方式引入各式資源,每個資源都是一行註解,極大提升業務方產出效率。


微服務框架saf-2:容器化部署allinone-demo


(2).saf-boot-starter-sample-allinone說明


工程地址:

https://github.com/saf-group/saf-boot-starter-sample/tree/master/saf-boot-starter-sample-allinone


saf-boot-starter-sample-allinone涉及到的組件

demo中使用了主要的中間件,如redis-cluster(分佈式緩存),druid(數據庫連接池),motan(rpc),rocketmq(消息隊列),apollo(分佈式配置中心)


saf-boot-starter-sample-allinone包含的工程:

saf-boot-starter-sample-allinone-api:定義motan(rpc)接口

saf-boot-starter-sample-allinone-service:rpc-provider

saf-boot-starter-sample-allinone-web:rpc-consumer


(3).容器化部署環境準備


1.硬件要求

理想配置是8core,32GB。


2.K8S單節點集群搭建


3.容器化相關基礎組件


3.1.基礎中間件容器化準備

由於PV適用的是local PV,所以先初始化本地pv目錄,執行local pv目錄初始化腳本:

https://github.com/hepyu/k8s-app-config/blob/master/yaml/init.sh


3.2.zookeeper容器化部署

https://github.com/hepyu/k8s-app-config/tree/master/yaml/min-cluster-allinone/zookeeper-min


3.3.mysql容器化部署

https://github.com/hepyu/k8s-app-config/tree/master/yaml/min-cluster-allinone/mysql-min


3.4.redis-cluster容器化部署

https://github.com/hepyu/k8s-app-config/tree/master/yaml/min-cluster-allinone/redis-cluster-min


3.5.rocketmq-cluster容器化部署

https://github.com/hepyu/k8s-app-config/tree/master/yaml/min-cluster-allinone/rocketmq-min


3.6.apollo容器化部署

https://github.com/hepyu/k8s-app-config/tree/master/yaml/min-cluster-allinone/apollo-min

注意:使用容器化方式部署基礎組件主要是為了快速構建allinone-demo的運行環境,實際生產中是需要權衡的,比如mysql不要放K8S裡,而且上述基礎組件的容器化都是最小資源防止超過硬件容量,比如rocketmq只有一組master/slave。


3.7.ingress容器化

https://github.com/hepyu/k8s-app-config/tree/master/yaml/min-cluster-allinone/ingress-nginx-min


3.8.配置相關的ingress代理

https://github.com/hepyu/k8s-app-config/tree/master/yaml/min-cluster-allinone/ingress-nginx-min/proxy

kubectl apply -f分別執行:

ingress-nginx-apollo-config.yaml

ingress-nginx-apollo-portal.yaml

ingress-nginx-rocketmq-min-c0-console.yaml

然後在本地PC配置host即可訪問對應的apollo和rocketmq後臺。


4.數據準備


saf-allinone數據庫數據初始化:

獲得mysql的root用戶的登錄密碼:

kubectl get secret --namespace mysql-min mysql-min -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo

root密碼是:9ZeNk0DghH


暴露mysql端口到容器外(可以選擇其他方式如nodeport等)方便訪問:

我這裡使用port-forward方式:

kubectl port-forward pod-name local-port:container-port

如:注意指定命名空間

kubectl port-forward mysql-min-6fdb6bb7bb-f5g9t -n mysql-min 3306:3306


登錄mysql:

mysql -h 127.0.0.1 -uroot -p9ZeNk0DghH

執行初始化sql,位於:

https://github.com/hepyu/saf/blob/master/saf-samples/saf-sample-allinone/saf-sample-allinone-service/sql/allinone.sql.txt


由於apollo我們只支持dev環境,所以需要修改一下數據庫記錄:

use ApolloPortalDB

update ServerConfig set Value='dev' where id=1\\G

否則apollo可能運行時出問題。


(4).編譯saf-boot-starter-sample-allinone


1.工程下載與編譯順序


git clone https://github.com/saf-group/saf.git

git clone https://github.com/saf-group/saf-boot-starter.git

git clone https://github.com/saf-group/saf-boot-starter-sample.git


他們的依賴關係是:

saf-boot-starter-sample.git依賴saf-boot-starter依賴saf。


所以編譯順序是:saf -> saf-boot-starter -> saf-boot-starter-sample。


2.編譯saf


由於saf-boot-starter使用的是saf-1.0.6,所以先下載這個tag。

git fetch origin tag saf-1.0.6

git checkout saf-1.0.6

mvn clean package install -Dmaven.test.skip=true


3.編譯saf-boot-starter


cd saf-boot-starter

mvn clean package install -Dmaven.test.skip=true


4.編譯saf-boot-starter-sample-allinone


cd saf-boot-starter-sample

mvn clean package install -Dmaven.test.skip=true


(5).進行apollo配置


進入目錄:

saf-boot-starter-sample-allinone/apollo-config


將apollo-config目錄中的配置文件配置到apollo配置中心:dev.apollo-portal.future.com

apollo默認用戶名是apollo,默認密碼是admin。


需要創建的項目/namespace,附帶每個namespace的內容地址:

配置內容的主路徑位於:

saf-boot-starter-sample-allinone/apollo-config


微服務框架saf-2:容器化部署allinone-demo


微服務框架saf-2:容器化部署allinone-demo


從上表可以看到,我們是對apollo配置中心的使用制定了自定義規約的,本文重點不在這裡,暫時不對此自定義規約進行詳細描述,後續會做詳細描述。


關於這部分,可以先參照文章:

裡邊有一張圖描述了自定義規約。


(6).容器化部署saf-boot-starter-sample-allinone-service


1.製作saf-boot-starter-sample-allinone-service鏡像


在目錄saf-boot-starter-sample-allinone/saf-boot-starter-sample-allinone-service下執行腳本docker.build.sh 製作docker鏡像:

sh docker.build.sh


如果失敗檢查下docker.build.sh和Dockerfile中的demo版本。


2.容器化saf-boot-starter-sample-allinone-service


進入目錄saf-boot-starter-sample-allinone-service/kubernetes:

直接執行腳本deploy.sh,會順次執行上述yaml文件,執行kubectl get pod -n inc查看Pod,


(7).容器化部署saf-sample-allinone-web


過程類似,這裡簡單描述。


進入目錄:

saf-boot-starter-sample-allinone/saf-boot-starter-sample-allinone-web


1.製作saf-boot-starter-sample-allinoe-web鏡像


在目錄saf-boot-starter-sample-allinone/saf-boot-starter-sample-allinone-web下執行腳本docker.build.sh 製作docker鏡像:

sh docker.build.sh


2.容器化saf-boot-starter-sample-allinoe-web


進入目錄saf-boot-starter-sample-allinoe-web/kubernetes直接執行腳本deploy.sh。


這裡有一個文件會把這個web服務掛到ingress上,從而將服務暴露到容器外部,供公網訪問:

saf-boot-starter-sample-allinone-web-prod-ingress.yaml


(8).驗證服務


我們先查看saf-boot-starter-sample-allinoe-web的域名:

<code>[root@future kubernetes]# kubectl get ingress -n inc

NAME HOSTS ADDRESS PORTS AGE

pro-apollo-configservice002 pro-apollo-configservice002 ip1 80 10d

pro-apollo-configservice003 pro-apollo-configservice003 ip1 80 10d

saf-sample-allinone-web-prod saf-web-allinone.future.com ip1 80 21m

service-apollo-portal-server dev.apollo-portal.future.com ip1 80 10d/<code>


配置host:

Ip1 saf-web-allinone.future.com


1.驗證spring boot actuator組件

瀏覽器訪問http://saf-web-allinone.future.com:30834/actuator/health

返回:{"status":"UP","details":{"diskSpace":{"status":"UP","details":{"total":105552769024,"free":54718111744,"threshold":10485760}}}}


2.驗證業務接口(redis-cluster,motanRPC)

業務接口使用了redis-cluster,motanRPC,jvmCache(guava),業務接口正常返回說明上述組件OK。


http://saf-web-allinone.future.com:30834/user/getUserDetail?userId=1

返回:

{"code":0,"msg":"getUserDetail success.","data":{"id":1,"name":"user1","shopList":[{"id":1,"name":"shop1","ownerId":1,"address":"address1"}]}}


http://saf-web-allinone.future.com:30834/shop/getShopDetail?shopId=1

返回:

{"code":0,"msg":"getShopDetail success.","data":{"id":1,"name":"shop1","ownerId":1,"address":"address1","owner":{"id":1,"name":"user1"}}}


http://saf-web-allinone.future.com:30834/config/getSMSConfig

返回:

{"code":0,"msg":"getSMSConfig success.","data":{"aliyunSMSUrl":"https://sms.aliyun.com/sendSMS","mobileSMSUrl":"https://sms.chinamobile.com/sendSMS","unicomSMSUrl":"https://sms.chinaunicom.com/sendSMS"}}


3.驗證jvmCache(guava)


這個只能到saf-service-allinone的pod容器裡看了,會定時打印guava的訪問統計信息,這個在真實業務中其實是可以做成metrics,然後加監控的,因為一般這麼用都是一個高訪問高併發的場景。

<code>framework.log-2019-11-28 22:09:00.001 [scheduling-1] [INFO] [-] [c.f.s.s.a.l.ShopModelSafWrapperLocalCache] - cache container current size is:5

framework.log-2019-11-28 22:09:00.001 [scheduling-1] [INFO] [-] [c.f.s.s.a.l.ShopModelSafWrapperLocalCache] - cache container: threadPool.queueInfo: size:0, remainingCapacity:2147483647

framework.log-2019-11-28 22:09:00.001 [scheduling-1] [INFO] [-] [c.f.s.s.a.l.ShopModelSafWrapperLocalCache] - cache container: threadPool.info: activeCount:0, corePoolSize:10, maximumPoolSize:10, poolSize:10, completedTaskCount:40, largestPoolSize:10, taskCount:40

framework.log-2019-11-28 22:09:00.001 [scheduling-1] [INFO] [-] [c.f.s.s.a.l.ShopModelSafWrapperLocalCache] - cache container: cache.stats: averageLoadPenalty:1.0904045166666666E7, evictionCount:0, hitCount:40, hitRate:0.8888888888888888, loadCount:42, loadExceptionCount:0, loadExceptionRate:0.0, loadSuccessCount:42, missCount:5, missRate:0.1111111111111111, requestCount:45, totalLoadTime:457969897,/<code>


4.驗證rocketmq使用


到rocketmq-console後臺查看:

http://pro-rocketmq-min-c0.console.future.com:30834/#/message

微服務框架saf-2:容器化部署allinone-demo

微服務框架saf-2:容器化部署allinone-demo


至此,驗證通過了redis-cluster, rocketmq, jvmCache(guava), spring boot actuator, motanRPC的正確使用。


5.通過saf-boot-starter-sample-allinone-service的日誌打印驗證


對應的代碼位於:

https://github.com/saf-group/saf-boot-starter-sample/blob/master/saf-boot-starter-sample-allinone/saf-boot-starter-sample-allinone-service/src/main/java/com/future/saf/sample/allinone/listener/AllinoneListener.java


微服務框架saf-2:容器化部署allinone-demo


(9).相關文檔



微服務框架saf-2:容器化部署allinone-demo


分享到:


相關文章: