在Spring上运行不同的配置文件

时间:2019-04-16 00:17:49

标签: spring spring-boot netflix-eureka

我有一个应用程序,需要运行3个配置文件(美国,法国和越南),如下所示的application.yml文件中所示。这3个配置文件应在端口9000上运行的我的Eureka服务器中获得注册服务。

我已经运行java -jar -Dspring.profiles.active=united-states xxx.jar来运行Spring应用程序,但是我的Spring应用程序默认使用端口8761,并且我的服务未在Eureka服务器中注册。

---
# This default profile is used when running a single instance completely standalone:
spring:
  profiles: default
server:
  port: 9000
eureka:
  instance:
    hostname: my-eureka-server.com
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

  # united-states, france, and vietnam illustrate running 3 intercommunicating instances.
  # This example has them running side-by-side on localhost
  # -- which is unrealistic in production
  # -- but does illustrate how multiple instances collaborate.
  #
  # Run by opening 3 separate command prompts:
  # java -jar -Dspring.profiles.active=united-states SpringCloudServiceRegistrationEurekaServer.jar
  # java -jar -Dspring.profiles.active=france SpringCloudServiceRegistrationEurekaServer.jar
  # java -jar -Dspring.profiles.active=vietnam SpringCloudServiceRegistrationEurekaServer.jar

---
spring:
  profiles: united-states
  application:
    name: eureka-server-clustered   # ==> This is Service-Id
server:
  port: 9001
eureka:
  instance:
    hostname: my-eureka-server-us.com
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://my-eureka-server-fr.com:9002/eureka/,http://my-eureka-server-vn.com:9003/eureka/

---
spring:
  profiles: france
  application:
    name: eureka-server-clustered   # ==> This is Service-Id   
server:
  port: 9002
eureka:
  instance:
    hostname: my-eureka-server-fr.com
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://my-eureka-server-us.com:9001/eureka/,http://my-eureka-server-vn.com:9003/eureka/

---
spring:
  profiles: vietnam
  application:
    name: eureka-server-clustered    # ==> This is Service-Id  
server:
  port: 9003
eureka:
  instance:
    hostname: my-eureka-server-vn.com
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://my-eureka-server-us.com:9001/eureka/,http://my-eureka-server-fr.com:9002/eureka/    

日志:

2019-04-16 06:41:10.681  INFO 31061 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$75804a9c] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.4.RELEASE)

2019-04-16 06:41:11.142  INFO 31061 --- [           main] rviceRegistrationEurekaServerApplication : The following profiles are active: united-states
2019-04-16 06:41:12.264  INFO 31061 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-04-16 06:41:12.284  INFO 31061 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 11ms. Found 0 repository interfaces.
2019-04-16 06:41:12.477  WARN 31061 --- [           main] o.s.boot.actuate.endpoint.EndpointId     : Endpoint ID 'service-registry' contains invalid characters, please migrate to a valid format.
2019-04-16 06:41:12.825  INFO 31061 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=aedccd61-e8da-3db8-953a-28c197e2743c
2019-04-16 06:41:13.026  INFO 31061 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$5966479f] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-04-16 06:41:13.076  INFO 31061 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$75804a9c] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-04-16 06:41:13.530  INFO 31061 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8761 (http)
2019-04-16 06:41:13.573  INFO 31061 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-04-16 06:41:13.574  INFO 31061 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.17]
2019-04-16 06:41:13.690  INFO 31061 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-04-16 06:41:13.690  INFO 31061 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2525 ms
2019-04-16 06:41:13.887  WARN 31061 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2019-04-16 06:41:14.683  INFO 31061 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2019-04-16 06:41:15.126  INFO 31061 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2019-04-16 06:41:15.230  INFO 31061 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
2019-04-16 06:41:15.316  INFO 31061 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.3.9.Final}
2019-04-16 06:41:15.320  INFO 31061 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2019-04-16 06:41:15.484  INFO 31061 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2019-04-16 06:41:15.769  INFO 31061 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2019-04-16 06:41:16.039  INFO 31061 --- [           main] o.h.t.schema.internal.SchemaCreatorImpl  : HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@6c841199'
2019-04-16 06:41:16.043  INFO 31061 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2019-04-16 06:41:16.068  WARN 31061 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2019-04-16 06:41:16.362  INFO 31061 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-04-16 06:41:16.436  WARN 31061 --- [           main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2019-04-16 06:41:17.084  INFO 31061 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
2019-04-16 06:41:17.226  INFO 31061 --- [           main] o.s.c.n.eureka.InstanceInfoFactory       : Setting initial instance status as: STARTING
2019-04-16 06:41:17.290  INFO 31061 --- [           main] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application EUREKA-SERVER with eureka with status UP
2019-04-16 06:41:17.353  INFO 31061 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8761 (http) with context path ''
2019-04-16 06:41:17.354  INFO 31061 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8761
2019-04-16 06:41:17.360  INFO 31061 --- [           main] rviceRegistrationEurekaServerApplication : Started SpringCloudServiceRegistrationEurekaServerApplication in 7.941 seconds (JVM running for 8.624)

注意:我在Ubuntu上使用Intellij,并已将/etc/hosts文件配置为包括以下主机:

127.0.0.1       my-eureka-server.com
127.0.0.1       my-eureka-server-us.com
127.0.0.1       my-eureka-server-fr.com
127.0.0.1       my-eureka-server-vn.com

1 个答案:

答案 0 :(得分:0)

您的应用程序正在作为Eureka Server运行。如果那不是您所期望的,请禁用@EnableEurekaServer批注(如果您在@SpringBootApplication批注的类中有此批注)