An overview,

  • website
  • SpringCloud Config provides centralized external configuration support for microservices in the microservices architecture, and the configuration server provides a centralized external configuration for all environments of various microservices applications.
  • In simple terms, the content of configuration files is centrally managed in the configuration center.
  • It is divided into two parts: server and client.
  • The server, also known as the distributed configuration center, is an independent micro-service application that connects to the configuration server and provides an access interface for the client to obtain configuration information, encrypt/decrypt information, etc
  • The client manages application resources and service-related configurations through the specified configuration center, and obtains and loads configuration information from the configuration center during startup
  • The configuration information of the configuration server is stored in Git by default, which facilitates version management of environment configurations. In addition, you can use git client tools to manage and access configuration information conveniently.
  • It can be configured by environment. Different deployment environments have different configuration files.
  • You can adjust the configuration dynamically and feel the configuration change without restarting. (Need to send a refresh request to the client)

Config Server configuration and test

1. Create a Git repository

  • You can create GitHub or Gitee repositories to upload configuration files to.

  • Configuration files can be uploaded to YML. The file name prefix indicates the real name, and the suffix dev indicates the deployment environment. You can select different suffixes based on different deployment environments.
  • The content of the configuration file depends on the actual situation. The format is the format of YML.

2. The package

  • The most important thing, of coursespring-cloud-config-server. The registry Eureka client package also needs to be imported.
<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>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
Copy the code

3. Configuration file

server:
  port: 3344

spring:
  application:
    name:  cloud-config-center # Eureka microservice name registered in the server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/waynedu/springcloud-config.git # is also the address of the warehouse download
        # Search directory
          search-paths:
            - springcloud-config Write the name of the warehouse
      # read branch
      label: master

Register service to eureka address
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
Copy the code

  • At this point, you can import the configuration from the configuration center into the configuration file as if you were using the local configuration.
  • The service is already registered with the registry by default. It’s not explicitly stated.

4. Main startup class

  • It’s important to note that you need to write one@EnableConfigServerNote that this is a configuration server.
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {
  public static void main(String[] args) { SpringApplication.run(ConfigCenterMain3344.class, args); }}Copy the code

5. Read the configuration information

  • You can send a request to the browser to obtain configuration file information.
  • /{label}/{application}-{profile}.yml, such ashttp://localhost:3344/master/config-dev.yml
  • /{application}-{profile}.yml, such ashttp://localhost:3344/config-dev.ymlThe default is master
  • /{application}/{profile}[/{label}], such ashttp://localhost:3344/config/dev/master
  • Label: branch
  • Name: indicates the service name
  • Profiles: environment (dev/test/prod)

Config Client configuration and test

1. The package

  • Again, you need to importspring-cloud-starter-config, which is basically the same as a server-side package.
<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>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
Copy the code

2. Configuration file

  • First, it needs to be explainedbootstrap.ymlIt’s system-level,application.ymlIs a user-level profile,bootstrap.ymlIs not overwritten locally and has a higher priority.bootstrap.ymlthanapplication.ymlLoad first.
  • Therefore, import external configuration files, preferably inbootstrap.xmlOn the import
server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    # Config client configuration
    config:
      label: master # branch name
      name: config # Config file name
      profile: dev # read name suffix The above three synthesis: master branch config - dev. Yml configuration files is read at http://config-3344.com:3344/master/config-dev.yml
      uri: http://localhost:3344 Configure the center address

Register service to eureka address
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

Expose monitor endpoints, which is key to dynamic refreshing
management:
  endpoints:
    web:
      exposure:
        include: "*"
Copy the code

3. Main startup class

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

4. Business class

  • Try to get information from the configuration file
@RestController
@Slf4j
@RefreshScope  // Allows you to refresh the configuration
public class ConfigController {
  @Value("${config.version}")
  Integer version;
  @Value("${config.info}")
  String info;

  @GetMapping("/config")
  public String config(a) {
    String result = "version: " + version + ", info: " + info;
    log.info(result);
    returnresult; }}Copy the code

5. Dynamic refresh

  • When the configuration file content on Git changes, the server can quickly pick it up, but the client is still not aware of it. You need to send a command to refresh dynamically.curl -X POST "http://localhost:3355/actuator/refresh"
  • In this way, the client configuration can be refreshed without a restart.
  • But there are also some problems, which are very troublesome. If you have a large number of clients, each of which needs to be refreshed manually, this can cause problems. Publishing subscriptions through the message bus can easily solve this problem.