简介
在分布式系统中,由于服务数量非常多,配置文件分散在不同的微服务项目中,管理不方便。为了方便配置文件集 中管理,需要分布式配置中心组件。在Spring Cloud中,提供了Spring Cloud Config,它支持配置文件放在配置服 务的本地,也支持放在远程Git仓库(GitHub、码云)。 使用Spring Cloud Config配置中心后的架构如下图
Git配置管理
知名的Git远程仓库有国外的GitHub和国内的码云(gitee);但是使用GitHub时,国内的用户经常遇到的问题是访 问速度太慢,有时候还会出现无法连接的情况。如果希望体验更好一些,可以使用国内的Git托管服务——码云 (gitee.com)。 与GitHub相比,码云也提供免费的Git仓库。此外,还集成了代码质量检测、项目演示等功能。 对于团队协作开发,码云还提供了项目管理、代码托管、文档管理的服务。本章中使用的远程Git仓库是码云。 码云访问地址:https://gitee.com/
1.首先要使用码云上的私有远程git仓库需要先注册帐号;请先自行访问网站并注册帐号,然后使用帐号登录码云控制 台并创建公开仓库。
3.2.3. 创建配置文件 在新建的仓库中创建需要被统一配置管理的配置文件。 配置文件的命名方式:{application}-{profile}.yml 或 {application}-{profile}.properties application为应用名称 profile用于区分开发环境,测试环境、生产环境等 如user-dev.yml,表示用户微服务开发环境下使用的配置文件。 这里将user-service工程的配置文件application.yml文件的内容复制作为user-dev.yml文件的内容,具体配置如 下:
搭建配置中心微服务
创建配置中心微服务工程
添加依赖,修改 config-server\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">
<parent>
<artifactId>Spring-cloud</artifactId>
<groupId>com.lmy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
启动类
创建配置中心工程 config-server 的启动类; config-server\src\main\java\com\lxs\config\ConfigServerApplication.java 如下
package com.lxs.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer //开启配置服务 public class
ConfigServerApplication {
public static void main(String[] args)
{ SpringApplication.run(ConfigServerApplication.class, args);
}
}
application.yml
server:
port: 10088
spring:
application:
name: eureka-server
eureka:
client:
service-url:
defaultZone: HTTP://127.0.0.1:10088/eureka
register-with-eureka: false
fetch-registry: false
启动eureka注册中心和配置中心;然后访问http://localhost:12000/user-dev.yml ,查看能否输出在码云存储管理 的user-dev.yml文件。并且可以在gitee上修改user-dev.yml然后刷新上述测试地址也能及时到最新数据。