结合Docker运行Spring Cloud微服务的多种方式

如何通过以下方式在Docker中运行Spring-Cloud MicroService拓扑:

  • Docker端口映射到外部主机网络
  • Docker Bridge网络

1. 从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://${DOCKER_HOST_IP}:9761/eureka
.
eureka.instance.preferIpAddress=true
eureka.instance.ip-address=${DOCKER_HOST_IP}
.

设置说明:

  • 在Eureka上注册此MicroService,运行时间为: http://${DOCKER_HOST_IP}:9761 / eureka
  • MicroService将通过$ {DOCKER_HOST_IP}系统属性指定的IP暴露给docker的外部世界。我们将在这里注入docker机器的当前IP地址。

同样在 eureka server端配置:

eureka.instance.prefer-ip-address=true
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 

ADD ./demo-0.0.1-SNAPSHOT.war /app/

CMD ["java", "-Xmx512m", "-jar", "/app/demo-0.0.1-SNAPSHOT.war"]

Eureka pom.xml (plugin configuration):

 
                com.spotify
                docker-maven-plugin
                0.4.11
                
                    ${docker.image.prefix}/${project.artifactId}
                    src/docker
                    
                        
                            /
                            ${project.build.directory}
                            ${project.build.finalName}.war
                        
                    
                
            

构建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://${DOCKER_HOST_IP}:9761/eureka

测试在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:/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://eureka:9761/eureka
eureka.client.healthcheck.enabled=true

eureka.instance.preferIpAddress=false
eureka.instance.hostname=service

ribbon.eureka.enabled=true

启动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 (this 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=true
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://eureka:9761/eureka

eureka.client.healthcheck.enabled=true
eureka.client.registryFetchIntervalSeconds=5
eureka.client.fetch-registry=true

运行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 :{"persons":[{"name":"Tomas","surname":"Kloucek","department":"Programmer"}]}

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网络或服务网格

你可能感兴趣的:(结合Docker运行Spring Cloud微服务的多种方式)