• 使用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中添加以下内容。

    <!-- 添加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。

    2:为Eureka Server所在容器配置一个主机名(如discover),并让各个微服务使用主机名访问Eureka Server。
    将所有微服务eureka.client.serviceUrl.defaultZone修改为如下内容:

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

    3:在每个项目的根目录执行以下命令,构建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上的微服务列表服务发布成功。

    简化Compose的编写

    同一个Compose工程中的所有服务共享一个隔离网络,可使用服务名称作为主机名来发现其他服务。

    因此,将docker-compose.yml简化:

    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
    
  • 相关阅读:
    RedHat 7 安装PostgreSQL 10.5
    百万级数据库优化方案
    所有文章的测试Demo
    PostGreSql安装
    windows server 2016部署服务
    Spring MVC Hello World 404
    Unity攻略
    Unity判断用户联网状态,WiFi/移动网络/无网络
    Unity UGUI Layout自动排版组件用法介绍
    Unity中对系统类进行扩展的方法
  • 原文地址:https://www.cnblogs.com/aixing/p/13327485.html
Copyright © 2020-2023  润新知