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

Source: Internet
Author: User

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 of the previous article, create a Eureka-server project, and use it as a service registration center.

In its pom.xml file introduced Eureka's start-dependent spring-cloud-starter-netflix-eureka-server, the code 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-chapter7</artifactId> -<version>0.0.1-SNAPSHOT</version> -</parent> +   -<dependencies> +<dependency> A<groupId>org.springframework.cloud</groupId> at<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> -</dependency> -<dependency> -<groupId>org.springframework.boot</groupId> -<artifactId>spring-boot-starter-web</artifactId> -</dependency> in<dependency> -<groupId>org.springframework.cloud</groupId> to<artifactId>spring-cloud-config-server</artifactId> +</dependency> -   the</dependencies> *<build> $<plugins>Panax Notoginseng<plugin> -<groupId>org.springframework.boot</groupId> the<artifactId>spring-boot-maven-plugin</artifactId> +</plugin> A</plugins> the</build> +</project>
    • On the profile application.yml, specify a service port of 8889, plus the basic configuration as the service registry, with the following code:
1 Server:2port:88893  4 Eureka:5 instance:6 Hostname:localhost7 Client:8Registerwitheureka:false9Fetchregistry:falseTen serviceurl: OneDefaultzone:http://${eureka.instance.hostname}:${server.port}/eureka/

Entry class:

1 @EnableEurekaServer 2 @SpringBootApplication 3  Public class eurekaserverapplication {4  5      Public Static void Main (string[] args) {6         Springapplication.run (eurekaserverapplication.  Class, args); 7     }8 }
Second, the transformation of Config-server

In its pom.xml file plus Eurekaclient's start dependent spring-cloud-starter-netflix-eureka-client, the code is as follows:

1<dependencies>2<dependency>3<groupId>org.springframework.cloud</groupId>4<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>5</dependency>6<dependency>7<groupId>org.springframework.boot</groupId>8<artifactId>spring-boot-starter-web</artifactId>9</dependency>Ten<dependency> One<groupId>org.springframework.cloud</groupId> A<artifactId>spring-cloud-config-server</artifactId> -</dependency> -</dependencies>

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

1spring.application.name=config-Server2server.port=88883  4Spring.cloud.config.server.git.uri=https://github.com/forezp/springcloudconfig/5spring.cloud.config.server.git.searchpaths=Respo6Spring.cloud.config.label=Master7Spring.cloud.config.server.git.username=your username8spring.cloud.config.server.git.password=Your password9Eureka.client.serviceurl.defaultzone=http://localhost:8889/eureka/

Finally, you need to add @enableeureka annotations to the program's startup class application.

Iii. Transformation of 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-netflix-eureka-client, the code is as follows:

1<dependencies>2<dependency>3<groupId>org.springframework.cloud</groupId>4<artifactId>spring-cloud-starter-config</artifactId>5</dependency>6  7<dependency>8<groupId>org.springframework.boot</groupId>9<artifactId>spring-boot-starter-web</artifactId>Ten</dependency> One   A<dependency> -<groupId>org.springframework.cloud</groupId> -<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> the</dependency> -</dependencies>

Configuration file bootstrap.properties, note is bootstrap. Plus the service registration address is http://localhost:8889/eureka/

1spring.application.name=config-Client2Spring.cloud.config.label=Master3spring.cloud.config.profile=Dev4#spring. cloud.config.uri= http://localhost:8888/5  6Eureka.client.serviceurl.defaultzone=http://localhost:8889/eureka/7Spring.cloud.config.discovery.enabled=true8spring.cloud.config.discovery.serviceid=config-Server9server.port=8881
    • 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 Eureka-servr,config-server,config-client in turn
Visit URL: http://localhost:8889/

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

Foo Version 3

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

Iv. references

Spring_cloud_config

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 | Seventh: Highly Available 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.