SpringCloud Config 配置

github创建仓库

  1. github 新建仓库 spring-cloud-config
  2. 仓库内新建文件夹config,文件夹内新建文件 config-dev.yml
    在这里插入图片描述
    在这里插入图片描述
  3. config-dev.yml 文件内容
    server:
     port: 3355
    

搭建配置中心

  1. 新建微服务模块 cloud-config-center3344
  2. pom引入依赖
        <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <!--eureka-client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--   config     -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>
    
  3. 修改创建 application.yml 文件
    server:
     port: 3344
    
    spring:
     application:
       name: cloud-config-center
     cloud:
       config:
         server:
           git:
             #uri: git@github.com:EiletXie/config-repo.git #Github上的git仓库名字
             # github仓库的地址,把地址栏复制粘贴即可
             uri: https://github.com/OT-mt/spring-cloud-config
             #搜索目录.这个目录指的是github上的目录
             search-paths: /config
             #因为github为外网访问较慢,所以设置超时时间较长
             timeout: 15
         # 分支,对应github仓库的分支,可查看本文章第一张图片上的Branch
         label: master
    eureka:
     client:
       service-url:
         defaultZone: http://eureka7001.com:7001/eureka
    
  4. 建立启动类
    @SpringBootApplication
    @EnableEurekaClient
    @EnableConfigServer
    public class Config_Main3344 {
       public static void main(String[] args) {
           SpringApplication.run(Config_Main3344.class,args);
       }
    }
    
  5. 测试
    访问 http://localhost:3344/config-dev.yml,即可查看到访问到github
    中的文件。

客户端配置

  1. 创建项目 cloud-config-client3355

  2. pom依赖
    注意与服务端的依赖不同

        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
            </dependency>
            <!--eureka-client-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <!-- 注意与服务端的依赖不同 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
        </dependencies>
       ```
    
    
  3. yml文件
    此时 yml 文件名应该设为 bootstrap.yml,因为bootstrap的优先级大于application防止被 配置中心的配置所覆盖

    server:
      port: 3355
    
    spring:
      application:
        name: cloud-client
      cloud:
        config:
          # 分支与配置中心含义一样
          label: master
          # name与profile在下面讲解
          name: config
          profile: dev
          #对应配置中心的端口
          uri: http://localhost:3344
    
    eureka:
      client:
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka
    
  4. SpringCloud Config HTTP服务具有以下格式的资源:

    /{application}/{profile}[/{label}]
    /{application}-{profile}.yml
    /{label}/{application}-{profile}.yml
    /{application}-{profile}.properties
    /{label}/{application}-{profile}.properties
    

    这代表可以用什么样的http路径来访问github中的资源。
    上述五种格式中:

    1. application 代表 config.name
    2. label 代表 config.label
    3. profile 代表 config.profile、

    由于本文中采用的是yml,所以这代表着可以用以下三个 路径访问github中的文件

    1. http://localhost:3344/config/dev/master
    2. http://localhost:3344/config-dev.yml
    3. http://localhost:3344/master/config-dev.yml
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值