[SpringCloud component learning notes series] Config component

Posted by acoole on Sat, 12 Feb 2022 13:00:02 +0100

5. Use and explanation of SpringCloud Netflix Config

5.1 introduction

In the distributed system, due to the large number of services, the configuration files are scattered in different micro service projects, which is inconvenient to manage.

The Spring Cloud Config component is mainly used to centrally manage configuration files. It supports the configuration files to be placed locally in the configuration service and in the Git warehouse (Github, Gitee)

The configuration center is essentially a micro service, which also needs to be registered with Eureka registration center.

Selection of Git platform

github access is too slow. This time we use gitee

5.3. Create remote warehouse

5.3.1. New warehouse

5.3.2. Configure warehouse information

5.3.3. Create configuration file

Create profile directly

Profile naming rules:

Naming method of configuration file: {application} - {profile} YML or {application} - {profile} properties

Application is the name of the application

Profile is used to distinguish development environment (dev), test environment (test), production environment (prod), etc

5.4. Create Config module

5.4.1. Introducing dependency

<!-- introduce Eureka Client dependency-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- introduce Config Service class dependency-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

5.4.2 configuration file

server:
  port: 12000
spring:
  application:
    name: Config-Server
  cloud:
    config:
      server:
        git:
          uri: https://gitee. com/superAliang/spring-cloud-config-demo. Git # the address of the project on gitee, that is, the address of the remote library. The public warehouse does not need a user name and password
          # username: user name
          # Password: password
eureka:
  client:
    service-url:
      defaultZone: http://Register this service with eureka.0 # / Eureka: 10086.0 # for Eureka

5.4.2 startup

@SpringBootApplication
@EnableConfigServer // Open configuration service
public class SpringCloud_Config_12000_Application {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloud_Config_12000_Application.class, args);
    }
}

5.5 test access remote configuration file

Browser input http://localhost:12000/user-dev.yml, the Config service module will get the file with the specified name from Gitee.

Check whether Eureka is registered successfully

5.6. Use the configuration file of remote warehouse

Before use, you need to transform the user service service, that is, the SpringCloud-User-8001 module.

5.6.1. Introducing dependency

<!-- config Component dependency, which enables the configuration center to obtain the configuration file-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

5.6.2 modification of configuration file

Delete the original profile

Create a file named bootstrap Configuration file of YML, bootstrap The special thing about the file name of YML is

bootstrap.yml file is also the default configuration file of Spring Boot, and its loading time is compared with that of application YML earlier.

application.yml and bootstrap Although YML is the default configuration file of Spring Boot, its positioning is different.

  • bootstrap.yml can be understood as some parameter configurations at the system level, which generally will not change.

  • application.yml can be used to define application level parameters. If used with spring cloud config, application The files defined in YML can be replaced dynamically.

The conclusion is, bootstrap YML file is equivalent to the boot file when the project is started, and the content is relatively fixed. application.yml files are some general configuration parameters of microservices, which change frequently.

bootstrap.yml

spring:
  cloud:
    config: # Note: the file created in the warehouse is user-dev.yml, and the following parameters should be configured according to the file name
      # It should be consistent with the application of the configuration file in the warehouse
      name: user
      # Be consistent with the profile of the configuration file in the warehouse
      profile: dev
      # It should be the same as the version (Branch) of the configuration file in the warehouse
      label: master
      discovery:
        # Use configuration center
        enabled: true
        # Configuration center service name
        service-id: Config-Server
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka

5.7 remote configuration file for testing

Close the Gateway filter, restart the service and test

The business logic of the request:

  • Enter address

  • The mapping request arrives at the consumer

  • discoveryClient obtains the real request host address according to "user service"

  • Splicing user service request path

  • RestTemplate sends a request to the corresponding interface of user service

  • After the corresponding interface processes the business, the result is encapsulated into CommonVo, sequenced and returned to the consumer

  • The consumer deserializes the response content, then encapsulates it into CommonVo and returns it to the front end

  • The front page displays the response content of the request

Topics: github Spring Spring Cloud