SpringCloud(九)微服务的配置中心-Spring Cloud Config
文章内索引
[显示]
配置中心(Spring Cloud Config)
spring cloud config ,支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。
服务端(Config Server)
Simple
在http://start.spring.io/中,输入config server
添加@EnableConfigServer,配置文件指定git地址与路径即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
<?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"> <modelVersion>4.0.0</modelVersion> <groupId>com.hanson</groupId> <artifactId>ConfigCenter</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>config</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.M8</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.hanson.config; 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); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
spring.application.name=config-server server.port=8888 #配置git仓库地址 #gitlab需要.git结尾 spring.cloud.config.server.git.uri=http://huhl@gitlab.hzbd.com/Finance/Config-Repository.git #spring.cloud.config.server.git.uri=https://github.com/hanlin5566/SpringCloud-Netflix #spring.cloud.config.server.git.uri=https://github.com/spring-cloud-samples/config-repo #配置仓库路径,支持通配符 #spring.cloud.config.server.git.searchPaths=/spring-cloud-code/Config-Repository/client* #gitlab spring.cloud.config.server.git.searchPaths=var* #springcloud github #spring.cloud.config.server.git.searchPaths=foo,bar* #配置仓库的分支 spring.cloud.config.label=master #访问git仓库的用户名 spring.cloud.config.server.git.username=huhanlin@huizhongcf.com #访问git仓库的密码 spring.cloud.config.server.git.password=hanlin5566 |
在git上上传配置文件,其中有配置,ps.github访问太慢,之后换为gitlib访问。
init.failed.exit=false
访问
http://localhost:8888/var-dev.properties
返回
证明已经读取到远端git上的配置。
http请求地址和资源文件映射如下:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
客户端(Config Client)
Simple
在http://start.spring.io/中,输入config client,web
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
<?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"> <modelVersion>4.0.0</modelVersion> <groupId>com.hanson</groupId> <artifactId>server-client</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>server-client</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.M8</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project> |
@Value(“${init.failed.exit}”) // 读取gitlab配置文件中的属性,如果我们读取到了值,说明客户端是OK的
@RefreshScope //注解@RefreshScope指示Config客户端在服务器配置改变时,也刷新注入的属性值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.hanson.serverclient; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController @RefreshScope //注解@RefreshScope指示Config客户端在服务器配置改变时,也刷新注入的属性值 public class ServerClientApplication { public static void main(String[] args) { SpringApplication.run(ServerClientApplication.class, args); } @Value("${init.failed.exit}") // 读取gitlab配置文件中的属性,如果我们读取到了值,说明客户端是OK的 private String exited; @RequestMapping(value = { "" }, method = RequestMethod.GET) public String hello() { return exited; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
spring.application.name=consumer server.port=8080 #与git上的${application}相对应 spring.cloud.config.name=var #仓库的分支 spring.cloud.config.label=master #dev开发环境配置文件 #test测试环境 #pro正式环境 spring.cloud.config.profile=dev #confServer的地址 spring.cloud.config.uri= http://localhost:8888/ |
spring.application.name需要与git上的{application}一致,如果spring.application.name和git上的属性文件名对应不上,需要配置spring.cloud.config.name: 文件名不包括profile
访问localhost:8080返回
此配置为git上读取到的配置,下一章会讲到配置中心的高可用以及配置推送。
©版权声明:本文为【翰林小院】(huhanlin.com)原创文章,转载时请注明出处!
发表评论