在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在 Spring Cloud 中,有分布式配置中心组件 Spring Cloud Config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程 Git 仓库中。在 Spring Cloud Config 组件中,分两个角色,一是 Config Server,二是 Config Client。
这是一个分布式配置文件的中心化管理系统
前提工作:存在eureka 服务端
1.构建项目 itoken-config
文件目录:
2.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>com.pengt</groupId> <artifactId>itoken-dependencies</artifactId> <version>1.0.0-SNAPSHOT</version> <relativePath>../itoken-dependencies/pom.xml</relativePath> </parent> <artifactId>itoken-config</artifactId> <packaging>jar</packaging> <name>itoken-config</name> <dependencies> <!-- Spring Boot Begin --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Spring Boot End --> <!-- Spring Cloud Begin --> <!--分布式配置中心--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!-- Spring Cloud End --> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.pengt.itoken.config.ItokenConfigApplication</mainClass> </configuration> </plugin> </plugins> </build> </project>
application.yml
spring: application: name: itoken-config cloud: config: label: master //master分支 server: git: uri: https://github.com/13671664132/center-config search-paths: respo username: //github的用户名 password: //密码 server: port: 8888 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
ItokenConfigApplication
@SpringBootApplication @EnableEurekaClient @EnableConfigServer public class ItokenConfigApplication { public static void main(String[] args) { SpringApplication.run(ItokenConfigApplication.class,args); } }
3. 测试服务功能,要求通过服务访问到git上的配置文件
1.在 https://github.com/13671664132/center-config/respo 路径下上传属性文件itoken-zuul.yml
2.启动eureka服务端,启动config服务
3.访问 http://localhost:8888/itoken-zuul/master
可以通过config服务读出github的分支上的属性文件,说明分布式配置中心的服务端已经搭建成功,
以后相关服务的属性文件可以直接维护在git上,做到统一管理。