欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

编排Spring Cloud微服务

程序员文章站 2022-04-30 18:22:17
...
一 编排的微服务列表
编排Spring Cloud微服务
二 使用Maven插件构建Docker镜像,在各个项目的pom,xml中添加以下内容
  <!-- 添加spring-boot的maven插件 -->
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>

      <!-- 添加docker-maven插件 -->
      <plugin>
        <groupId>com.spotify</groupId>
        <artifactId>docker-maven-plugin</artifactId>
        <version>0.4.13</version>
        <configuration>
          <imageName>itmuch/${project.artifactId}:${project.version}</imageName>
          <baseImage>java</baseImage>
          <entryPoint>["java", "-jar", "/${project.build.finalName}.jar"]</entryPoint>
          <resources>
            <resource>
              <targetPath>/</targetPath>
              <directory>${project.build.directory}</directory>
              <include>${project.build.finalName}.jar</include>
            </resource>
          </resources>
        </configuration>
      </plugin>
    </plugins>
  </build>
由配置可知,构建出来的镜像名称是itmuch/各个微服务的aitifactId:各个微服务的版本,例如:itmuch/microservice-provider-user:0.0.1-SNAPSHOT
三 修改各微服务的配置文件application.yml
由于Docker默认的网络模式是bridge,各个容器的IP都不相同,因此使用http://localhost:8761/eureka满足不了需求。可在Eureka所在的容器配置一个主机名discovery,并让各个微服务使用主机名访问Eureka Server。
各微服务配置修改如下:
eureka:
  client:
    serviceUrl:
      defaultZone: http://discovery:8761/eureka/
四 将各个微服务代码拷贝到Linux
[aaa@qq.com springcloud]# ll
total 16
-rw-r--r-- 1 root root  682 Jul  8 12:57 docker-compose-easy.yml
-rw-r--r-- 1 root root 1352 Jul  8 12:57 docker-compose.yml
drwxr-xr-x 4 root root   43 Jul  8 12:59 microservice-consumer-movie-ribbon-hystrix
drwxr-xr-x 4 root root 4096 Jul  8 13:00 microservice-discovery-eureka
drwxr-xr-x 4 root root   43 Jul  8 13:01 microservice-gateway-zuul
drwxr-xr-x 4 root root   43 Jul  8 13:01 microservice-hystrix-dashboard
drwxr-xr-x 4 root root   43 Jul  8 13:02 microservice-hystrix-turbine
drwxr-xr-x 4 root root   60 Jul  8 13:02 microservice-provider-user
-rw-r--r-- 1 root root 1520 Jul  8 12:57 pom.xml
五 在每个项目的根目录执行以下命令,构建各个微服务的镜像
mvn clean package docker:build
[aaa@qq.com springcloud]# docker images
REPOSITORY                                          TAG                 IMAGE ID            CREATED             SIZE
itmuch/microservice-provider-user                   0.0.1-SNAPSHOT      7da7fcfb4ded        31 minutes ago      693MB
itmuch/microservice-hystrix-turbine                 0.0.1-SNAPSHOT      cc22655d299f        32 minutes ago      683MB
itmuch/microservice-hystrix-dashboard               0.0.1-SNAPSHOT      870de69167aa        32 minutes ago      671MB
itmuch/microservice-gateway-zuul                    0.0.1-SNAPSHOT      c2a78b5998b9        33 minutes ago      682MB
itmuch/microservice-discovery-eureka                0.0.1-SNAPSHOT      deff6ac69b16        33 minutes ago      685MB
itmuch/microservice-consumer-movie-ribbon-hystrix   0.0.1-SNAPSHOT      656eccee221c        34 minutes ago      682MB
六 编写docker-compose.yml
[aaa@qq.com springcloud]# cat docker-compose.yml
version: '2'        # 表示该docker-compose.yml文件使用的是Version 2 file format
services:           # Version 2 file format的固定写法,为project定义服务。
  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
    links:          # 链接到microservice-discovery-eureka,这边使用的是SERVICE:ALIAS的形式
      - microservice-discovery-eureka:discovery
  microservice-consumer-movie-ribbon-hystrix:
    image: itmuch/microservice-consumer-movie-ribbon-hystrix:0.0.1-SNAPSHOT
    links:
      - microservice-discovery-eureka:discovery
  microservice-gateway-zuul:
    image: itmuch/microservice-gateway-zuul:0.0.1-SNAPSHOT
    links:
      - microservice-discovery-eureka:discovery
  microservice-hystrix-dashboard:
    image: itmuch/microservice-hystrix-dashboard:0.0.1-SNAPSHOT
    links:
      - microservice-discovery-eureka:discovery
  microservice-hystrix-turbine:
    image: itmuch/microservice-hystrix-turbine:0.0.1-SNAPSHOT
    links:
      - microservice-discovery-eureka:discovery
七 执行以下命令启动项目
docker-compose up -d
八 测试
输入http://192.168.0.100:8761/
编排Spring Cloud微服务