springcloud中的配置中心-config 知识讲解

本文介绍如何使用 Spring Cloud Config 实现配置中心,通过 Git 存储配置信息,并实现远程配置更新。涵盖依赖配置、启动类编写、配置文件设置等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

作用:通过config配置中心,我们将配置信息全部保存在git上面,然后拉取下来使用,有修改时,我们只是需要修改git上面的内容即可
首先是对pom文件的解析:

<dependencies>

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

        <!--eureka client 的pom-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <!--eureka 集成了hystrix-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

        <!--需要使用到web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

启动类的代码展示:

@SpringCloudApplication
@EnableConfigServer  //表明这是一个config配置中心的启动类
public class ConfigApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class, args);
    }
}

配置文件的属性讲解:

server:
  port: 8101
spring:
  application:
    name: config
  profiles:
    active: dev
  cloud:
    config:
      server:
        git:
          uri: https://github.com/wait-love/news.git #配置 git 仓库地址
          username: ***** #访问 git 仓库的用户名
          password: ***196135 #访问 git 仓库的用户密码
      label: master #配置仓库的分支

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8888/eureka/
在这里插入代码片

同时我们需要给其他模块用到config中心的pom文件如下:

<!--需要用到config中心的配置文件内容-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

然后在其bootstrap.yml文件

spring:
  cloud:
    config:
      name: eurekaclient # 这是表明需要用到的yml文件名部分,如果需要用到多个就可以用,隔开
      label: master  #表明branch名称
      discovery:
        enabled: true
        serviceId: config #config server中application name


#配置服务中心的地址
eureka:
  client:
    service-url:
      defaultZone:  http://127.0.0.1:8082/eureka/

应用了config导致的一个问题的发生,就是每次更新了文件配置都需要去启动项目,所以我们可以应用到手动刷新的模式,它是针对client端的
主要的操作流程有:pom文件的导入:配置文件的修改信息:定义一个手动的刷新路径:访问路径:http://localhost:8763/actuator/refresh 刷新这个路径之后,就会从git仓库中拉取到对应的内容了

`<!--需要添加手动刷新的pom文件-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>``

配置文件的修改内容:(记住是在git仓库中的配置文件信息)

management:
  endpoints:
    web:
      exposure:
        include: refresh,health,info

定义一个手动刷新的路径:

@RestController
@RefreshScope  //手动刷新的注解,添加这个注解,主要是因为用到git上面的文件内容
public class HelloClient {

    @Value("${server.port}")
    private String port;

    @RequestMapping(value = "hello")
    public String test(){
        return "hello client" + port;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值