您的位置:首页 > 编程语言 > Java开发

Spring Cloud Config 分布式配置中心使用教程

2019-03-26 18:20 453 查看

        在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。市面上开源的配置中心有很多,BAT每家都出过,360的QConf、淘宝的diamond、百度的disconf都是解决这类问题。国外也有很多开源的配置中心Apache的Apache Commons Configuration、owner、cfg4j等等。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。

        一个配置中心提供的核心功能

  • 提供服务端和客户端支持
  • 集中管理各环境的配置文件
  • 配置文件修改之后,可以快速的生效
  • 可以进行版本管理
  • 支持大的并发查询
  • 支持各种语言

 

1.Config Server从本地读取配置文件

        Config Server可以从本地仓库读取配置文件,本地仓库是指将所有的配置文件统一写在Config Server工程目录下。Config Server暴露Http API接口,Config Client通过调用Config Server的HTTP API接口来读取配置文件。

1.1 构建Config Server

        新建一个Model工程,工程名为config-server,在pom文件中引入Config Server的起步依赖spring-cloud-config-server,pom文件代码如下:

[code]<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-server</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</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>

</project>

        在程序的启动类ConfigServerApplication加上@EnableConfigServer注解,开启Config Server的功能,代码如下:

[code]@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}

}

        在工程的配置文件application.yml中做相关的配置,包括指定程序名为config-serve,端口号为8769。通过spring.profiles.active=native来配置Config Server从本读取配置,读取配置的路径为classpath下的shared目录。application.yml配置文件的代码如下:

[code]server:
port: 8769
spring:
cloud:
config:
server:
native:
search-locations: classpath:/shared #可以配置多个路径,用“,”分隔开
profiles:
active: native
application:
name: config-server

        在工程的Resources目录下建一个shared文件夹,用于存放本地配置文件。在shared目录下,新建一个config-client-dev.yml文件,用作config-client工程的dev(开发环境)的配置文件。在config-client-dev.yml的配置文件中,指定端口号为8768,并定义一个变量foo,代码如下:

[code]server:
port: 8768

foo: version 1

1.2 构建Config Client

新建一个工程,取名为config-client,该工程作为Config Client从Config Server读取配置文件,在其pom文件引入Config的起步依赖spring-cloud-starter-config和Web功能的起步依赖spring-boot-starter-web。代码如下:

[code]<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-client</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</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>

</project>

        在其配置文件bootstrap.yml中做程序的配置,注意这里用的是bootstrap.yml,而不是application.yml,bootstrap相对于application具有优先的执行顺序。在bootstrap.yml配置文件中指定了程序名为config-client,向Url地址为http://localhost:8769的Config Server读取配置文件。如果没有读取成功,则执行快速失败(fail-fast),读取的是dev文件。bootstrap.yml配置文件中的变量spring.application.name和变量spring.profiles.active。两者以“-”相连,构成可向Config Server读取的配置文件名,所有本案例在配置中心读取的配置文件名为config-client-dev.yml。配置文件bootstrap.yml代码如下:

[code]spring:
application:
name: config-client

cloud:

2cc49
config:
uri: http://localhost:8769

profiles:
active: dev

        启动config-server工程成功后,启动config-client工程,你会控制台的日志中发现config-client向Url地址为http://localhost:8769的Config Server读取配置文件。最终程序启动端口为876,这个端口是在Config Server的Resources/shared目录中的config-client-dev.yml的配置文件中配置的,可见config-client成功的向config-server读取了配置文件信息。日志信息如下:

[code]2019-03-26 17:33:58.283  INFO 3780 --- [main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8769
2019-03-26 17:33:59.506  INFO 3780 --- [main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=config-client, profiles=[dev], label=null, version=null, state=null
2019-03-26 17:33:59.506  INFO 3780 --- [main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='classpath:/shared/config-client-dev.yml'}]}

为了进一步验证,在config-client工程里写一个API接口,读取配置文件的foo变量,并通过API接口返回,代码如下:

[code]@Controller
public class TestController {

@Value("${foo}")
protected String foo;

@GetMapping(value = "/foo")
@ResponseBody
public String hi()
{
return "配置文件远程读取测试:"+foo;
}
}

打开浏览器访问http://127.0.0.1:8768/foo,浏览器显示:

[code]配置文件远程读取测试:version 1

 

2. Config Server从远程Git仓库读取配置文件

        Spring Cloud Config支持从远程Git仓库读取配置文件,即Config Server可以不从本地的仓库读取,而是从远程Git仓库读取。这样做的好处是将配置统一管理,并且可以通过Spring Cloud Bus在不人工启动程序的情况下对Config Client的配置进行刷新。本例采用Gitee作为远程仓库。

        首先修改config-server的配置文件application.yml,代码如下:

[code]server:
port: 8769
spring:
cloud:
config:
server:
git:
uri: https://gitee.com/yangyy/springcloudconfig.git
username: ***
password: ***
search-paths: respo
label: master
application:
name: config-server

        uri是远程仓库的地址,search-paths为搜索远程仓库文件夹的地址,username和password是Git仓库的登录名和密码。如果是私人Git仓库,登录名和密码是必须的,如果是公开的Git仓库,可以不需要。label为git仓库的分支名,本例从master读取。

        将上面用到了config-client-dev.yml上传到远程仓库中,上传路径是https://gitee.com/yangyy/springcloudconfig.git。重新启动config-server,启动成功后启动config-client,可以发现config-client的端口是8768。打开浏览器访问http://127.0.0.1:8768/foo,浏览器显示:

[code]配置文件远程读取测试:version 1

        可见,config-server从远程Git仓库读取了配置文件,config-client从config-server读取了配置文件。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