在上一篇文章里http://www.cnblogs.com/lyhero11/p/8686058.html, 講解了如何在windows10下安裝docker社區版。
那如何利用docker落地一個分布式微服務架構的系統呢? github上有一個不錯的開源項目PiggyMetrics,通過這個可以學習用docker和spring cloud做分布式微服務架構。
原項目地址:https://github.com/sqshq/PiggyMetrics
國內有個網友把里邊應用的mongodb數據庫示例由4個合並成1個、更適合本機學習用:https://github.com/huangmeng4520/PiggyMetrics
搭建文檔:https://www.jianshu.com/p/e26570d1b297?mType=Group
按照里邊步驟搭建問題都不大,但整個項目的容器啟動之后,其他都沒問題就mongodb一直連不上。無論是應用java代碼連接,還是通過第三方數據庫工具Robo 3T都連不上,而進入到該mongo容器內,用mongo命令卻是可以連上的。
最后https://stackoverflow.com/questions/43781672/mongo-doesnt-run-after-installing-on-docker-centos-image-failed-to-connect-to 給了我啟發,
在Mongodb啟動的時候帶上--bind_ip_all參數。
於是乎,修改PiggyMetrics\mongodb\init.sh文件:
#!/bin/bash if test -z "$MONGODB_PASSWORD"; then echo "MONGODB_PASSWORD not defined" exit 1 fi auth="-u user -p $MONGODB_PASSWORD" # MONGODB USER CREATION ( echo "setup mongodb auth" create_user="if (!db.getUser('user')) { db.createUser({ user: 'user', pwd: '$MONGODB_PASSWORD', roles: [ {role:'readWrite', db:'piggymetrics'} ]}) }" until mongo piggymetrics --eval "$create_user" || mongo piggymetrics $auth --eval "$create_user"; do sleep 5; done killall mongod sleep 1 killall -9 mongod ) & # INIT DUMP EXECUTION ( if test -n "$INIT_DUMP"; then echo "execute dump file" until mongo piggymetrics $auth $INIT_DUMP; do sleep 5; done fi ) & echo "start mongodb without auth" chown -R mongodb /data/db gosu mongodb mongod "$@" --bind_ip_all echo "restarting with auth on" sleep 5 exec gosu mongodb mongod --auth "$@" --bind_ip_all
重新build sqshq/piggymetrics-mongodb image,然后重新啟動data-mongodb容器。一切正常。
最后記錄一下學習這個項目過程中的筆記吧:
https://blog.csdn.net/shi1451042748/article/details/52996046
https://blog.csdn.net/nihaomanihao11/article/details/73822755
https://www.jianshu.com/p/e26570d1b297?mType=Group
https://github.com/sqshq/PiggyMetrics
開發模式:
docker-compose -f docker-compose.yml -f docker-compose.dev.yml build
docker-compose -f docker-compose.yml -f docker-compose.dev.yml up
阿里雲鏡像加速:
https://wk8aklru.mirror.aliyuncs.com
DaoCloud鏡像加速:
http://7c698057.m.daocloud.io
查看所有容器
docker ps -a
查看所有鏡像
docker images
docker rm $(docker ps -aq) 刪除所有的容器
#
# 刪除容器應用
#
docker ps -a --format "{{.ID}}" | foreach {
docker stop $_
docker rm $_
}
#
# 刪除本地容器鏡像
#
docker images --format "{{.ID}}" | foreach {
docker rmi stop $_
}
PiggyMetrics項目重要端口:
http://localhost:80 - Gateway
http://localhost:8761 - Eureka Dashboard
http://localhost:9000/hystrix - Hystrix Dashboard (paste Turbine stream link on the form)
http://localhost:8989 - Turbine stream (source for the Hystrix Dashboard)
http://localhost:15672 - RabbitMq management (default login/password: guest/guest)
mongodb啟動腳本init.sh需用notepad++轉成unix格式
init.sh中mongodb的ip綁定到了127.0.0.1,可用如下方法去除Ip綁定,在init.sh最后做如下修改:
gosu mongodb mongod "$@" --bind_ip_all
exec gosu mongodb mongod --auth "$@" --bind_ip_all
兩個地方都加上 --bind_ip_all