Springcloud 2.x Distributed Configuration Center

Posted by vponz on Thu, 14 Nov 2019 21:31:54 +0100

1. What is a Distributed Configuration Center?

To provide centralized external configuration support for microservices in the microservice architecture, the Configuration Center provides centralized external configuration for all environments of each microservice application (which may be difficult to understand, but to know what it means, you need to know why: This configuration is to solve the application.properties configuration in many provider s in a microservice.Manage issues, as well as configuration redundancy issues, aggregate these configurations for storage, and extract duplicate configurations to resolve redundancy)

2. Diagram Running


1. Store our profile on git hub
2. config-server remote connection to git hub
3. config-client to connect to config-server
Run: When we start the config-client service, the client gets the configuration file on the remote git through the connected config-server and loads it into the object through Spring.

3. How to simply implement a distributed configuration center for springcloud config

1. Create a github account

2. Create repository on github

A github account can have many warehouses - > a warehouse can only correspond to one project - > so the name of the warehouse is the name of the project to be submitted
If you have a new account, you must first have a namespace (also created by yourself, you can name it as you like)

3. Load the project locally using github desktop

Select File->clone repository->Select the project you want to load locally

4. Create three files (dev: development environment, test: test environment, pro: online environment)

  Development environment:
  application-dev.properties
  spring.profiles=dev

  server.port=3081
    
  spring.application.name=application-dev
  spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  spring.datasource.url=jdbc:mysql://localhost:3306/dev?useSSL=false
  spring.datasource.username=root
  spring.datasource.password=root
  spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
  Test environment:
  application-test.properties
  spring.profiles=test

  server.port=3081

  spring.application.name=application-test
  spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false
  spring.datasource.username=root
  spring.datasource.password=root
  spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

5. Submit the three configured files to github

When submitting code to github using github desktop, only one submission can be made, not all together
Choose commit to master (remember this master)
Select repository Selection ->push

4. Access rules for the Distributed Configuration Center:

Both yml and properties can be accessed using this rule:
  /{application}/{profile}[/{label}]

Properrties file:
  /{application}-{profile}.properties
/{label( branch)}/{application}-{profile}.properties

yml file:
  /{application}-{profile}.yml
  /{label}/{application}-{profile}.yml

5. Configure springcloud config

   Configuration of 5.1 server layer

     5.1.1 jar package
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>

     5.1.2 application.properties configuration
    #First, it's normal:
    server.port=4081
    server.servlet.context-path=/
    #Configure application.name (configurable, not configurable) to remind eureka of this configuration (since service discovery in eureka is the name it is looking for), and don't forget
    spring.application.name=springcloud-config-server-4081
    #Start Configuration GitHub
    #Configure the address of GitHub's repository first (copy it directly from the browser's address bar)     spring.cloud.config.server.git.uri=https://github.com/namespace/repository name     #Configure GitHub's account and password     spring.cloud.config.server.git.username=mailbox/Account number     spring.cloud.config.server.git.password=Password     #Configure the search path for GitHub's repository (Fixed Don't Complete!!)     spring.cloud.config.server.git.search-paths=config-repo     #Skip SSL authentication     spring.cloud.config.server.git.skip-ssl-validation=true   
5.1.3 ApplicationRun Startup Class Comment
In addition to the regular @@SpringBootApplication, there is also an @EnableConfigServer, marked as the configuration center for the server layer

  5.2 client Layer Configuration

   5.2.1 jar package
  <dependencies>
      <dependency>
          <groupId>com.wlx.springcloud</groupId>
          <artifactId>20191108-management-model</artifactId>
          <version>1.0-SNAPSHOT</version>
      </dependency>
      <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
      </dependency>
      <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>druid</artifactId>
      </dependency>
      <dependency>
          <groupId>org.mybatis.spring.boot</groupId>
          <artifactId>mybatis-spring-boot-starter</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-config</artifactId>
      </dependency>
  </dependencies>
   5.2.2 Configuration config file

There are two profiles, bootstrap.properties and application.properties, which are used to check for gaps.
Place the same configuration on GitHub and the different configurations in the application file, which will be merged when loaded
Bootstrap.properties file:

  #Read the name of the file to configure from github
  #Read file name from repository on GitHub
  #According to the rule of reading: Do not append a suffix name.properties or.yml
  spring.cloud.config.name=application-dev
  #Name of configuration prpfile
  #Must match the value of spring.profile in the File Configuration Center on GitHub or it will not match
  spring.cloud.config.profile=dev
  #Configuring label (master) does not require configuration if the default is used
  spring.cloud.config.label=master
  #Configure the server-side address and port for config
  spring.cloud.config.uri=http://localhost:Port number

The Application.properties file:

  #Be sure to match the value of spring.cloud.config.name in bootstrap.properties or it will not map`
  spring.application.name=application-dev
5.2.3 Tests whether to link servers in the successful server layer and load cloud profiles

Create a new controller directory -> Create a controller test class TestController that uses the @Value annotation to get the values in the configuration file

  @RestController
  public class TestController {

     @Value("${spring.datasource.driver-class-name}")
    	  private String driverClassName;
  	  
   	 @RequestMapping("/test")
  	  public String test(){
  	     return driverClassName;
   	}
}

Blog for the first time, don't spray!!!

Topics: Java Spring github git MySQL