Profile
-
简单说一下 Profile
在前面,eureka-server 和 album-provider 的模块中,都提供了3份格式如
application-{profile}.yml
的配置文件,他们分别是application.yml
、application-dev.yml
和application-perf.yml
。配置文件名 profile application.yml default application-dev.yml dev application-perf.yml perf - 如何应用
通过
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 内实现
- 点击 Tool Bar 上的
Run/Debug Configurations
- 在下拉框中点击
Edit Configurations
- 在弹出对话框中点击左上角
+
号,弹出Add New Configuration
- 在
Add New Configuration
中找到Spring Boot
并点击 - 填写该启动项目的名称,可以使用实例名,只要是便于区分命名方式都可以
- 填写运行主类
-
填写命令行参数。这里创建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
通过命令行重写了spring.profiles.active
的值,选用 profile 为 perf 的配置文件application-perf.yml
, 它指定了 eureka-server 集群的注册地址。重写server.port
的值,伪集群通过不同端口来实现。 - 为了在 IDE 中操作方便,建议打开
Services
工具窗口,可以很方便的控制应用的运行状态。 - 启动 eureka-server 应用的3个实例 ,就实现了 eureka-server 的集群部署。
Eureka Server 集群部署 —— 执行 jar 传入命令行参数
-
依靠 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>
- 打包程序,说是打包,这里我们用 maven 的
install
生命周期 。在需要打包的项目根目录下执行mvn install
;在 Intellij 的 Maven Tool Window 里更可一键完成。 - 打包完成后,产出 jar 会出现在 target 文件夹下。通过系统命令行启动,但是太麻烦了。
-
编辑启动命令行文件,存放到
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 文件。 -
拷贝命令行文件到
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 的方式
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。