使用Docker Compose編排Spring Cloud微服務

微服務項目名稱 項目微服務中的角色
microservice-discovery-eureka 服務發現組件
microservice-provider-user 服務提供者
microservice-consumer-movie-ribbon-hystrix 服務消費者
microservice-gateway-zuul API Gateway
microservice-hystrix-turbine Hystrix聚合監控工具
microservice-hystrix-dashboard Hystrix監控界面

微服務構建實例

1:使用Maven插件構建鏡像,在各個項目的pom.xml中添加如下內容。java

<!-- 添加docker-maven插件 -->
<plugin>
	<groupId>com.sptify</groupId>
	<artifactId>docker-maven-plugin</artifactId>
	<version>0.4.13</version>
	<configuration>
		<imageName>itmuch/${project.artifactId}:${project.version}</imageName>
		<forceTages>true</forceTages>
		<baseImage>java</baseImage>
		<entryPoint>["java","-jar","/${project.build.finalName}.jar"]</entryPoint>
		<resoureces>
			<resourece>
				<targetPath>/</targetPath>
				<directory>${project.build.directory}</directory>
				<include>${project.build.finalName}.jar</include>
			</resource>
		</resources>
	</configuration>
</plugin>

由配置可知,構建出來的鏡像名稱是itmuch/各個微服務的artifactId:各個微服務的版本,例如:microservice-discovery-eureka:0.0.1-SNAPSHOT。web

2:爲Eureka Server所在容器配置一個主機名(如discover),並讓各個微服務使用主機名訪問Eureka Server。
將全部微服務eureka.client.serviceUrl.defaultZone修改成以下內容:spring

eureka:
	client:
		serviceUrl:
			defaultZone:http://discovery:8761/eureka/

3:在每一個項目的根目錄執行如下命令,構建Docker鏡像。docker

mvn clean package docker:build

4:編寫docker-compose.yml網絡

version: '3'
#Version 2 file format的固定寫法,爲project定義服務
services:
	#指定服務名稱
	microservice-discovery-eureka:
		#指定服務所使用的鏡像
		image: itmuch/microservice-discovery-eureka:0.0.1-SNAPSHOT
		#暴露端口信息
		ports:
			- "8761:8761"
	microservice-provider-user:
		image: itmuch/microservice-provider-user:0.0.1-SNAPSHOT
		#鏈接到microservice-discovery-eureka,這邊使用的是SERVICE:ALIAS的形式
		links:
			-	microservice-disvoery-eureka:discovery
			-
	microservice-consumer-movie-ribbon-hystrix:
		image: itmuch/microservice-consumer-movie-ribbon-hystrix:0.0.1-SNAPSHOT
		links:
			-	microservice-disvoery-eureka:discovery
	microservice-gateway-zuul:
		image: itmuch/microservice-gateway-zuul:0.0.1-SNAPSHOT
		links:
			-	microservice-disvoery-eureka:discovery
	microservice-hystrix-dashboard:
		image: itmuch/microservice-hystrix-dashboard:0.0.1-SNAPSHOT
		links:
			-	microservice-disvoery-eureka:discovery
	microservice-hystrix-turbine:
		image: itmuch/microservice-hystrix-turbine:0.0.1-SNAPSHOT
		links:
			-	microservice-disvoery-eureka:discovery

測試,執行docker-compose up啓動項目,便可看到Eureka Server上的微服務列表服務發佈成功。maven

簡化Compose的編寫

同一個Compose工程中的全部服務共享一個隔離網絡,可以使用服務名稱做爲主機名來發現其餘服務。ide

所以,將docker-compose.yml簡化:svg

