preface

I haven’t updated it for 4 days. I don’t know. It’s busy, busy, busy. It is mainly about graduation and moving from one city to another. During this period, we should try our best to keep it for 2 or 3 days and no more than 4 or 5 days at the latest.

With the increase of modules, there will be a common problem of complex configuration files, each time you have to open multiple directories to find the configuration file, the Config component in SpringCloud is to solve this problem, through simple configuration can achieve unified configuration file management.

Config service side

The Config server is introduced

Create a Config empty parent module, create a config-server submodule below, modify the poM file of the submodule

Note that it is the poM file of the submodule, 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>
Copy the code

The configuration file

Since there is no need to register with Eureka, 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
Copy the code

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 the repository is public, username and password are not written

Since the springCloud-study-config repository is set with open permissions, you can use my repository directly or create your own repository on Github or Gitee.

The repository houses the configuration files for each microservice

The example is to manage the config file of the config-client microservice (which will be created later), so you need to create a config-client-dev.properties in the repository (-dev means the config file in the development environment).

The contents of the config-client-dev.properties file are as follows, which can be seen as ** some configuration of the config-client service **

The main start class

Add the @enableconFigServer annotation to provide config service support

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

test

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 only need to run the ConfigServer8101 project. You can access resources from the server using the preceding five methods

The config client

The Config client is imported

The previously mentioned config-client is the module to be created next. The final project structure directory is as follows

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

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

The configuration file

The configuration file is named bootstrap.yml

Because the client sets 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
Copy the code

Spring.cloud.config. uri: server address, where to get 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(a) {
        return "hello, "+ name; }}Copy the code

${name} is a placeholder that you may not be able to find

test

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

You do not need to stop the project on the server, and then start the config-client9501 project

Localhost :9501/hi

In fact, it has been quite a lot of fragmentary time to write, but sometimes encountered some problems also need to understand and speak out as much as possible, I hope to help read this article partners!!

Creation is not easy, if it is helpful to you, welcome to like, collect and share!