Springcloud Distributed Configuration

Source: Internet
Author: User
Tags naming convention

Transfer http://www.cnblogs.com/zhangjianbin/p/6347247.html Preface

In a monolithic application, it's common practice to put configuration files and code together, which is nothing wrong. As your application grows larger and you have to service-split, you will find that there are more and more provider instances, and it becomes more and more troublesome to modify a particular configuration, and you often have to restart all provider instances of a service to modify a Configuration. Even for grayscale on-line needs to update some provider Configuration. This time a set of configuration files centralized management program becomes very important, Springcloudconfig and Springcloudbus is one of the solutions to this problem, the industry also has some well-known similar open source products, such as Baidu's disconf.

Compared to similar products, Springcloudconfig's greatest advantage is that it integrates seamlessly with spring, supports the interface within spring Environment and PropertySource is very low on the migration cost of existing spring applications, and is exactly the same as the interfaces acquired by the Configuration. Combining Springboot allows you to have more uniform standards for your project (including dependent versions and constraint specifications), avoiding dependency versioning conflicts that should be caused by the integration of different software Sources.

I. INTRODUCTION

Springcloudconfig is our usual configuration center, the application of the original local file configuration extracted from the central server, so as to provide better management, publishing Capabilities. Springcloudconfig Sub-server and client, The server is responsible for the git (svn) stored in the configuration file published as a rest interface, the client can get the configuration from the server side rest Interface. however, the client is not actively aware of the changes to the configuration, thereby proactively acquiring a new configuration, which requires each client to trigger its own through the Post method /refresh .

Springcloudbus connects the nodes of a distributed system through a lightweight message broker. This can be used for broadcast state changes (such as configuration changes) or other management Directives. Springcloudbus provides access to the endpoint via the post method, which is /bus/refresh typically called by the Git hook function to notify the client of each springcloudconfig to update the configuration on the server Side.

Springcloudconfig is a workflow that combines springcloudbus for distributed configuration

注意:This is the flowchart of the work, the actual deployment of Springcloudbus is not a standalone service, which is listed here in order to clearly show the WORKFLOW.

Two. Springcloudconfig Server

Springcloudconfig provides configuration management based on the following 3 dimensions:

    • Application
      • This is a good understanding, each configuration belongs to an application
    • Environment
      • Each configuration is environment-sensitive, such as dev, test, prod, etc.
    • Version
      • This may be the lack of a general configuration center, which is the different version management of the same configuration
      • Spring Cloud Config provides support for versions, which means that for different deployment instances of an application, different versions of the configuration can be obtained from the server, which provides good support for special scenarios such as grayscale publishing, a/b testing, and so On.
2.1 Configserver Configuration

Service side to rely on the Pom spring-cloud-config-server ,spring-cloud-starter-bus-kafka

<Dependency><Groupid>org.springframework.cloud</Groupid><Artifactid>spring-cloud-config-server</Artifactid></Dependency><Dependency><groupid>org.springframework.cloud </groupid> < artifactid>spring-cloud-starter-bus-kafka</artifactid></dependency> <dependency> < groupid>org.springframework.cloud</ groupid> <artifactId> Spring-cloud-config-monitor</artifactid> </dependency>        

Application.properties to configure the warehouse description and Message Queuing address, if it is a private project, you also need to configure the user name password

spring.cloud.config.server.git.uri=https://github.com/seagrape/SpringCloudConfig.gitspring.cloud.config.server.git.searchPaths=alan-config-repo#spring.cloud.config.server.git.username=sihan2#spring.cloud.config.server.git.password=MYPASSWORDspring.cloud.stream.kafka.binder.brokers=10.79.96.52:9092spring.cloud.stream.kafka.binder.zk-nodes=10.79.96.52:2182

To have annotations in the startup class @EnableConfigServer

@SpringBootApplication@EnableConfigServerpublic class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); }}
2.2 Configserver Start

After Server-side startup, provide the following interface address, parameter description

    • Application: Application Name
    • Profile: Environment
    • Label: version
/{application}/{profile}[/{label}]/{application}-{profile}.yml/{label}/{application}-{profile}.yml/{application}-{profile}.properties/{label}/{application}-{profile}.properties

