Working with Spring Cloud Configuration
1. Setting Up Spring Cloud Config Server
Example: Config server dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
Example: Enable config server
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApp { public static void main(String[] a) { SpringApplication.run(ConfigServerApp.class, a); } }
2. Configuring Config Server with Git Backend
Example: Configure Git-backed config server
server.port: 8888
spring:
cloud:
config:
server:
git:
uri: https://github.com/example/config-repo
search-paths: '{application}'
default-label: main
username: ${GIT_USER}
password: ${GIT_TOKEN}
3. Using Config Client in Applications
Example: Config client with fail-fast and retry
# application.yml (client)
spring:
application.name: orders
config.import: "optional:configserver:http://config-server:8888"
cloud:
config:
profile: ${spring.profiles.active:default}
fail-fast: true
retry: { initial-interval: 1000, max-attempts: 6 }
4. Managing Environment-Specific Configurations
| Repo File | Resolved For |
|---|---|
orders.yml |
orders (default profile) |
orders-dev.yml |
orders + dev |
orders-prod.yml |
orders + prod |
application.yml |
Shared by all apps |
5. Refreshing Configuration (@RefreshScope)
Example: Refresh-scoped bean on POST /actuator/refresh
@RestController
@RefreshScope
public class FeatureController {
@Value("${feature.banner:hello}") String banner;
@GetMapping("/banner") public String banner() { return banner; }
}
// POST /actuator/refresh to re-read external config
6. Encrypting Properties in Config Server
Example: Encrypt and store secrets with config server
# server requires JCE + encrypt.key (or asymmetric keystore)
curl http://config-server:8888/encrypt -d 'mySecret'
# → AQAB... place in repo as: db.password: '{cipher}AQAB...'
7. Using Config Server with Vault Backend
Example: Vault-backed config server
spring:
profiles.active: vault
cloud:
config:
server:
vault:
host: vault.local
port: 8200
scheme: https
backend: secret
default-key: application
8. Implementing Config Server Security
Example: Secure config server with basic auth
# Server
spring.security.user.name: configadmin
spring.security.user.password: ${CONFIG_ADMIN_PASSWORD}
# Client
spring.cloud.config.username: configadmin
spring.cloud.config.password: ${CONFIG_ADMIN_PASSWORD}
9. Using Bus Refresh for Dynamic Updates
Example: Spring Cloud Bus AMQP dependency
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
Note: POST
/actuator/busrefresh on any node broadcasts a refresh
to all clients via RabbitMQ/Kafka.10. Testing Configuration Management
| Tool | Use |
|---|---|
@SpringBootTest(properties=...) |
Override at test time |
spring.cloud.config.enabled=false |
Disable client in tests |
| WireMock for Config Server | Stub responses |
| Local file backend | spring.profiles.active=native + search-locations |