您的位置:首页 > 其它

基于阿里云容器服务的微服务实践 - Part 3. 容器微服务实践

2017-02-12 20:27 615 查看

基于阿里云容器服务的微服务实践 - Part 3. 容器微服务实践

作者:chszs,未经博主允许不得转载。经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs

三、设置Docker的Swarm模式

Docker Swarm提供了一个本地集群能力,可以在单个虚拟Docker引擎中运行一组Docker引擎。



可以使用下面的命令来初始化Swarm模式:

docker swarm init


这将启动Swarm管理器。默认情况下,管理器阶段也是worker节点,而且可以只配置为管理器节点。

要查看关于单节点集群的信息,可以使用docker info命令:

Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 17
Server Version: 1.13.0
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Swarm: active
NodeID: 92mydh0e09ba5hx3wtmcmvktz
Is Manager: true
ClusterID: v68ikyaff7rdxpaw1j0c9i60s
Managers: 1
Nodes: 1
Orchestration:
Task History Retention Limit: 5
Raft:
Snapshot Interval: 10000
Number of Old Snapshots to Retain: 0
Heartbeat Tick: 1
Election Tick: 3
Dispatcher:
Heartbeat Period: 5 seconds
CA Configuration:
Expiry Duration: 3 months
Node Address: 192.168.65.2
Manager 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:
seccomp
Profile: 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): true
File Descriptors: 31
Goroutines: 124
System Time: 2017-01-27T08:25:58.032295342Z
EventsListeners: 1
No Proxy: *.local, 169.254/16
Username: chszs
Registry: https://index.docker.io/v1/ Experimental: true
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false


上面所述的集群只有一个节点,而且这个节点是一个管理器节点。

此外,多主机集群可以使用下面的方法来设置:

https://github.com/docker/labs/blob/master/developer-tools/java/chapters/ch08-aws.adoc

四、部署微服务

微服务可以这样启动:

docker stack deploy --compose-file=docker-compose.yml webapp


输出如下:

Creating network webapp_default
Creating service webapp_web
Creating service webapp_db


WildFly和Couchbase服务在这个节点上启动了,每一个服务都有单个的容器。如果Swarm模式在多个节点上启用,那么容器会分布到这些节点上。

这样会创建一个新的覆盖网络。这允许多个容器在不同的主机上相互通信。

可以使用docker service来验证WildFly服务和Couchbase服务是否正在运行:

ID            NAME        MODE        REPLICAS  IMAGE
a9pkiziw3vgw  webapp_db   replicated  1/1       chszs/couchbase:travel
hr5s6ue54kwj  webapp_web  replicated  1/1       chszs/couchbase-javaee:travel


服务的日志可以使用命令docker service logs if 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:

https://github.com/docker/labs/tree/master/developer-tools/java

上面是一个自学动手的实验,可以轻松上手使用Docker。

获取单条资源记录:

curl -v http://localhost:8080/airlines/resources/airline/137


输出结果如下:

*   Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
> GET /airlines/resources/airline/137 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: 131
< Date: Wed, 01 Feb 2017 03:05:02 GMT
<
* Connection #0 to host localhost left intact
{"travel-sample":{"country":"France","iata":"AF","callsign":"AIRFRANS","name":"Air France","icao":"AFR","id":137,"type":"airline"}}


创建一条新资源记录:

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


输出结果如下:

*   Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
> POST /airlines/resources/airline HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 104
>
* upload completely sent off: 104 out of 104 bytes
< HTTP/1.1 200 OK
< Connection: keep-alive
< X-Powered-By: Undertow/1
< Server: WildFly/10
< Content-Type: application/octet-stream
< Content-Length: 117
< Date: Wed, 01 Feb 2017 03:05:18 GMT
<
* Connection #0 to host localhost left intact
{"country":"France","iata":"A5","callsign":"AIRLINAIR","name":"Airlinair","icao":"RLA","id":"19810","type":"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


输出结果如下:

*   Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
> PUT /airlines/resources/airline/19810 HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 119
>
* upload completely sent off: 119 out of 119 bytes
< HTTP/1.1 200 OK
< Connection: keep-alive
< X-Powered-By: Undertow/1
< Server: WildFly/10
< Content-Type: application/octet-stream
< Content-Length: 118
< Date: Wed, 01 Feb 2017 03:05:41 GMT
<
* Connection #0 to host localhost left intact
{"country":"France","iata":"A5","callsign":"AIRLINAIR","name":"Airlin Air","icao":"RLA","id":"19810","type":"airline"}


删除一条资源记录:

curl -v -X DELETE http://localhost:8080/airlines/resources/airline/19810


输出结果如下:

*   Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
> DELETE /airlines/resources/airline/19810 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: 136
< Date: Wed, 01 Feb 2017 03:05:57 GMT
<
* Connection #0 to host localhost left intact
{"travel-sample":{"country":"France","iata":"A5","callsign":"AIRLINAIR","name":"Airlin Air","icao":"RLA","id":"19810","type":"airline"}}


六、删除微服务

可以使用docker stack rm webapp命令来移除微服务:

Removing service webapp_web
Removing service webapp_db
Removing network webapp_default


七、总结

尽管在阿里云的产品系列中还没看到Docker容器服务产品,但这并不代表就不能在阿里云上部署和实施容器类的服务和应用。实际上,阿里云提供了一个Docker市场,通过阿里云的Docker市场,开发者可以方便地进行Docker容器的创建、分享、下载和评价。而对于Dockerhub官方的镜像,阿里云的Docker市场还提供了镜像加速服务,让开发者可以更快地下载和使用这些来自官方的Docker镜像。借助阿里云虚拟主机,我们可以在单台或多台主机上搭建和部署多个Docker容器,每个Docker容器上又部署各种各样的业务服务。通过这些业务服务,构建我们的微服务生态系统。

至此,本系列文章已全部结束。另两篇文章见:

基于阿里云容器服务的微服务实践 - Part 2. Docker Compose

基于阿里云容器服务的微服务实践 - Part 1. 微服务与Docker
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: