Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持,配置服务中心采用Git的方式存储配置文件,
因此我们很容易部署修改,有助于对环境配置进行版本管理。
一、配置中心
在根目录spring_cloud中创建Maven Moudle模块:config-server
pom.xml
-
<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.sam</groupId> -
<version>0.0.1</version> -
</parent> -
<modelVersion>4.0.0</modelVersion> -
<artifactId>config-server</artifactId> -
<dependencies> -
<dependency> -
<groupId>org.springframework.cloud</groupId> -
<artifactId>spring-cloud-config-server</artifactId> -
</dependency> -
</dependencies> -
</project>
创建消费者服务启动类:ConfigApplication
-
package com.sam.config.server; -
import org.springframework.boot.SpringApplication; -
import org.springframework.boot.autoconfigure.SpringBootApplication; -
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; -
import org.springframework.cloud.config.server.EnableConfigServer; -
/** -
* @ClassName: ConfigApplication -
* @Description: 配置中心服务 -
* @author sam -
* @date 2018年8月10日 下午3:49:01 -
*/ -
@SpringBootApplication -
@EnableConfigServer -
@EnableDiscoveryClient -
public class ConfigApplication { -
public static void main(String[] args) { -
SpringApplication.run(ConfigApplication.class, args); -
} -
}
创建配置文件:application.yml
-
server: -
port: 8030 -
eureka: -
client: -
serviceUrl: -
defaultZone: http://localhost:8010/eureka/ #eureka服务注册地址 -
# git管理配置 -
spring: -
cloud: -
config: -
server: -
git: -
uri: https://github.com/vtopqx/config/ #git仓库地址 -
searchPaths: demo* #搜索路径 -
# username: username -
# password: password -
application: -
name: config-server -
management: -
security: -
enabled: false #是否开启actuator安全认证
在配置的git仓库下新建一个demo1的文件夹,在里面创建一个叫client-a-dev.properties的配置文件
文件中随便加上两个配置
本文详细介绍如何使用Spring Cloud Config构建配置中心,并通过Git管理配置文件。文章包括配置中心服务的搭建步骤,从创建Maven模块到配置YAML文件,再到启动消费者服务。
167万+

被折叠的 条评论
为什么被折叠?



