JHipster 3

发布于 2019-01-17  689 次阅读


JHipster配置文件介绍
jhipster生成的微服务项目通常有多个配置文件,比如:application-dev.yml,application-prod.yml,application.yml,bootstrap.yml,bootstrap-prod.yml。那么一个项目中有这么多配置文件,在启动的时候到底是如何加载配置信息的呢?下面我们来一点点的分析。

首先,很明显的名称后面带有dev字母的表示存储开发环境的配置信息,名称后面带有prod字母的表示存储生产环境的配置信息。通过简单的分类之后,接下来我们只需要分析其中一类就好了,这里我们以含有dev名称的配置文件为例进行分析。

bootstrap.yml文件

bootstrap.yml指的是开发环境的配置文件,bootstrap-prod.yml则指的是生产环境的配置文件。那bootstrap.yml文件是什么呢?我们先来看一下这个文件的介绍:

要说bootstrap文件,先要从spring cloud说起,毕竟JHipster这个开发平台只是把spring cloud整合到自己的平台上。在spring cloud中有一个“引导上下文”的概念,这是主应用程序的父上下文。引导上下文负责从配置服务器加载配置属性,以及解密外部配置文件中的属性。和主应用程序加载application.(yml或properties)中的属性不同,引导上下文加载bootstrap.中的属性。配置在bootstrap.*中的属性有更高的优先级,因此默认情况下它们不能被本地配置文件覆盖。如需禁用引导过程,可设置spring.cloud.bootstrap.enabled=false。

总之,在JHipster中项目启动时总是先加载bootstrap.yml文件,然后再加载其他的配置文件,并且该文件的内容不会被覆盖。那么我们来看看bootstrap文件中的内容吧:

1.user微服务的bootstrap.yml文件

===================================================================

Spring Cloud Config bootstrap configuration for the "dev" profile

In prod profile, properties will be overwriten by the ones defined in bootstrap-prod.yml

===================================================================

jhipster:
registry:
password: admin #user微服务注册到registry服务中心的密码是admin

spring: #user微服务的项目名称
application:
name: user
profiles: #user微服务启动时指定加载dev配置文件还是prod配置文件,这里我们不指定

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 WAR 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: #这段配置是指将user微服务的配置文件交给注册中心registry里面的config servier管理
config: #user微服务首先去registy中的config目录下加载自己的配置文件(文件名是name-profile.yml,即user-dev.yml)

如果registry中获取配置文件的方式是从git上获取,那么这里的label:master就是指定user微服务获取哪个分支上的配置文件

fail-fast: true #JHipster整合了Netflix的Hystrix(一个实现了超时机制和断路器模式的工具类库),这里是指是否开启快速失败机制,通常选择true
uri: http://admin:${jhipster.registry.password}@localhost:8761/config

name of the config server's property source (file.yml) that we want to use

name: user
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 config source git repository

info: #自行设置user微服务的版本,此处未设置
project:
version: #project.version#
2.注册中心registry的bootstray.yml配置文件

===================================================================

Spring Cloud Config bootstrap configuration for the "dev" profile

In prod profile, properties will be overwriten by the ones defined in bootstrap-prod.yml

===================================================================

spring: #注册中心的应用名称
application:
name: jhipster-registry
profiles: #此处是指加载配置文件方式:dev类型的文件,native本地加载,(git远程加载)
active: dev,native
cloud: #这里是对registry中的spring cloud config进行配置,如果active包含native则所有项目的配置文件都只从本地加载,
config: #如果active包含git则所有配置文件都从git仓库中加载,uri指定了git仓库的地址,prefix是指搜索配置文件在git仓库中的
server: #路径,由于我把配置文件都放在git仓库的config文件夹下,所以需要这样配置
git: #剩下的配置属性和user微服务的属性雷同,就不再一一介绍了。
uri: https://git.oschina.net/liupengf/profiles
native:
search-locations: file:./central-config
prefix: /config
bootstrap: true
fail-fast: true

name of the config server's property source (file.yml) that we want to use

