Springcloud Seventh: Highly Available Distributed Configuration Center (Spring Cloud Config)

Source: Internet
Author: User

Objective:

Must learn to springboot basic knowledge

Brief introduction:

Spring Cloud provides developers with tools to quickly build distributed systems, including configuration management, service discovery, circuit breakers, routing, micro-proxies, event busses, global locks, decision-making campaigns, distributed sessions, and more. It runs in a simple environment and can run on a developer's computer.

Tools:

JDK8

apache-maven-3.5.2

IntelliJ Idea 2018.1 x64

The previous article described how a service reads a file from the configuration center, how the configuration center reads the configuration file from a remote git, and when the service instance is many, reads the file from the configuration center, you can consider the configuration center as a micro-service, clustering it to achieve high availability, the frame composition is as follows:

First, the preparatory work

Continue with the project for the previous article, create a Eureka-config-server-cluster project, and use it as a service registry.

The Pom.xml File introduces Eureka to the start-dependent spring-cloud-starter-eureka-server, the code is as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0/http Maven.apache.org/xsd/maven-4.0.0.xsd "&GT;&LT;MODELVERSION&GT;4.0.0&LT;/MODELVERSION&GT;&LT;GROUPID&GT;COM.LWC </groupid><artifactid>eureka-config-server-cluster</artifactid><version>0.0.1-snapshot </version><packaging>jar</packaging><name>eureka-config-server-cluster</name> <description>demo Project for Spring boot</description><parent><groupid> org.springframework.boot</groupid><artifactid>spring-boot-starter-parent</artifactid>< Version>1.5.10.release</version><relativepath/> <!--lookup parent from repository--></ parent><properties><project.build.sourceencoding>utf-8</project.build.sourceencoding>< Project.reporting.outputencoding>utf-8</project.reporting.outputencoding><java.version>1.8</java.version>< spring-cloud.version>edgware.sr3</spring-cloud.version></properties><dependencies>< Dependency><groupid>org.springframework.cloud</groupid><artifactid> Spring-cloud-starter-eureka-server</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-test</artifactid><scope>test</scope></dependency></dependencies ><dependencymanagement><dependencies><dependency><groupid>org.springframework.cloud </groupid><artifactid>spring-cloud-dependencies</artifactid><version>${ Spring-cloud.version}</version><type>pom</type><scope>impoRt</scope></dependency></dependencies></dependencymanagement><build><plugins ><plugin><groupId>org.springframework.boot</groupId><artifactId> Spring-boot-maven-plugin</artifactid></plugin></plugins></build></project>

In Application-dev.yml, specify a service port of 8889, configure the service registry, with the following code:

Server:  Port:8889eureka:  instance:    hostname:localhost  client:    registerwitheureka:false    fetchregistry:false    serviceurl:      defaultzone:http://${eureka.instance.hostname}:${server.port}/ eureka/

Startup class:

Package Com.lwc;import Org.springframework.boot.springapplication;import Org.springframework.boot.autoconfigure.springbootapplication;import org.springframework.cloud.netflix.eureka.server.enableeurekaserver;/** * @author Eddie */@ Enableeurekaserver@springbootapplicationpublic class Eurekaconfigserverclusterapplication {public    static void Main (string[] args) {        springapplication.run (eurekaconfigserverclusterapplication.class, args);}    }

Ii. Modification of Eureka-config-server

The Pom.xml File introduces Eureka to the start-dependent spring-cloud-starter-eureka-server, the code is as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0 Http://maven.apache . org/xsd/maven-4.0.0.xsd "> <modelVersion>4.0.0</modelVersion> <groupid>com.lwc</groupid > <artifactId>eureka-config-server</artifactId> <version>0.0.1-SNAPSHOT</version> < Packaging>jar</packaging> <name>eureka-config-server</name> <description>demo Project        For Spring boot</description> <parent> <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> <relativePath/> <!--lookup parent from repository to </parent> <properties> &lt ;p roject.build.sourceencoding>utf-8</Project.build.sourceencoding> <project.reporting.outputencoding>utf-8</ Project.reporting.outputencoding> <java.version>1.8</java.version> &LT;SPRING-CLOUD.VERSION&G T            edgware.sr3</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactid>spring-cloud-config-server</ar tifactid> </dependency> <dependency> <groupid>org.springframework.boot</g Roupid> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope > </dependency> <dependency> <groupid>org.springframework.cloud</groupid&            Gt <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> </dependencies> &L T;dependencymanagement> <dEpendencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.ver            Sion}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins&            Gt <plugin> <groupId>org.springframework.boot</groupId> &LT;ARTIFACTID&GT;SPR ing-boot-maven-plugin</artifactid> </plugin> </plugins> </build></project& Gt

