Spring Cloud入门实战(五)------配置中心

前言

这一篇主要介绍Spring Cloud如何实现配置中心管理。主要解决不同环境的配置混乱的问题,抽出单独的配置中心模块来集中管理配置文件。

正文

实例主要分三个模块:

  • 配置文件
  • 配置服务器
  • 配置客户端

配置文件

实例项目路径

配置文件
此实例假定分为开发和线上两个环境,分两个不同的配置文件,分别以-dev以及-prod作为环境标识,后面将介绍两个标识的作用。

配置文件内容
配置文件中只包含一个示例属性,生产环境即test.hello=hello world prod!,开发环境即test.hello=hello world dev!

配置服务器

实例项目路径

第一步,依赖

Maven的.pom文件中客户端所需依赖:

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
        <version>1.3.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
        <version>1.3.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.8</version>
    </dependency>

第二步,配置

定义配置文件,命名为application.yml或application.properties均可。

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/keysilence/spring-cloud-silence.git
          searchPaths: config-file
    label: master
server:
  port: 7788
  • uri指定git的地址;
  • searchPaths指定配置文件对应git的实际路径;
  • label代表分支;

第三步,启动

构建启动类:

package com.silence.spring.cloud.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);

    }

}

验证

服务器验证结果

运行并键入http://localhost:7788/config-client-prod/master 链接,可以查看配置服务是否正常读取对应的配置文件。

配置客户端

第一步,依赖

Maven的.pom文件中客户端所需依赖:

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
        <version>1.3.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
        <version>1.3.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>1.5.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.8</version>
    </dependency>

第二步,配置

定义配置文件,命名为application.yml或application.properties均可。

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      profile: prod
      uri: http://localhost:7788/
server:
  port: 7799
  • label代表分支;
  • profile代表环境标识,这里将根据应用名字即config-client拼上profile才能找到具体的文件;
  • uri指定对应的config-server的地址;

第三步,调用类

构建配置客户端类,以rest为例:

package com.silence.spring.cloud.config.client;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigController {

    @Value("${test.hello}")
    private String testString;

    @RequestMapping("/test")
    @ResponseBody
    public String testString() {

        return testString;

    }

}

第四步,启动

构建启动类:

package com.silence.spring.cloud.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConfigClientApplication {

    public static void main(String[] args) {

        SpringApplication.run(ConfigClientApplication.class, args);

    }

}

验证

客户端验证结果

运行并查看结果,能够取到对应的配置信息,此处着重说明,如果git上面配置信息有变化,则需要重启ConfigClientApplication后才能够刷新新的配置内容,启动ConfigServerApplication是无效的,因为ConfigClientApplication启动后@Value(“${test.hello}”)注解将获取对应的值,如果client不重启,将不会动态更新内容。

猜你喜欢

转载自blog.csdn.net/keysilence1/article/details/80228607