运行 "java -jar <jar name> --spring.profiles.active=...." 时出现错误

Error raised when run "java -jar <jar name> --spring.profiles.active=...."

我正在学习 spring 云并在 Intellij IDEA 中编写一些关于 spring 云尤里卡的代码。现在让我描述一下问题。

我已经成功完成了一个服务注册中心(单节点)。

现在改成HA模式

  1. 在文件夹\src\main\resources下(与application.properties同一个文件夹),我创建了application-peer1.properties和application-peer2.properties,并清理了[=65=的内容].

申请-peer1.properties:

spring.application.name=eureka-server
server.port=1111
eureka.instance.hostname=peer1
eureka.client.serviceUrl.defaultZone=http://peer2:1112/eureka/
spring.profiles.active=peer1

申请-peer2.properties:

spring.application.name=eureka-server
server.port=1112
eureka.instance.hostname=peer2
eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/
spring.profiles.active=peer2

application.properties 为空。

  1. 在我笔记本电脑的 Windows 10 C:\Windows\System32\drivers\etc\hosts 中,添加以下行:

    127.0.0.1 peer1

    127.0.0.1 peer2

  2. 在 IDEA 终端中,运行“mvn install”并生成 .jar 文件。

  3. 运行 “java -jar target\eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=peer1”。它报告:

    org.springframework.boot.context.config.InvalidConfigDataPropertyException: 属性 'spring.profiles.active' imported from location 'class path resource [application-peer1.properties] '在配置文件特定资源中无效 [来源:class 路径资源 [application-peer1.properties] 来自 eureka-server-0.0.1-SNAPSHOT.jar - 12:24]

pom.xml显示:

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>

谢谢和问候,

问题不在您的命令行参数中,问题出在 属性 文件中:

申请-peer1.properties:

spring.profiles.active=peer1

您已经从命令行参数中选择了“peer1”配置文件,因此重新激活相同的配置文件没有意义,因为此时它已经处于活动状态。

如果同一行在非配置文件特定的 属性 文件中,例如“application.properties”,它可能会被使用,但不能在这里使用。