Amateur Grass Springcloud Tutorial | Sixth: Distributed Configuration Center (Spring Cloud Config) (Finchley version)

Source: Internet
Author: User

In the previous article about Zuul, it has been mentioned that the configuration service is used to store the configuration files for each service. It's spring Cloud Config.

First, Introduction

In distributed systems, because of the large number of services, in order to facilitate the unified management of service profiles, real-time updates, so the need for distributed configuration Center components. In spring cloud, there is a distributed Configuration center component, Spring Cloud Config, which enables configuration services to be placed in the memory of the configuration service (that is, local) and also in a remote Git repository. In Spring Cloud Config component, there are two roles, one is config server and the other is config client.

Second, build config Server

Parent MAVEN project omitted, parent Pom file:

1<?xml version= "1.0" encoding= "UTF-8"?>2<project xmlns= "http://maven.apache.org/POM/4.0.0"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >5<modelVersion>4.0.0</modelVersion>6  7<groupId>com.forezp</groupId>8<artifactId>sc-f-chapter6</artifactId>9<version>0.0.1-SNAPSHOT</version>Ten<packaging>pom</packaging> One   A<modules> -<module>config-server</module> -<module>config-client</module> the</modules> -   -<name>sc-f-chapter6</name> -<description>demo Project forSpring boot</description> +   -<parent> +<groupId>org.springframework.boot</groupId> A<artifactId>spring-boot-starter-parent</artifactId> at<version>2.0.3.RELEASE</version> -<relativePath/> -</parent> -   -   -<properties> in<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> -<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> to<java.version>1.8</java.version> +<spring-cloud.version>Finchley.RELEASE</spring-cloud.version> -</properties> the   *<dependencies> $<dependency>Panax Notoginseng<groupId>org.springframework.boot</groupId> -<artifactId>spring-boot-starter-test</artifactId> the<scope>test</scope> +</dependency> A</dependencies> the   +<dependencyManagement> -<dependencies> $<dependency> $<groupId>org.springframework.cloud</groupId> -<artifactId>spring-cloud-dependencies</artifactId> -<version>${spring-cloud.version}</version> the<type>pom</type> -<scope>Import</scope>Wuyi</dependency> the</dependencies> -</dependencyManagement> Wu   -<build> About<plugins> $<plugin> -<groupId>org.springframework.boot</groupId> -<artifactId>spring-boot-maven-plugin</artifactId> -</plugin> A</plugins> +</build> the</project>

Create a Spring-boot project, named Config-server, whose pom.xml is as follows:

1<?xml version= "1.0" encoding= "UTF-8"?>2<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"3xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >4<modelVersion>4.0.0</modelVersion>5  6<groupId>com.forezp</groupId>7<artifactId>config-server</artifactId>8<version>0.0.1-SNAPSHOT</version>9<packaging>jar</packaging>Ten   One<name>config-server</name> A<description>demo Project forSpring boot</description> -   -<parent> the<groupId>com.forezp</groupId> -<artifactId>sc-f-chapter6</artifactId> -<version>0.0.1-SNAPSHOT</version> -</parent> +   -<dependencies> +<dependency> A<groupId>org.springframework.boot</groupId> at<artifactId>spring-boot-starter-web</artifactId> -</dependency> -<dependency> -<groupId>org.springframework.cloud</groupId> -<artifactId>spring-cloud-config-server</artifactId> -</dependency> in</dependencies> -<build> to<plugins> +<plugin> -<groupId>org.springframework.boot</groupId> the<artifactId>spring-boot-maven-plugin</artifactId> *</plugin> $</plugins>Panax Notoginseng</build> -</project>

