Spring cloud config 配置中心

目录

创建远程仓库

创建本地仓库

业务模块的配置文件,放到 git 仓库

搭建配置中心


创建远程仓库

- 在 gitee 中,右上角点加号新建仓库
- 设置仓库名
- 设置成开源项目

创建本地仓库

- vcs -- create git repository

- 选择项目工程文件夹作为本地仓库目录

- 向本地仓库提交文件

- 选中全部文件、填写提交信息,执行提交

业务模块的配置文件,放到 git 仓库

1. 在项目工程中新建文件夹:config
2. 复制业务模块的 application.yml 到 config 目录
   - item-service-dev.yml
   - user-service-dev.yml
   - order-service-dev.yml
3. 三个文件中添加 `override-none: true`
   防止下载的配置,覆盖本地参数设置

spring:
  cloud:
    config:
      override-none: true

4. 提交到本地仓库
5. 向远程仓库推送

搭建配置中心


1. 新建 config 模块

2. 添加 config server 、eureka client 依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

3. yml配置

   ```yml
   git 仓库地址
   仓库中存放配置文件的文件夹路径
   ```

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          #后面出现错误的话,可能是远程仓库有问题
          #或者仓库中文件夹路径不对
          #或者是仓库中的配置文件不对
          uri: Git仓库地址
          search-paths: config
server:
  port: 6001
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka

4. 启动类注解:`@EnalbeConfigServer`

修改业务模块的 application.yml,代码全部注释

添加config client依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

新建 bootstrap.yml (在引导配置阶段,从配置中心下载 application.yml)

   - 添加三条配置:
     - 连接 eureka
     - 指定配置中心的服务id
     - 从配置中心下载 user-service-dev.yml

eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
spring:
  cloud:
    config:
      discovery:
        enabled: true
        service-id: config-server
      name: item-service
      profile: dev

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值