SpringCloud Micro Services (06): Config components, to achieve unified management configuration

A, Config Introduction

In the micro-service system, the more services, the same configuration: Database information such as, cache, and other parameters, will appear on a different service, if a configuration change occurs, you need to modify a lot of service configuration. spring cloud provides configuration center, to solve this problem scene.
Common configuration storage system at the same address: GitHub, Gitee, local configuration services, and then configure the center reads the configuration to restful released, other services may call interface for configuration information.

Second, configure the server

1, project structure

  • Core Notes: @EnableConfigServer

2, rely on the core

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

3, the core configuration file

Note here that the configuration file is read

  • active: native, read local configuration;
  • active: git, read warehouse network configuration;
server:
  port: 9001
spring:
  application:
    name: config-server-9001
  profiles:
    # 读取本地
    # active: native
    # 读取Git
    active: git
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/config
        git:
          # 读取的仓库地址
          uri: https://gitee.com/cicadasmile/spring-cloud-config.git
          # 读取仓库指定文件夹下
          search-paths: /cloudbaseconfig
          # 非公开需要的登录账号
          username:
          password:
      label: master

4, reads the configuration content

Different results for different environments read.

info:
  date: 20190814
  author: cicada
  sign: develop
  version: V1.0

Third, configure the client

1, dependent on the core

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

2, the core configuration file

In the above configuration center, configuration read Git resources, so the configuration here is to read Git resources.

server:
  port: 8001
spring:
  application:
    name: config-client-8001
  profiles:
    active: dev
  cloud:
    config:
      # 读取本地配置 ---------------------------
      #uri: http://localhost:9001
      ## 读取策略:快速失败
      #fail-fast: true
      ## 读取的文件名:无后缀
      #name: client-8001
      ## 读取的配置环境
      #profile: dev  # client-8001-dev.yml
      # ----------------------------------------

      # github上的资源名称 -----------------------
      name: client-8001
      # 读取的配置环境
      profile: dev
      label: master
      # 本微服务启动后,通过配置中心6001服务,获取GitHub的配置文件
      uri: http://localhost:9001
      # ----------------------------------------

3, test interface

@RestController
public class ClientController {
    @Value("${info.date}")
    private String date ;
    @Value("${info.author}")
    private String author ;
    @Value("${info.sign}")
    private String sign ;
    @Value("${info.version}")
    private String version ;
    /**
     * 获取配置信息
     */
    @RequestMapping("/getConfigInfo")
    public String getConfigInfo (){
        return date+"-"+author+"-"+sign+"-"+version ;
    }
}

Fourth, based on the Eureka configuration

The above model, through service centers, direct access to the configuration. The following registry Eureka add to the mix.

1, project structure

The boot sequence is as follows:

node06-eureka-7001
config-server-9001
config-client-8001

2, modify configuration items

  • Add the config-server-9001 to the registry;
  • Config-client-8001 configured to read the registry;

After completion of the Eureka registry renderings, starting in the following order:

3, modify the client configuration

Access to services through the registry, avoid using URI address.

After testing, correct.

  • Reminder: If you read the git domestic configuration, the problem may often not be loaded out of the case using Gitee address.

Fifth, the source code address

GitHub地址:知了一笑
https://github.com/cicadasmile/spring-cloud-base
码云地址:知了一笑
https://gitee.com/cicadasmile/spring-cloud-base

Guess you like

Origin www.cnblogs.com/cicada-smile/p/11370329.html