name: jhipster-registry
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 config source git repository

info:
project:
version: #project.version#

uncomment to enable encryption features

encrypt: #这个属性是配置加解密的key,由于配置文件中有些敏感信息如数据库密码等不方便放在git仓库中,所以需要加解密。
key: my-secret-encryption-key-to-change-in-production
到这里为止,有关bootstrap.yml中的相关属性都介绍完了,其中红色的注解是我对配置属性的理解。
对于微服务user来说,在项目启动时加载完之后会去加载哪个配置文件呢?

这里要分两种情况,如果registry注册中心bootstrap配置文件中spring.profile.active:指定native,则user微服务就会加载本地的配置文件,如果指定的是git,则会去git仓库中加载user-dev|prod.yml。

假定是从本地加载配置文件,我们先来看一下user微服务的application.yml配置文件:

===================================================================

Spring Boot configuration.

#

This configuration will be overriden by the Spring profile you use,

for example application-dev.yml if you use the "dev" profile.

#

More information on profiles: https://jhipster.github.io/profiles/

More information on configuration properties: https://jhipster.github.io/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: user
instanceId: user:${spring.application.instance-id:${random.value}}
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 10
status-page-url-path: ${management.context-path}/info
health-check-url-path: ${management.context-path}/health
metadata-map:
profile: ${spring.profiles.active}
version: ${info.project.version}
ribbon:
eureka:
enabled: true

See

hystrix:

command:

default:

execution:

isolation:

thread:

timeoutInMilliseconds: 10000

management:
security:
roles: ADMIN
context-path: /management
health:
mail:
enabled: false # When using the MailService, configure an SMTP server and set this to true
spring:
application:
name: user
jackson:
serialization.write_dates_as_timestamps: false
jpa:
open-in-view: false
hibernate:
ddl-auto: none
naming:
physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
implicit-strategy: org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy
messages:
basename: i18n/messages
mvc:
favicon:
enabled: false
thymeleaf:
mode: XHTML

security:
basic:
enabled: false

server:
session:
cookie:
http-only: true

===================================================================

JHipster specific properties

#

Full reference is available at: https://jhipster.github.io/common-application-properties/

===================================================================

jhipster:
async:
core-pool-size: 2
max-pool-size: 50
queue-capacity: 10000

By default CORS is disabled. Uncomment to enable.

cors:

allowed-origins: "*"

allowed-methods: GET, PUT, POST, DELETE, OPTIONS

allowed-headers: "*"

exposed-headers:

allow-credentials: true

max-age: 1800

mail:
from: user@localhost
swagger:
default-include-pattern: /api/.*
title: user API
description: user API documentation
version: 0.0.1
terms-of-service-url:
contact-name:
contact-url:
contact-email:
license:
license-url:
ribbon:
display-on-active-profiles: dev

===================================================================

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://jhipster.github.io/common-application-properties/

===================================================================

application:
关于application.yml这个配置文件里面的属性我并不是完全都了解,所以我就在这里统一总结一下:这个配置文件中通常会放一些公共的配置信息,一般情况下不需要修改,比如eureka设置的相关信息、ribbon配置以及Restful风格的相关设置。但是这个配置文件中的信息会被application-dev|prod.yml中的内容所替换,所以需要修改的配置我们通常定义在application-dev|prod.yml中。
下面来看一下user微服务的application-dev.yml的配置信息:

===================================================================

Spring Boot configuration for the "dev" profile.

#

This configuration overrides the application.yml file.

#

More information on profiles: https://jhipster.github.io/profiles/

More information on configuration properties: https://jhipster.github.io/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: #eureka的相关设置,设置注册到eureka的默认地址上http://admin:${jhipster.registry.password}@localhost:8761/eureka/
instance:
prefer-ip-address: true #指定微服务在服务中心的instansId
instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}:${random.value}
client:
service-url:
defaultZone: http://admin:${jhipster.registry.password}@localhost:8761/eureka/