Configuration file Application-dev.yml, specify the service registration address is http://localhost:8889/eureka/, the other configuration of the same article, the complete configuration is as follows:

Server:  Port:8888eureka:  client:    serviceurl:      defaultzone:http://localhost:8889/eureka/spring:  Cloud:    config:      server:        git:          uri:https://github.com/eddie-code/springclouddemo          Search-paths:config-repo          Username:          Passphrase:      label:master

and start the class to add annotations for @enableconfigserver and @enableeurekaclient

Third, modify the Eureka-config-client

To register it to the service registry as a Eureka client, the Pom file is required plus a start dependent Spring-cloud-starter-eureka, the code is as follows:

<?xml version= "1.0" encoding= "UTF-8"? ><project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http ://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" http://maven.apache.org/POM/4.0.0 Http://maven.apache . org/xsd/maven-4.0.0.xsd "> <modelVersion>4.0.0</modelVersion> <groupid>com.lwc</groupid > <artifactId>eureka-config-client</artifactId> <version>0.0.1-SNAPSHOT</version> < Packaging>jar</packaging> <name>eureka-config-client</name> <description>demo Project        For Spring boot</description> <parent> <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> <relativePath/> <!--lookup parent from repository to </parent> <properties> &lt ;p roject.build.sourceencoding>utf-8</Project.build.sourceencoding> <project.reporting.outputencoding>utf-8</ Project.reporting.outputencoding> <java.version>1.8</java.version> &LT;SPRING-CLOUD.VERSION&G T            edgware.sr3</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactid>spring-cloud-starter-config</a rtifactid> </dependency> <dependency> <groupid>org.springframework.boot</ Groupid> <artifactId>spring-boot-starter-web</artifactId> </dependency> < Dependency> <groupId>org.springframework.cloud</groupId> <artifactid>spring-clo ud-starter-eureka</artifactid> </dependency> <dependency> <groupid>org.sp Ringframework.boot</groupid> <artiFactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency&gt    ;                </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> &LT;ARTIFACTID&GT;SPRING-CLOUD-DEPENDENCIES&L T;/artifactid> <version>${spring-cloud.version}</version> <type>pom<    /type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupid            >org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId>            </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>spring milestones</name> <url>https://repo.sprin g.io/milestone</url> <snapshots> <enabled>false</enabled> &lt ;/snapshots> </repository> </repositories></project>

Configuration file Bootstrap-local.yml, note is bootstrap. Plus the service registration address is http://localhost:8889/eureka/

Server:  Port:8881eureka:  client:    serviceurl:      defaultzone:http://localhost:8889/eureka/spring:  Cloud:    config:      label:master      profile:local      discovery:        serviceid:eureka-config-server        Enabled:true

Spring.cloud.config.discovery.enabled is to read files from the configuration center.

Spring.cloud.config.discovery.serviceId the Servieid of the configuration Center, which is the service name.

It is found that the read configuration file no longer writes the IP address, but the service name, at this time if the configuration service deployed multiple copies, through load balancing, thereby high availability.

Start in sequence

    1. Eureka-config-server-cluster
    2. Eureka-config-server
    3. Eureka-config-client

Visit URL: http://localhost:8889/

To access Http://localhost:8881/configs, the browser displays:

This is local config blog:http://www.cnblogs.com/eddieblog/

Iv. Source code Download

Label 7-1

Https://github.com/eddie-code/SpringCloudDemo/tree/master/eureka-config-server-cluster

V. The pits that have been trampled

Pay attention to annotations

VI. Understanding

If you have done the classmate should be found that we are a new registry, and then configure the server and configure the client to register together to the center, the rest of the same as the first one, the specific understanding or look at the individual, master with the introduction, practice on their own!

Springcloud Seventh: Highly Available Distributed Configuration Center (Spring Cloud Config)

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.