canna-cloud【十二】spring cloud config center fresh配置

本文详细介绍Spring Cloud Config Server的搭建及配置,包括依赖引入、YAML文件设置、Actuator集成,以及Config Client的使用与配置刷新测试。适用于微服务架构下统一管理配置的需求。

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

spring cloud 版本:Greenwich.RELEASE

一、config server,依赖引入

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

application.yml配置

​
server:
  port: 7010
  servlet:
        context-path: /config-server

eureka:
  client:
    healthcheck:
      enabled: true
    registerWithEureka: false
    fetchRegistry: false
  server:
    waitTimeInMsWhenSyncEmpty: 0

​

bootstrap.yml配置

spring:
  application:
    name: "canna-cloud-module-eureka-server"
  cloud:
    config:
      uri: http://localhost:8888
      server:
        git:
          search-paths: config
          username: ""
          password: ""
          uri:""
      label: master
      name: config-client
      profile: prod

启动类

@SpringBootApplication
@EnableConfigServer
@RestController
public class ConfigServerApplication {

    @RequestMapping("/hello")
    public String home() {
        return "eureka server start success";
    }

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

}

二、config client使用

application.yml配置

server:
  port: 7020
  servlet:
        context-path: /config-client

management:  #actuator
  server:
    port: 7021
  endpoints:
      web:
        base-path: /actuator #默认是/actuator 前缀,可以在这里修改
        exposure:
          include: "*"  #打开全部请求端点
  #        include: refresh,health,info #打开部分

eureka:
  client:
    healthcheck:
      enabled: true
    registerWithEureka: false
    fetchRegistry: false
  server:
    waitTimeInMsWhenSyncEmpty: 0

bootstrap.yml配置

spring:
  application:
    name: "canna-cloud-module-eureka-server"
  cloud:
    config:
      uri: http://localhost:7010/config-server
      label: master
      profile: dev
      name: config-client

启动类

@SpringBootApplication
public class ConfigClientServerApplication {

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

}

配置刷新测试Controller。注意在需要的地方添加注解:@RefreshScope

@RestController
@RefreshScope
public class ConfigController {

    @Value("${myww}")
    String myww;

    @RequestMapping("/hello")
    public String home() {
        return "config client server start success, value:" + myww;
    }


}

调用postman,访问POST方法:

http://localhost:7021/actuator/refresh

 

再请求controller即可

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值