springcloud config demo搭建

springcloud config demo

服务端搭建

新建远程git仓库,保存配置,并将仓库拉到本地,以后更新配置可在本地更新,在push到远程仓库

在这里插入图片描述

在这里插入图片描述

新建3355配置中心模块

添加依赖:

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

主启动开启配置:

@SpringBootApplication
@EnableConfigServer
public class ConfigServer {

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

yaml配置文件:

server:
  port: 3355

spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          #uri: #Github上的git仓库名字
          uri: https://gitee.com/kevin_cai_ren/springcloud-config.git
          ##搜索目录.这个目录指的是github上的目录
          search-paths:
            - springcloud-config
          username: XXXXXX@qq.com
          password: XXXXXX
      ##读取分支
      label: master

eureka:
  client:
    service-url:
      defaultZone: http://eureka7779.com:7779/eureka/

启动服务拉取配置

在这里插入图片描述

客户端搭建

引入依赖:

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

修改配置文件,注意,这里使用的是bootstrap.yml,原因是bootstrap是系统级配置文件,优先级高于application.yml

server:
  port: 3366

spring:
  application:
    name: config-client
  cloud:
    #Config客户端配置
    config:
      label: master #分支名称
      name: config #配置文件名称
      profile: dev #读取后缀名称 上诉3个综合就是 master分支上 config-dev.yml
      uri: http://config-3355.com:3355 # 配置中心地址

eureka:
  client:
    service-url:
      defaultZone:  http://eureka7779.com:7779/eureka/

主启动:

@EnableEurekaClient
@SpringBootApplication
public class ConfigClient {

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

@Value注解读取配置

@RestController
public class TestController {

    @Value("${config.info: test}")//读远程配置,:后边是默认值
    private String info;

    @RequestMapping("/get")
    public Object get(){
        return info;
    }
}

测试:

在这里插入图片描述

但是存在一个问题,如果我修改了远程配置仓库的配置,发现客户端读取的到配置并没有立马更新,得重启一次才行。

解决:

采取动态刷新

引入监控依赖:

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

修改yml暴露监控端点:

#暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

在读取配置的类上新增注解@RefreshScope

@RestController
@RefreshScope
public class TestController {

    @Value("${config.info: test}")//读远程配置,:后边是默认值
    private String info;

    @RequestMapping("/get")
    public Object get(){
        return info;
    }
}

ok,这样就实现了动态刷新,需要两个步骤:

  • git修改配置
  • 发送一个post请求,请求地址:http://localhost:3366/actuator/refresh

这样就可以是实现半自动更细hhh

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值