Previous implementations of Spring Cloud Config typically required clients to specify the server's URL directly using spring.cloud.config.uri. This approach becomes problematic when scaling the server into a cluster, as changes to the server's IP address or load-balancing setup necessitate manual updates to every client. To align with the service governance principles of Spring Cloud, the configuration server should be registered as a service within a service registry like Eureka. Cleints can then dynamically discover the configuration server via service discovery rather than hardcoded URLs.
Server-Side Modifications
1. Add Eureka Client Dependency
Include the Eureka client starter in the configuration server's build file (e.g., pom.xml).
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2. Configure Eureka Registration
Update application.properties to point the service to the Eureka registry and define the application name.
spring.application.name=config-center-service
eureka.client.serviceUrl.defaultZone=http://localhost:8088/eureka/
3. Enable Service Discovery
Annotate the main application class with @EnableDiscoveryClient to allow the config server to register itself.
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigCenterApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterApplication.class, args);
}
}
Client-Side Modifications
1. Add Eureka Client Dependency
Similar to the server, the client application must also include the Eureka client dependency to participate in service discovery.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2. Update Client Configuration
Modify bootstrap.properties (or bootstrap.yml) to disable the direct URI lookup and enable service discovery. The client will now look for a service registered under the name defined in spring.cloud.config.discovery.serviceId.
spring.application.name=config-client-app
spring.cloud.config.name=app-settings
spring.cloud.config.profile=dev
spring.cloud.config.label=master
# Enable Config Service Discovery
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-center-service
# Eureka Registry Location
eureka.client.serviceUrl.defaultZone=http://localhost:8088/eureka/
spring.cloud.config.discovery.enabled: Activates the service discovery feature for the config client.spring.cloud.config.discovery.serviceId: The name of the configuration server as registered in Eureka.
3. Enable Discovery on Client
Ensure the client's main class is also annotated with @EnableDiscoveryClient.
Verification
- Start the Eureka Server application.
- Start the modified Config Server (Config Center).
- Start the Client Application.
- Navigate to the Eureka dashboard (e.g.,
http://localhost:8088/) to confirm both the config server and client are registered. - Access the client endpoint (e.g.,
http://localhost:8002/hello) to verify that properties are being loaded dynamically from the config server via Eureka.