标签:enable sso oid report desc 搜索 分布 run .net
在上一篇文章讲述zuul的时候,已经提到过,使用配置服务来保存各个服务的配置文件。它就是Spring Cloud Config。
在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。
父maven工程省略,父pom文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.forezp</groupId> 8 <artifactId>sc-f-chapter6</artifactId> 9 <version>0.0.1-SNAPSHOT</version> 10 <packaging>pom</packaging> 11 12 <modules> 13 <module>config-server</module> 14 <module>config-client</module> 15 </modules> 16 17 <name>sc-f-chapter6</name> 18 <description>Demo project for Spring Boot</description> 19 20 <parent> 21 <groupId>org.springframework.boot</groupId> 22 <artifactId>spring-boot-starter-parent</artifactId> 23 <version>2.0.3.RELEASE</version> 24 <relativePath/> 25 </parent> 26 27 28 <properties> 29 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 30 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 31 <java.version>1.8</java.version> 32 <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> 33 </properties> 34 35 <dependencies> 36 <dependency> 37 <groupId>org.springframework.boot</groupId> 38 <artifactId>spring-boot-starter-test</artifactId> 39 <scope>test</scope> 40 </dependency> 41 </dependencies> 42 43 <dependencyManagement> 44 <dependencies> 45 <dependency> 46 <groupId>org.springframework.cloud</groupId> 47 <artifactId>spring-cloud-dependencies</artifactId> 48 <version>${spring-cloud.version}</version> 49 <type>pom</type> 50 <scope>import</scope> 51 </dependency> 52 </dependencies> 53 </dependencyManagement> 54 55 <build> 56 <plugins> 57 <plugin> 58 <groupId>org.springframework.boot</groupId> 59 <artifactId>spring-boot-maven-plugin</artifactId> 60 </plugin> 61 </plugins> 62 </build> 63 </project>
创建一个spring-boot项目,取名为config-server,其pom.xml如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.forezp</groupId> 7 <artifactId>config-server</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>config-server</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>com.forezp</groupId> 16 <artifactId>sc-f-chapter6</artifactId> 17 <version>0.0.1-SNAPSHOT</version> 18 </parent> 19 20 <dependencies> 21 <dependency> 22 <groupId>org.springframework.boot</groupId> 23 <artifactId>spring-boot-starter-web</artifactId> 24 </dependency> 25 <dependency> 26 <groupId>org.springframework.cloud</groupId> 27 <artifactId>spring-cloud-config-server</artifactId> 28 </dependency> 29 </dependencies> 30 <build> 31 <plugins> 32 <plugin> 33 <groupId>org.springframework.boot</groupId> 34 <artifactId>spring-boot-maven-plugin</artifactId> 35 </plugin> 36 </plugins> 37 </build> 38 </project>
在程序的入口Application类加上@EnableConfigServer注解开启配置服务器的功能,代码如下:
1 @SpringBootApplication 2 @EnableConfigServer 3 public class ConfigServerApplication { 4 5 public static void main(String[] args) { 6 SpringApplication.run(ConfigServerApplication.class, args); 7 } 8 }
需要在程序的配置文件application.properties文件配置以下:
1 spring.application.name=config-server 2 server.port=8888 3 4 spring.cloud.config.server.git.uri=https://github.com/forezp/SpringcloudConfig/ 5 spring.cloud.config.server.git.searchPaths=respo 6 spring.cloud.config.label=master 7 spring.cloud.config.server.git.username= 8 spring.cloud.config.server.git.password=
如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写,本例子是公开仓库,放心使用。
远程仓库https://github.com/forezp/SpringcloudConfig/ 中有个文件config-client-dev.properties文件中有一个属性:
foo = foo version 3
启动程序:访问http://localhost:8888/foo/dev
1 {"name":"foo","profiles":["dev"],"label":"master", 2 "version":"792ffc77c03f4b138d28e89b576900ac5e01a44b","state":null,"propertySources":[]}
证明配置服务中心可以从远程程序获取配置信息。
http请求地址和资源文件映射如下:
重新创建一个springboot项目,取名为config-client,其pom文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.forezp</groupId> 7 <artifactId>config-client</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>config-client</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>com.forezp</groupId> 16 <artifactId>sc-f-chapter6</artifactId> 17 <version>0.0.1-SNAPSHOT</version> 18 </parent> 19 20 <dependencies> 21 <dependency> 22 <groupId>org.springframework.boot</groupId> 23 <artifactId>spring-boot-starter-web</artifactId> 24 </dependency> 25 <dependency> 26 <groupId>org.springframework.cloud</groupId> 27 <artifactId>spring-cloud-starter-config</artifactId> 28 </dependency> 29 </dependencies> 30 31 <build> 32 <plugins> 33 <plugin> 34 <groupId>org.springframework.boot</groupId> 35 <artifactId>spring-boot-maven-plugin</artifactId> 36 </plugin> 37 </plugins> 38 </build> 39 40 41 </project>
其配置文件bootstrap.properties:
1 spring.application.name=config-client 2 spring.cloud.config.label=master 3 spring.cloud.config.profile=dev 4 spring.cloud.config.uri= http://localhost:8888/ 5 server.port=8881
spring.cloud.config.profile
spring.cloud.config.uri= http://localhost:8888/ 指明配置服务中心的网址。
程序的入口类,写一个API接口“/hi”,返回从配置中心读取的foo变量的值,代码如下:
1 @SpringBootApplication 2 @RestController 3 public class ConfigClientApplication { 4 5 public static void main(String[] args) { 6 SpringApplication.run(ConfigClientApplication.class, args); 7 } 8 9 @Value("${foo}") 10 String foo; 11 @RequestMapping(value = "/hi") 12 public String hi(){ 13 return foo; 14 } 15 }
打开网址访问:http://localhost:8881/hi,网页显示:
foo version 3
这就说明,config-client从config-server获取了foo的属性,而config-server是从git仓库读取的,如图:
本文源码下载:
https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-chapter6
http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html
感谢您的关注!可加QQ1群:135430763,QQ2群:454796847,QQ3群:187424846。QQ群进群密码:xttblog,想加微信群的朋友,可以微信搜索:xmtxtt,备注:“xttblog”,添加助理微信拉你进群。备注错误不会同意好友申请。再次感谢您的关注!后续有精彩内容会第一时间发给您!原创文章投稿请发送至532009913@qq.com邮箱。商务合作可添加助理微信进行沟通!
业余草 SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)(Finchley版本)
标签:enable sso oid report desc 搜索 分布 run .net
原文地址:https://www.cnblogs.com/panda2/p/9419110.html