docker 部署java_使用Docker堆栈部署的微服务-WildFly,Java EE和Couchbase
docker 部署java
關于微服務的資料很多,只是用谷歌搜索就可以了 ! 幾年前,我在比利時的Devoxx上發表了有關將單片重構為微服務的演講,它獲得了很好的評價:
該博客將展示Docker如何簡化微服務的創建和關閉。
該博客中使用的所有代碼都在github.com/arun-gupta/couchbase-javaee上 。
使用撰寫的微服務定義
Docker 1.13引入了v3的Docker Compose 。 語法上的更改很小,但主要區別是添加了deploy屬性。 此屬性允許為容器指定副本,滾動更新和重新啟動策略。
我們的微服務將使用預先部署的Java EE應用程序啟動WldFly應用程序服務器。 該應用程序將與Couchbase數據庫對話以獲取CRUD應用程序數據。
這是Compose定義:
version: '3' services:web:image: arungupta/couchbase-javaee:travelenvironment:- COUCHBASE_URI=dbports:- 8080:8080- 9990:9990depends_on:- dbdb:image: arungupta/couchbase:travelports:- 8091:8091- 8092:8092 - 8093:8093 - 11210:11210在此撰寫文件中:
Compose定義文件中的depends_on屬性可確保容器啟動順序。 但是需要在容器內運行的應用程序確保應用程序級別的啟動。 在我們的例子中,WildFly的啟動速度非常快,但是要花幾秒鐘才能啟動數據庫。 這意味著WildFly中部署的Java EE應用程序無法與數據庫進行通信。 這概述了構建微服務應用程序時的最佳實踐:您必須進行防御性編碼,并在應用程序初始化中確保依賴的微服務已啟動,而無需假定啟動順序。 這顯示在https://github.com/arun-gupta/couchbase-javaee/blob/master/src/main/java/org/couchbase/sample/javaee/Database.java的數據庫初始化代碼中。 它執行以下檢查:
可以在單個主機上使用docker-compose up -d命令啟動該應用程序。 或使用docker stack deploy命令以群集模式運行的Docker引擎集群。
設置Docker Swarm模式
使用以下命令初始化Swarm模式:
docker swarm init這將啟動“群管理器”。 默認情況下,管理器節點也是工作程序,但可以配置為僅管理器。
使用命令docker info命令查找有關此單節點集群的一些信息:
Containers: 0Running: 0Paused: 0Stopped: 0 Images: 17 Server Version: 1.13.0 Storage Driver: overlay2Backing Filesystem: extfsSupports d_type: trueNative Overlay Diff: true Logging Driver: json-file Cgroup Driver: cgroupfs Plugins:Volume: localNetwork: bridge host ipvlan macvlan null overlay Swarm: activeNodeID: 92mydh0e09ba5hx3wtmcmvktzIs Manager: trueClusterID: v68ikyaff7rdxpaw1j0c9i60sManagers: 1Nodes: 1Orchestration:Task History Retention Limit: 5Raft:Snapshot Interval: 10000Number of Old Snapshots to Retain: 0Heartbeat Tick: 1Election Tick: 3Dispatcher:Heartbeat Period: 5 secondsCA Configuration:Expiry Duration: 3 monthsNode Address: 192.168.65.2Manager Addresses:192.168.65.2:2377 Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: 03e5862ec0d8d3b3f750e19fca3ee367e13c090e runc version: 2f7393a47307a16f8cee44a37b262e8b81021e3e init version: 949e6fa Security Options:seccompProfile: default Kernel Version: 4.9.5-moby Operating System: Alpine Linux v3.5 OSType: linux Architecture: x86_64 CPUs: 4 Total Memory: 1.952 GiB Name: moby ID: SGCM:KDRD:G3M7:PZHN:J4RL:VFFR:G2SR:EKD5:JV4J:RL3X:LF7T:XF6V Docker Root Dir: /var/lib/docker Debug Mode (client): false Debug Mode (server): trueFile Descriptors: 31Goroutines: 124System Time: 2017-01-27T08:25:58.032295342ZEventsListeners: 1 No Proxy: *.local, 169.254/16 Username: arungupta Registry: https://index.docker.io/v1/ Experimental: true Insecure Registries:127.0.0.0/8 Live Restore Enabled: false該集群有1個節點,即節點。
或者,可以使用Docker for AWS輕松設置多主機集群。
部署微服務
可以通過以下方式啟動微服務:
docker stack deploy --compose-file=docker-compose.yml webapp顯示輸出:
Creating network webapp_default Creating service webapp_web Creating service webapp_dbWildFly和Couchbase服務在此節點上啟動。 每個服務都有一個容器。 如果在多個節點上啟用了Swarm模式,則容器將分布在多個節點上。
創建一個新的覆蓋網絡。 這允許不同主機上的多個容器相互通信。
使用docker docker service ls驗證WildFly和Couchbase服務是否正在運行:
可以使用docker service logs -f webapp_web查看該服務的docker service logs -f webapp_web :
webapp_web.1.wby0b04t7bap@moby | ========================================================================= webapp_web.1.wby0b04t7bap@moby | webapp_web.1.wby0b04t7bap@moby | JBoss Bootstrap Environment webapp_web.1.wby0b04t7bap@moby | webapp_web.1.wby0b04t7bap@moby | JBOSS_HOME: /opt/jboss/wildfly webapp_web.1.wby0b04t7bap@moby | webapp_web.1.wby0b04t7bap@moby | JAVA: /usr/lib/jvm/java/bin/java webapp_web.1.wby0b04t7bap@moby | webapp_web.1.wby0b04t7bap@moby | JAVA_OPTS: -server -Xms64m -Xmx512m -XX:MetaspaceSize=96M -XX:MaxMetaspaceSize=256m -Djava.net.preferIPv4Stack=true -Djboss.modules.system.pkgs=org.jboss.byteman -Djava.awt.headless=true webapp_web.1.wby0b04t7bap@moby | webapp_web.1.wby0b04t7bap@moby | =========================================================================. . .webapp_web.1.wby0b04t7bap@moby | 23:14:15,811 INFO [org.jboss.as.server] (ServerService Thread Pool -- 34) WFLYSRV0010: Deployed "airlines.war" (runtime-name : "airlines.war") webapp_web.1.wby0b04t7bap@moby | 23:14:16,076 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: Http management interface listening on http://127.0.0.1:9990/management webapp_web.1.wby0b04t7bap@moby | 23:14:16,077 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin console listening on http://127.0.0.1:9990 webapp_web.1.wby0b04t7bap@moby | 23:14:16,077 INFO [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: WildFly Full 10.1.0.Final (WildFly Core 2.2.0.Final) started in 98623ms - Started 443 of 691 services (404 services are lazy, passive or on-demand) 確保等待最后一條日志語句顯示。存取微服務
從微服務獲取10家航空公司:
curl -v http://localhost:8080/airlines/resources/airline結果顯示為:
* Trying ::1... * Connected to localhost (::1) port 8080 (#0) > GET /airlines/resources/airline HTTP/1.1 > Host: localhost:8080 > User-Agent: curl/7.43.0 > Accept: */* > < HTTP/1.1 200 OK < Connection: keep-alive < X-Powered-By: Undertow/1 < Server: WildFly/10 < Content-Type: application/octet-stream < Content-Length: 1402 < Date: Fri, 03 Feb 2017 17:02:45 GMT < * Connection #0 to host localhost left intact [{"travel-sample":{"country":"United States","iata":"Q5","callsign":"MILE-AIR","name":"40-Mile Air","icao":"MLA","id":10,"type":"airline"}}, {"travel-sample":{"country":"United States","iata":"TQ","callsign":"TXW","name":"Texas Wings","icao":"TXW","id":10123,"type":"airline"}}, {"travel-sample":{"country":"United States","iata":"A1","callsign":"atifly","name":"Atifly","icao":"A1F","id":10226,"type":"airline"}}, {"travel-sample":{"country":"United Kingdom","iata":null,"callsign":null,"name":"Jc royal.britannica","icao":"JRB","id":10642,"type":"airline"}}, {"travel-sample":{"country":"United States","iata":"ZQ","callsign":"LOCAIR","name":"Locair","icao":"LOC","id":10748,"type":"airline"}}, {"travel-sample":{"country":"United States","iata":"K5","callsign":"SASQUATCH","name":"SeaPort Airlines","icao":"SQH","id":10765,"type":"airline"}}, {"travel-sample":{"country":"United States","iata":"KO","callsign":"ACE AIR","name":"Alaska Central Express","icao":"AER","id":109,"type":"airline"}}, {"travel-sample":{"country":"United Kingdom","iata":"5W","callsign":"FLYSTAR","name":"Astraeus","icao":"AEU","id":112,"type":"airline"}}, {"travel-sample":{"country":"France","iata":"UU","callsign":"REUNION","name":"Air Austral","icao":"REU","id":1191,"type":"airline"}}, {"travel-sample":{"country":"France","iata":"A5","callsign":"AIRLINAIR","name":"Airlinair","icao":"RLA","id":1203,"type":"airline"}}]Docker for Java Developers研討會是一個自定進度的動手實驗,可讓您輕松開始使用Docker。
獲取單個資源:
curl -v http://localhost:8080/airlines/resources/airline/137創建一個新資源:
curl -v -H "Content-Type: application/json" -X POST -d '{"country":"France","iata":"A5","callsign":"AIRLINAIR","name":"Airlinair","icao":"RLA","type":"airline"}' http://localhost:8080/airlines/resources/airline更新資源:
curl -v -H "Content-Type: application/json" -X PUT -d '{"country":"France","iata":"A5","callsign":"AIRLINAIR","name":"Airlin Air","icao":"RLA","type":"airline","id": "19810"}' http://localhost:8080/airlines/resources/airline/19810刪除資源:
curl -v -X DELETE http://localhost:8080/airlines/resources/airline/19810每個命令的詳細輸出在github.com/arun-gupta/couchbase-javaee上 。
刪除微服務
可以使用命令docker stack rm webapp刪除微服務:
Removing service webapp_web Removing service webapp_db Removing network webapp_default想要開始使用Couchbase嗎? 查看Couchbase入門套件 。
想更多地了解如何在容器中運行Couchbase?
- 容器上的Couchbase
- Couchbase論壇
- Couchbase開發人員門戶
- @couchhasedev和@couchbase
翻譯自: https://www.javacodegeeks.com/2017/02/microservice-using-docker-stack-deploy-wildfly-java-ee-couchbase.html
docker 部署java
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的docker 部署java_使用Docker堆栈部署的微服务-WildFly,Java EE和Couchbase的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: xml linux阅读工具(xml li
- 下一篇: url转发 备案(Url备案库)