Spring Boot Remote Config: Access Files Remotely
In Spring Boot, you can utilize Spring Cloud Config to remotely read configuration files. Here are the steps for using Spring Cloud Config to remotely read configuration files.
- pom.xml file
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
- configuration file for an application
- configuration file named application.yml
spring.cloud.config.uri=http://config-server:8888
- This setup/configuration
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
@Configuration
@RefreshScope
public class AppConfig {
@Value("${my.property}")
private String myProperty;
public String getMyProperty() {
return myProperty;
}
}
- Configuration file for the application
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@Autowired
private AppConfig appConfig;
@GetMapping("/myproperty")
public String getMyProperty() {
return appConfig.getMyProperty();
}
}
By following the above steps, Spring Boot can be used to remotely read configuration files. When there are changes to the configuration files, dynamic refresh of the configuration can be achieved through Spring Cloud Bus.