preface

It’s been 4 days without an update, how to put it, it’s busy, busy, busy. It is mainly about graduation and moving from one city to another. During this period, I will try my best to keep two or three days a day, and no more than four or five days a day at the latest.

As the number of modules increases, there will be a common problem of complex configuration files. It is necessary to open multiple layers of directories to find the configuration files each time. The Config component in SpringCloud aims to solve this problem by achieving unified management of configuration files through simple configuration.

Config service side

Introduce the Config server

Create a config-server submodule below and modify the POM file of the submodule

Note that it is the POM file of the child module, not the POM file of the empty parent module as before

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

The configuration file

Since there is no need to register with Eureka for the time being, the configuration file is relatively simple to write

server:
  port: 8101

spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/cutey_none/springcloud-study-config
          username:
          password:
      label: master

Spring. Cloud. Config. Server. Git. Uri: storing the addresses of the file, then the client from here for configuration files, can be local, can also be a git

If it’s a public repository, then
username
passwordDon’t have to write

The Springcloud-study-config repository is created with open permissions, so you can use mine or create your own repository. You can simply create a repository on GitHub or Gitee.

The repository houses the configuration files for the individual microservices

The example is the configuration file for managing the config-client microservice (created later), so you need to create a config-client-dev.properties in the repository (-dev represents a configuration file for the development environment).

The contents of the config-client-dev.properties file are shown below and can be viewed as some configuration of the config-client service

The main start class

Added @EnableConfigServer annotation to provide config service support

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

test

The SpringCloud Config has its own form of HTTP service access to resources

  • /{application}/{profile}[/{label}] >> /config-client/dev
  • /{application}-{profile}.yml >> /config-client-dev.yml
  • /{label}/{application}-{profile}.yml >> /master/config-client-dev.yml
  • /{application}-{profile}.properties >> /config-client-dev.properties
  • /{label}/{application}-{profile}.properties >> /master/config-client-profile.properties

You can simply run the ConfigServer8101 project alone. You can access the resource in all five ways above, and you can see that you can access the resource successfully from the server side

The config client

Introduce the Config client

The config-client mentioned above is the module to be created next, and the final project structure directory is shown below

Next, modify the POM file of the config-client9501 module, noting that the dependencies introduced by the server and the client are different

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

The configuration file

Note that the configuration file name is
bootstrap.yml

Because the client is setting the server URI, the client configuration file should be loaded first

server:
  port: 9501
spring:
  application:
    name: config-client
  cloud:
    config:
      profile: dev
      label: master
      uri: http://localhost:8101

Spring.cloud.config. uri: The address of the server, where to get the configuration file

The main startup class and the business class

@SpringBootApplication @RestController public class ConfigClient9501 { public static void main(String[] args) { SpringApplication.run(ConfigClient9501.class, args); } @Value("${name}") String name; @GetMapping("/hi") public String hello() { return "hello, " + name; }}

You might get a hint that you can’t find the placeholder ${name}, which means that something is not following the steps

test

Note that there is no name variable in the client configuration file above

The server project does not need to be stopped, and then the config-client9501 project is opened. The following projects are opened

If you go to localhost:9501/hi, you will normally see the following

In fact, I have taken out a lot of spare time to write, but sometimes I still need to understand some problems as much as possible and then speak out popularly. I hope I can help the friends who read this article!!

It is not easy to create, if you have help, welcome thumb up, collection and share!

The following is a personal public number, interested can pay attention to, perhaps is your treasure public number oh, basic 2,3 days 1 more technical articles!!