Skip to content

Spring Config Server

shoecillo edited this page Nov 1, 2016 · 1 revision

Spring Config Server

Spring Cloud is centralized way to serve properties to services.We have to work with repository, in this case I used git for store properties.This server is a Spring boot application too.

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>CloudConfigServer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

We include spring-cloud-config-server for use it as cloud config server.

In this project we start for the configuration file, src/main/resources/application.properties

server.port=8888
spring.cloud.config.server.git.uri=/path/to/your/git/repository

In properties file we check 2 entries, we listen on port 8888 and our properties repository will be located in uri entry. This is a local repository, we have to create in this path a git repository with git init. Will leave the repository empty for now,we will back later.

We have configured the Config server, now we have to write the Spring boot application:

package com.sh;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer
@SpringBootApplication
public class ConfigApp {
    
    public static void main(String[] args) {
        SpringApplication.run(ConfigApp.class, args);
    }
}

Very simple,only adding @EnableConfigServer we have created our config server,listening on port 8888 and with properties repository to store services configurations. Now we can build the project and execute the jar.

Clone this wiki locally