Spring Cloud 学习笔记(三)--配置中心Config Demo

概述

本文将继续介绍的是SpringCloud中的分布式配置中心Config的相关使用教程。Spring Cloud Config项目是一个解决分布式系统的配置管理方案。它包含了Client和Server两个部分,server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client通过接口获取数据、并依据此数据初始化自己的应用。

 

服务端Server

pom依赖包配置

<dependencies>
    <!--配置中心 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>

    <!--web 模块 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!--eureka 客户端 -->
    <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-netflix-hystrix</artifactId>
    </dependency>

    <!--配置文件处理器 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>

    <!--监控 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

 

启动类

@EnableConfigServer
@SpringCloudApplication
public class ConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class, args);
    }
}

 

bootstrap.yml配置文件

server:
  port: 8888
spring:
  application:
    name: demo-config
  profiles:
    active: native
  # 配置中心
  cloud:
    config:
      server:
        native:
          search-locations: file:config/
# 注册中心
eureka:
  client:
    serviceUrl:
      defaultZone: ${EUREKA_SERVICE_URL:http://demo:123456@fams-eureka:8761/eureka/}
    registry-fetch-interval-seconds: 10
  instance:
    prefer-ip-address: true
    lease-renewal-interval-in-seconds: 10
    lease-expiration-duration-in-seconds: 15
    instanceId: ${spring.application.name}:${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}

 

配置文件路径放在 项目路径/config 下。

 

客户端Client

bootstrap.yml配置文件

server:
  port: 8089

spring:
  application:
    name: demo-service
  profiles:
    active: dev
  cloud:
    config:
      fail-fast: true
      name: ${spring.application.name}
      profile: ${spring.profiles.active}
      discovery:
        enabled: true
        service-id: demo-config

# 注册中心
eureka:
  client:
    serviceUrl:
      defaultZone: ${EUREKA_SERVICE_URL:http://demo:123456@localhost:8761/eureka/}

 

启动后,就会从demo-config中尝试获取 demo-service-dev.yml 配置文件。

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值