dependencies { compile('org.springframework.cloud:spring-cloud-config-server', 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server' ) testCompile('org.springframework.boot:spring-boot-starter-test') } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } }
server: port: 8200 spring: application: name: lind-config-server cloud: config: server: git: uri: https://github.com/bfyxzls/lindconfig.repo.git/ search-paths: config-repo username: bfyxzls@sina.com password: gemini123
@EnableDiscoveryClient @EnableConfigServer @SpringBootApplication class Application { public static void main(String[] args) { // demo http://localhost://8200/email-svt.yml SpringApplication.run(Application.class, args); } }
dependencies { compile('org.springframework.boot:spring-boot-starter-web', 'org.springframework.cloud:spring-cloud-starter-config', 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server') testCompile('org.springframework.boot:spring-boot-starter-test') } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } }
spring: application: name: lind cloud: config: uri: http://localhost:8200 profile: svt label: master #当 ConfigServer 的后端存储的是 Git 的时候,默认就是 master eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
@EnableEurekaClient @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } @RestController public class HomeController { @Autowired private Environment profile; @RequestMapping("/") public String index(@RequestParam String key) { return key + "=" + profile.getProperty(key); } }
这是一个GIT仓库,主要存储我们的配置文件的,以application.name作为文件名,profile作为后缀,我们客户端就是某个应用程序,它以后从仓库获取配置信息。
graph TD B(lind.yml)-->A(lind项目最终配置) C(lind.svt.yml)-->A D(lind.production.yml)-->A
lind.yml
默认的配置
lind.svt.yml
测试环境配置,将集成默认配置
lind.production.yml
生成环境配置,将集成默认配置
graph TD A(用户服务)-->B(配置中心) C(订单服务)-->B D(商品服务)-->B B-->E(Git仓库)