美文网首页程序员
SpringCloud-Spring Cloud Config配

SpringCloud-Spring Cloud Config配

作者: zpwd63 | 来源:发表于2019-04-10 10:54 被阅读0次

    Spring Cloud Config(以下简称配置中心)支持配置放在本地的配置服务内存中,同时也可以放在git仓库中进行读取,本文示例通过git仓库实现配置读取。

    一、服务端配置

    • 服务端配置分创建配置git仓库和服务端配置读取。
    1、git仓库创建

    在创建配置中心时为后续维护方便,应对配置文件进行分包、分环境进行创建。具体注意事项见代码注释

    1. 分包:这里所指的分包其实是只git的目录结构符合一定规则。
    2. 分环境:不同的环境对应不同的配置,比如:生产环境、测试环境、开发环境。

    示例配置文件:config-client-dev.properties(该配置文件命名应符合一定规则,具体说明见客户端代码注释)
    配置信息如下:

    file = 1.jpg
    word = 666

    2、配置中心服务搭建
    • maven配置
    <groupId>com.wk.sc</groupId>
        <artifactId>springcloud-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
        </properties>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.4.RELEASE</version>
            <relativePath/>
        </parent>
    
        <dependencies>
              <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-config-server</artifactId>
            </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>
    
    • application.yml配置
    server:
      port: 6060
    
    spring:
      application:
        name: config-server
      cloud:
        config:
          server:
            git:
              uri: https://gitee.com/umeone/springcloud-config.git  # 配置文件存放git地址,基于哪个git平台或自己搭建,随意
              search-paths: pro-test # 配置文件存放路径(文件夹),如果配置文件在项目根目录,则不需要配置该值
              username:   # git账号  当git项目创建为public时,不需要username和password
              password:  # git密码
          label: master # git配置文件所在分支
    
    • 启动类
    @EnableConfigServer  //开启配置服务器功能
    @SpringBootApplication
    public class ConfigServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ConfigServerApplication.class, args);
        }
    
    }
    
    

    至此,简单的配置中心服务端就算搭建完成了。启动服务进行测试,看是否搭建成功。
    执行请求:http://localhost:6060/config-client/dev (该地址符合一定规则,客户端配置文件注释会说明) 得到如下返回结果:

    {
        "name": "config-client",
        "profiles": ["dev"],
        "label": null,
        "version": "167af1f8aeb67958c22f656d2e14b032f6319da2",
        "state": null,
        "propertySources": [{
            "name": "https://gitee.com/umeone/springcloud-config.git/config-client-dev.properties",
            "source": {
                "file": "1.jpg",
                "word": "666"
            }
        }]
    }
    

    从该结果中可以看到,git中配置的信息已经获取到,说明服务没问题。

    二、客户端配置

    客户端通过配置中心读取git的配置信息,需要注意一些细节和规则,详情在代码注释中进行描述。

    • maven配置
    <groupId>com.wk.sc</groupId>
        <artifactId>springcloud-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <properties>
            <java.version>1.8</java.version>
            <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
        </properties>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.4.RELEASE</version>
            <relativePath/>
        </parent>
    
        <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>
        </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>
    
    • bootstrap.yml配置
    #  注意此配置文件名为bootstrap.yml,而不是application.yml  后者不是默认读取文件名
    server:
      port: 6061
    
    spring:
      application:
        name: config-client1
      cloud:
        config:
          label: master # 指定分支
          profile: dev # 指定环境, 和配置文件后缀dev一致
          uri: http://localhost:6060/  # 配置服务中心地址
          name: config-client
    
        # spring.cloud.config.name 值和配置文件的名称保持一定规则。如此处名称,在git中的配置文件名应为config-client-dev.properties ,前缀保持一致,后面的dev为不同环境的区分,这个规则和Springboot配置文件的环境区分类似。
        # 如果该值没有配置,项目启动时会取spring.application.name值作为默认值(这仅仅是个猜测,通过更改name值进行测试,没有查看源码实现,要想确定是否猜测正确,需要从源码进行确认)
        # URL与配置文件的映射关系:
        # /{application}/{profile}[/{label}]
        # /{application}-{profile}.yml
        # /{label}/{application}-{profile}.yml
        # /{application}-{profile}.properties
        # /{label}/{application}-{profile}.properties
    

    此处需要特别注意,配置文件名变成了bootstrap.yml,如果使用application.yml,项目启动时不会去加载配置信息,所有配置将采用默认配置

    • 启动类和配置获取
    @RestController
    @SpringBootApplication
    public class ConfigClientApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ConfigClientApplication.class, args);
        }
    
        @Value("${file}")  //如果配置中心获取失败则项目启动报错
        private String file;
        @Value("${word}")
        private String word;
    
        @RequestMapping(value = "/test")
        public String test() {
            return file + ":" + word;
        }
    }
    

    至此客户端配置完成。启动项目看是否成功,如果报错提示获取不到对应配置信息,说明配置有问题,在项目启动的输出日志中仔细查看配置信息是否正确。以下为成功启动的部分输出日志:

    2019-04-10 10:00:33.690  INFO 5708 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:6060/
    2019-04-10 10:00:35.368  INFO 5708 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=config-client, profiles=[dev], label=master, version=167af1f8aeb67958c22f656d2e14b032f6319da2, state=null
    2019-04-10 10:00:35.368  INFO 5708 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://gitee.com/umeone/springcloud-config.git/config-client-dev.properties'}]}
    2019-04-10 10:00:35.372  INFO 5708 --- [           main] com.wk.sc.cc.ConfigClientApplication     : No active profile set, falling back to default profiles: default
    

    如果启动报错,注意查看Fetching config from server Located environment Located property source对应值是否是配置文件所配置的信息。
    上文提到的配置文件名称bootstrap.yml如果不正确,就会出现配置信息和这里的加载信息不一致的问题,可以明显看出项目启动加载的是默认配置信息。

    1.jpg:666
    

    以上结果说明配置可用。

    相关文章

      网友评论

        本文标题:SpringCloud-Spring Cloud Config配

        本文链接:https://www.haomeiwen.com/subject/pltniqtx.html