SpringCloud Learning Series Four ----- Configuration Center (Config) uses detailed

Foreword

This part describes a distributed configuration in the center SpringCloud (SpringCloud Config) related to the use of tutorials.

SpringCloud Config

Config Introduction

Spring Cloud Config project is a configuration management solution to address the distributed system. It contains two parts Client and Server, server to provide storage profile, in the form of an interface configuration file to provide the contents out, client data acquired through the interface, and based on this data to initialize their own applications.

Development Readiness

Development environment

  • JDK:1.8
  • SpringBoot:2.1.1.RELEASE
  • SpringCloud:Finchley

Note: do not have the above-described version, it can be adjusted according to the situation. Note that after SpringBoot2.x, jdk version must be 1.8 or more!

After confirming the development environment, let's add the relevant pom-dependent.

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
</dependencies>

SpringCloud Config examples

Currently SpringCloud Config of use is mainly done through a distribution center Git / SVN way, then each of the services from which to obtain its configuration parameters required. SpringCloud Config also support access to local configuration parameters. If you use a locally stored manner, application.propertiesor application.ymlfile to add spring.profiles.active=nativeconfiguration can be, it will be from the project's resources reads the configuration file path. If you read the specified configuration file, you can use spring.cloud.config.server.native.searchLocations = file:D:/properties/to read.

Server

First, the server piece, first create a registry, in order to distinguish, create a springcloud-config-eurekaproject. And before the code and configuration essentially the same.
application.propertiesConfiguration information:

Configuration information:

spring.application.name=springcloud-hystrix-eureka-server
server.port=8005
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:8005/eureka/

Configuration instructions:

  • spring.application.name: This is to specify the service name.
  • server.port: Service specified port.
  • eureka.client.register-with-eureka: Indicates whether to register itself to Eureka Server, the default is true.
  • eureka.client.fetch-registry: Indicates whether obtaining registration information from Eureka Server, the default is true.
  • eureka.client.serviceUrl.defaultZone: This is to set the address Eureka Server interaction, client tracking and registration services will need to rely on this address.

The server side only need to add SpringBoot start class @EnableEurekaServernotes on it, the annotation indicates that this service is a service registry service.

Code Example:


    @SpringBootApplication
    @EnableEurekaServer
    public class ConfigEurekaApplication {

        public static void main(String[] args) {
            SpringApplication.run(ConfigEurekaApplication.class, args);
             System.out.println("config 注册中心服务启动...");
        }
    }

After creating the registry, let's create a distribution center for managing configuration.
Create a springcloud-config-serverproject. Then application.propertiesadd the following configuration profiles:

Configuration information:

 spring.application.name=springcloud-config-server
 server.port=9005
 eureka.client.serviceUrl.defaultZone=http://localhost:8005/eureka/
 spring.cloud.config.server.git.uri = https://github.com/xuwujing/springcloud-study/
 spring.cloud.config.server.git.search-paths = /springcloud-config/config-repo
 spring.cloud.config.server.git.username = 
 spring.cloud.config.server.git.password = 

Configuration instructions:

  • spring.application.name: This is to specify the service name.
  • server.port: Service specified port.
  • eureka.client.serviceUrl.defaultZone: This is to set the address Eureka Server interaction, client tracking and registration services will need to rely on this address.
  • spring.cloud.config.server.git.uri: Git address configuration of trousers.
  • spring.cloud.config.server.git.search-paths: the relative address with the address of the warehouse git plurality comma "," split.
  • spring.cloud.config.server.git.username: git repository account.
  • spring.cloud.config.server.git.password: Password git repository.

Note: If you want to read configuration information locally, then only the spring.cloud.config.server.gitconfiguration change spring.profiles.active=native, and then add a file to the path in resources.

Here For local configuration file test, create a configtest.propertiesconfiguration file, add the following:


    word=hello world

This piece of code is also very simple, the main program class, add additional @EnableConfigServernotes, the notes indicate enable config configuration center. code show as below:

、、、

@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
        System.out.println("配置中心服务端启动成功!");
    }
}

、、、

After completing the above code, our configuration center server has built completed.

Client

We create a new springcloud-config-clientproject, do the configuration for reading configuration center. pom or dependent and configuration centers, but need to add a configuration for reading the specified configuration.
Create a bootstrap.propertiesfile, and add the following information:

Configuration information:

spring.cloud.config.name=configtest
spring.cloud.config.profile=pro
spring.cloud.config.label=master
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=springcloud-config-server
eureka.client.serviceUrl.defaultZone=http://localhost:8005/eureka/

Configuration instructions:

  • spring.cloud.config.name: Gets the name of the configuration file.
  • spring.cloud.config.profile: get configured policy.
  • spring.cloud.config.label: acquiring branch of the configuration file, the default is master. If it is a local acquisition, then no use.
  • spring.cloud.config.discovery.enabled: open configuration information discovery.
  • spring.cloud.config.discovery.serviceId: Specifies the configuration of the central service-id, scalable cluster is highly available configuration.
  • eureka.client.serviceUrl.defaultZone: This is to set the address Eureka Server interaction, client tracking and registration services will need to rely on this address.

