threeperson
发布于 2020-10-17 / 1 阅读
0
0

springboot 配置文件运行时更新

项目上线后,经常需要更新配置。springboot下配置默认加载后无法做到运行时更新。不过springcloud里提供了刷新

支持。

##相关依赖Spring actuator,Spring cloud starter

```

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-actuator</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter</artifactId>

</dependency>

```

##dependencyManagement 自动装配configer refresh actuator

```

<dependencyManagement>

<dependencies>

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-dependencies</artifactId>

<version>${spring-cloud.version}</version>

<type>pom</type>

<scope>import</scope>

</dependency>

</dependencies>

</dependencyManagement>

```

## 暴露配置刷新端点

```

management.endpoints.web.exposure.include=refresh

```

## 配置中追加RefreshScope 注解

```

@Component

@ConfigurationProperties(prefix = "sys")

@RefreshScope

public class Config {

...

}

```

## 刷新接口 post 请求

```

http://localhost:8080/actuator/refresh

```

```


评论