创建一个远程仓库(本地建立一个配置仓库由 idea 提交给 git)
本地
git
我的配置仓库
java
创建一个 config-server 组件
依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>springcloud</artifactId> <groupId>com.qn</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>config-server</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies>
</project>
|
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| server: port: 8769
eureka: client: register-with-eureka: false fetch-registry: false spring: application: name: spring-cloud-config-server cloud: config: server: git: uri: https://github.com/qn101630/qn101630.github.io.git # 配置git仓库的地址,这是我的公开git,可以不用登录密码 search-paths: config-repo # git仓库地址下的相对地址,可以配置多个,用,分割。 username: qn101630 # git仓库的账号 password: qn192837QN default-label: code
|
启动类
1 2 3 4 5 6 7
| @SpringBootApplication @EnableConfigServer public class ConfigServerApp { public static void main(String[] args) { SpringApplication.run(ConfigServerApp.class, args); } }
|
访问远程配置
访问地址:http://localhost:8769/spring-cloud-config-dev.properties
创建一个 config-client 组件
依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>springcloud</artifactId> <groupId>com.qn</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>config-client</artifactId>
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency> </dependencies>
</project>
|
配置
application.yml
1 2 3 4 5 6 7 8 9
| server: port: 8771 eureka: client: register-with-eureka: false fetch-registry: false spring: application: name: spring-cloud-config-client
|
bootstrap.yml
1 2 3 4 5 6 7
| spring: cloud: config: name: spring-cloud-config profile: dev uri: http://localhost:8769/ label: code
|
启动类
1 2 3 4 5 6
| @SpringBootApplication public class ClientApp { public static void main(String[] args) { SpringApplication.run(ClientApp.class,args); } }
|
业务类
1 2 3 4 5 6 7 8 9 10 11
| @RestController public class HelloController { @Value("${config.hello}") private String hello;
@RequestMapping("testConfig") public String test() { return "读取到配置中心:" + hello; } }
|
最终测试
启动项目后访问:http://localhost:8769/hello
问题
目前我们的配置信息是由 client 去拉取,但是 git 上的配置发生了改变,client 无法及时的获取更改后的配置,必须重新启动 client 才可以,这显然是不合适的,所以我们还要实现动态获取配置,详情请见springcloud实战之Config(2)
参考资料
配置中心 Spring Cloud Config 初体验