dependencies { compile 'org.springframework.cloud:spring-cloud-config-server' compile 'org.springframework.cloud:spring-cloud-starter-eureka' compile 'org.springframework.boot:spring-boot-starter-actuator' }
@SpringBootApplication @EnableConfigServer public class ConfigServerApp { public static void main(String[] args) { SpringApplication.run(ConfigServerApp.class, args); } }
spring: application: name: config-server cloud: config: server: git: uri: file://${user.home}/space/app-config search-paths: '{application}/{profile}' server: port: 7001
上面使用的本地文件系统方式进行配置仓库的内容管理,该方式仅用于开发和测试。在生产环境中务必搭建本身的Git配置库。git
eureka: client: serviceUrl: defaultZone: http://localhost:11111/eureka healthcheck: enabled: true
management: security: enabled: false
dependencies { compile 'org.springframework.cloud:spring-cloud-starter-eureka' compile 'org.springframework.cloud:spring-cloud-starter-config' compile 'org.springframework.boot:spring-boot-starter-actuator' }
spring: cloud: config: \# uri: http://localhost:7001 discovery: enabled: true service-id: config-server fail-fast: true application: name: hello-cloud profiles: active: ${SPRING_PROFILES_ACTIVE:local} eureka: client: serviceUrl: defaultZone: http://localhost:11111/eureka healthcheck: enabled: true server: port: 8001
上面spring.application.name即会用在search-paths的{application},spring.profiles.active便是search-paths的{profile}。
同时从服务发现中查找config-server。若是不使用服务发现,可以使用spring.cloud.config.uri指定静态的url。
spring.cloud.config.fail-fast用于快速验证config server的链接可用状态,防止container前期load时长过长,到后面config server才发现不能用而启动失败。github
@EnableDiscoveryClient @SpringBootApplication public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } }
management: context-path: /actuator security: enabled: false
@RefreshScope @RestController public class HelloController { private final Logger logger = Logger.getLogger(getClass().getName()); @Value("${from}") private String from; @GetMapping("/from") public String from(){ return this.from; } }
spring: application: name: config-server cloud: config: server: git: uri: git@github.com:XXX/app-config.git search-paths: '{application}/{profile}' passphrase: ******** force-pull: true repos: none_prod: pattern: - '*/dev' uri: git@github.com:XXX/app-config.git searchPaths: '{application}/{profile}' prod: pattern: - '*/prod' uri: git@github.com:XXX/prod-app-config.git searchPaths: '{application}' health: repositories: none_prod: name: none_prod profiles: dev server: port: 7001 eureka: client: serviceUrl: defaultZone: http://localhost:11111/eureka healthcheck: enabled: true
spring.cloud.config.server.git.uri为默认配置库。 spring.cloud.config.server.health.repositories用来配置/health endpoint中健康检查的repos。web
$ eval "$(ssh-agent -s)" \# 添加config文件 \# Host gitlab.com \# HostName gitlab.com \# AddKeysToAgent yes \# UseKeychain yes \# User TTT \# IdentityFile ~/.ssh/id_tw_rsa $ ssh-add -K ~/.ssh/id_tw_rsa \# 查看agent public keys $ ssh-add -l