spring: #指定启动的配置文件类型
profiles:
active: dev
include: no-liquibase,swagger
devtools:
restart:
enabled: true
livereload:
enabled: false # we use gulp + BrowserSync for livereload
jackson:
serialization.indent_output: true
datasource: #配置数据库相关的信息
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:mysql://127.0.0.1:3306/jhipster?useUnicode=true&characterEncoding=utf8&characterResultSets=utf8
username: root
password: root
hikari:
data-source-properties:
cachePrepStmts: true
prepStmtCacheSize: 250
prepStmtCacheSqlLimit: 2048
useServerPrepStmts: true
jpa:
database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
database: MYSQL
show-sql: true
generate-ddl: true
properties:
hibernate.id.new_generator_mappings: true
hibernate.cache.use_second_level_cache: true
hibernate.cache.use_query_cache: false
hibernate.generate_statistics: true
hibernate.cache.region.factory_class: com.hazelcast.hibernate.HazelcastCacheRegionFactory
hibernate.cache.hazelcast.instance_name: user
hibernate.cache.use_minimal_puts: true
hibernate.cache.hazelcast.use_lite_member: true
mail:
host: localhost
port: 25
username:
password:
messages:
cache-seconds: 1
thymeleaf:
cache: false
zipkin: # Use the "zipkin" Maven profile to have the Spring Cloud Zipkin dependencies
base-url: http://localhost:9411
enabled: false
locator:
discovery:
enabled: true

liquibase:
contexts: dev

===================================================================

To enable SSL, generate a certificate using:

keytool -genkey -alias user -storetype PKCS12 -keyalg RSA -keysize 2048 -keystore keystore.p12 -validity 3650

#

You can also use Let's Encrypt:

https://maximilian-boehm.com/hp2121/Create-a-Java-Keystore-JKS-from-Let-s-Encrypt-Certificates.htm

#

Then, modify the server.ssl properties so your "server" configuration looks like:

#

server:

port: 8443

ssl:

key-store: keystore.p12

key-store-password: <your-password>

keyStoreType: PKCS12

keyAlias: user

===================================================================

server: #设置端口号
port: 8081

===================================================================

JHipster specific properties

#

Full reference is available at: https://jhipster.github.io/common-application-properties/

===================================================================

jhipster:
http:
version: V_1_1 # To use HTTP/2 you will need SSL support (see above the "server.ssl" configuration)
cache: # Cache configuration
hazelcast: # Hazelcast distributed cache
time-to-live-seconds: 3600
backup-count: 1

CORS is only enabled by default with the "dev" profile, so BrowserSync can access the API

cors:
allowed-origins: "" allowed-methods: GET, PUT, POST, DELETE, OPTIONS allowed-headers: ""
exposed-headers:
allow-credentials: true
max-age: 1800
security:
authentication:
jwt:
secret: my-secret-token-to-change-in-production

Token is valid 24 hours

token-validity-in-seconds: 86400
token-validity-in-seconds-for-remember-me: 2592000
mail: # specific JHipster mail property, for standard properties see MailProperties
from: user@localhost
base-url:
metrics: # DropWizard Metrics configuration, used by MetricsConfiguration
jmx.enabled: true
graphite: # Use the "graphite" Maven profile to have the Graphite dependencies
enabled: false
host: localhost
port: 2003
prefix: user
prometheus: # Use the "prometheus" Maven profile to have the Prometheus dependencies
enabled: false
endpoint: /prometheusMetrics
logs: # Reports Dropwizard metrics in the logs
enabled: false
report-frequency: 60 # in seconds
logging:
logstash: # Forward logs to logstash over a socket, used by LoggingConfiguration
enabled: false
host: localhost
port: 5000
queue-size: 512
spectator-metrics: # Reports Spectator Circuit Breaker metrics in the logs
enabled: false

edit spring.metrics.export.delay-millis to set report frequency

===================================================================

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://jhipster.github.io/common-application-properties/

===================================================================

application:


公交车司机终于在众人的指责中将座位让给了老太太