简体   繁体   中英

Jhipster Spring Boot 2 instances of a microservice on different databases

In my project I'm using Jhipster Spring Boot and I would like to start 2 instances of one microservise at the same time, but on different instances of a database (MongoDB).

In this microservice I have classes, services, rests that are used for collections A, B C,.. for which now I would like to have also history collections A_history, B_history, C_history (that are structured exactly the same like A, B, C) stored in separated instance of a database. It makes no sense to me to create "really separated" microservice since I would have to copy all logic from the first one and end up with doubled code that is very hard to maintain. So, the idea is to have 2 instances of the same microservice, one for A, B, C collections stored in "MicroserviceDB" and second for A_history, B_history, C_history collections stored in "HistoryDB".

I've tried with creating 2 profiles, but when I start from a command line History microservice, it is started ok, but if I also try to start "original" microservice at the same time, it is started but immediately history service becomes "original" microservice. Like they cannot work at the same time.

Is this concept even possible in microservice architecture? Does anyone have an idea how to make this to work, or have some other solution for my problem?

Thanks.

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:

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/
# ===================================================================

application-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>

start history service:

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"

start microservice:

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"

In general, this concept should be easily achievable with microservices and a suiting configuration. And yes, you should be able to use profiles to define different database connections so that you can have multiple instances running.

I assume you are overwriting temporary build artifacts, that's why it is not working somehow. But that is hard to diagnose from distance. You might consider using Docker containers with a suiting configuration to increase isolation in this regard.

I've found solution for my problem.

First, order of parameters when jar is executed was wrong, it should be like this:

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'

And the same for historyDev profile.

Second, as it can be seen here, arguments should be parameters APP_NAME, APP_CLIENT_NAME,... and application.yml should take parameters:

    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:}
...
...
...

Thanks everyone for taking time to help me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM