Запускать разные профили на Spring - PullRequest
0 голосов
/ 16 апреля 2019

У меня есть приложение, в котором мне нужно запустить 3 профиля (Соединенные Штаты, Франция и Вьетнам), как показано в файле application.yml, показанном ниже.Эти 3 профиля должны входить в зарегистрированную службу на моем сервере Eureka, который работает на порту 9000.

Я запустил 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)

Примечание. Я использую Intellij в Ubuntu и настроил в моем файле /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 голосов
/ 22 апреля 2019

Ваше приложение работает как Eureka Server.Если это не то, что вы ожидаете, отключите аннотацию @EnableEurekaServer, если она есть в аннотированном классе @SpringBootApplication

...