1. Introduction of Config

In a distributed system, due to a large number of services, a distributed configuration center is required to facilitate unified management and real-time update of service configuration files. In Spring Cloud, there is a distributed configuration center component, Spring Cloud Config, which enables configuration services to be placed in the memory of the configuration service (that is, locally) as well as in a remote Git repository. In the Spring Cloud Config component, there are two roles: Config Server and Config client. The server stores the configuration file and provides the content of the configuration file through an interface. The client obtains data through the interface and initializes its own application based on the data.

2. Create a server project for the configuration center

At present, SpringCloud Config is mainly used to make a configuration center through Git/SVN, from which each service obtains its own configuration parameters. SpringCloud Config also supports fetching local parameter configurations. For local storage, add spring.profiles. Active =native to application.properties or application.yml. It reads the configuration file from the resources path of the project. If it is to read the specified configuration file, you can use spring. Cloud. Config. Server native. SearchLocations = file: E: / properties/to read. Git file configuration is used as a demonstration in this article

2.1 Preparations

Start Eureka Server from the Eureka demo project in the previous article

2.2 Creating a Project

Create a New Maven project named spring-cloud-config-server and add the following dependencies to 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:The schemaLocation = "HTTP: / / http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd ">
    <parent>
        <artifactId>spring-cloud-demo</artifactId>
        <groupId>com.hxmec</groupId>
        < version > 1.0 - the SNAPSHOT < / version >
    </parent>
    The < modelVersion > 4.0.0 < / modelVersion >

    <artifactId>spring-cloud-config-server</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <! -- config Server dependencies -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

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

</project>
Copy the code

Create a new application.yml and add the following configuration

server:
  port: 9005
spring:
  application:
    name: config-server
  cloud:
    config:
      label: master
      server:
        git:
          #github config address
		  If Git repository is a public repository, you can leave the username and password blank. If Git repository is a private repository, you can leave the username and password blank.
          uri: https://github.com/ty1972873004/spring-cloud-config.git
          Configuration file path
          search-paths: appconfig
          #username:
          #password:
          # git branch
          default-label: master

logging:
  pattern:
    console: '%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n'
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8888/eureka/
Copy the code

Create a startup class ConfigServerApplication with the @enableconFigServer annotation to enable configuration server functionality.

@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerApplication {
    public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); }}Copy the code

Remote warehouse have a config file in https://github.com/ty1972873004/spring-cloud-config.git – the client – dev. Yml file has an attribute: Start the program can be accessed through http://localhost:9005/config-client/dev verify success for configuration

The mapping between the URL of SpringCloud Config and the configuration file is as follows:

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

3. Create a client demo project for the configuration center

Create a New Maven project named spring-cloud-config-client and add the following dependencies to 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">
    <parent>
        <artifactId>spring-cloud-demo</artifactId>
        <groupId>com.hxmec</groupId>
        <version>1.0 the SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-cloud-config-client</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <! -- config client dependencies -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
Copy the code

Create bootstrap.yml and add the following configuration: config The related configuration must be configured in boostrap.yml

spring:
  application:
    name: config-client
  cloud:
    config:
      uri: http://127.0.0.1:9005/
      label: master
      profile: dev
      name: ${spring.application.name}
      discovery:
        enabled: true
        service-id: config-server

server:
  port: 9006

logging:
  pattern:
    console: '%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n'

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8888/eureka/
Copy the code

Create a startup class, ConfigClientApplication

@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientApplication {

    public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); }}Copy the code

Write DemoController to add a demo interface to get the configuration from the configuration center

@RestController
@RequestMapping("/demo")
@Slf4j
public class DemoController {

    @Value("${hx.name:#{null}}")
    private String name;

    @GetMapping(value = "/test1")
    public String  test1(a){
        return "Hello "+ name; }}Copy the code

Visit http://localhost:9006/demo/test1 to verify whether the configuration was obtained

4. Project address

Github.com/ty197287300…