如何通過以下方式在Docker中運行Spring-Cloud MicroService拓撲:
Docker端口映射到外部主機網絡
Docker Links
Docker Bridge網絡
- 從Docker機器的外部訪問Docker容器中微服務
Eureka和MicroServices可通過docker機器的靜態IP地址訪問。客戶端只需獲取eureka註冊表並通過docker-machine的IP調用特定的MicroService,並將容器的端口映射到客戶端的網絡端口。
如果設置eureka.instance.prefer-ip-address = true,則使用Eureka的MicroService註冊將使用eureka.instance.ip-address指定的IP地址(解析後默認爲主機的IP)。
因此MicroService Spring Boot屬性文件將是:
eureka.client.serviceUrl.defaultZone=http:<font><i>//${DOCKER_HOST_IP}:9761/eureka</i></font><font>
.
eureka.instance.preferIpAddress=<b>true</b>
eureka.instance.ip-address=${DOCKER_HOST_IP}
.
</font>
設置說明:
在Eureka上註冊此MicroService,運行時間爲: http://${DOCKER_HOST_IP}:9761 / eureka
MicroService將通過$ {DOCKER_HOST_IP}系統屬性指定的IP暴露給docker的外部世界。我們將在這裏注入docker機器的當前IP地址。
同樣在 eureka server端配置:
eureka.instance.prefer-ip-address=<b>true</b>
eureka.instance.ip-address=${DOCKER_HOST_IP}
Eureka服務器實例將在$ {DOCKER_HOST_IP}上運行並可訪問。
Docker maven集成
作爲Docker Maven插件,我選擇了 Spotify的Docker-Maven-Plugin 。功能齊全。
Eureka Dockerfile:
FROM java:8-jre
MAINTAINER Tomas Kloucek <[email protected]>
ADD ./demo-0.0.1-SNAPSHOT.war /app/
CMD [<font>"java"</font><font>, </font><font>"-Xmx512m"</font><font>, </font><font>"-jar"</font><font>, </font><font>"/app/demo-0.0.1-SNAPSHOT.war"</font><font>]
</font>
Eureka pom.xml (plugin configuration):
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.11</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>src/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.war</include>
</resource>
</resources>
</configuration>
</plugin>
構建Docker鏡像並在容器中運行它們:
(in the spring-microservice-registry folder)
mvn clean install docker:build
docker images (verify your image was built)
docker run --env DOCKER_HOST_IP=$(boot2docker ip) -it --name eureka -p 9761:9761 registry/demo
訪問 http://DOCKER_HOST_IP:9761/ 確認eureka服務器正在運行
微服務客戶端啓動:
(in the spring-microservice-service folder)
mvn clean install -DskipTests docker:build
docker images (verify your image was built)
docker run --env DOCKER_HOST_IP=$(boot2docker ip) -it --name service -p 8080:8080 service/demo
docker ps (verify that service container is running)
從外部調用MicroService
外部客戶只需要知道eureka服務器的地址
application.properties:
eureka.client.serviceUrl.defaultZone=http:<font><i>//${DOCKER_HOST_IP}:9761/eureka</i></font><font>
</font>
測試在Docker中啓動的微服務:
(in the spring-microservice-client folder) mvn clean install java -jar -DDOCKER_HOST_IP=$(boot2docker ip) target/demo-0.0.1-SNAPSHOT.war
2.Docker Links
eureka服務器application.properties:
eureka.instance.prefer-ip-address = false
eureka.instance.hostname = eureka
通過此設置,Eureka服務器註冊表將可以在地址 http://eureka:<port>/eureka 下載給客戶端,
在Docker中啓動Eureka註冊表:
(in the spring-microservice-registry folder)
mvn clean install docker:build
docker images (verify your image was built)
docker run -it -p 9761:9761 --name eureka registry/demo
我只使用端口映射,因爲我想使用eureka網頁。重要的是使用容器名。
讓我們將先前配置的Eureka設置爲MicroService的註冊表源,在微服務項目中配置:
spring.application.name=personsService
eureka.client.serviceUrl.defaultZone=http:<font><i>//eureka:9761/eureka</i></font><font>
eureka.client.healthcheck.enabled=<b>true</b>
eureka.instance.preferIpAddress=false
eureka.instance.hostname=service
ribbon.eureka.enabled=<b>true</b>
</font>
啓動Docker中微服務:
(in the spring-microservice-service folder)
mvn clean install -DskipTests docker:build
docker images (verify your image was built)
docker run -it --link eureka:eureka --name service service/demo
docker ps (verify that service container is running)
使用“docker run”在service/demo容器將在別名eureka下看到Eureka服務器,讓我們驗證(運行以下命令):
docker exec -it service bash (<b>this</b> will connect you to the MicroService cont.)
ping eureka
輸出:
ping eureka
PING eureka (172.17.0.2): 56 data bytes
64 bytes from 172.17.0.2: icmp_seq=0 ttl=64 time=0.269 ms
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.084 ms
網關容器
網關容器也需要鏈接到eureka和服務容器,因爲eureka將以 http://service:8080 的形式提供ribbon負載平衡的MicroService地址。因此網關需要了解“服務”的含義。
application.properties:
server.port=8888
hystrix.command.invokePersonsMicroService.fallback.enabled=<b>true</b>
hystrix.command.invokePersonsMicroService.metrics.rollingStats.timeInMilliseconds=35000
hystrix.command.invokePersonsMicroService.circuitBreaker.sleepWindowInMilliseconds=5000
hystrix.command.invokePersonsMicroService.circuitBreaker.requestVolumeThreshold=6
hystrix.command.invokePersonsMicroService.circuitBreaker.errorThresholdPercentage=100
hystrix.command.invokePersonsMicroService.execution.isolation.strategy=THREAD
eureka.client.serviceUrl.defaultZone=http:<font><i>//eureka:9761/eureka</i></font><font>
eureka.client.healthcheck.enabled=<b>true</b>
eureka.client.registryFetchIntervalSeconds=5
eureka.client.fetch-registry=<b>true</b>
</font>
運行Docker中網關:
(in the spring-microservice-client folder)
mvn clean install -DskipTests docker:build
docker images (verify that image was built)
docker run -it --link eureka:eureka --link service:service --name client client/demo
運行鏡像後,您應該再次看到重複輸出:
Simple MicroService Feign invocation result :{<font>"persons"</font><font>:[{</font><font>"name"</font><font>:</font><font>"Tomas"</font><font>,</font><font>"surname"</font><font>:</font><font>"Kloucek"</font><font>,</font><font>"department"</font><font>:</font><font>"Programmer"</font><font>}]}
</font>
- Docker橋接網絡
展示如何在所有節點之間創建基本的“橋樑”,這樣他們就能看到對方!那麼不需要手動Docker鏈接。
運行以下命令:
docker network create spring-cloud-network
既然我們現在有了一個網絡,讓我們把所有的節點放在其中
Maven編譯和鏡像構建與前面部分相同。
要啓動eureka容器,請使用:
docker run -i -t --name eureka --net spring-cloud-network -p 9761:971 registry/demo
要啓動MicroService容器,請使用:
docker run -it --name service --net spring-cloud-network service/demo
網格客戶端,啓動:
docker run -it --name client --net spring-cloud-network client/demo
讓我們測試是否看到彼此:
docker exec -it service bash
60623256:/# ping eureka
PING eureka (172.19.0.2): 56 data bytes
64 bytes from 172.19.0.2: icmp_seq=0 ttl=64 time=0.085 ms
64 bytes from 172.19.0.2: icmp_seq=1 ttl=64 time=0.100 ms
64 bytes from 172.19.0.2: icmp_seq=2 ttl=64 time=0.110 ms
或
ping client
PING client (172.19.0.4): 56 data bytes
64 bytes from 172.19.0.4: icmp_seq=0 ttl=64 time=0.110 ms
64 bytes from 172.19.0.4: icmp_seq=1 ttl=64 time=0.106 ms
64 bytes from 172.19.0.4: icmp_seq=2 ttl=64 time=0.089 ms
如果您對每個容器彼此看到的拓撲結構都很好,那麼基本的docker bridge網絡就可以實現了,但請記住它只能在一個主機上運行。對於多個主機,您需要使用Overlay網絡或服務網格