{"id":21037406,"url":"https://github.com/piinalpin/spring-cloud-config","last_synced_at":"2025-09-02T10:43:15.152Z","repository":{"id":111688598,"uuid":"387327867","full_name":"piinalpin/spring-cloud-config","owner":"piinalpin","description":"Example spring cloud config. Centralized configuration","archived":false,"fork":false,"pushed_at":"2024-06-26T12:10:12.000Z","size":104,"stargazers_count":0,"open_issues_count":2,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-20T16:22:03.126Z","etag":null,"topics":["spring","spring-boot","spring-cloud"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/piinalpin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-07-19T03:28:46.000Z","updated_at":"2021-10-14T03:59:54.000Z","dependencies_parsed_at":"2025-01-20T16:20:44.498Z","dependency_job_id":"d99a4462-d324-4a3f-bbc6-e010ba9c7481","html_url":"https://github.com/piinalpin/spring-cloud-config","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piinalpin%2Fspring-cloud-config","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piinalpin%2Fspring-cloud-config/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piinalpin%2Fspring-cloud-config/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/piinalpin%2Fspring-cloud-config/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/piinalpin","download_url":"https://codeload.github.com/piinalpin/spring-cloud-config/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243482877,"owners_count":20297897,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["spring","spring-boot","spring-cloud"],"created_at":"2024-11-19T13:26:04.771Z","updated_at":"2025-03-13T20:42:35.022Z","avatar_url":"https://github.com/piinalpin.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Spring Cloud Configuration\n\n![Spring Cloud Config](spring-cloud-config-roadmap.jpg)\n\nIn micro-service world, managing configurations of each service separately is a tedious and time-consuming task. In other words, if there are many number of modules, and managing properties for each module with the traditional approach is very difficult.\n\nCentral configuration server provides configurations (properties) to each micro service connected. As mentioned in the above diagram, Spring Cloud Config Server can be used as a central cloud config server by integrating to several environments.\n\n**Environment Repository** — Spring uses environment repositories to store the configuration data. it supports various of authentication mechanisms to protect the configuration data when retrieving.\n\n**Spring Cloud Config** is Spring's client/server approach for storing and serving distributed configurations across multiple applications and environments.\n\nThis configuration store is ideally versioned under Git version control and can be modified at application runtime. While it fits very well in Spring applications using all the supported configuration file formats together with constructs like Environment, PropertySource or @Value, it can be used in any environment running any programming language.\n\nIn this write-up, we'll focus on an example of how to setup a Git-backed config server, use it in a simple REST application server and setup a secure environment including encrypted property values.\n\n\n### Project Setup and Dependencies\n\nI'm depending [Spring Initializr](https://start.spring.io/) for this as it is much easier. And we have to create two spring boot projects and started with maven project.\n\n- config-server\n- config-client\n\nTo get ready for writing some code, we create two new Maven projects first. The server project is relying on the `spring-cloud-config-server` module, as well as the `spring-boot-starter-security` and `spring-boot-starter-web` starter bundles.\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.springframework.boot\u003c/groupId\u003e\n    \u003cartifactId\u003espring-boot-starter-security\u003c/artifactId\u003e\n    \u003cversion\u003e2.5.2\u003c/version\u003e\n\u003c/dependency\u003e\n\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.springframework.cloud\u003c/groupId\u003e\n    \u003cartifactId\u003espring-cloud-config-server\u003c/artifactId\u003e\n    \u003cversion\u003e3.0.4\u003c/version\u003e\n\u003c/dependency\u003e\n\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.springframework.boot\u003c/groupId\u003e\n    \u003cartifactId\u003espring-boot-starter-web\u003c/artifactId\u003e\n\u003c/dependency\u003e\n```\n\nHowever for the client project we're going to only need the `spring-cloud-starter-config`, `spring-cloud-starter-bootstrap` and the `spring-boot-starter-web` modules.\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.springframework.cloud\u003c/groupId\u003e\n    \u003cartifactId\u003espring-cloud-starter-config\u003c/artifactId\u003e\n    \u003cversion\u003e3.0.4\u003c/version\u003e\n\u003c/dependency\u003e\n\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.springframework.cloud\u003c/groupId\u003e\n    \u003cartifactId\u003espring-cloud-starter-bootstrap\u003c/artifactId\u003e\n    \u003cversion\u003e3.0.3\u003c/version\u003e\n\u003c/dependency\u003e\n\n\u003cdependency\u003e\n    \u003cgroupId\u003eorg.springframework.boot\u003c/groupId\u003e\n    \u003cartifactId\u003espring-boot-starter-web\u003c/artifactId\u003e\n\u003c/dependency\u003e\n```\n\n### Config Server Implementation\n\nThe main part of the application is a config class – more specifically a @SpringBootApplication – which pulls in all the required setup through the auto-configure annotation `@EnableConfigServer`.\n\n```java\n@SpringBootApplication\n@EnableConfigServer\npublic class ConfigServerApplication {\n\n\tpublic static void main(String[] args) {\n\t\tSpringApplication.run(ConfigServerApplication.class, args);\n\t}\n\n}\n```\n\nWe also need to set a username and a password for the Basic-Authentication in our `application.properties` to avoid an auto-generated password on every application restart.\n\n```bash\nspring.cloud.config.server.git.uri=\u003cCHANGE YOUR GITHUB REPOSITORY\u003e\nspring.cloud.config.server.git.clone-on-start=true\nspring.security.user.name=root\nspring.security.user.password=secret\nspring.application.name=config-server\nserver.port=8080\n```\n\n### Git Repository as Configuration Storage\nTo complete our server, we have to initialize a Git repository under the configured url, create some new properties files and popularize them with some values.\n\nWe will use a configuration yaml file lika a normal Spring `application.yml`, but instead of the word ‘application' a configured name, e.g. the value of the property ‘spring.application.name' of the client is used, followed by a dash and the active profile. We will create 3 yaml files of configuration.\n\n- config-client-local.yml\n- config-client-development.yml\n- config-client-production.yml\n\n**config-client-local.yml**\n\n```yaml\napp:\n  name: \"spring-cloud-config-client.local\"\n  website: \"http://piinalpin.com\"\nconfig:\n  profile: \"local\"\nserver:\n  port: 8081\n```\n\n**config-client-development.yml**\n\n```yaml\napp:\n  name: \"spring-cloud-config-client.dev\"\n  website: \"http://piinalpin.com\"\nconfig:\n  profile: \"development\"\nserver:\n  port: 7001\n```\n\n**config-client-production.yml**\n\n```yaml\napp:\n  name: \"spring-cloud-config-client.prod\"\n  website: \"http://piinalpin.com\"\nconfig:\n  profile: \"production\"\nserver:\n  port: 9001\n```\n\n### Querying the Configuration\n\nNow we're able to start our server. The Git-backed configuration API provided by our server can be queried using the following paths.\n\n```bash\n/{application}/{profile}[/{label}]\n/{application}-{profile}.yml\n/{label}/{application}-{profile}.yml\n/{application}-{profile}.properties\n/{label}/{application}-{profile}.properties\n```\n\nIn which the {label} placeholder refers to a Git branch, {application} to the client's application name and the {profile} to the client's current active application profile.\n\nSo we can retrieve the configuration for our planned config client running under local profile in branch `master` via.\n\n```bash\n curl http://root:secret@localhost:8080/config-client/local/master\n```\n\nThen we got a response like below.\n\n```json\n{\n  \"name\": \"config-client\",\n  \"profiles\": [\n    \"local\"\n  ],\n  \"label\": \"master\",\n  \"version\": \"de4a15d886d53050ec4bbe80e939106259614803\",\n  \"state\": null,\n  \"propertySources\": [\n    {\n      \"name\": \"https://github.com/piinalpin/spring-cloud-config.git/config-client-local.yml\",\n      \"source\": {\n        \"app.name\": \"spring-cloud-config-client.local\",\n        \"app.website\": \"http://piinalpin.com\",\n        \"config.profile\": \"local\",\n        \"server.port\": 8081\n      }\n    }\n  ]\n}\n```\n\n### Client Implementation\n\nNext, let's take care of the client. This will be a very simple client application, consisting of a REST controller with one GET method.\n\nNow, We will create a controller in `com.maverick.configclient.controller.ConfigClientController` like below.\n\n```java\n@RestController\npublic class ConfigClientController {\n\n    @Value(\"${config.profile:}\")\n    private String profile;\n\n    @Value(\"${app.name:}\")\n    private String appName;\n\n    @GetMapping(path = \"/\")\n    public Map\u003cString, String\u003e main() {\n        Map\u003cString, String\u003e map = new HashMap\u003c\u003e();\n        map.put(\"activeProfile\", profile);\n        map.put(\"appName\", appName);\n        return map;\n    }\n\n}\n```\n\nThe configuration, to fetch our server, must be placed in a resource file named `bootstrap.application`, because this file (like the name implies) will be loaded very early while the application starts.\n\n```bash\nspring.application.name=config-client\nspring.cloud.config.uri=http://localhost:8080\nspring.cloud.config.username=root\nspring.cloud.config.password=secret\nspring.profiles.active=local\n```\n\nTo test, if the configuration is properly received from our server and the role value gets injected in our controller method, we simply `curl` it after booting the client.\n\n```bash\ncurl http://localhost:\u003cPORT_ACTIVE_PROFILE\u003e\n```\n\nIf the response is as follows, our Spring Cloud Config Server and its client are working fine for now.\n\n```json\n{\"activeProfile\":\"local\",\"appName\":\"spring-cloud-config-client.local\"}\n```\n\n### Clone or Download\n\nYou can clone or download this project\n```bash\nhttps://github.com/piinalpin/spring-cloud-config.git\n```\n\n### Thankyou\n\n[Medium](https://medium.com/@ijayakantha/microservices-centralized-configuration-with-spring-cloud-f2a1f7b78cc2) - Microservices Centralized Configuration with Spring Cloud\n\n[Baeldung](https://www.baeldung.com/spring-cloud-configuration) - Quick Intro to Spring Cloud Configuration\n\n[Github](https://github.com/chuchip/servercloudconfig) - Servidor Configuraciones en Spring Cloud","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpiinalpin%2Fspring-cloud-config","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpiinalpin%2Fspring-cloud-config","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpiinalpin%2Fspring-cloud-config/lists"}