Config配置中心

Config配置中心

Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。

Config配置中心的基本使用,git中心存在文件

在这里插入图片描述

服务端

从git中拉取文件。

  1. 添加依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    
  2. 主启动类

    @SpringBootApplication
    @EnableConfigServer
    public class ConfigCenterApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ConfigCenterApplication.class, args);
        }
    
    }
    
  3. application.yml配置文件

    server:
      port: 3344
    
    spring:
      application:
        name: cloud-config-center
      cloud:
        config:
          server:
            git:
              uri: https://gitee.com/talent-player/spring-cloud #github仓库名字
              #搜索目录
              search-paths:
                - spring-cloud
          #读取分支
          label: master
    
客户端

连接服务端使用git中的配置文件。

  1. 添加客户端依赖以及bootstrap配置文件依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bootstrap</artifactId>
    </dependency>
    
  2. bootstrap.yml配置文件

    bootstrap也是一种配置文件,是系统级配置文件,加载时间比application早。

    #分支名称
    spring.cloud.config.label=master
    #配置文件名称
    spring.cloud.config.name=config
    #读取后缀名称
    spring.cloud.config.profile=dev
    #配置中心地址
    spring.cloud.config.uri=http://localhost:3344
    
  3. 配置完即可使用${}来引入配置

    由于bootstrap的加载时间比application时间早,bootstrap加载完后配置即可使用,所有可以在application中用${}引入配置。下面为application文件

    server.port=8002
    server.servlet.context-path=/sent
    #服务名称
    spring.application.name=cloud-payment-service
    
    spring.datasource.driver-class-name=${spring.datasource.driver-class-name}
    spring.datasource.url=${spring.datasource.url}
    spring.datasource.username=${spring.datasource.username}
    spring.datasource.password=${spring.datasource.password}
    
    #指定mapper文件路径
    mybatis-plus.mapper-locations=classpath:mapper/*.xml
    #开启日志输出
    mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
    
    #false表示向注册中心注册自己
    eureka.client.register-with-eureka=true
    #false表示自己就是注册中心
    eureka.client.fetch-registry=true
    #设置与eureka server交互地址查询服务和注册服务都依赖这个地址
    eureka.client.service-url.defaultZone=http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
    
    eureka.instance.instance-id=payment8002
    eureka.instance.prefer-ip-address=true
    

    java代码中也可通过@Value引入配置

    @Value("${msg}")
    private String msg;
    
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值