Note: the name of the configuration file is usually made up of two parts, for example, to feel the alan-provider-data-config-dev.properties, where Alan-provider-data-config is the first part, this section recommends using a naming convention to let you know which project is configured, and the client wants to configure spring.cloud.config.name=alan-provider-data-config To let the client know which profile to look for on the Server. Dev is the second part of the scenario that distinguishes the profile application from the development environment, the test environment, or the production environment

Interface returns sample Curl Http://localhost:8888/alan-provider-data-config/dev/master

{    "Name ":"alan-provider-data-config", "Profiles ":["dev"], "Label ":"master", "Version ":"78dce2b71473749a5298e11ef0d004ffa8d26bd1", "Propertysources ":[{"name": source ": {"  Spring.datasource.driver-class-name ": " com.mysql.jdbc.Driver "," spring.datasource.username ": spring.datasource.password ":  "password", "spring.datasource.url":  "jdbc:mysql://devip:port/dbname?characterencoding=utf-8"}}]}        

Interface returns sample Curl Http://localhost:8888/alan-provider-data-config-dev.properties

spring.datasource.driver-class-name: com.mysql.jdbc.Driverspring.datasource.password: passwordspring.datasource.url: jdbc:mysql://DEVIP:PORT/DBNAME?characterEncoding=UTF-8spring.datasource.username: username
2.3 Configserver File System

git file system, The files will be clone to the local file system, the default files will be placed in the Config-repo-prefix system temporary directory, Linux should be the/tmp/config-repo-directory, if you encounter unpredictable problems appear, You can set spring.cloud.config.server.git.basedir the parameter value to a Non-system temp directory.

In Config server, There is also a way to load a configuration file from a local classpath or file system, which can be spring.cloud.config.server.native.searchLocations set up. But if you don't have a git environment, you can go back to drinking milk ...

Three. springcloudconfig Client3.1 configclient Configuration

Client to rely on in the Pom spring-cloud-starter-config ,spring-cloud-starter-bus-kafka

<Dependency><Groupid>org.springframework.cloud</Groupid><Artifactid>spring-cloud-starter-config</Artifactid></Dependency><Dependency><Groupid>org.springframework.cloud</Groupid><Artifactid>spring-cloud-starter-bus-kafka</Artifactid></Dependency><Dependency><groupid>org.springframework.boot </groupid> <artifactId> Spring-boot-starter-web</artifactid> </dependency>< dependency> <groupId> Org.springframework.boot</groupid> <artifactid>spring-boot-starter-actuator </artifactid></ dependency>            

Configuring the Configuration center address and Message Queuing address in bootstrap.properties

1. pay special attention to the configuration center address must be placed in the bootstrap.properties, this configuration file is loaded by the "root" context first, you can ensure that the program started to perceive the remote configuration Center exists, and remotely obtain the configuration, and then continue to start the system, This is very important. 2. While application.properties is loaded by child contexts, the load order is lower than the former, if the configuration center address is here, and you remotely configure some of the necessary startup-related parameters, your program is likely to fail because of missing parameters. 3. The following code, the most critical is the first line, the second line if you do not configure the system default read spring.application.name , the third line if not configured, the system default, that is: ${spring.application.name}.properties 4. Our general practice is that When the system starts up, it is used in --spring.cloud.config.profile=dev|prod|test the command line, because at startup we know exactly which configuration I want to get. The 5.bus-related configuration (kafka in this example) can be placed entirely remotely.

spring.cloud.config.uri=http://127.0.0.1:${config.port:8888}spring.cloud.config.name=alan-provider-data-configspring.cloud.config.profile=${config.profile:dev}spring.cloud.stream.kafka.binder.brokers=10.79.96.52:9092spring.cloud.stream.kafka.binder.zk-nodes=10.79.96.52:2182

Referencing a configured class to add @refreshscope annotations

