Jhipster Spring 在不同的数据库上启动 2 个微服务实例

Jhipster Spring Boot 2 instances of a microservice on different databases

在我的项目中,我正在使用 Jhipster Spring Boot,我想同时启动一个微服务的 2 个实例,但是在数据库的不同实例上 (MongoDB)。

在这个微服务中,我有 类、用于集合 A、B C、.. 的服务、休息,现在我还想拥有历史集合 A_history、B_history, C_history(结构与 A、B、C 完全相同)存储在数据库的单独实例中。创建“真正分离的”微服务对我来说毫无意义,因为我必须从第一个微服务中复制所有逻辑,并最终得到难以维护的双重代码。因此,我们的想法是拥有同一微服务的 2 个实例,一个用于存储在“MicroserviceDB”中的 A、B、C 集合,第二个用于存储在“MicroserviceDB”中的 A_history、B_history、C_history 集合“历史数据库”。

我试过创建 2 个配置文件,但是当我从命令行历史微服务启动时,它启动正常,但如果我也尝试同时启动“原始”微服务,它启动了但是历史服务立即成为“原始”微服务。好像他们不能同时工作。

这个概念在微服务架构中是否可行?有谁知道如何让它工作,或者对我的问题有其他解决方案吗?

谢谢。

application.yml

# ===================================================================
# Spring Boot configuration.
#
# This configuration will be overridden by the Spring profile you use,
# for example application-dev.yml if you use the "dev" profile.
#
# More information on profiles: https://www.jhipster.tech/profiles/
# More information on configuration properties: https://www.jhipster.tech/common-application-properties/
# ===================================================================

# ===================================================================
# Standard Spring Boot properties.
# Full reference is available at:
# http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
# ===================================================================

eureka:
  client:
    enabled: true
    healthcheck:
      enabled: true
    fetch-registry: true
    register-with-eureka: true
    instance-info-replication-interval-seconds: 10
    registry-fetch-interval-seconds: 10
  instance:
    appname: microservice
    instanceId: microservice:${spring.application.instance-id:${random.value}}
    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 10
    status-page-url-path: ${management.endpoints.web.base-path}/info
    health-check-url-path: ${management.endpoints.web.base-path}/health
    metadata-map:
      zone: primary # This is needed for the load balancer
      profile: ${spring.profiles.active}
      version: #project.version#
      git-version: ${git.commit.id.describe:}
      git-commit: ${git.commit.id.abbrev:}
      git-branch: ${git.branch:}
ribbon:
  eureka:
    enabled: true
feign:
  hystrix:
    enabled: true
  client:
    config:
      default:
        connectTimeout: 160000000
        readTimeout: 160000000

# See https://github.com/Netflix/Hystrix/wiki/Configuration
hystrix:
  command:
    default:
      execution:
        timeout:
          enabled: false
        isolation:
          strategy: SEMAPHORE
          # See https://github.com/spring-cloud/spring-cloud-netflix/issues/1330
          thread:
            timeoutInMilliseconds: 5000
  shareSecurityContext: true

management:
  endpoints:
    web:
      base-path: /management
      exposure:
        include: ['configprops', 'env', 'health', 'info', 'jhimetrics', 'logfile', 'loggers', 'prometheus', 'threaddump']
  endpoint:
    health:
      show-details: when_authorized
      roles: 'ROLE_TENANT_ADMIN'
    jhimetrics:
      enabled: true
  info:
    git:
      mode: full
  health:
    mail:
      enabled: false # When using the MailService, configure an SMTP server and set this to true
  metrics:
    export:
      # Prometheus is the default metrics backend
      prometheus:
        enabled: true
        step: 60
    enable:
      http: true
      jvm: true
      logback: true
      process: true
      system: true
    distribution:
      percentiles-histogram:
        all: true
      percentiles:
        all: 0, 0.5, 0.75, 0.95, 0.99, 1.0
    tags:
      application: ${spring.application.name}
    web:
      server:
        request:
          autotime:
            enabled: true

