zoukankan      html  css  js  c++  java
  • 配置中心初识

          在项目开发完毕,进行部署运维的时候,总有很多配置文件要修改,稍有不慎漏掉或者跟运维人员沟通不畅就会很麻烦,很多时候上线时候的加班时间就是这么秏进去的。spring cloud的配置中心提供了让我们把配置文件统一管理的解决方案,只需要在一个地方统一改一个配置文件,上线运维不用做任何修改,是不是很爽!其实这不是spring 独创,java也有一些类似定位的产品,比如百度的disconf,360的qconf以及淘宝的diamond。
    ------------------------------------------------------------------------------------------------------------------------------
    准备git环境:
          本实例采用码云作为git服务端,创建项目cloudconfig,在其下创建文件夹demo1,demo1中创建两个文件:client-a-envconf.prpperties跟client-a-master.yml,文件内容为:
      yml文件:
    server: 
        ip: localhost
        port: 9999 
        msg: client1 配置文件测试用例
    
          properties文件:
    server.ip=localhost
    server.port=9999
    server.msg=client1 配置文件测试用例
    
    服务端搭建:
          1、创建spring boot项目configserver,pom文件主要内容如下:
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
          依赖starter:
    <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>
    
          如果要跟eureka之类的进行搭配,自行添加相关starter即可。
    2、application.yml配置:
    server:
      port: 8080
    spring:
      application: 
        name:config-server
      cloud:
        config:
          server:
            git:
              uri: https://gitee.com/anhdbbt/cloudconfig.git #git地址
              search-paths: demo*     #在git中的搜索路径
              default-label: master      #默认分支
              username: wzy830715@163.com  #git账号
              password: ******    #git密码
    
    3、启动并测试
          在启动类配置@EnableConfigServer注解,启用配置中心。访问localhost:8080/client-a/envconfig可以看到如下页面:
          可以看到client-a-envconf.properties中的内容,包括ip,port,msg,不过msg我本地显示为乱码,,,,不要在意这些细节。
          这个地址的规则是啥?
    /{application}/{profile}[/{label}]
    /{application}-{profile}.yml
    /{label}/{application}-{profile}.yml
    /{application}-{profile}.properties
    /{label}/{application}-{profile}.properties
    

    application就是文件名的前缀,profile就是后缀,label就是git的分支所以该文件还可以这么访问:

          yml文件也是一样,根据以上规则进行填写即可。
          这样,我们发现访问的不是git而是configserver,也可以获取到配置文件信息。个人觉得,这个地址的匹配规则是初学者不了解的时候最不容易懂的地方。
    客户端搭建:
    1、搭建spring boot项目configclient,pom文件主要内容如下:
          parent部分跟服务端相同,都是引入spring boot跟cloud的默认配置
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
          依赖的starter:
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- client需要添加以下依赖,否则访问/refresh将会得到404 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    
          这里跟服务端的区别是config的两个starter不同,一个是config-server,一个是starter-config。
    2、配置bootstrap.yml
    spring:
      application:
        name: client-a
      cloud:
        config:
          uri: http://localhost:8080/    #configserver的地址
          profile: master     #文件后缀
          label: master        #分支为master
    
    3、启动类作为restcontroller并进行测试
          添加@RefreshScope@RestController注解,然后添加代码:
    @Value("${server.msg}")   //配置文件中的节点
    private String configValue;
    @RequestMapping("testConfig")
    public String test(){
        return "读取到配置中心:" + configValue;
    }
    

        我们发现端口号9999在本地并没有配置,而是在git的配置文件中的,客户端读到了这个配置。而页面的显示更是表明读到了配置文件中的msg字段。

    ------------------------------------------------------------------------------------------------------------------------------
        以上只是配置中心最简单的helloword的实现,它还支持敏感信息加密,配置security跟encrypt相关选项即可,不作赘述。

  • 相关阅读:
    SQL中内连接和外连接
    MySQL执行计划解读
    排序算法
    Edge浏览器安装sci-hub插件及使用教程
    MATLAB R2020B 使用教学——窗口布局设置
    PHP半年了,已经可以独立支撑项目,几点心得记录
    看1000行代码不如自己写10行代码
    PHP逻辑运算符中的and和&&以及or和||是有区别的
    自学PHP的野方法
    PHP中SQL查询语句的id=%d解释
  • 原文地址:https://www.cnblogs.com/nevermorewang/p/9281489.html
Copyright © 2011-2022 走看看