SpringCloud config distributed configuration

Source address: https://gitee.com/peachtec/springcloud

The configuration file problem faced by the distributed system system

  Microservices means to split the business in a single application into sub-services. The granularity of each service is relatively small, so there will be a large number of services in the system, because each service needs to be configured with the necessary configuration information to run , So a set of centralized and dynamic configuration management facilities is indispensable.
  SpringCloud provides ConfigServer to solve this problem. Each of our microservices comes with an application.yaml. When hundreds of configuration files are modified, the workload Is quite big
  

What is SpringCloud config distributed configuration center

Insert picture description here
  Spring Cloud Config provides server and client support for external configuration in distributed systems. Using Config Server, you can manage the external properties of the application in all environments. The conceptual mapping on the client and server is the same as the Spring Environment and PropertySource abstractions, so they fit well with Spring applications, but can be used with any application running in any language. As the application goes through the deployment process from developer to test and production, you can manage the configuration between these environments and make sure that the application has everything it needs to run when it migrates. The default implementation of the server storage backend uses git, so it easily supports the configuration environment of the tag version and can access various tools for managing content. You can easily add alternative implementations and plug them in using Spring configuration.
  Spring Cloud Config offers a centralized service for the micro-micro framework of external service configuration support, configure the server for the various micro-service applications provide a link to all the centers of the external configuration
  Spring Cloud Config is divided into client and server-side
  server also Called a distributed configuration center , it is an independent microservice application that is used to connect to the configuration server and provide access interfaces for the client to obtain configuration information, encryption, and decryption information. The
  client manages application resources through a designated configuration center. , And business-related configuration content, and obtain and load configuration information from the configuration center at startup. The configuration server uses git to store configuration information by default, which is helpful for version management of the environment configuration, and can be accessed through the git client tool Convenient management and access to configuration information content

SpringCloud config fee is not what the configuration center can do
  1. Centralized management of configuration files
  2. Different environments, different configurations, dynamic configuration updates, and environment deployment
  3. Dynamically adjust the configuration during operation, no need to write configuration files on each service deployment machine, the service will uniformly pull and configure its own information from the configuration center
  4. When the configuration file changes dynamically, the service does not need to be restarted, it can sense the configuration change and apply the new configuration, requiring hot deployment of the plug-in
  5. Expose configuration information in the form of rest interface
SpringCloud config configuration management center and github integration

  Since SpringCloud config uses git to store configuration files by default, you can also use other such as svn, local files, but it is recommended to use git, and use http/https access; it is recommended to use code cloud warehouse for management in China

  1. Install and configure Git. There should be no problem installing and configuring Git on this machine. I won’t talk about this. If you don’t understand, you can go to Baidu to read other bloggers’ articles. It’s basically a brainless next step.
  2. Create a new warehouse, this warehouse is the warehouse where we manage configuration files, and then create a yaml configuration file, this configuration file is used as a test for a while
spring:
    profiles:
        active: dev 
---
spring:
    profiles: dev
    application:
        name: springcloud-config-dev
---
spring:
    profiles: test
    application:
        name: springcloud-config-test
  1. The server connects to the Git configuration, creates a module to be used as the server to read the Git remote configuration file, and then imports the jar package configuration to
    import the JAR package
<dependencies>
    <!-- spring-cloud-config-server -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

Configure yaml file

server:
  port: 8100
spring:
  application:
    name: springcloud-config-server
  # 连接远程仓库
  cloud:
    config:
      server:
        git: # 根据自己的仓库类型选择git还是svn
          uri: https://gitee.com/peachtec/springcloud-config.git # 选择https的地址
          # 注意:如果是创建的私有仓库,还需要配置用户名和密码,否则验证会失败
          username: ******
          password: ******

Start the configuration service

@SpringBootApplication
@EnableConfigServer //开启配置服务
public class ConfigServer_8100 {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(ConfigServer_8100.class, args);
    }
}
  1. Start this module to test whether the application.yaml file can be read from the warehouse. If the configuration in the remote warehouse can be read, the configuration is successful.
    Insert picture description here
    Note that the access address can be accessed in the following ways, label is a branch
    Insert picture description here
  2. The client connects to the Git configuration and creates a module for the client to use the server to obtain the configuration file and use it to
    create a yaml file and configure the information, upload it to the warehouse.
    Multi-document modules or multiple configuration files can be used
spring:
  profiles:
    active: dev
---
server:
  port: 8201
spring:
  profiles: dev
  application:
    name: springcloud-config-client-8201
# Eureka 配置
eureka:
  client:
    service-url: # 注册中心地址
      defaultZone: http://eureka7002:7002/eureka/,http://eureka7003:7003/eureka/,http://eureka7004:7004/eureka/
---
server:
  port: 8202
spring:
  profiles: test
  application:
    name: springcloud-config-client-8202
# Eureka 配置
eureka:
  client:
    service-url: # 注册中心地址
      defaultZone: http://eureka7002:7002/eureka/,http://eureka7003:7003/eureka/,http://eureka7004:7004/eureka/

Configure bootstrap.yaml to prevent conflicts with remote

# 系统级别的配置
spring:
  cloud:
    config:
      uri: http://localhost:8100 # 连接服务端配置模块,通过它中转去获取远程仓库的文件
      name: config-client # 从远程仓库读取的资源名称,不需要后缀
      profile: dev # 访问的环境
      label: master # 远程分支

Configure the application.yaml file
or you don’t need to configure it, it will be read from the remote warehouse

spring:
  application:
    name: springcloud-config-client

Write a control layer interface to test and read the remote configuration

@RestController
public class ConfigClientApi {
    
    
    @Value("${server.port}")
    private String port;
    @Value("${spring.application.name}")
    private String name;
    @Value("${eureka.client.service-url.defaultZone}")
    private String defaultZone;

    @GetMapping("/client/get/config")
    public Object getConfig() {
    
    
        Map<String, String> map = new HashMap<>();
        map.put("port", port);
        map.put("name", name);
        map.put("defaultZone", defaultZone);
        return map;
    }
}

Start the test, access the interface
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45481406/article/details/110563766