spring:
  application:
    name: Microservice
  jmx:
    enabled: false
  messages:
    basename: i18n/messages
  main:
    allow-bean-definition-overriding: true
  mvc:
    favicon:
      enabled: false
  task:
    execution:
      thread-name-prefix: microservice-task-
      pool:
        core-size: 2
        max-size: 50
        queue-capacity: 10000
    scheduling:
      thread-name-prefix: microservice-scheduling-
      pool:
        size: 2
  thymeleaf:
    mode: HTML
  output:
    ansi:
      console-available: true
  servlet:
    multipart:
      enabled: true # Whether to enable support of multipart uploads.
      max-file-size: -1 #Max file size.
      max-request-size: -1 # Max request size.
  http:
    multipart:
      enabled: true # Whether to enable support of multipart uploads.

server:
  tomcat:
    max-http-form-post-size: -1
    max-http-post-size: -1
    max-swallow-size: -1
  servlet:
    session:
      cookie:
        http-only: true

# Properties to be exposed on the /info management endpoint
info:
  # Comma separated list of profiles that will trigger the ribbon to show
  display-ribbon-on-profiles: 'microserviceDev'

# ===================================================================
# JHipster specific properties
#
# Full reference is available at: https://www.jhipster.tech/common-application-properties/
# ===================================================================

jhipster:
  clientApp:
    name: 'microserviceApp'
  # By default CORS is disabled. Uncomment to enable.
  # cors:
  #     allowed-origins: "*"
  #     allowed-methods: "*"
  #     allowed-headers: "*"
  #     exposed-headers: "Authorization,Link,X-Total-Count"
  #     allow-credentials: true
  #     max-age: 1800
  mail:
    from: Microservice@localhost
  swagger:
    default-include-pattern: /api/.*
    title: Microservice API
    description: Microservice API documentation
    version: 0.0.1
    terms-of-service-url:
    contact-name:
    contact-url:
    contact-email:
    license:
    license-url:
# ===================================================================
# Application specific properties
# Add your own application properties here, see the ApplicationProperties class
# to have type-safe configuration, like in the JHipsterProperties above
#
# More documentation is available at:
# https://www.jhipster.tech/common-application-properties/
# ===================================================================

# application:

应用-dev.yml

# ===================================================================
# Spring Boot configuration for the "dev" profile.
#
# This configuration overrides the application.yml file.
#
# More information on profiles: https://www.jhipster.tech/profiles/
# More information on configuration properties: https://www.jhipster.tech/common-application-properties/
# ===================================================================

# ===================================================================
# Standard Spring Boot properties.
# Full reference is available at:
# http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
# ===================================================================

logging:
  level:
    ROOT: DEBUG
    io.github.jhipster: DEBUG
    com.it.nn: DEBUG
    org.springframework.data.mongodb.core.MongoTemplate: DEBUG

eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://admin:${jhipster.registry.password}@${REGISTRY_HOST:localhost}:8861/eureka/

