cloud config + bus

一、native本地配置,通过服务发现去找配置中心

1、服务端(config-server)

  • 添加依赖
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
  • 启动类添加注解:@EnableConfigServer

  • bootstrap.yml关键部分配置:

spring:
  profiles:
    active: native 		# native 表示从本地获取配置文件
    include: local
---
spring:
  profiles: local
  cloud:
    config:
      server:
        native:
          search-locations: classpath:config/local/
  • 在resources文件夹下创建文件夹并添加配置文件: config/local
|resources
|--config
|----local
|-----application-local.yml       # 所有模块共享的配置,单个模块个性化配置可以再下方具体配置文件中覆盖此文件
|-----order-server-local.yml      # 指定模块个性化配置,优先级最高

2、客户端(order-server)

  • 添加依赖

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    
  • 配置文件中添加:

spring:
  cloud:
    config:
      discovery:
        enabled: true 				# 通过服务发现的方式去找配置中心
        service-id: config-server      
      profile: local
      #username: yourusername
      #password: yourpassword

二、使用git地址

除了yml配置不一样,其他的与上面保持一致

1、git仓库目录:

|config
|--local
|----application-local.yml
|----order-server-local.yml
|--dev
|----application-dev.yml
|----order-server-dev.yml
|--test
|--master

2、服务端yml配置

spring:
  profiles: local        # local配置
  cloud:
    config:
      server:
        git:
          uri: http://***/config-server.git
          username: yourusername
          password: yourpassword
          search-paths: /config/local            # 使用local中配置

3、客户端yml配置

与第一种情况保持一致即可,这里不需改变

三、Spring Cloud Bus

1、客户端与服务端都引入依赖

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

2、客户端与服务端都添加配置

spring:
  profiles: local
  cloud:
  	# server配置这里省略 
  	# bus配置,还需要rabbitmq的支持
	bus:
      enabled: true
      refresh:
        enabled: true
  rabbitmq:
    host: server-pc1
    port: 5672
    username: username
    password: password

3、启动eureka、config、order-port1、order-port2

  • 修改配置后单独刷新模块order-port1、order-port2,查看是否生效
  • 修改配置后,只调用config的bus-refresh,查看所有客户端是否都被刷新了
bus刷新地址:   			IP:port/actuator/bus-refresh
带有security认证的地址:  username:password@IP:port/actuator/bus-refresh
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Config是一个分布式配置管理工具,它提供了集中式的外部配置管理,可以帮助我们在微服务架构中管理和维护应用程序的配置。 Spring Cloud Config的核心思想是将应用程序的配置从代码中分离出来,以便在不重新部署应用程序的情况下进行配置的修改和更新。它使用Git或其他版本控制系统来存储和管理配置文件,并通过HTTP或消息总线将配置文件提供给应用程序。 Spring Cloud Config的主要组件包括: 1. Config Server:配置服务器,负责从Git或其他版本控制系统中读取配置文件,并将其提供给客户端应用程序。它可以通过HTTP或消息总线的方式将配置文件推送给客户端。 2. Config Client:配置客户端,是应用程序中的一个模块,负责从Config Server获取配置文件,并将其应用到应用程序中。 3. Spring Cloud Bus:消息总线,用于在微服务架构中传播配置文件的变更。当配置文件发生变化时,Config Server会通过消息总线通知所有的Config Client,从而实现配置的动态更新。 使用Spring Cloud Config可以实现以下功能: 1. 集中式管理和维护应用程序的配置,避免了在每个应用程序中硬编码配置信息的问题。 2. 实现配置的动态更新,当配置文件发生变化时,应用程序可以自动获取最新的配置。 3. 支持多环境的配置管理,可以为不同的环境提供不同的配置文件。 4. 支持配置的版本控制,可以通过Git或其他版本控制系统管理配置文件的历史记录。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值