springcloud - spring cloud config (configuration Center)

1. Understand the Configuration Center

1.1. What is the distribution center

  • In a distributed system, due to the huge number of multi-service, service profile in order to facilitate unified management, real-time updates, so they need the central component of a distributed configuration. In Spring Cloud, there is a central component of a distributed configuration spring cloud config, which supports the configuration file in the configuration of the local service, but also on the support remote Git repository. In the spring cloud config assembly, the two roles, one config server.

1.2. Workflow Configuration Center

  • We need to upload the configuration file to the micro-service Git repository, and then set up an independent ConfigServer service, in addition to ConfigServer pull from a Git repository configuration, should also be registered to EurekaServer because ConfigServer is a micro-services, as this requires ConfigClient integrated into the specific micro-services, such as payment services, order services. The workflow is configured to obtain micro-service distribution center, distribution center to get the configuration from the GIT repository, and then all the way back to the micro-services.
  • Caution is therefore required, not to ConfigServer EurekaServer configuration management, because you must first start EurekaServer to start ConfigServer, did not ask EurekaServer configure how to start?

Here Insert Picture Description

2.Git Warehouse Management Configuration

2.1. Use code to create a remote repository cloud

  • We use the code as a cloud git repository, create a code repository cloud
    Here Insert Picture Description
  • Creating zuul configuration to create a file in the repository, we assign a code to the cloud first zuul profile
    Here Insert Picture Description
  • Note warehouse address good copy, copy clones to take, would be to build a distribution center will be used when

3. Set up the configuration center

3.1. Building project (springcloud_config_server_1060)

3.2. Import dependence

 <dependencies>
     <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>
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-config-server</artifactId>
     </dependency>
</dependencies>

3.3. Configuration Center open

/**
 * @EnableEurekaServer:开启eurekaServer服务端
 * @EnableCircuitBreaker:开启Hystrix断路器注解
 */
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigServerApplicationConfig {

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

3.4. Configuration File

  • ConfigServer configuration file to do two things, 1 registered to EurekaServer, 2. Clouds address configuration code
eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:1010/eureka/,http://peer2:1011/eureka/,http://peer3:1012/eureka/ #注册中心地址
  instance:
    prefer-ip-address: true #使用ip地址注册
    instance-id: config-server  #指定服务的id
server:
  port: 1060
spring:
  application:
    name: config-server:1060
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/wang_ha_ha_1/spring-cloud.git #配置远程仓库地址
          username: 注册的账号	#仓库是私有的需要账号
          password: 注册的密码


3.5. Test Configuration Center

  • Browser to access: http: // localhost: 1060 / application-zuul-dev.yml you can see from the Git repository to get ConfigServer of zuul profile reads as follows:
    Here Insert Picture Description

4.Zuul integrated ConfigClient

  • The last step we can already get from the code by ConfigServer cloud to the configuration file, our ultimate goal is to enable our micro-services, zuul-server, order-server so you can take from ConfigServer pulled to the configuration file, here we function We need to give micro-services integration ConfigClient to achieve. Modify "springcloud-zuul-server-1050" zuul works as follows:

4.1. Import dependence

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

4.2. Creating bootstrap.yml profile

  • In need of special note, although we zuul profile moved yards above the clouds, but still need some basic zuul local configuration is required bootstrap.yml in ConfigClient as a basis for the profile, because the priority of bootstrap.yml higher than application.yml or other configuration sources, we have to make service a priority micro pull configuration files from the cloud after the code can do other things.
  • Create a configuration file resources / bootstrap.yml, add the following
#配置中心的地址
spring:
  cloud:
    config:
      uri: http://localhost:1060 #zuul要从配置中心拉取文件
      #你要拉取具体的哪个配置文件
      name: application-zuul  #配置文件名字   dev是环境
      profile: dev #环境  组成完整的文件名:application-zuul-dev.yml
      label: master #主分支
  • Tip: We need to take the Zuul configuration file from ConfigServer pull, so here by ... config.uripointing to configure the center address, nameproperty worth is the name of the configuration file, profileis the name of the environment, the value of the name and profile taken together form a complete profile name , the benefits can modify the value to switch profile configuration environment labelis git branch, the default is master.

4. Configure centers error problem solving

If you find that your micro unsuccessful start service, which caused the problem:

1. Profile not pull

2. profile to pull, but the configuration of the content is wrong

3. Project else is wrong - the problem is not the configuration file

4.1. Configuration pull less than

  • Start observing port is not in the start of 8080, if it is, must not pull configuration files (unless you really configurations is 8080) to troubleshoot ways:

Check whether the configuration center server has a problem

  • Check the ConfigServer not start, ports, addresses right

  • Open a browser to access some configuration files to see if I can pull the configuration file, as follows:
    HTTP: // localhost: 1060 / the Application-Zuul-dev.yml
    If the configuration center can get to the configuration file, the error must be in ConfigClient, or problematic content

  • If the configuration can not pull the central configuration, there are two cases, one is the IP address is incorrect git, 2 SUMMARY coding errors, formatting errors than pulling

  • Check the configuration of the center git address, user name, password, if there are problems

  • Open the code cloud confirm whether the content in question, format, encoding

Check the configuration center client - is pulling the micro-service configuration

  • Are you comparing the spring.cloud.config.name and profile and configuration files on the cloud, like code, and whether the same branch label
  • Check your spring.cloud.config.uri to write a no, is not your address configuration center services
  • Check your dependent: spring-cloud-config-client no import, the import was successful Mody, press ctrl to click your dependent (or remove the dependency of a local factory library re-download)

4.2 configuration file to pull

  • If the configuration file to the pull, the service should start port is the port your configuration file, but if you start the log or abnormal, can be configured with a file to correct the error in the local mode:

  • Comment out spring-cloud-config-client packet

  • Restore local profile

    If the local configuration or can not start, that is the other issue, the problem may be the configuration file content, it could be a code problem, according to troubleshoot error logs

Published 33 original articles · won praise 0 · Views 391

Guess you like

Origin blog.csdn.net/weixin_45737653/article/details/105001831