package com.itmuch.cloud; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ConfigClientController { @Value("${profile}") private String profile; //可以获取本地application.yml配置文件profile: ssss的值 @GetMapping("/profile") public String getProfile() { System.out.println(this.profile); return this.profile; } }
package com.itmuch.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
application.yml
server: port: 8081 # profile: ssss # 通过localhsot:8081/profile就可以访问到config server里面的配置,如果本地有profile属性就加载本地的profile属性 #
bootstrap.yml
# springcloud里面有一个启动上下文,用来加载远端的配置就是config server里面的配置 # 先加载bootstrap.yml加载config server里面的配置,然后去加载application.yml里面的配置 # spring: cloud: config: uri: http://localhost:8080 #config server的端口 profile: dev label: master # 当configserver的后端存储是Git时,默认就是master application: name: foobar #本工程的名字,去config server的git仓库里面找foobar-dev.yml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.itmuch.cloud</groupId>
<artifactId>microservice-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>microservice-config-client</artifactId>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- config client依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- springmvc依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
package com.itmuch.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }
application.yml
server: port: 8080 spring: cloud: config: server: git: uri: https://git.oschina.net/it-much/config-repo-51cto-video username: password: # git里面 foobar-dev.yml内容profile: profile-dev,给工程foobar使用的。 # git里面application.yml内容profile: profile-default,档foobar工程找不到foobar-dev.yml时候使用。
application.yml.bak1-基础使用方式
server: port: 8080 spring: cloud: config: server: git: uri: https://git.oschina.net/it-much/config-repo-51cto-video
application.yml.bak2-通配符
server: port: 8080 spring: cloud: config: server: git: uri: https://git.oschina.net/it-much/{application}
application.yml.bak3-模式匹配
server: port: 8080 spring: cloud: config: server: git: uri: https://git.oschina.net/it-much/config-repo-51cto-video # 公用 repos: simple: https://git.oschina.net/it-much/simple special: pattern: special*/dev*,special*/test* uri: https://git.oschina.net/it-much/special
application.yml.bak4-搜索路径
server: port: 8080 spring: cloud: config: server: git: uri: https://git.oschina.net/it-much/config-repo-51cto-video # 公用 search-paths: - foo # foo路径 - bar # bar路径
application.yml.bak5-cloneOnStart
server: port: 8080 spring: cloud: config: server: git: uri: https://git.oschina.net/it-much/config-repo-51cto-video # 公用 clone-on-start: true repos: simple: https://git.oschina.net/it-much/simple special: pattern: special*/dev*,special*/test* uri: https://git.oschina.net/it-much/special cloneOnStart: false
application.yml.bak6-账号密码
server: port: 8080 spring: cloud: config: server: git: uri: https://git.oschina.net/it-much/config-repo-51cto-video username: password:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.itmuch.cloud</groupId>
<artifactId>microservice-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>microservice-config-server</artifactId>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- config server依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
</project>