SpringBoot e-Commerce project mall (20K + STAR) address: github.com/macrozheng/…

Abstract

Spring Cloud Config provides centralized external configuration support for applications in microservices architecture. It is divided into two parts, server and client, and its usage is described in detail in this article.

Introduction to Spring Cloud Config

Spring Cloud Config is divided into two parts: server and client. The server is called a distributed configuration center, which is an independent application that obtains configuration information from the configuration repository and provides it to the client. The client can obtain configuration information from the configuration center and load the configuration at startup. The Spring Cloud Config configuration center uses Git by default to store configuration information, so versioning of configuration information is naturally supported, and configuration information can be easily managed and accessed using Git clients.

Prepare the configuration information in the Git repository

Spring Cloud Config requires a Git repository to store configuration information. The Git repository address is gitee.com/macrozheng/… .

Configure the warehouse directory structure

Configuration information of the master branch

  • config-dev.yml:
config:
  info: "config info for dev(master)"
Copy the code
  • config-test.yml:
config:
  info: "config info for test(master)"
Copy the code
  • config-prod.yml:
config:
  info: "config info for prod(master)"
Copy the code

Configuration information in the dev branch

  • config-dev.yml:
config:
  info: "config info for dev(dev)"
Copy the code
  • config-test.yml:
config:
  info: "config info for test(dev)"
Copy the code
  • config-prod.yml:
config:
  info: "config info for prod(dev)"
Copy the code

Example Create the config-server module

Here we create a config-server module to demonstrate Spring Cloud Config as a configuration hub.

Add related dependencies in pom.xml

<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>
Copy the code

Configure in application.yml

server:
  port: 8901
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git: Configure Git repository to store configuration information
          uri: https://gitee.com/macrozheng/springcloud-config.git
          username: macro
          password: 123456
          clone-on-start: true Get configuration directly from Git at startup
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8001/eureka/
Copy the code

Add the @enableconFigServer annotation to the startup class to enable configuration center functionality

@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigServerApplication {

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

You can obtain the configuration information from config-server

Here we use config-server to demonstrate how to obtain configuration information.

Gets the access format of configuration file information

Get configuration information
/{label}/{application}-{profile}
Get configuration file information
/{label}/{application}-{profile}.yml
Copy the code

Placeholder related explanation

  • Application: indicates the application name. The default name is Spring.application. name in the configuration file. If spring.cloud.config.name is configured, the name is used.
  • Label: on behalf of the branch name, corresponding to the spring in the configuration file. The cloud. Config. The label;
  • Profile: on behalf of the name of the environment, in the configuration file is corresponding to the spring. Cloud. Config. Profile.

Get a configuration information demo

  • Start the Eureka-server and config-server services.

  • Visit http://localhost:8901/master/config-dev to obtain the master branch dev environment configuration information;

  • Visit http://localhost:8901/master/config-dev.yml to obtain the master branch dev environment configuration file information, compared to the above information, you can see the configuration information and the configuration file is not the same concept;

  • Visit http://localhost:8901/master/config-test.yml to get the test environment configuration file information on the master branch:

  • Visit http://localhost:8901/dev/config-dev.yml to get the dev branch dev environment profile information:

Create the config-client module

We create a config-client module to get the configuration from the config-server.

Add related dependencies in pom.xml

<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>
Copy the code

Do the configuration in bootstrap.yml

server:
  port: 9001
spring:
  application:
    name: config-client
  cloud:
    config: #Config client configuration
      profile: dev # enable configure suffix name
      label: dev # branch name
      uri: http://localhost:8901 Configure the center address
      name: config # Config file name
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8001/eureka/
Copy the code

Add the ConfigClientController class to get the configuration

/** * Created by macro on 2019/9/11. */
@RestController
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo(a) {
        returnconfigInfo; }}Copy the code

Demonstrates getting the configuration from the configuration center

  • Start the config-client service.

  • Visit http://localhost:9001/configInfo, you can get to the dev branch under the dev environment configuration;

