springcloud~~配置config配置中心

本文介绍如何使用 Spring Cloud Config 实现配置中心,并通过示例演示如何搭建配置服务器和服务客户端,实现配置的动态刷新。

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

配置config配置中心

config-server

新建model,叫service-config-server8688,依赖添加config-server

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

配置文件

server:
  port: 8688

spring:
  application:
    name: service-config-server8688
  cloud:
    config:
      server:
        git:
        #git仓库路径
          uri: https://github.com/zgl-1/cloud-config.git
          search-paths: cloud-config

eureka:
  client:
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:8300/eureka/

git仓库上面有三个配置文件
在这里插入图片描述

主启动类上面加注解

@EnableEurekaClient
@EnableConfigServer
@SpringBootApplication

然后启动服务,测试http://localhost:8688/master/application-dev.yml,结果显示“servernameversion: 1”,服务连接git仓库中心成功

config-client

新建model,叫service-config-client8689,添加依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

配置文件bootstrap.yml

server:
  port: 8689

spring:
  application:
    name: service-config-client8689
  cloud:
    config:
      label: master
      name: application
      profile: dev
      # 客户端直接去服务端找
      uri: http://localhost:8688

eureka:
  client:
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:8300/eureka/

新建controller类

@RestController
public class HelloController {
   @Value("${servernameversion}")
   public String servernameversion;

   @RequestMapping("/hello")
   public String get(){
      return servernameversion;
   }
}

启动服务,测试http://localhost:8689/hello,结果显示“1”,客户端获取成功

动态刷新配置

如果说git仓库更改了,但是客户端肯定不知道,所以就需要刷新配置,这里就介绍的是手动的刷新配置

需要添加actuator的依赖

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

配置文件添加配置

management:
  endpoints:
    web:
      exposure:
        include: "*"

controller类添加刷新注解

@RestController
@RefreshScope//刷新注解
public class HelloController {
   @Value("${servernameversion}")
   public String servernameversion;

   @RequestMapping("/hello")
   public String get(){
      return servernameversion;
   }
}

启动服务

先测试http://localhost:8689/hello,肯定获取的是“1”

现在去更改git上的配置文件,将值改为2

在测试http://localhost:8689/hello,结果还是显示的“1”

所以现在就需要我们手动的刷新一下,执行一下post请求http://localhost:8689/actuator/refresh,

然后再次测试http://localhost:8689/hello,结果就会显示“2”了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值