@SpringBootApplication@RestController@RefreshScopepublic class configclientapplication { @Value ("${spring.datasource.username}") String name = " World "; @RequestMapping ("/") public String home() {System.out.println (name); return name; public static void main(string[] Args) {springapplication.run ( configclientapplication.class, args); }}
3.2 Refreshscope Annotations

We know that spring native provides some scope, such as singleton,prototype,request and so On. Spring Cloud provides a new scope-refreshscope for the purpose of updating the values that have been injected into the bean in order to implement the configuration Update.

Spring Cloud defines the Refreshscope as Follows:

A Scope implementation that allows for beans to being refreshed dynamically at runtime (see refresh (String) and Refreshall ()) . If a bean is refreshed and the next time the bean is accessed (I.E. a method was executed) a new instance is Created.

so, for those beans that have injected values, we can mark them as refreshscope, so that when the runtime discovers a configuration update, by calling Refreshscope.refresh (beanname) or Refreshscope.refreshall () so that the next time these beans are used, they will be reinitialized, and then re-injected into the value, so it's up to the Update.

3.3 Configclient Boot Sequence

Configclient It is best to start after configserver, Spring load profiles are sequential, the previous configuration file will overwrite the value of the same key in the later configuration file, If Configserver first starts to ensure that configclient will load the remote configuration file to the front, if you do not notice this in use, it is possible to cause your local configuration file to load before remote, causing the local configuration to overwrite the remote Configuration. of course, You can also make the local configuration and remote configuration completely non-repeatable, which also avoids the issue of Key/value Overrides.

This part of the relevant knowledge points will be further explained Later.

Four. Background Knowledge 4.1 environment and Propertysource in spring
    • Environment
      • Spring's ApplicationContext will contain a environment
      • Environment itself contains a lot of propertysource.
    • Propertysource
      • Property source
      • Can be understood as a property configuration for many Key-value

The structure at run time is as Follows:

It is important to note that there is a priority order between propertysource, and if there is a key that exists in multiple property source, then the previous property source takes Precedence. So for example:

    • Env.getproperty ("key1"), value1
    • env.getProperty(“key2”) -> value2
    • Env.getproperty ("key3"), Value4

In the Configclient startup phase, The configuration is obtained from the configserver and then assembled into the propertysource and inserted into the first one, and in the subsequent get configuration process from the Config The configuration of the server and other local configurations make no difference to the user, enabling seamless Integration.

It should be noted that if Configserver is started after configclient, the remote configuration will be loaded to the end, which requires special attention in Use. It is possible to see that the remote configuration is loaded preferentially

4.2/env

/env is spring-boot-starter-actuator an interface provided, The Get method call can view the system environment variables, the post call can change the value of the environment variable, and the value of the variable modified in this way has the highest precedence. It is helpful to learn springcloudconfig by observing the changes of the interface Data.

curl -X POST http://localhost:8080/env -d spring.datasource.username=wsh

If you want the above modifications to take effect, you will also need to reload all @refreshscope-decorated bean classes With/refresh.

curl -X POST http://localhost:8080/refresh

If you want to reset these modifications

curl -X POST http://localhost:8080/env/reset
4.3/refresh

Enables the @refreshscope decorated bean class to reload the configuration at the next Call.

4.4/bus/env

function with/env, The difference is that it takes effect on all nodes

curl -X POST http://localhost:8888/bus/env -d spring.datasource.username=wsh
4.5/bus/refresh

function with/refresh, The difference is that it takes effect on all nodes

Sends a message to the messaging broker, and all applications that listen to the broker receive the above message and each start updating the Configuration. Each springcloudbus node has this interface, and these interfaces are equivalent, and calling any one can have the same effect. But usually we call the bus configured on the configserver, which is more in line with people's understanding of the Process.

curl -X POST http://localhost:8888/bus/refresh
Five. Springcloudbus

Springcloudbus is not a standalone service that he configures in each configclient, and makes all nodes aware of state changes through message queuing. Springcloudconfig is not directly integrated with the bus function is beneficial, bus is pluggable design and is not perfect, if there is a personal need to completely replace the bus with their own scheme, the stripping bus cost is almost equal to Zero.

Currently there are two implementations of bus spring-cloud-starter-bus-amqp or spring-cloud-starter-bus-kafka , the official website is based on amqp, need to run rabbitmq, The example of this article is Kafka.

Now we have the ability to update the application configuration without rebooting.

Six. Project Code

Springcloudconfig GitHub

Seven. questions
    1. When the code warehouse is slow to access, the read configuration is slower, taking into account the availability of the Code warehouse
    2. To increase the high reliability of config server, the number of config servers needs to be scaled up
    3. [to be perfected, at any time to add ...]
Eight. references
    • Dive into Spring Cloud Config
Nine. Extended Reading
    • Springcloudbus Official guide Chinese Translation

Springcloud Distributed Configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.