[英]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).在我的项目中,我使用 Jhipster Spring Boot,我想同时启动一个微服务的 2 个实例,但在数据库(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.在这个微服务中,我有用于 collections A、B C 的类、服务和休息,现在我还想有历史 collections A_history (结构与 C_history、B_history、B_history 完全相同) C) 存储在单独的数据库实例中。 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".因此,我们的想法是让相同微服务的 2 个实例,一个用于 A、B、C collections 存储在“MicroserviceDB”中,第二个用于 A_history、B_history、C_history Z0B9ABFE67CC31FCF1ECD022EB16。
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.我尝试创建 2 个配置文件,但是当我从命令行历史微服务启动时,它启动正常,但如果我也尝试同时启动“原始”微服务,它会启动但立即历史服务变为“原创”微服务。 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应用程序.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应用程序-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应用程序-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引导程序.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 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 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.您可以考虑使用具有合适配置的 Docker 容器来增加这方面的隔离。
I've found solution for my problem.我找到了解决我的问题的方法。
First, order of parameters when jar is executed was wrong, it should be like this:一、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'
And the same for historyDev profile. historyDev 配置文件也是如此。
Second, as it can be seen here, arguments should be parameters APP_NAME, APP_CLIENT_NAME,... and application.yml should take parameters:其次,从这里可以看出,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:}
...
...
...
Thanks everyone for taking time to help me.感谢大家花时间帮助我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.