spring:
  profiles:
    active: dev
    include:
      - swagger
      # Uncomment to activate TLS for the dev profile
      #- tls
  devtools:
    restart:
      enabled: true
      additional-exclude: static/**
    livereload:
      enabled: false # we use Webpack dev server + BrowserSync for livereload
  jackson:
    serialization:
      indent-output: true
  data:
    mongodb:
      uri: mongodb://10.172.192.15:27017
      database: Microservice_Dev
  mail:
    host: localhost
    port: 25
    username:
    password:
  messages:
    cache-duration: PT1S # 1 second, see the ISO 8601 standard
  thymeleaf:
    cache: false
  sleuth:
    sampler:
      probability: 1 # report 100% of traces
  zipkin: # Use the "zipkin" Maven profile to have the Spring Cloud Zipkin dependencies
    base-url: http://localhost:9411
    enabled: false
    locator:
      discovery:
        enabled: true

server:
  port: 7040

# ===================================================================
# JHipster specific properties
#
# Full reference is available at: https://www.jhipster.tech/common-application-properties/
# ===================================================================

jhipster:
  cache: # Cache configuration
    hazelcast: # Hazelcast distributed cache
      time-to-live-seconds: 3600
      backup-count: 1
      management-center: # Full reference is available at: http://docs.hazelcast.org/docs/management-center/3.9/manual/html/Deploying_and_Starting.html
        enabled: false
        update-interval: 3
        url: http://localhost:8180/hazelcast-mancenter
  # CORS is disabled by default on microservices, as you should access them through a gateway.
  # If you want to enable it, please uncomment the configuration below.
  # cors:
  #     allowed-origins: "*"
  #     allowed-methods: "*"
  #     allowed-headers: "*"
  #     exposed-headers: "Authorization,Link,X-Total-Count"
  #     allow-credentials: true
  #     max-age: 1800
  security:
    
  logging:
    use-json-format: false # By default, logs are not in Json format
    logstash: # Forward logs to logstash over a socket, used by LoggingConfiguration
      enabled: false
      host: localhost
      port: 5000
      queue-size: 512
  audit-events:
    retention-period: 30 # Number of days before audit events are deleted.

# ===================================================================
# Application specific properties
# Add your own application properties here, see the ApplicationProperties class
# to have type-safe configuration, like in the JHipsterProperties above
#
# More documentation is available at:
# https://www.jhipster.tech/common-application-properties/
# ===================================================================

申请-historyDev.yml

    # ===================================================================
# Spring Boot configuration for the "dev" profile.
#
# This configuration overrides the application.yml file.
#
# More information on profiles: https://www.jhipster.tech/profiles/
# More information on configuration properties: https://www.jhipster.tech/common-application-properties/
# ===================================================================

# ===================================================================
# Standard Spring Boot properties.
# Full reference is available at:
# http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html
# ===================================================================

logging:
  level:
    ROOT: DEBUG
    io.github.jhipster: DEBUG
    com.it.nn: DEBUG
    org.springframework.data.mongodb.core.MongoTemplate: DEBUG

eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://admin:${jhipster.registry.password}@${REGISTRY_HOST:localhost}:8861/eureka/

spring:
  profiles:
    active: historyDev
    include:
      - swagger
      # Uncomment to activate TLS for the dev profile
      #- tls
  devtools:
    restart:
      enabled: true
      additional-exclude: static/**
    livereload:
      enabled: false # we use Webpack dev server + BrowserSync for livereload
  jackson:
    serialization:
      indent-output: true
  data:
    mongodb:
      uri: mongodb://10.172.192.15:27017
      database: History_Dev
  mail:
    host: localhost
    port: 25
    username:
    password:
  messages:
    cache-duration: PT1S # 1 second, see the ISO 8601 standard
  thymeleaf:
    cache: false
  sleuth:
    sampler:
      probability: 1 # report 100% of traces
  zipkin: # Use the "zipkin" Maven profile to have the Spring Cloud Zipkin dependencies
    base-url: http://localhost:9411
    enabled: false
    locator:
      discovery:
        enabled: true

server:
  port: 7042

# ===================================================================
# JHipster specific properties
#
# Full reference is available at: https://www.jhipster.tech/common-application-properties/
# ===================================================================

jhipster:
  cache: # Cache configuration
    hazelcast: # Hazelcast distributed cache
      time-to-live-seconds: 3600
      backup-count: 1
      management-center: # Full reference is available at: http://docs.hazelcast.org/docs/management-center/3.9/manual/html/Deploying_and_Starting.html
        enabled: false
        update-interval: 3
        url: http://localhost:8180/hazelcast-mancenter
  # CORS is disabled by default on microservices, as you should access them through a gateway.
  # If you want to enable it, please uncomment the configuration below.
  # cors:
  #     allowed-origins: "*"
  #     allowed-methods: "*"
  #     allowed-headers: "*"
  #     exposed-headers: "Authorization,Link,X-Total-Count"
  #     allow-credentials: true
  #     max-age: 1800
  security:
   
# ===================================================================
# Application specific properties
# Add your own application properties here, see the ApplicationProperties class
# to have type-safe configuration, like in the JHipsterProperties above
#
# More documentation is available at:
# https://www.jhipster.tech/common-application-properties/
# ===================================================================

# application:

bootstrap.yml

# ===================================================================
# Spring Cloud Config bootstrap configuration for the "dev" profile
# In prod profile, properties will be overwritten by the ones defined in bootstrap-prod.yml
# ===================================================================

jhipster:
  registry:
    password: admin

spring:
  application:
    name: Microservice
  profiles:
    # The commented value for `active` can be replaced with valid Spring profiles to load.
    # Otherwise, it will be filled in by maven when building the JAR file
    # Either way, it can be overridden by `--spring.profiles.active` value passed in the commandline or `-Dspring.profiles.active` set in `JAVA_OPTS`
    active: #spring.profiles.active#
  cloud:
    config:
      fail-fast: false # if not in "prod" profile, do not force to use Spring Cloud Config
      uri: http://admin:${jhipster.registry.password}@localhost:8861/config
      # name of the config server's property source (file.yml) that we want to use
      name: Microservice
      profile: dev # profile(s) of the property source
      label: master # toggle to switch to a different version of the configuration as stored in git
      # it can be set to any label, branch or commit of the configuration source Git repository

bootstrap-historyDev.yml

# ===================================================================
# Spring Cloud Config bootstrap configuration for the "dev" profile
# In prod profile, properties will be overwritten by the ones defined in bootstrap-prod.yml
# ===================================================================

jhipster:
  registry:
    password: admin

spring:
  application:
    name: History
  profiles:
    # The commented value for `active` can be replaced with valid Spring profiles to load.
    # Otherwise, it will be filled in by maven when building the JAR file
    # Either way, it can be overridden by `--spring.profiles.active` value passed in the commandline or `-Dspring.profiles.active` set in `JAVA_OPTS`
    active: #spring.profiles.active#
  cloud:
    config:
      fail-fast: false # if not in "prod" profile, do not force to use Spring Cloud Config
      uri: http://admin:${jhipster.registry.password}@localhost:8861/config
      # name of the config server's property source (file.yml) that we want to use
      name: History
      profile: historyDev # profile(s) of the property source
      label: master # toggle to switch to a different version of the configuration as stored in git
      # it can be set to any label, branch or commit of the configuration source Git repository

pom.xml

<profiles>
        <profile>
            <id>history</id>
            <properties>
                <!-- default Spring profiles -->
                <spring.profiles.active>history${profile.swagger}    </spring.profiles.active>
            </properties>
        </profile>
        <profile>
            <id>microservice</id>
            <properties>
                <!-- default Spring profiles -->
                <spring.profiles.active>microservice${profile.swagger}    </spring.profiles.active>
            </properties>
        </profile>
        <profile>
            <id>swagger</id>
            <properties>
                <profile.swagger>,swagger</profile.swagger>
            </properties>
        </profile>
        <profile>
            <id>tls</id>
            <properties>
                <profile.tls>,tls</profile.tls>
            </properties>
        </profile>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-undertow</artifactId>
                </dependency>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-devtools</artifactId>
                    <optional>true</optional>
                </dependency>
            </dependencies>
            <properties>
                <!-- default Spring profiles -->
                <spring.profiles.active>dev${profile.tls}</spring.profiles.active>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-undertow</artifactId>
                </dependency>
            </dependencies>
            <build>
                <plugins>
                    <plugin>
                        <artifactId>maven-clean-plugin</artifactId>
                        <configuration>
                            <filesets>
                                <fileset>
                                    <directory>target/classes/static/</directory>
                                </fileset>
                            </filesets>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>build-info</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>pl.project13.maven</groupId>
                        <artifactId>git-commit-id-plugin</artifactId>
                    </plugin>
                </plugins>
            </build>
            <properties>
                <!-- default Spring profiles -->
                <spring.profiles.active>prod${profile.swagger}${profile.tls}</spring.profiles.active>
            </properties>
        </profile>
        <profile>
            <id>war</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-war-plugin</artifactId>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <!--
                Profile for tracing requests with Zipkin.
            -->
            <id>zipkin</id>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-starter-zipkin</artifactId>
                </dependency>
            </dependencies>
        </profile>
        <profile>
            <!--
                Profile for applying IDE-specific configuration.
                At the moment it configures MapStruct and Hibernate JPA Metamodel Generator, which you need when working
                with DTOs and entity filtering.
            -->
            <id>IDE</id>
            <dependencies>
                <dependency>
                    <groupId>org.mapstruct</groupId>
                    <artifactId>mapstruct-processor</artifactId>
                </dependency>
            </dependencies>
        </profile>
        <!-- jhipster-needle-maven-add-profile -->
    </profiles>

启动历史服务:

mvnw -Dspring.profiles.active=historyDev -Dspring-boot.run.arguments=--eureka.instance.appname=history,--eureka.instance.instanceId=history:${spring.application.instance-id:${random.value}},--spring.application.name=History,--spring.cloud.config.name=History,--jhipster.clientApp.name='HistoryApp',--jhipster.swagger.title="History API",--jhipster.swagger.description="History API documentation"

启动微服务:

mvnw -Dspring.profiles.active=dev -Dspring-boot.run.arguments=--eureka.instance.appname=microservice,--eureka.instance.instanceId=microservice:${spring.application.instance-id:${random.value}},--spring.application.name=Microservice,--spring.cloud.config.name=Microservice,--jhipster.clientApp.name='MicroserviceApp',--jhipster.swagger.title="Microservice API",--jhipster.swagger.description="Microservice API documentation"

一般来说,这个概念应该可以通过微服务和合适的配置轻松实现。是的,您应该能够使用配置文件来定义不同的数据库连接,以便您可以拥有多个实例 运行.

我假设您正在覆盖临时构建工件,这就是它无法正常工作的原因。但这很难从远处诊断。您可以考虑使用 Docker 具有合适配置的容器来增加这方面的隔离。

我找到了解决问题的方法。

首先jar执行时参数顺序错误,应该是这样的:

java -Dspring.profiles.active=dev -Dserver.port=7040 -jar microservice-0.0.1-SNAPSHOT.jar -Dspring-boot.run.arguments= --hazelcast.port=12741 --APP_INSTANCE_NAME=microservice --APP_NAME=Microservice --APP_CLIENT_NAME='MicroserviceApp' --mainflux.broker=10.172.192.26 --APP_SWAGGER_TITLE='Microservice API' --APP_SWAGGER_DESC='Microservice API documentation'

对于 historyDe​​v 配置文件也是如此。

其次,从这里可以看出,arguments应该是参数APP_NAME,APP_CLIENT_NAME,...而application.yml应该带参数:

    eureka:
      client:
        enabled: true
        healthcheck:
          enabled: true
        fetch-registry: true
        register-with-eureka: true
        instance-info-replication-interval-seconds: 10
        registry-fetch-interval-seconds: 10
      instance:
        appname: ${APP_INSTANCE_NAME:microservice}
        instanceId: ${APP_INSTANCE_NAME:microservice}:${spring.application.instance-id:${random.value}}
        lease-renewal-interval-in-seconds: 5
        lease-expiration-duration-in-seconds: 10
        status-page-url-path: ${management.endpoints.web.base-path}/info
        health-check-url-path: ${management.endpoints.web.base-path}/health
        metadata-map:
          zone: primary # This is needed for the load balancer
          profile: ${spring.profiles.active}
          version: #project.version#
          git-version: ${git.commit.id.describe:}
          git-commit: ${git.commit.id.abbrev:}
          git-branch: ${git.branch:}
...
...
...

感谢大家花时间帮助我。