Springcloud Tutorials | 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:

<?xml version= "1.0" encoding= "UTF-8"?><Projectxmlns="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.forezp</Groupid><Artifactid>config-server</Artifactid><Version>0.0.1-snapshot</Version><Packaging>jar</Packaging><Name>config-server</Name><Description>demo Project for Spring Boot</Description><Parent><Groupid>com.forezp</Groupid><Artifactid>sc-f-chapter7</Artifactid><Version>0.0.1-snapshot</Version></Parent><Dependencies><Dependency><Groupid>org.springframework.cloud</Groupid><Artifactid>spring-cloud-starter-netflix-eureka-client</Artifactid></Dependency><Dependency><Groupid>org.springframework.boot</Groupid><Artifactid>spring-boot-starter-web</Artifactid></Dependency><Dependency><Groupid>org.springframework.cloud</Groupid><Artifactid>spring-cloud-config-server</Artifactid></Dependency></dependencies> <build> <plugins> < plugin> <groupid>org.springframework.boot </groupid> < artifactid>spring-boot-maven-plugin</ artifactid> </plugin> </plugins> </ 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:

server:  port: 8889eureka:  instance:    hostname: localhost  client:    registerWithEureka: false    fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

Entry class:

@EnableEurekaServer@SpringBootApplicationpublic class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); }}
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:

<Dependencies><Dependency><Groupid>org.springframework.cloud</Groupid><Artifactid>spring-cloud-starter-netflix-eureka-client</Artifactid></Dependency><Dependency><Groupid>org.springframework.boot</groupid> < artifactid>spring-boot-starter-web</ artifactid> </dependency> Span class= "Hljs-tag" ><dependency> < groupid>org.springframework.cloud</ groupid> <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:

Spring. Application. name=config-serverserver. port=8888spring. Cloud. config. server. git. Uri=https://github. com/forezp/springcloudconfig/spring. Cloud. config. server.git.searchpaths=respospring.cloud.config.label= Masterspring.cloud.config .server.git.cloud.config.server.git.password = Your Passwordeureka.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:

<Dependencies><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> Span class= "Hljs-tag" ><dependency> < groupid>org.springframework.cloud</ groupid> <artifactId> Spring-cloud-starter-netflix-eureka-client</artifactId> Span class= "Hljs-tag" ></dependency> </DEPENDENCIES>          

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

Spring. Application. name=config-clientspring. Cloud. config. label=masterspring.cloud.config #spring. cloud.config.uri= Http://localhost:8888/eureka.client.serviceurl8889/eureka/spring.config.discovery .enabled=truespring.cloud .config.discovery .serviceid=config-serverserver.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

Original address: Http://blog.csdn.net/forezp. 81041045

Springcloud Tutorials | 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.