从本章开始,我们一起在Docker下实战Spring Cloud的开发和部署,整个实战分为以下三章进行:
本章是三部曲的第一篇,我们用最短的时间、最简单的操做一起在Docker下体验SpringCloud的注册中心、服务提供者、服务消费者;
原文地址:http://blog.csdn.net/boling_cavalry/article/details/79177930
本次实战的网络机器信息如下图:
这个实战环境提供以下服务:
环境中的eureka、provider、consumer都是docker容器,它们的名字和IP如下表所示:
容器名 | ip | 功能 |
---|---|---|
springcloud_eureka_1 | 172.17.0.2 | 注册中心 |
springcloud_provider_1 | 172.17.0.3 | 提供服务 |
springcloud_consumer_1 | 172.17.0.4 | 提供浏览器访问的http接口,对应的响应会调用provider的服务 |
本次实战的环境信息如下:
创建一个docker-compose.yml文件,内容如下:
eureka:
image: bolingcavalry/eureka-server:0.0.1-SNAPSHOT
ports:
- "8081:8080"
tty: true
provider:
image: bolingcavalry/service-provider:0.0.1-SNAPSHOT
links:
- eureka:eurekahost
tty: true
consumer:
image: bolingcavalry/service-consumer:0.0.1-SNAPSHOT
links:
- eureka:eurekahost
ports:
- "8083:8080"
tty: true
在docker-compose.yml所在目录下执行以下命令,启动所有容器:
sudo docker-compose up -d
启动时控制台显示以下信息:
ubuntu@VM-129-56-ubuntu:~/work/springcloud$ clear
Creating springcloud_eureka_1 ... done
Creating springcloud_eureka_1 ...
Creating springcloud_consumer_1 ... done
Creating springcloud_provider_1 ... done
执行命令sudo docker logs -f springcloud_eureka_1查看注册中心启动的情况,看到如下信息时表示注册中心启动成功:
2018-01-26 16:56:52.540 INFO 1 --- [ Thread-11] c.n.e.r.PeerAwareInstanceRegistryImpl : Got 1 instances from neighboring DS node
2018-01-26 16:56:52.540 INFO 1 --- [ Thread-11] c.n.e.r.PeerAwareInstanceRegistryImpl : Renew threshold is: 1
2018-01-26 16:56:52.540 INFO 1 --- [ Thread-11] c.n.e.r.PeerAwareInstanceRegistryImpl : Changing status to UP
2018-01-26 16:56:52.601 INFO 1 --- [ Thread-11] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
2018-01-26 16:56:52.833 INFO 1 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
执行命令sudo docker logs -f springcloud_provider_1查看服务提供者的启动情况,看到如下信息时表示服务提供者启动成功:
2018-01-26 16:56:51.826 INFO 1 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-01-26 16:56:51.833 INFO 1 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8080
2018-01-26 16:56:51.871 INFO 1 --- [ main] c.b.s.ServiceProviderApplication : Started ServiceProviderApplication in 58.522 seconds (JVM running for 62.865)
执行命令sudo docker logs -f springcloud_consumer_1查看服务消费者的启动情况,看到如下信息时表示服务消费者启动成功:
2018-01-26 16:56:54.097 INFO 1 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_SERVICE-CONSUMER/d39efe3da1c0:service-consumer:8080: registering service...
2018-01-26 16:56:54.649 INFO 1 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-01-26 16:56:54.655 INFO 1 --- [ main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8080
2018-01-26 16:56:54.676 INFO 1 --- [ main] c.b.s.ServiceConsumerApplication : Started ServiceConsumerApplication in 61.268 seconds (JVM running for 65.699)
访问http://localhost:8081/可以查看注册中心的首页,如下图所示,红框中表示服务提供者和消费者都连上了注册中心:
访问http://www.bolingcavalry.com:8083/user/aaa/bbb可以调用到服务消费者server的网页,此时服务消费者会调用服务提供者的服务,服务提供者的响应是用户的名称和id信息,以及自己的IP地址,consumer收到这些信息后,返回到网页,如下图:
至此,我们已在Docker下快速体验了SpringCloud的注册中心以及服务的提供和消费等功能,接下来的章节我们将这些功能背后的实现逐个展开;