Simple understanding of spring cloud build config process instance

Time:2021-4-14

This article mainly introduces a simple understanding of the spring cloud build config process example, the article through the example code is very detailed, for everyone’s study or work has a certain reference learning value, need friends can refer to

Using spring cloud’s spring cloud config server component to build its own configuration center config server

Configuration files can be stored in GitHub, gitlab, etc. to build their own registry, complete the unified management and access of configuration, and we will realize the dynamic refresh of configuration later.

Put the configuration file on GitHub

https://github.com/halouprogramer/spring-config-repository

File name format

Application name – environment configuration.yml

For example: Spring School (application name) – dev (profiles environment configuration)

Server configuration:

1. POM introduction


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

2. Configuration file


spring:
 application:
  name: spring-config
 cloud:
  config:
   server:
    git:
     uri: https://github.com/halouprogramer/spring-config-repository.git
#     username: ***
#     password: ***
     basedir: ~/temp/gitlab

3. Start class annotation


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

@EnableEurekaClient
@EnableConfigServer
@SpringBootApplication
public class SpringConfigApplication {

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

}

Start the server locally to access the configuration that exists in the warehouse http://localhost :8083/spring-school- dev.yml If the visit is explained, there is no problem

The client pulls the configuration through the configuration center

spring:
 application:
  name: spring-school
 cloud:
  config:
   discovery:
    enabled: true
    Service ID: spring-config # access configuration center project name
   profile: dev
 profiles:
  active: dev

Full code access:

https://github.com/halouprogramer/spring-cloud-demo

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.