购买
下载掌阅APP,畅读海量书库
立即打开
畅读海量书库
扫码下载掌阅APP

3.2 使用Config Server配置服务端

本节先使用Git作为配置文件存储仓库,后文中会介绍使用SVN、本地目录以及自行扩展等方式。

首先,我们需要在以Maven作为依赖管理的项目pom.xml中添加spring-cloud-starter-config、spring-cloud-config-server两项依赖,以及以spring-boot-starter-parent作为父项目。


<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Edgware.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId> spring-cloud-config-server</artifactId>
    </dependency>
</dependencies>

在项目中创建ConfigServerApplication类,其中@EnableConfigServer注解表示允许该服务以HTTP形式对外提供配置管理服务。


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

添加application.yml,新增如下内容指定Git仓库的地址:


server:
    port: 8888
spring:
    cloud:
config:
application:
    name: myConfigServer
        server:
            git:
                #Git仓库地址
        uri: https://git.oschina.net/wawzw123/SpringCloudBookCode.git
        search-paths: config-repo

如下为配置文件中的配置项。

1)spring.cloud.config.server.git.uri:配置Git仓库位置。

2)spring.cloud.config.server.git.searchPaths:配置仓库路径下的相对搜索位置,可以配置多个。

3)spring.cloud.config.server.git.username:访问Git仓库的用户名。

4)spring.cloud.config.server.git.password:访问Git仓库的用户密码。

读者在自行测试的时候需要自行创建Git仓库,并根据Git仓库信息自行替换application.properties中的内容。我们已经事先在实例的仓库中添加了如下几个文件,用于进行不同分支的不同key的读取测试。

在Master分支中添加如下文件和内容。

1)configServerDemo.properties:key1=master-default-value1;

2)configServerDemo-dev.properties:key1=master-dev-value1;

3)configServerDemo-test.properties:key1=master-test-value1;

4)configServerDemo-prd.properties:key1=master-prd-value1。

在Branch分支中添加如下文件和内容。

1)configServerDemo.properties:key1=branch-prd-value1;

2)configServerDemo-dev.properties:key1=branch-dev-value1;

3)configServerDemo-test.properties:key1=branch-test-value1;

4)configServerDemo-prd.properties:key1=branch-prd-value1。

在服务端启动后,可以基于HTTP请求访问如下URL进行配置获取。可以通过如下几种格式的HTTP向配置中心发起配置文件获取的请求:

1)/{application}/{profile}[/{label}];

2)/{application}-{profile}.yml;

3)/{application}-{profile}.json;

4)/{label}/{application}-{profile}.yml;

5)/{label}/{application}-{profile}.json;

6)/{application}-{profile}.properties;

7)/{label}/{application}-{profile}.properties。

·application:应用名称,默认取spring.application.name,也可以通过spring.cloud.config.name指定。

·profile:环境,如dev(开发)、test(测试)、prd(生产)等,通过spring.cloud.config.profile指定。

·label:版本,对应不同的分支,通过spring.cloud.config.label指定。

比如,尝试通过curl或者浏览器等发起HTTP请求“ http://localhost:8888/configServer-Demo/test/master ”,将会得到如下响应内容。


{
    "name": "configServerDemo",
    "profiles": [
        "test"
    ],
    "label": null,
    "version": "32d326655ae7d17be752685f29d017ba42e8541a",
    "propertySources": [
        {
            "name": "https://git.oschina.net/wawzw123/Spring CloudBookCode.git/config-repo/configServerDemo-test.properties",
            "source": {
                "key1": "master-test-value1"
            }
        },
        {
"name":"https://git.oschina.net/wawzw123/Spring CloudBookCode.git/config-repo/configServerDemo.properties",
            "source": {
                "key1": "master-default-value1"
            }
        }
    ]
}

访问 http://localhost:8888/configServerDemo-test.yml ,则会得到如下结果:


key1: master-test-value1

在尝试了手动从配置中心获取配置项之后,我们接下来尝试启动一个示例项目来自动从配置中心获取配置项。 0qJk+qtp52Cqx/bhRFciRbgCCnCBoJ0HApzw+dAXXpQ6kgr1XPuEsG22ljSBHn0m

点击中间区域
呼出菜单
上一章
目录
下一章
×