At the entrance of the program application class plus the @enableconfigserver annotation opens the function of the configuration server, the code is as follows:

 1   @SpringBootApplication  2   @EnableConfigServer  3  public  class   Configserverapplication {      4  5  public  static  void   Main (string[] args) { 6  Springapplicatio N.run (Configserverapplication. Class   7   8  }

You need to configure the following in the program configuration file Application.properties file:

1 spring.application.name=config-server2 server.port=88883  4 Spring.cloud.config.server.git.uri=https://github.com/forezp/springcloudconfig/5 spring.cloud.config.server.git.searchpaths=respo6 spring.cloud.config.label=Master  7 spring.cloud.config.server.git.username=8 spring.cloud.config.server.git.password=
    • Spring.cloud.config.server.git.uri: Configuring the Git repository address
    • Spring.cloud.config.server.git.searchPaths: Configuring the Warehouse Path
    • Spring.cloud.config.label: Configuring a branch of a warehouse
    • Spring.cloud.config.server.git.username: User name to access the GIT repository
    • Spring.cloud.config.server.git.password: Access the user password for the GIT repository

If the Git warehouse is a public warehouse, you can not fill in the user name and password, if the private warehouse needs to fill out, this example is open warehouse, rest assured that use.

There is a file in the remote repository https://github.com/forezp/SpringcloudConfig/. config-client-dev.properties file has a property:

foo = Foo Version 3

Startup program: Access Http://localhost:8888/foo/dev

1 {"name": "foo", "Profiles": ["Dev"], "label": "Master",2 "version": " 792ffc77c03f4b138d28e89b576900ac5e01a44b "," state ":null," propertysources ": []}

proves that the Configuration service center can obtain configuration information from remote programs.

The HTTP request address and resource file mappings are as follows:

    • /{application}/{profile}[/{label}]
    • /{application}-{profile}.yml
    • /{label}/{application}-{profile}.yml
    • /{application}-{profile}.properties
    • /{label}/{application}-{profile}.properties
Third, build a config client

Re-create a springboot project named Config-client, whose pom file:

1<?xml version= "1.0" encoding= "UTF-8"?>2<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"3xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >4<modelVersion>4.0.0</modelVersion>5  6<groupId>com.forezp</groupId>7<artifactId>config-client</artifactId>8<version>0.0.1-SNAPSHOT</version>9<packaging>jar</packaging>Ten   One<name>config-client</name> A<description>demo Project forSpring boot</description> -   -<parent> the<groupId>com.forezp</groupId> -<artifactId>sc-f-chapter6</artifactId> -<version>0.0.1-SNAPSHOT</version> -</parent> +   -<dependencies> +<dependency> A<groupId>org.springframework.boot</groupId> at<artifactId>spring-boot-starter-web</artifactId> -</dependency> -<dependency> -<groupId>org.springframework.cloud</groupId> -<artifactId>spring-cloud-starter-config</artifactId> -</dependency> in</dependencies> -   to<build> +<plugins> -<plugin> the<groupId>org.springframework.boot</groupId> *<artifactId>spring-boot-maven-plugin</artifactId> $</plugin>Panax Notoginseng</plugins> -</build> the   +   A</project>

Its configuration file bootstrap.properties:

1 spring.application.name=config-client2 spring.cloud.config.label=Master 3 spring.cloud.config.profile=Dev4 spring.cloud.config.uri= http://  localhost:8888/5 server.port=8881
    • Spring.cloud.config.label indicates the branch of the remote repository
    • Spring.cloud.config.profile

      • Dev Development environment configuration file
      • Test environment
      • Pro Formal environment
    • Spring.cloud.config.uri= http://localhost:8888/indicates the URL of the Configuration service center.

The entry class of the program, write an API interface "/hi", return the value of the Foo variable read from the configuration center, the code is as follows:

1 @SpringBootApplication2 @RestController3  Public classconfigclientapplication {4  5      Public Static voidMain (string[] args) {6Springapplication.run (configclientapplication.class, args);7     }8  9@Value ("${foo}")Ten String foo; One@RequestMapping (value = "/hi") A      PublicString hi () { -         returnfoo; -     } the}

Open URL Access: Http://localhost:8881/hi, page display:

Foo Version 3

This means that config-client gets the properties of Foo from Config-server, and Config-server is read from a git repository,

This article source code download:
Https://github.com/forezp/SpringCloudLearning/tree/master/sc-f-chapter6

Iv. references

Http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html

Thank you for your attention! Add QQ1 Group: 135430763,QQ2 Group: 454796847,QQ3 Group: 187424846. QQ Group into the group password: xttblog, want to add a group of friends, you can search: Xmtxtt, note: "Xttblog", add Assistant pull you into the group. Note errors do not agree with a friend application. Thanks again for your attention! Follow up with wonderful content will be sent to you the first time! Please send the original article to [email protected] email. Business cooperation can add assistants to communicate!

Amateur Grass Springcloud Tutorial | Sixth: Distributed Configuration Center (Spring Cloud Config) (Finchley version)

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.