对于spring cloud有一定了解之后,我们会发现,spring cloud config server是非常重要的,它可以让你很方便的更新各个服务的配置,但是如何正确的使用这个服务呢,请看下面的介绍。
首先介绍环境信息:
1. spring cloud version: Greenwich 2.1.6
2. eureka server 做注册中心,config server 做配置中心
3. 了解bootstrap.yml和application.yml的加载顺序,对于注册和配置的加载,最好放到bootstrap.yml里边
第一步,先添加并配置eureka server
spring:
security:
user:
name: abc
password: 123456
eureka:
instance:
hostname: localhost
prefer-ip-address: false
client:
register-with-eureka: false
fetch-registry: false
service-url:
default-zone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/
第二步,添加并配置config server
server:
port: 8011
spring:
application:
name: baseservice-config-server
profiles: default
devtools:
add-properties: false
cloud:
config:
server:
git:
uri: https://github.com/alecchyi/farm-config
username: xxxxx
password:
search-paths: config-dev
default-label: master
basedir: ./git_config
fail-fast: true
retry:
max-attempts: 3
security:
user:
name: abc
password: 123456
eureka:
instance:
appname: baseservice-config-server
client:
serviceUrl:
defaultZone: http://${username}:${password}@eureka-single-server:8010/eureka/
第三步,添加config client
spring:
profiles: default, dev
cloud:
config:
label: master
# uri: http://abc:123456@127.0.0.1:8011
discovery:
enabled: true
service-id: baseservice-config-server
username: abc
password: 123456
eureka:
client:
serviceUrl:
defaultZone: http://${spring.cloud.config.username}:${spring.cloud.config.password}@eureka-single-server:8010/eureka/
management:
endpoints:
web:
exposure:
include: health, refresh, info
对于spring cloud的这个版本,我这边实验的结果是:(spring cloud的版本变化还是很多坑的)
- config client这边,如果使用config.uri, 然后把eureka的配置放到git的配置文件里边,会出现下边的错误”spring cloud config server—No instances found of configserver“ , 但是eureka server还是能发现这个服务的,只是会出现这个错误而已
- 如果使用上文里边的配置,使用discovery.service id, 那边就需要把eureka的配置信息放到bootstrap.yml里边, 而且也不会出现1里边的错误。
沟通交流合作请加微信!