目录
在本文中,将会介绍如何使用SpringCloud+Git搭建一个分布式配置中心。拓扑图如下:
(图片来自《SpringCloud微服务实战》)
1、搭建远程Git仓库
在Git上面创建一个仓库名为springcloud_config,并在仓库中创建一个git-config-repo目录,在目录中准备好两个配置文件。liuzhoujian-prd.properties和liuzhoujian-test.properties,这两个配置文件的内容分别是name=liuzhoujian-prd.properties和name=liuzhoujian-test.properties。
注意:git仓库中文件命名规则如下:{application}-{profile}.yml或者{application}-{profile}.properties,这里的{application}对应config client的{spring.application.name}属性,两者需保持一致!{profile}代表不同的环境,有test(测试环境)、prd(生成环境)、pre(预生产环境)、dev(开发环境)等。
2、搭建注册中心Eureka Server
2.1 pom.xml
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lzj</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</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>
</bu