Spring Cloud-- Configuration Center Config

Part I: the Spring Cloud - distributed fault-tolerant components Hystrix breaker system delay

1. Spring Cloud Config Introduction

External Spring Cloud Config for infrastructure and micro-service applications in a distributed system provides centralized configuration support, divided into server and client-side. a distributed server-side distribution center, an independent micro service application; client terminal is set based distributed systems or micro-service application, related to the management center configuration by specifying configuration.
Spring Cloud Config build distribution center, in addition to built for applications Spring, but also can be used in any application built in other languages.
Here Insert Picture Description
Spring Cloud Config default using Git to store configuration information, version management support for configuration information. Unified configuration has a config-server end, which is rounded configuration from the distal end of the pull git and synchronized to the local git, the git hang even if the distal end, can still support the local git. The right product and order two services are unified micro-configured clients can obtain configuration from the config-server. Official website address: https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.1.RELEASE/reference/html/

2. build config-server service

2.1 introduction config server dependent
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>
2.2 config start adding annotations
/**
 * @author Administrator
 * @version 1.0
 **/
@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigApplication {
	public static void main(String[] args) throws Exception {
		SpringApplication.run(ConfigApplication.class, args);
	}
	
}
2.3 Configuring git repository address
server:
  port: 8080 
spring:
  application:
    name: springcloud-config
cloud:
  config:
    server:
      git:
        uri: https://github.com/qqxhb/springcloud-demo.git #配置文件在github上的地址
        search-paths: config  #Configserver会在 Git仓库根目录、 config子目录中查找配置文件。
#          clone-on-start: true  #启动时就clone仓库到本地,默认是在配置被首次请求时,config server才会clone git仓库
        #native:
          #search-locations: classpath:/config #若配置中心在本地,本地的地址
eureka:
  client:
    registerWithEureka: true #服务注册开关
    fetchRegistry: true #服务发现开关
    serviceUrl: #Eureka客户端与Eureka服务端进行交互的地址,多个中间用逗号分隔
        defaultZone: ${EUREKA_SERVER:http://localhost:8761/eureka/,http://localhost:8762/eureka/}
  instance:
    prefer-ip-address:  true  #将自己的ip地址注册到Eureka服务中
    ip-address: ${IP_ADDRESS:127.0.0.1}
    instance-id: ${spring.application.name}:${server.port} #指定实例id

2.4 access to the configuration file warehouse

Here Insert Picture Description
The contents of the file access application-eureka.yml image above: http://127.0.0.1:8080/application-eureka.yml
Here Insert Picture Description
the Spring Cloud config can automatically convert yml and properties, such as the same access to just the file, the address bar suffix into the Properties: http://127.0.0.1:8080/application-eureka.properties
Here Insert Picture Description

2.5 Path Access Rules

Reference official website: https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.1.RELEASE/reference/html/

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

application is to configure the file name, profile is the corresponding environmental label is different branch (default master).

3. The client obtains configuration

3.1 introduction config client-dependent
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
3.2 配置 bootstrap.yml

Reference official website: https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.1.RELEASE/reference/html/#_spring_cloud_config_client

spring:
  cloud:
    config:
      name: application-teacher #这是我们要读取的配置文件名 对应获取规则的{application}
      profile: dev   #这个是要获取的环境 对应的便是{profile}
      label: master #这个就是获取的节点 对应的是{label}
      uri: http://localhost:8080/ #这就是我们config server的一个地址
     

There is a spring cloud "leading context" concept, which is the main application of the parent context. Responsible for loading the boot context configuration properties from a configuration server, and a decryption external configuration file attributes. And a main application file application loading. Different (YML or properties) attributes, loading boot context (on Bootstrap.) Attributes. Configuring the bootstrap. * The property has a higher priority.
The above config-server configuration can build a plurality of service availability cluster, the configuration can not be configured and arranged uri Service ID

spring:
  cloud:
    config:
      name: application-teacher #这是我们要读取的配置文件名 对应获取规则的{application}
      profile: dev   #这个是要获取的环境 对应的便是{profile}
      label: master #这个就是获取的节点 对应的是{label}
      discovery:
        enabled: true
        service-id: springcloud-config

Source Address: https://github.com/qqxhb/springcloud-demo in springcloud-config and teacher-service module.
Next: the Spring Cloud - link tracking Sleuth

Published 118 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43792385/article/details/104700128