Spring Cloud配置中心搭建(集成Git)
1. 在Github(或其他)创建配置中心仓库bounter-config-repo,然后在仓库创建两个配置文件:simon.properties、susan.properties,链接如下:
https://github.com/13babybear/bounter-config-repo
2. 基于SpringBoot创建配置服务器项目bounter-config-server,pom文件如下:
<?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>cn.bounter</groupId> <artifactId>bounter-config-server</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>bounter-config-server</name> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> <spring-cloud.version>Finchley.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-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> </build> </project>
3. 添加Git仓库配置,application.properties 如下:
server.port=8888
## Git Repo
spring.cloud.config.server.git.uri=https://github.com/13babybear/bounter-config-repo
# Github账号
spring.cloud.config.server.git.username=13babybear
spring.cloud.config.server.git.password=******
4. 启动类上添加注解 @EnableConfigServer,如下:
@SpringBootApplication @EnableConfigServer public class ConfigApplication { public static void main(String[] args) { SpringApplication.run(ConfigApplication.class, args); } }
5. 启动服务器并验证,浏览器输入:http://localhost:8888/simon/name,返回结果如下:
{ name: "simon", profiles: [ "name" ], label: null, version: "87209a5512d67f9e04496df30c5843b8d8274715", state: null, propertySources: [ { name: "https://github.com/13babybear/bounter-config-repo/simon.properties", source: { name: "simon" } } ] }
是不是觉得很简单啊,那就赶快自己动手试试吧!源代码见Github:
https://github.com/13babybear/bounter-config-server