学科分类
目录
Spring Cloud

Config Server从本地仓库读取配置文件

下面通过一个案例演示Config Server如何从本地仓库读取配置文件,具体步骤如下:

1、 搭建Config Server

(1)使用Spring Initializr方式创建一个Spring Boot项目,这里将Group命名为com.itheima,将Artifact命名为config-server,添加Config Server、Test和Eureka Server的起步依赖。如下所示。

<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>
        <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
       </dependency>
</dependencies>

(2)在程序的启动类ConfigServerApplication添加@EnableConfigServer注解,开启Config Serve功能,代码如例1所示。

例1 config-server\src\main\java\com\itheima\configserver\ConfigServerApplication.java

 1     import org.springframework.boot.SpringApplication;
 2     import org.springframework.boot.autoconfigure.SpringBootApplication;
 3     import org.springframework.cloud.config.server.EnableConfigServer;
 4     @SpringBootApplication
 5     @EnableConfigServer
 6     public class ConfigServerApplication {
 7         public static void main(String[] args) {
 8             SpringApplication.run(ConfigServerApplication.class, args);
 9            }
 10     }

(3)在项目的配置文件application.yml中进行相关配置,包括指定服务名、端口号、本地读取配置以及读取路径,如例2所示。

例2 config-server\src\main\resources\application.yml

 1  spring :
 2           cloud:
 3             config:
 4               server:
 5                 native:
 6                   search-locations: classpath:/shared       #读取路径
 7           profiles:
 8              active: native                          #Config Server从本地读取的配置
 9           application:
 10          name: config-server
 11     server:
 12       port: 8769

(4)在项目的resource目录下建一个 shared 文件夹,用于存放本地配置文件。在 shared目录下,新建一个config-client-dev.yml文件,用作后续将要创建的config-client工程的dev开发环境的配置文件。在config-client-dev.yml配置文件中,配置端口号和自定义变量,代码如例3所示。

例3 config-server\src\main\resources\shared\config-client-dev.yml

 1     server :
 2       port : 8762
 3     foo : foo version 1

2、 构建Config Client

(1)使用Spring Initializr方式创建一个Spring Boot项目,这里将Group命名为com.itheima,将Artifact命名为config-client,添加Config、Web和Test的起步依赖。如下所示。

<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>

(2)在resources文件下新建bootstrap.yml文件,需要注意的是这里使用的是 bootstrap.yml,而不是application.yml,这是因为bootstrap.yml相对于application.yml来说,执行的优先级更高,如例4所示。

例4 config-client\src\main\resources\bootstrap.yml

 1     spring:
 2       application:
 3         name: config-client
 4       profiles:
 5         active: dev
 6       cloud:
 7         config:
 8           uri: http://localhost:8769
 9           fail-fast: true

在例4中,第3行代码指定了名称为config-client的Config Client,第4-5行指定了配置文件的开发环境。第8行指定了Config Server的uri地址。第9行代码指定是否开启快速失败。config-client会从地址为http://localhost:8769的Config Server中读取配置文件,并且该文件所处的开发环境是dev。如果配置文件读取失败,则会执行快速失败。

(3)为了更直观的看到配置文件config-client-dev.yml被读取,我们在启动类中添加一个hi()方法进行测试,修改后的ConfigClientApplication如例5所示。

例5 config-client\src\main\java\com\itheima\configclient\ConfigClientApplication.java

 1     import org.springframework.beans.factory.annotation.Value;
 2     import org.springframework.boot.SpringApplication;
 3     import org.springframework.boot.autoconfigure.SpringBootApplication;
 4     import org.springframework.web.bind.annotation.RequestMapping;
 5     import org.springframework.web.bind.annotation.RestController;
 6     @SpringBootApplication
 7     @RestController
 8     public class ConfigClientApplication {
 9         public static void main(String[] args) {
 10             SpringApplication.run(ConfigClientApplication.class, args);
 11         }
 12         @Value("${foo}")
 13         String foo;
 14         @RequestMapping(value = "/foo")
 15         public String hi(){
 16             return foo;
 17         }
 18     }

3、 测试运行

先启动config-server项目,再启动config-client项目,我们可以在控制台看到如下所示的日志。

Located environment:
name=config-client, profiles=[dev], label=null,
Located property source:CompositePropertySource {name='configService', 
propertySources=[MapPropertySource 
{name='classpath:/shared/config-client-dev.yml'}]}
The following profiles are active: dev

从控制台消息中可以看出,config-client客户端从dev开发环境的classpath:/shared下的config-client-dev.yml文件读取了配置信息。

使用浏览器访问http://localhost:8762/foo,效果如图1所示。

图1 config-client访问效果图

从图1中可以看到,页面输出了foo变量中的值。说明config-client客户端从config-server中的classpath:/shared路径下的config-client-dev.yml文件读取了配置信息。

点击此处
隐藏目录