SpringCloud study notes (7): Use Spring Cloud Config configuration center

Brief introduction

Spring Cloud Config configured to externalize distributed system provides server and client support, server-side unified management of all profiles, client configuration information from the server at startup. The server has a variety of configurations, as will be configured locally or stored on a remote Git repository to store files and so on, and when the configuration file is changed, through a variety of ways, such as actuator / refresh or Spring Cloud Bus endpoints dynamically refresh configure the client without having to restart the client.

Project Introduction

  1. sc-parent, parent module (please refer SpringCloud Study Notes (1): Eureka registry )
  2. sc-eureka, Registration Center (refer to SpringCloud Study Notes (1): Eureka registry )
  3. sc-config-client, client-side access to the configuration center
  4. sc-config-server, local distribution center
  5. sc-config-server-git, remote configuration center

Creating Client Access configuration center

1. Create a sub-module project sc-config-client at the parent module, pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.cf</groupId>
    <artifactId>sc-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>sc-config-client</artifactId>
  
  <dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
  </dependencies>
</project>

2. Create a startup class configclient.ConfigClientApplication:

package configclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConfigClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}

3.创建configclient.controller.ConfigClientController:

package configclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/client")
public class ConfigClientController {
    @Value("${nickName}")
    private String nickName;
    
    @GetMapping("/hello")
    public String hello(){
        return "hello," + nickName;
    }
}

4. Create bootstrap.yml:

spring:
  application:
    name: sc-config-client
  profiles:
    active: dev
  cloud:
    config:
      uri:  http://localhost:9003
      fail-fast: true

server:
  port: 9002

spring.cloud.config.uri : specify the configuration center address
spring.cloud.config.fail-Fase : When the connection is not on the hub server, whether the current client abnormal stop, rather than the default configuration starts.

Use local configuration center

1. Create a sub-module project sc-config-server in the parent module, pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.cf</groupId>
    <artifactId>sc-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>sc-config-server</artifactId>
  
  <dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
  </dependencies>
</project>

2. Create a startup class configserver.ConfigServerApplication:

package configserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

3. Create application.yml:

server:
  port: 9003
      
spring:
  application:
    name: sc-config-server
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          searchLocations: classpath:/conf    

spring.profiles.active : Obtaining the configuration file
spring.cloud.config.server.native.search-locations : configuration file path of the local storage

4. Create /src/main/resources/conf/sc-config-client-dev.yml file:

nickName: Luke 

The contents of the file to the client need to configure the information obtained from the server, the file name and client configuration is corresponding to such sc-config-client-dev.yml = [spring.application.name] - [spring.profiles. active] .yml

5. Test

Start the local distribution center sc-config-server after the successful launch client sc-config-client, visit http: // localhost: 9002 / client / hello, the client successfully obtained nickName configuration from the configuration center:

Use Remote Configuration Center

1. Create a sub-module project sc-config-server-git in the parent module, pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.cf</groupId>
    <artifactId>sc-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>sc-config-server-git</artifactId>
  
  <dependencies>
    <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>
  </dependencies>
</project>

2. Create a startup class configserver.GitConfigServerApplication:

package configserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class GitConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(GitConfigServerApplication.class, args);
    }
}

3. Create application.yml:

server:
  port: 9005

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/ 

spring:
  application:
    name: sc-config-server-git
  cloud:
    config:
      server:
        git:
          uri: https://github.com/yinchao3577/test777.git
          username: xxxxx
          password: xxxxx
      label: master    
      fail-fast: true       

spring.cloud.config.server.git.uri : git repository address
spring.cloud.config.server.git.username : Username
spring.cloud.config.server.git.password : Password
spring.cloud.config.server .git.searchPaths : configuration file directory, if you do not need to configure in the root directory
spring.cloud.config.label : branch Git Repository, the default is master

4. Change the client configuration

pom.xml add Eureka dependent on:

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

bootstrap.yml read:

spring:
  application:
    name: sc-config-client
  cloud:
    config:
      name: myconfig2
      label: master
      discovery:
        enabled: true
        service-id: sc-config-server-git #使用Eureka注册中心来发现Config配置中心服务

server:
  port: 9002
    
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/

spring.cloud.config.name : remote repository name of the profile
spring.cloud.config.discovery.enabled : whether to open distribution center service discovery
spring.cloud.config.discovery.service-the above mentioned id : Name Service Configuration Center

5. Test

Remote repository myconfig2.yml value:

nickName: Grace

In turn starts registry sc-eureka, remote configuration center sc-config-server-git, sc-config-server-git started successfully after starting the client sc-config-client, visit http: // localhost: 9002 / client / hello, the client successfully obtained nickName configuration from the configuration center:

Manually refresh configuration

the actuator comprising a terminal / refresh for refreshing the configuration. Essence of the call is a call to endpoint RefreshScope class, RefreshScope context is a bean which comprises a common refreshAll () method and a refresh (String) method, respectively, for all bean refreshed within the specified range or a single bean .

1. Change the client configuration

pom.xml add actuator dependent on:

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

bootstrap.yml add open configuration refresh node:

management:
  endpoints:
    web:
      exposure:
        include: 'refresh'

2. Add @RefreshScope comment on ConfigClientController class that represents the class can refresh the configuration at run time, when After calling / actuator / refresh endpoint, the next time access to the Controller, the Controller will re-initialize and injected into the container, the initialization when will reload the configuration, so when you visit will have access to the latest configuration values.

Follow step before the test to start and access, the revised values ​​github configured, re-access client after the value does not change, by sending a POST request to http Postman tool: // localhost: After 9002 / actuator / refresh, re-access clients, values ​​have been updated to the latest configuration values.

Guess you like

Origin www.cnblogs.com/seve/p/11571193.html