java version b2b2c social micro-distributed electricity supplier spring cloud services: Distributed Configuration Center

Spring Cloud Config is a new project Spring Cloud team created for distributed systems infrastructure and application services to provide centralized micro external configuration support, which is divided into server and client in two parts. Wherein the service center side is also referred to a distributed configuration, it is an independent micro-service applications, warehouse and configured to connect the client to provide access to configuration information, the encryption / decryption information access interface and the like; and the client is a micro-service architecture the individual micro-service applications or infrastructure, they manage application resources and business-related content through the configuration specified configuration center, and obtaining and loading configuration information from a configuration center at boot time. Spring Cloud Config achieve the abstract mapping of server and client environment variables and configuration properties, so it is applicable not only to build Spring applications outside, also can be used in any application to run in other languages. Since the Spring Cloud Config achieve configuration center default using Git to store configuration information, use the configuration server Spring Cloud Config constructed natural to support version management for micro-service application configuration information, and can be easily by Git client tools of management configuration and access content. Of course, it also provides support for other storage methods, such as: SVN repository, localization file system.
In this article, we will learn how to build a distribution center based Git distributed storage, and clients to transform and let it be able to obtain configuration information from the configuration center and bound to the process code.

Preparing to configure the warehouse
to prepare a git repository, you can create code in the cloud or on Github.
Suppose we read the configuration of the application center is named config-client, we can project the default configuration file config-client.yml in the git repository:

info:
profile: default

To demonstrate loading configuration different environments, we can then create a config-client-dev.yml dev environment for configuration files in the git repository:

info:
 profile: dev

Build configuration center
constructed by Spring Cloud Config a distributed configuration center is very simple, just three steps:

Spring Boot create a basic project named: config-server-git, and introducing the following dependencies in pom.xml (not part of the parent and dependencyManagement):

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

Creating the program's main class Spring Boot and add @EnableConfigServer notes, open the service end of Spring Cloud Config function.

@EnableConfigServer
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }

}

Add configuration services in application.yml basic information as well as information Git repository, for example:

spring
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: http://git.oschina.net/didispace/config-repo-demo/
server:
  port: 1201

Here, by using a Spring Cloud Config implement and use Git Distributed Configuration Management Configuration center contents it has been completed. We can first start up the application, make sure that no error occurs, then try the following.

If we need access to the Git repository, it can be implemented by configuring the following two properties; 
spring.cloud.config.server.git.username: access Git repository username 
spring.cloud.config.server.git.password : access Git repository user password

After the completion of these preparations, we can directly access to our content through configuration and other browsers, POSTMAN or CURL tool. URL mapping between the configuration file access configuration information is as follows:

/ {file application} / {Profile} [/ label {}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/ { label} / {application} - { profile} .properties
url above maps {application} - {profile} .properties corresponding profile, wherein the corresponding {label} Git on different branches, the default is master. We can try different configurations url to access the contents of different configurations, for example, to access the master branch, config-client application dev environment, you can visit this url: http: // localhost: 1201 / config-client / dev / master and received the following returns:

{
    "name": "config-client",
    "profiles": [
        "dev"
    ],
    "label": "master",
    "version": null,
    "state": null,
    "propertySources": [
        {
            "name": "http://git.oschina.net/didispace/config-repo-demo/config-client-dev.yml",
            "source": {
                "info.profile": "dev"
            }
        },
        {
            "name": "http://git.oschina.net/didispace/config-repo-demo/config-client.yml",
            "source": {
                "info.profile": "default"
            }
        }
    ]
}

We can see that the Json returned the application name: config-client, Environmental name: dev, branch name: master, and content default configuration environment and dev environments.
spring cloud b2b2c e-commerce social platform source code, please add penguin beg: three four five three six II qi II fifty-nine

Guess you like

Origin www.cnblogs.com/summercode/p/11287501.html