version: '2'
services:
	discovery:
		image: itmuch/discovery-eureka:0.0.1-SNAPSHOT
		ports:
			-	"8761:8761"
	microservice-provider-user:
		image: itmuch/microservice-provider-user:0.0.1-SNAPSHOT
	microservice-consumer-movie-ribbon-hystrix:
		image: itmuch/microservice-consumer-movie-ribbon-hystrix:0.0.1-SNAPSHOT
	microservice-gateway-zuul:
		image: itmuch/microservice-gateway-zuul:0.0.1-SNAPSHOT
	microservice-hystrix-dashboard:
		image: itmuch/microservice-hystrix-dashboard:0.0.1-SNAPSHOT
	microservice-hystrix-turbine:
		image: itmuch/microservice-hystrix-turbine:0.0.1-SNAPSHOT

編排高可用的Eureka Server

使用Compose編排Eureka Server集羣。微服務

1.執行如下命令構建Dokcer鏡像。工具

mvn clean package docker:build

2.編寫docker-compose.yml

version: '3'
services:
	microservice-discovery-eureka-ha1:
		hostname: peer1   #指定hostname
		image: itmuch/discovery=eureka-ha:0.0.1:SNAPSHOT
		links:
			-	discovery-eureka-ha2
		ports:
			-	"8761:8761"
		environment:
			-	spring.profiles.active=peer1
	microservice-discovery-eureka-ha2:
		hostname: peer2   #指定hostname
		image: itmuch/discovery=eureka-ha:0.0.1:SNAPSHOT
		links:
			-	discovery-eureka-ha1
		ports:
			-	"8762:8762"
		environment:
			-	spring.profiles.active=peer2

3.執行如下命令啓動項目。

docker-compose up

可是,這樣並不會成功,終端會輸出異常。
由於存在循環依賴,links沒法實現雙向鏈接。
如何解決呢? 例如使用 ambassador pattern,使用外部DNS容器等。

編排高可用Spring Cloud微服務集羣及動態伸縮

微服務項目名稱 項目微服務中的角色
microservice-discovery-eureka-ha 服務發現組件
microservice-provider-user 服務提供者
microservice-consumer-movie-ribbon-hystrix 服務消費者
microservice-gateway-zuul API Gateway
microservice-hystrix-turbine Hystrix聚合監控工具
microservice-hystrix-dashboard Hystrix監控界面

1.因爲使用了microservice-discovery-eureka-ha,須要將全部的微服務eureka.client.serviceUrl.defaultZone屬性修改成以下內容:

eureka:
	client:
		service-url:
			defaultZone: http://peer1:8761/eureka/,http://peer2:8762/eureka/

2.在每一個項目的根目錄,執行如下命令建立Docker鏡像。

mvn clean package docker:build

3.編寫docker-compose.yml

version: "2"
services:
	peer1:
		image: itemuch/microservice-discovery-eureka-ha:0.0.1-SNAPSHOT
		ports:
			-	"8761:8761"
		enviroment:
			-	spring.profiles.active=peer1
	peer2:
		image: itemuch/microservice-discovery-eureka-ha:0.0.1-SNAPSHOT
		hostname: peer2
		ports:
			-	"8761:8761"
		enviroment:
			-	spring.profiles.active=peer2
	microservice-provider-user:
		image: itmuch/microservice-provider-user:0.0.1-SNAPSHOT
	microservice-consumer-movie-ribbon-hystrix:
		image: itmuch/microservice-consumer-movie-ribbon-hystrix:0.0.1-SNAPSHOT
	microservice-gateway-zuul:
		image: itmuch/microservice-gateway-zuul:0.0.1-SNAPSHOT
	microservice-hystrix-dashboard:
		image: itmuch/microservice-hystrix-dashboard:0.0.1-SNAPSHOT
	microservice-hystrix-turbine:
		image: itmuch/microservice-hystrix-turbine:0.0.1-SNAPSHOT

測試,執行docker-compose up啓動項目。

而後能夠在Eureka Server看到微服務列表。

執行如下命令,爲各個微服務動態擴容,讓除Eureka Server之外的全部微服務都啓動3個實例。

docker-compose scale microservice-provider-user=3 microservice-consumer-movie-ribbon-hystrix=3 microservice-gateway-zuul=3
microservice-hystrix-turbine=3
相關文章
相關標籤/搜索