I’m using consul as my external config center, but it makes me annoying to copy lots of repeatable properties from a yaml to another, such as
spring:
data:
redis:
host: 192.168.124.133
port: 6379
password: root
config:
import: 'consul:'
cloud:
consul:
host: 192.168.124.147
port: 8500
config:
enabled: true
prefixes: config
default-context: application
profile-separator: ','
data-key: data
format: yaml
discovery:
register: true
prefer-ip-address: true
instance-id: ${spring.application.name}-${server.port}
service-name: ${spring.application.name}
port: ${server.port}
heartbeat:
enabled: true
When I create another service-module, I have to copy this to another, even I don’t need change anywhere.
Definately I have move some secret properties to config-center, but there are also properties to connect config-center and some necessary but not important properties
Now I have a parent-childrent project and create 2 modules auth, common
I have tried to declaring common as a dependency in auth, but it didn’t work, the missing properties which have been moved to common caused the application start failure
Is there any solutation to resolve my problem?
addition:
My idea may like this:
parent
|
|--auth
| |
| |--resource/application.yaml.
|
|--common
|
|--resource/application.yaml
auth and common are modules of parent, now I can edit common’s application.yaml
# config in commons
spring:
cloud:
consul:
host: 192.168.124.147
port: 8500
config: ...
com:
example:
my:
auth:
parameter: Authorization
time: 1
unit: hours
config: ...
......
cause common declares
dependency of [spring.cloud.consul] and [com.example.my.auth], then auth import common
and can extends its yaml config properties
, and it also can replace any propertiy easily
# config in auth
spring:
cloud:
consul:
host: 192.168.124.148
# no need to change other default props defined in common
com:
example:
my:
auth:
time: 2
# no need to change other default props defined in common
2
Answers
Finally I got the answer.
I have searched a lot and finally I got the reason.
Defaultly, if there are two modules
auth
andcommon
, both of them have the same name properties fileresourceapplication.yaml
, in this case, ifauth depends on common
, then the properties file of commonwill be coverd and ignored
cause one module will only have a applcation.yaml valid inresource
, definately auth's priority is bigger than commom'sHowever, Spring supports another way to allow us do that. According to 2.3. External Application Properties, Spring boot will load properties files from different location and has different priority. To avoid the shared props file be covered by current project, we can put it at
resourceconfigapplication.yaml
, then it will works well.I suppose, since the common module is included in all those sub-modules, that you should be able to place the property file there, and then use
@PropertySource("classpath:<file_name>")
on top of a@Configuration
class in each sub-module.OR, you could create the configuration in the shared module itself, and then export it as an auto-configuration.
So, sth like:
and then, inside
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
: