[Config] SpringCloud implementation of distributed uniformly configured Center

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/wrs120/article/details/90679049

1. The existence of a distributed system problems - configuration issues

  Distributed systems means that there are a lot of small services, a large number of such service exists in the system, but each service needs to run the configuration information, maps the SpringBoot project means there are many application.yml profile, difficult to manage, which is SpringCloud provides ConfigServer to solve this problem, namely the use of SpringServer this service implements the configuration file all services were centralized, dynamic configuration

What 2. Config service

  SpringCloud Config offers a centralized service for the micro-micro-architecture support services external configuration, the configuration server provides a centralized external configuration for each different micro-environments for all service applications

3. Config service can do

  1. Centralized management profiles
  2. Different configurations in different environments, dynamic configuration updates
  3. Dynamically adjust the configuration during operation, no longer need to write the configuration file on each machine service deployment, unified service will pull their configuration information to the configuration center
  4. When configuration changes, you can restart the service do not need to change the perception of the configuration and apply the new configuration
  5. The configuration information in the form of REST interfaces exposed

4. project combat

  SpringCloud Config divided into server and client, the server is called a distributed configuration center, an independent service is used to connect the server and client configured to provide configuration information, encryption / decryption and other access interface; client by established distribution center to manage application resources, and configure content-related business, and obtaining and loading configuration information from a configuration center at boot time, you can understand the provider for the service provider. Configuration files are usually placed on git

4.1 gitHub Configuration

Placed on the respective profile gitHub, file name: integral-game.yml

spring:
  profiles:
    active: dev   //@profileActive@

---

server:
  port: 8090
  servlet:
    context-path: /game-web
spring:
  profiles: dev
  application:
    name: integral-game-provider
  main:
    allow-bean-definition-overriding: true
       
eureka:
  client:
    service-url:
      #客户端注册进eureka服务列表内
      defaultZone: http://192.168.22.126:7001/eureka/
  instance:
    preferIpAddress: true
    ipAddress: 192.168.22.126
    #注册到eureka后,status的名字(服务在eureka的唯一标志)
    instance-id: ${spring.application.name}:${random.int}
    #访问路径可以显示IP地址
    prefer-ip-address: true

info:
  app.name: provider-dept-8001
  company.name: www.tfjybj.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

---

server:
  port: 8090
  servlet:
    context-path: /game-web
spring:
  profiles: test
  application:
    name: integral-game-provider
  main:
    allow-bean-definition-overriding: true
              
eureka:
  client:
    service-url:
      #客户端注册进eureka服务列表内
      defaultZone: http://192.168.22.227:7001/eureka/
  instance:
    #注册到eureka后,status的名字(服务在eureka的唯一标志)
    instance-id: ${spring.application.name}:${random.int}
    #访问路径可以显示IP地址
    prefer-ip-address: true

info:
  app.name: provider-dept-8001
  company.name: www.tfjybj.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

4.2 Config server configuration

4.2.1 Config server connection github

Config configuration is as follows yml file server:

server: 
  port: 3344 
  
spring:
  application:
    name:  cloud-config-service
  cloud:
    config:
      server:
        git:
          uri: [email protected]:zzyybs/microservicecloud-config.git #GitHub上面的git仓库名字
 

And so on github server connection is established, it can be read remotely configure git repository

4.2.2 Configuration startup items annotated @EnableConfigServer

@EnableConfigServer
@SpringBootApplication
public class CloudConfigApplication {

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

4.3 Client Configuration

4.3.1 application.yml configured as follows:

spring:
  application:
    name: integral-game-provider

4.3.2 bootstrap.yml configured as follows:

spring:
  cloud:
    config:
      name: integral-game   //读取github上的那个文件对应的文件名 integral-game.yml,不带yml后缀名
      #正常方式应该从github上更改
      profile: dev  //@profileActive@ ,拉取dev环境配置
      #label: master    // 从github的master
      label: integral-config
      uri: http://192.168.22.126:3344  // Config服务端地址
  main:
    allow-bean-definition-overriding: true

4.3.3 Startup Items annotated

@SpringBootApplication
//本服务启动后会自动注册进eureka服务中
@EnableEurekaClient
//服务发现
@EnableDiscoveryClient
public class IntegralGameProviderApplication {

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

  It noted that the external file to read configuration information written in the bootstrap.yml file, why should write the configuration information in a file that bootstrap.yml: application.yml user-level resource configuration item; and is bootstrap.yml system level, a higher priority. SpringCloud creates a Bootstrap Context Application Context Spring as the parent application context. Initialization, Bootstrap Context is responsible for loading configuration properties from external resources and resolve configuration , both a context sharing Environment acquired from the outside, but Bootstrap higher priority, by default, they will not be covered by the local configuration.

Guess you like

Origin blog.csdn.net/wrs120/article/details/90679049