spring cloud config Integrates gitlab to build a distributed configuration center

  • 2020-12-22 17:40:50
  • OfStack

In front of the blog, we are all the configuration files in their respective services, but do have a drawback, the 1 denier configuration changes, so we have to stop, then change the configuration file and then to online, less service, it also understandable, but if is the hundreds of thousands of service, this time, you need to use the distributed configuration management. spring cloud config is designed to solve this problem. The following is combined with gitlab to achieve the establishment of the distributed configuration center. spring cloud config configuration center consists of server end and client end,

Premise: Create a new configuration file under the project in gitlab configserver-ES14en.properties

1. Configuration Server

1. Add dependencies


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

2. Open support in Application main class


@EnableConfigServer 

3. Configure application. yml file


server: 
 port: 8888 
spring: 
 application: 
  name: config 
 cloud: 
  config: 
   server: 
    git: 
     uri: https://gitlab.xxx.com/xxxxx/xxxxx.git   #  configuration gitlab The address of the warehouse, note, must be addressed here .git At the end  
     search-paths: /config-repo # gitlab Warehouse address under the relative address, can be configured with multiple, using , Segmentation.  
     username: your username                       # gitlab Warehouse account number  
     password: your password                       # gitlab The password of the warehouse  

Note: There is no need to use the searchPaths parameter if the configuration file is placed in the root directory of the Git repository. The configuration file in this case is in the ES38en-ES39en directory, so use the searchPaths parameter to prompt the Config server to search the ES42en-ES43en subdirectory

4, start server, and in the browser input http: / / localhost: 8888 / configserver dev/master


{ 
 
  "name": "configserver", 
  "profiles": [ 
    "dev" 
  ], 
  "label": "master", 
  "version": "073cda9ce85a3eed00e406f4ebcc4651ee4d9b19", 
  "state": null, 
  "propertySources": [ 
    { 
      "name": "https://gitlab.xxx.com/xxxxx/xxxxx/project/config-repo/configserver.properties", 
      "source": { 
        "name": "chhliuxyh", 
        "hello": "i'm the king of the world!!!", 
        "profile": "profile-default" 
      } 
    } 
  ] 
 
} 

You can see that the server side is ready to read the configuration file from gitlab. Resources on gitlab can be accessed as shown in the form below


/{application}/{profile}[/{label}] 
/{application}-{profile}.yml 
/{label}/{application}-{profile}.yml 
/{application}-{profile}.properties 
/{label}/{application}-{profile}.properties 

For example in the browser input: http: / / localhost: 8888 / configserver - dev yml, the results are as follows:


hello: i'm the king of the world!!! 
name: chhliuxyh 
profile: profile-default 

2. Configure the client

1. Add pom dependency


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

2. Configure bootstrap. yml file

Note: the configuration file here needs to be in the ES83en.properties or bootstrap.yml file because the configuration of config precedes application.properties, and bootstrap.properties loads before application.properties


server: 
 port: 8889 
spring: 
 application: 
  name: configserver  #  Must be prefixed with configuration file 1 To, for example here our profile name is configserver-dev.properties, Then you need to configure as configserver 
 cloud: 
  config: 
   uri: http://localhost:8888/ // configuration spring cloud config server-side url 
   profile: dev           #  The specified profile 
   label: master           #  The specified gitlab Branch of the warehouse  

3. Verify the client

Add 1 Controller to the client


package com.chhliu.springcloud.config;  
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.cloud.context.config.annotation.RefreshScope; 
import org.springframework.web.bind.annotation.GetMapping; 
import org.springframework.web.bind.annotation.RestController;  
@SpringBootApplication 
@RestController 
@RefreshScope // annotations @RefreshScope instructions Config The client also refreshes the injected property values when the server configuration changes  
public class SpringcloudConfigClientApplication { 
 
  public static void main(String[] args) { 
    SpringApplication.run(SpringcloudConfigClientApplication.class, args); 
  } 
 
  @Value("${hello}") //  read gitlab Properties in the configuration file. If we read a value, the client is OK the  
  private String profile; 
 
  @GetMapping("/hello") 
  public String hello() { 
    return this.profile; 
  } 
} 

In the browser to access: http: / / localhost: 8889 / hello, the results are as follows:

i'm the king of the world!!!

Indicates that the client can already get the value from the server.

3. Dynamic refresh

Update the Spring Cloud Config managed configuration without restarting the client

1. Update the corresponding attribute value of hello in configserver-ES129en.properties configuration file in gitlab warehouse

2, access http: / / localhost: 8888 / configserver/dev/master, found server content has been updated

3, the Conf client sends a request POST http: / / localhost: 8889 / refresh, return 200 OK. Visit http again: / / localhost: 8889 / hello, visible in the case of not restart the client service, read the attribute value has been updated dynamically

PS: To achieve a dynamic refresh, add the following starter to the pom file


<dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-actuator</artifactId> 
    </dependency> 

Related articles: