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