好问题。
首先,确保spring-cloud-starter-config 位于要使用配置服务远程配置的应用程序的类路径中。
http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_client_side_usage
了解配置服务是否正确地为应用程序提供环境配置的最佳方法是启用运行状况检查。
在您的配置服务配置中,确保为您的一个应用程序启用以下功能。我为movie 服务添加了健康检查,标签为master(表示使用我的git 存储库的主分支)。
spring:
cloud:
config:
server:
git:
uri: https://github.com/kbastani/spring-boot-microservice-config
health:
repositories:
movie:
label: master
现在我需要确保的是,我的 git 存储库有一个可用于我的应用程序的配置,其名称为 movie。此配置的名称可能是movie.{properties|yml}。我选择使用 yaml:https://github.com/kbastani/spring-boot-microservice-config/blob/master/movie.yml
现在,在您启动配置服务后,您可以运行健康检查以查看远程存储库是否正在使用。
$ curl http://localhost:8888/health
这将返回以下响应:
{
"status" : "UP",
"configServer" : {
"status" : "UP",
"repositories" : [ {
"sources" : [ "https://github.com/kbastani/spring-boot-microservice-config/movie.yml", "https://github.com/kbastani/spring-boot-microservice-config/application.yml" ],
"name" : "movie",
"profiles" : [ "default" ],
"label" : "master"
} ]
},
"discoveryComposite" : {
"description" : "Spring Cloud Eureka Discovery Client",
"status" : "UP",
"discoveryClient" : {
"description" : "Spring Cloud Eureka Discovery Client",
"status" : "UP",
"services" : [ "configserver" ]
}
},
"diskSpace" : {
"status" : "UP",
"total" : 498954403840,
"free" : 445484142592,
"threshold" : 10485760
},
"hystrix" : {
"status" : "UP"
}
}
现在在电影服务中,确保在bootstrap.yml中设置了以下配置。
spring:
application:
name: movie
profiles:
active: default
cloud:
config:
uri: http://localhost:8888
failFast: true
现在启动您的电影服务,首先确保配置服务正在运行并且在http://localhost:8888 可用,远程配置将用于指定的配置文件。