Note : The above associated with spring-cloud properties must be configured in bootstrap.properties, config is part of the contents in order to be loaded correctly. Because bootstrap.properties related configuration will precede application.properties, but also prior to loading bootstrap.properties application.properties. Note that eureka.client.serviceUrl.defaultZoneyou want to configure bootstrap.properties, otherwise the client is unable to obtain configuration parameters of the center, it will fail to start!

application.properties Configuration

spring.application.name=springcloud-config-client
server.port=9006

Configuration instructions:

  • spring.application.name: This is to specify the service name.
  • server.port: Service specified port.

Main program class code, and basically the same before. code show as below:

Code Example:


    @EnableDiscoveryClient
    @SpringBootApplication
    public class ConfigClientApplication {

        public static void main(String[] args) {
            SpringApplication.run(ConfigClientApplication.class, args);
            System.out.println("配置中心客户端启动成功!");
        }
    }

In order to facilitate inquiries, the parameters in the control acquisition, and return. @ValueNotes from the default application.propertiesacquisition parameters configuration file, but here we are not configured in the client, the server configured in the configuration center, to be used after that we simply specify a good profile.

Code Example:


    @RestController
    public class ClientController {

        @Value("${word}")
        private String word;

        @RequestMapping("/hello")
        public String index(@RequestParam String name) {
            return name+","+this.word;
        }
    }

This, the client will build the project completed.

function test

After completion of the above project development, we have to be tested.

Local test

First, we put springcloud-config-serverthe project application.propertiesto add a profile spring.profiles.active=nativeconfiguration, comment out the spring.cloud.config.server.gitrelevant configuration, then create a new in src / main under / resources directory configtest.propertiesfile, then add a configuration in it word=hello world.
After the addition is complete, we turn starts springcloud-config-eureka, springcloud-config-server, springcloud-config-clientthese three projects. Before starting success, it seems to look at the configuration center server configuration file to obtain, in the browser, type:

http://localhost:9005/configtest-1.properties

Check the configuration of the file.

Note : The name of the configuration file is configtest.properties, but if the direct words of the name is not acquired, as required by the configuration file name -to obtain, if not the profile name -, then added -later, will automatically match the search.

The URL mapping relationship springcloud config configuration file is as follows:

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

The above mapped url {application} - {profile} .properties corresponding configuration file, {label} git corresponding to a different branch, the default is master.

Interface Returns:

word: hello world

Then call the client interface, see if can obtain configuration information. In the browser input:

http://localhost:9006//hello?name=pancm

Interface Returns:

pancm,hello world

sample graph:

Here Insert Picture Description
Here Insert Picture Description

Git test

After the completion of the local test, we put this spring.profiles.active=nativeconfiguration commented out, lift the spring.cloud.config.server.gitrelated notes (account number and password to fill in the real), and then create a config-repo folder on the new git repository configtest-pro.properties, configtest-dev.propertiestwo configurations, both of are arranged word=hello world!!and word=hello world!, and then configtest.propertiesupload the configuration file together config-repo folder.

First in your browser and enter:

http://localhost:9005/configtest-dev.properties

Browser Returns:

word: hello world!

Then your browser and enter:

http://localhost:9005/configtest-pro.properties

Browser Returns:

word: hello world!!

Upload the configtest.propertiesfile, but the file name does not -, we want to get information about the parameters which then can then -feel free to add a parameter, it will be matched automatically, enter in your browser:

http://localhost:9005/configtest-1.properties

Browser Returns:

word: hello world

Then the client interface called Test, enter in your browser:

http://localhost:9006/hello?name=pancm

Browser Returns:

pancm,Hello World!!

Because here I configured a prefix pro, so reading is configtest-pro.properties data files, you want to get other configurations, modify spring.cloud.config.profilethe configuration can be.

sample graph:

Here Insert Picture Description

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

other

project address

Based SpringBoot2.x, SpringCloud version of Finchley address development : https://github.com/xuwujing/springcloud-study

Based SpringBoot1.x, SpringCloud version of Dalston address development : https://github.com/xuwujing/springcloud-study-old

If it feels good project, we hope to give a star, thank you!

Recommended Music

<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=1344874921&auto=0&height=66"></iframe>

The original is not easy, if you feel good, I hope the message is recommended! Your support is the greatest driving force of my writing!
Copyright:
Author: nothingness territory
blog Park Source: http://www.cnblogs.com/xuwujing
CSDN Source: http://blog.csdn.net/qazwsxpcm    
personal blog Source: http://www.panchengming.com

Guess you like

Origin blog.51cto.com/12965378/2403683