SpringBoot Unified Configuration Center

Keywords: Java Spring Maven github Apache

Backend projects have been built using springboot, and all configurations are written to their own resource directory. As more and more micro-service projects grow, each project needs its own configuration files.And once you want to modify the configuration file later, you have to republish it again. Now, let's teach you how to manage these configurations on github in a unified way, so that every modification takes effect without having to republish the project.

1 Create a unified service project

You can use STS to initialize your project, just choose your own.

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mike</groupId>
    <artifactId>config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-server</name>
    <description>config server</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <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>${spring-cloud.version}</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>

Create a bootstrap.yml file, of course you can use application.yml or application.properties

spring:
  application:
    name: config-repo
  cloud:
    config:
      server:
        git:
          uri: https://Github.com/mike/config-repo.git #github repository address
          username: mike      # User name
          password: 123456      # Password

Create a config-repo repository on github and add a configuration file:

Configuration of two different environments
hello-pj-dev.yml

hello:
  text: hello spring dev

hello-pj-uat.yml

hello:
  text: hello spring uat

Create a startup class:

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }

}

Start the application and access:
http://localhost:8080/hello-pj-dev.yml
http://localhost:8080/hello-pj-uat.yml
You can see the remote configuration center.

2 Create a test project

pom

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mike</groupId>
    <artifactId>hello-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hello-server</name>
    <description>hello server</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</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>${spring-cloud.version}</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>

Create bootstrap.yml file, only this file, not the application file

server:
  port: 8082
spring:
  application:
    name: hello-pj
  cloud:
    config:
      profile: dev
      uri: http://localhost:8080/

The uri to read the remote configuration file is configured here. Note that application.name must correspond to the file name on github, and the final access is application.name+profile

Create test controller


@RestController
public class TestController {
    @Value("${hello.text}")
    private String text;
    
    @GetMapping("/say")
    public String sayHello(){
        return text;
    }
}

Visit http://localhost : 8082/say, you can see the corresponding configuration.This way you only need to manage all the configurations on github, but remember that the'config-server'project has to be started all the time to get the configurations on github.

3 Dynamic refresh configuration

Currently, if we modify the configuration on github and it doesn't take effect immediately, we need to restart our client project and now we need to transform it to automatic refresh.

Add a new dependency to the client project:

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

Modify bootstrap.yml file

server:
  port: 8082
spring:
  application:
    name: hello-pj
  cloud:
    config:
      profile: dev
      uri: http://localhost:8080/
    bus:
      trace:
        enabled: true
rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

Note that you need to use rabbitmq, so you need to start rabbitmq locally.

Add a comment @RefreshScope where refresh is required


@RestController
@RefreshScope
public class TestController {
    @Value("${hello.text}")
    private String text;
    
    @GetMapping("/say")
    public String sayHello(){
        return text;
    }
}

This allows us to modify the configuration file on github at any time without restarting the application.

If it helps you, I hope you can follow my Public Number:

Posted by jonki on Fri, 26 Apr 2019 22:42:36 -0700