Springcloud Tutorial VII: Highly Available Distributed Configuration Center (Springcloud Config)

Source: Internet
Author: User

In the previous article we described a service how to read a file from the configuration center, configuration Center How to read the configuration file from remote git, when a lot of service instances, all the files 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:

1: Create a new Eureka Server project, named Config-eureka-server, as the registry for the Configuration service. The pom file 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.spri Ngcloud</groupid> <artifactId>config-eureka-server</artifactId> <version>0.0.1-snapshot& lt;/version> <packaging>jar</packaging> <name>config-eureka-server</name> <desc Ription>demo Project for Spring boot</description> <parent> <groupid>org.springframework .boot</groupid> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9 .

    Release</version> <relativePath/> <!--lookup parent from repository to </parent> <properties> &LT;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.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency&
            Gt <groupId>org.springframework.cloud</groupId> <artifactid>spring-cloud-starter-eureka-server& Lt;/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</artif Actid> <version>${spring-cloud.version}</version> <type>pom</type&
                Gt <scope>import</scope> </dependency> </dependencies> </dependencymanagem ent> <build> <plugins> <plugin> <groupid>org.springfra Mework.boot</groupid> <artifactId>spring-boot-maven-plugin</artifactId> <
 /plugin> </plugins> </build> </project>

The entry class joins the corresponding annotations:

@EnableEurekaServer
@SpringBootApplication Public
class Configeurekaserverapplication {public

    static void Main (string[] args) {

        springapplication.run (configeurekaserverapplication.class, args);
    }
}

Modify the configuration file as follows:

server.port=8889
eureka.instance.hostname=localhost
#配置成服务
eureka.client.registerwitheureka= false
eureka.client.fetchregistry=false
eureka.client.service-url.defaultzone= http://${ eureka.instance.hostname}:${server.port}/eureka/

2: Modify the previous Config-server project
Add the required dependencies in the POM;

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

To modify a configuration file:

Spring.application.name=config-server
server.port=8888


#配置文件在本地
#spring. profiles.active=native
#配置文件的目录
#spring. cloud.config.server.native.search-locations=d:/workspace/github/spring-config/ Config-file

#spring. cloud.config.server.git.uri=https://github.com/forezp/springcloudconfig/
Spring.cloud.config.server.git.uri=https://github.com/iot-wangshuyu/springcloud
#配置仓库路径
spring.cloud.config.server.git.searchpaths=springcloudconfig/respo/
#配置仓库的分支
Spring.cloud.config.label=master
#访问git仓库的用户名
spring.cloud.config.server.git.username=
# User password to access git repository
spring.cloud.config.server.git.password=
#注册服务地址
eureka.client.service-url.defaultzone=http://localhost:8889/eureka/

To modify the Startup class:

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient Public
class configserverapplication {public

    static void Main (string[] args) {

        Springapplication.run ( Configserverapplication.class, args);
    }
}

3: Modify the Config-client project

Add the appropriate dependencies in the POM:

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId> spring-cloud-starter-config</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>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

Modifying the profile name is called: bootstrap.properties
Content modified to:

#服务名称
spring.application.name=config-client
#仓库分支
spring.cloud.config.label=master
# Dev development Environment Profile
#test测试环境
#pro正式环境
spring.cloud.config.profile=dev
#使用eureka Server needs to be commented out
# spring.cloud.config.uri= http://localhost:8888/

#服务注册地址
eureka.client.service-url.defaultzone=http:// localhost:8889/eureka/
#从远程获取配置文件
spring.cloud.config.discovery.enabled=true
#订阅的服务名称
Spring.cloud.config.discovery.service-id=config-server
server.port=8881

If the configuration service deploys multiple copies, it is highly available through load balancing.
Start the program in turn, config-eureka-server,config-server,config-client, browser access: http://localhost:8889/can view the service registration situation;
Browser access: Http://localhost:8881/hi; returns the Foo attribute in the configuration file;
Description of the highly available distributed Configuration Center configuration succeeded

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.