Profile

  1. 简单说一下 Profile

    在前面,eureka-server 和 album-provider 的模块中,都提供了3份格式如 application-{profile}.yml 的配置文件,他们分别是 application.ymlapplication-dev.ymlapplication-perf.yml

    配置文件名 profile
    application.yml default
    application-dev.yml dev
    application-perf.yml perf
  2. 如何应用

    通过 bootstrap.yml 中的 spring.profiles.active: dev/perf 来指定 profile 。
    application.yml 作为默认配置文件,一定会被系统加载,然后在根据指定的 profile 加载 application-{profile}.yml , 加载完成后使用 application-{profile}.yml 中的内容,向 application.yml 覆盖和填充:相同的 key 覆盖,没有的 key 填充。
    所以 application.yml 一般存放无差异的配置,application-{profile}.yml 一般存放因环境不同而有差异的配置。

Eureka Server 集群部署 —— Intellij IDEA 内实现

  1. 点击 Tool Bar 上的 Run/Debug Configurations
  2. 在下拉框中点击 Edit Configurations
  3. 在弹出对话框中点击左上角 + 号,弹出 Add New Configuration
  4. Add New Configuration 中找到 Spring Boot 并点击
  5. 填写该启动项目的名称,可以使用实例名,只要是便于区分命名方式都可以
  6. 填写运行主类
  7. 填写命令行参数。这里创建3个 eureka-server 实例组成集群

    Name Program arguments
    EurekaServer-perf-34001 --spring.profiles.active=perf --server.port=34001 --eureka.instance.hostname=eureka-server-1
    EurekaServer-perf-34002 --spring.profiles.active=perf --server.port=34001 --eureka.instance.hostname=eureka-server-2
    EurekaServer-perf-34003 --spring.profiles.active=perf --server.port=34001 --eureka.instance.hostname=eureka-server-3

    创建 multi 运行环境
    通过命令行重写了 spring.profiles.active 的值,选用 profile 为 perf 的配置文件 application-perf.yml , 它指定了 eureka-server 集群的注册地址。重写 server.port 的值,伪集群通过不同端口来实现。

  8. 为了在 IDE 中操作方便,建议打开 Services 工具窗口,可以很方便的控制应用的运行状态。
  9. 启动 eureka-server 应用的3个实例 ,就实现了 eureka-server 的集群部署。

Eureka Server 集群部署 —— 执行 jar 传入命令行参数

  1. 依靠 spring boot 的打包插件
    创建根项目的时候,继承了 spring-boot-starter-parent ,这个启动器里包含了 spring-boot 的打包插件,按需引入到需要打包(有主程序)的项目里的 pom 中即可:

      <build>
        <plugins>
          <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
        </plugins>
      </build>
  2. 打包程序,说是打包,这里我们用 maven 的 install 生命周期 。在需要打包的项目根目录下执行 mvn install ;在 Intellij 的 Maven Tool Window 里更可一键完成。
  3. 打包完成后,产出 jar 会出现在 target 文件夹下。通过系统命令行启动,但是太麻烦了。
  4. 编辑启动命令行文件,存放到 resources 目录下

    文件名 文件内容
    eureka-server-1.bat java -jar <your jar> --spring.profiles.active=perf --server.port=33001 --eureka.instance.hostname=eureka-server-1
    eureka-server-2.bat java -jar <your jar> --spring.profiles.active=perf --server.port=33002 --eureka.instance.hostname=eureka-server-2
    eureka-server-3.bat java -jar <your jar> --spring.profiles.active=perf --server.port=33003 --eureka.instance.hostname=eureka-server-3
    <your jar> 需要替换成你打包生成的 jar 的名字;在 windows 系统里使用 bat 文件。
  5. 拷贝命令行文件到 target 目录下
    maven 默认会将 resources 下的所有文件都拷贝到 target/classes 目录下。但是在命令行使用的是相对路劲,所以命令行文件要与 jar 一样,在 target 文件夹下。拷贝配置文件由通用性,所以写在项目根 pom 中

    • 排除 bat 文件拷贝到 target/classes

        <build>
          <resources>
            <resource>
              <directory>src/main/resources</directory>
              <filtering>true</filtering>
              <excludes>
                <exclude>*.bat</exclude>
              </excludes>
            </resource>
          </resources>
        </build>
    • 拷贝 *.bat 文件到 target , 在 <build> 标签内追加

      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-resources-plugin</artifactId>
          <executions>
            <execution>
              <id>copy-resources</id>
              <phase>validate</phase>
              <goals>
                <goal>copy-resources</goal>
              </goals>
              <configuration>
                <resources>
                  <resource>
                    <directory>src/main/resources</directory>
                    <filtering>true</filtering>
                    <includes>
                      <include>*.bat</include>
                    </includes>
                  </resource>
                </resources>
                <outputDirectory>${project.build.directory}</outputDirectory>
              </configuration>
            </execution>
          </executions>
        </plugin>
      </plugins>

Provider 集群部署

同 eureka-server 的方式


wowxhycoming
0 声望1 粉丝

引用和评论

0 条评论