spring-cloud学习(六)———config分布式配置中心

Config

1.概念
    官网:https://cloud.spring.io/spring-cloud-config/spring-cloud-config.html
    	 https://springcloud.cc/spring-cloud-dalston.html#_spring_cloud_config  中文文档
    		Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性。客户端和服务器上的概念映射与Spring Environment和PropertySource抽象相同,因此它们与Spring应用程序非常契合,但可以与任何以任何语言运行的应用程序一起使用。随着应用程序通过从开发人员到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用程序具有迁移时需要运行的一切。服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置环境,以及可以访问用于管理内容的各种工具。很容易添加替代实现,并使用Spring配置将其插入。

总结:config就是作为分布式服务的配置中心,为分布式服务提供统一的配置,并允许单个微服务有自己独立的配置

2.GitHub上创建配置文件
2.1.本地创建config文件夹

#####2.2.config文件夹下创建如下3个配置文件

    #文件名:application.yml
    spring:
      profiles:
        active:
          - dev

    #文件名:application-dev.yml
    spring:
      application:
        name: config-dev
        
    test:
      name: devName

    #文件名:application-test.yml
    spring:
      application:
        name: config-test
    
    test:
      name: testName
2.3.将config文件夹提交(push)到GitHub
3.server端
3.1.GAV(pom)
    <!--springcloud的server -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
3.2.主启动类上
    @EnableConfigServer
3.3.properties/yml
    server:
      port: 5353
    
    spring:
      application:
        name: config
      profiles:
        active:
          - dev
      cloud:
        config:
          server:
            git:
              uri: https://github.com/maple313/config.git  #GitHub中配置文件的仓库地址:即config文件夹的GitHub地址
              search-paths: /**  #表示仓库下的子目录
          label: master   
3.4.测试是否成功
    #1.域名映射:计算机etc文件夹的hosts文件中下添加
    127.0.0.1       myconfig.com          #测试server端
    127.0.0.1       clientconfig.com      #测试client端
    
    #2.访问
    浏览器中输入
    http://myconfig.com:5353/application-test.yml   #显示test配置内容
    http://myconfig.com:5353/application-dev.yml 	#显示dev配置内容
    
    #3.访问规则
    /{application}/{profile}[/{label}]
    /{application}-{profile}.yml
    /{label}/{application}-{profile}.yml
    /{application}-{profile}.properties
    /{label}/{application}-{profile}.properties

4.client端
4.1.GAV(pom)
    <!--springcloud的server -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
4.2.application.yml
    spring:
      application:
        name: config-client-application

4.3.bootstrap.yml(最高优先级)
    #当配置相同时,bootstrap的配置的优先级是最高的
    spring:
      cloud:
        config:
          name: config
          profile: test  #配置文件
          label: master
          uri: http://myconfig.com:5353  #server端的IP和端口
4.4.主启动类上
    @EnableConfigServer
4.5.controller
    package com.qin.configclient.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * @author WangZB
     * @date 2019/5/13 23:05
     * @description 分布式配置中心-客户端
     * @version 1.0
     */
    @RestController
    public class ConfigTestController {
    
        @Value("${test.application.name:''}")
        private String applicationName;
        @Value("${test.name:''}")
        private String name;
    
        @RequestMapping("/config")
        public String getConfig(){
            return applicationName+"------------"+name;
        }
    }
4.5.测试
    //浏览器访问:
    http://clientconfig.com:8080/config
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值