結合Docker運行Spring Cloud微服務的多種方式

如何經過如下方式在Docker中運行Spring-Cloud MicroService拓撲:java

  • Docker端口映射到外部主機網絡
  • Docker Bridge網絡

1. 從Docker機器的外部訪問Docker容器中微服務git

Eureka和MicroServices可經過docker機器的靜態IP地址訪問。客戶端只需獲取eureka註冊表並經過docker-machine的IP調用特定的MicroService,並將容器的端口映射到客戶端的網絡端口。github

若是設置eureka.instance.prefer-ip-address = true,則使用Eureka的MicroService註冊將使用eureka.instance.ip-address指定的IP地址(解析後默認爲主機的IP)。spring

所以MicroService Spring Boot屬性文件將是:docker

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>

設置說明:bash

  • 在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集成app

做爲Docker Maven插件,我選擇了 Spotify的Docker-Maven-Plugin 。功能齊全。maven

Eureka Dockerfile:

FROM java:8-jre
MAINTAINER Tomas Kloucek <tomas.kloucek@embedit.cz>

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>

3. 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網絡或服務網格

相關文章
相關標籤/搜索