config info for dev(dev)
Copy the code

Gets the configuration in the subdirectory

Not only can you store the configuration of each project in a different Git repository, but you can also store the configuration of multiple projects in a Git repository, where you need to search for configuration information in a subdirectory.

  • First, we need to add the relevant configuration in config-server to search the configuration in the subdirectory. Here we use the application placeholder to indicate that for different applications, we search the configuration in the subdirectory corresponding to the application name, for example, config subdirectory corresponding to config application.
spring:
  cloud:
    config:
      server:
        git: 
          search-paths: '{application}'
Copy the code
  • Visit http://localhost:9001/configInfo for testing, can find access to the config subdirectory of configuration information.
config info for config dir dev(dev)
Copy the code

Refresh the configuration

When the configuration information in the Git repository is changed, we can refresh the client configuration information through the Refresh endpoint of the SpringBoot Actuator. The following changes need to be made in the config-client.

  • Add the following dependencies in POM.xml:
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Copy the code
  • Enable refresh endpoint in bootstrap.yml:
management:
  endpoints:
    web:
      exposure:
        include: 'refresh'
Copy the code
  • Add the @refreshScope annotation to the ConfigClientController class to refresh the configuration:
/** * Created by macro on 2019/9/11. */
@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo(a) {
        returnconfigInfo; }}Copy the code
  • After restarting the config-client, call the Refresh endpoint to refresh the configuration:

  • Visit http://localhost:9001/configInfo for testing, can find configuration information was refreshed.
update config info for config dir dev(dev)
Copy the code

Add security authentication in the configuration center

We can add security authentication to the configuration center by integrating SpringSecurity.

Create the config-security-server module

  • Add dependencies to pom.xml:
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
Copy the code
  • Configure in application.yml:
server:
  port: 8905
spring:
  application:
    name: config-security-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/macrozheng/springcloud-config.git
          username: macro
          password: 123456
          clone-on-start: true Get configuration directly from Git at startup
  security: Configure the user name and password
    user:
      name: macro
      password: 123456
Copy the code
  • Start the config-security-server service.

Example Modify the config-client configuration

  • Add the bootstrap-security.yml configuration file, mainly to configure the user name and password of the configuration center:
server:
  port: 9002
spring:
  application:
    name: config-client
  cloud:
    config:
      profile: dev # enable configure suffix name
      label: dev # branch name
      uri: http://localhost:8905 Configure the center address
      name: config # Config file name
      username: macro
      password: 123456
Copy the code
  • Use bootstrap-security.yml to start the config-client service.

  • Visit http://localhost:9002/configInfo, found that can get to the configuration information.

config info for dev(dev)
Copy the code

Config-sever Cluster construction

In the microservice architecture, all services get their configurations from the configuration center. Once the configuration center goes down, serious problems will occur. Let’s build a two-node configuration center cluster to solve this problem.

  • Start two config-servers to run on port 8902 and port 8903 respectively.

  • Yml: bootstrap-cluster.yml: bootstrap-cluster.yml: bootstrap-cluster.yml: bootstrap-cluster.yml: bootstrap-cluster.yml: bootstrap-cluster.yml: bootstrap-cluster.yml

spring:
  cloud:
    config:
      profile: dev # Enable the environment name
      label: dev # branch name
      name: config # Config file name
      discovery:
        enabled: true
        service-id: config-server
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8001/eureka/
Copy the code
  • Run the bootstrap-cluster.yml command to start the config-client service. The following information is displayed in the registry:

  • Visit http://localhost:9003/configInfo to find the config – client can get to the configuration information.
config info for config dir dev(dev)
Copy the code

The module used

Springcloud - learning ├ ─ ─ eureka - server-- Eureka Registry├ ─ ─config-server - Configure the center service├ ─ ─config-security-server -- Configuration center service with security authentication└ ─ ─config-client -- Get the configured client service
Copy the code

Project source code address

Github.com/macrozheng/…

The public,

Mall project full set of learning tutorials serialized, attention to the public number the first time access.