How to Use Config Server in Spring Cloud: Setup and Example
To use
Spring Cloud Config Server, create a Spring Boot application with the @EnableConfigServer annotation and configure it to serve configuration files from a Git or local repository. Then, client applications connect to this server to fetch their configuration properties dynamically at startup or runtime.Syntax
The basic setup involves creating a Spring Boot application annotated with @EnableConfigServer. You configure the server's source of configuration files in application.yml or application.properties. Clients use spring.cloud.config.uri to point to the config server URL.
@EnableConfigServer: Activates the config server functionality.spring.cloud.config.server.git.uri: URL of the Git repo holding config files.spring.cloud.config.uri: Client property to locate the config server.
java/yaml
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } } # application.yml for Config Server spring: cloud: config: server: git: uri: https://github.com/your-repo/config-repo clone-on-start: true # application.yml for Config Client spring: application: name: demo-client cloud: config: uri: http://localhost:8888
Example
This example shows a simple Spring Cloud Config Server serving configuration from a GitHub repository and a client fetching properties from it.
The server runs on port 8888 and serves config files stored in the Git repo. The client application fetches its configuration from the server at startup.
java/yaml
/* Config Server Application (Java) */ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } } /* application.yml for Config Server */ spring: cloud: config: server: git: uri: https://github.com/example/config-repo clone-on-start: true server: port: 8888 /* Config Client Application (Java) */ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.beans.factory.annotation.Value; @SpringBootApplication @RestController public class ConfigClientApplication { @Value("${custom.message:Default message}") private String message; public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } @GetMapping("/message") public String getMessage() { return message; } } /* application.yml for Config Client */ spring: application: name: demo-client cloud: config: uri: http://localhost:8888 server: port: 8080
Output
When you run the Config Server and then the Config Client, accessing http://localhost:8080/message returns the value of 'custom.message' from the Git repo config file, e.g., "Hello from Config Server!"
Common Pitfalls
- Incorrect Git URI: The config server won't start or serve configs if the Git repository URL is wrong or inaccessible.
- Missing
@EnableConfigServerannotation: Without this, the app won't act as a config server. - Client not pointing to server: Clients must set
spring.cloud.config.urito the server URL. - Config file naming: Config files must follow the pattern
{application}-{profile}.ymlor{application}.ymlin the repo. - Network issues: Clients must be able to reach the config server URL.
yaml
/* Wrong client config (missing URI) */ spring: application: name: demo-client /* Correct client config */ spring: application: name: demo-client cloud: config: uri: http://localhost:8888
Quick Reference
- Config Server: Annotate main class with
@EnableConfigServer. - Config Source: Set
spring.cloud.config.server.git.urito your config repo. - Client Setup: Use
spring.cloud.config.urito point to the server. - Config Files: Store files as
{application}-{profile}.ymlin the repo. - Run Order: Start config server before clients.
Key Takeaways
Add @EnableConfigServer to your Spring Boot app to create a config server.
Configure the server to read config files from a Git repository or local source.
Clients must set spring.cloud.config.uri to fetch configuration from the server.
Ensure config files follow naming conventions like application-profile.yml in the repo.
Start the config server before client applications to avoid connection errors.