概述
spring cloud config 配置中心配置中心分为 server 服务端 client 和客户端,
Spring Cloud Config 项目:
1.提供服务端和客户端支持。
2.集中式管理分布式环境下的应用配置。
3.基于Spring 环境,无缝与Spring应用集成。
4.可用于 任何 语言开发的程序。
5.默认实现基于git仓库,可以进行版本管理。
6.可替换 自定义实现。
Spring Cloud Config Server作为配置中心服务端:
1.拉取配置时更新git仓库副本,保证是最新结果。
2.支持数据结构丰富,yml,json,properties 等。
3.配合eureke可实现服务发现,配合cloud bus可实现配置推送更新。
4.配置存储基于git仓库,可进行版本管理。
5.简单可靠,有丰富的配套方案。
Spring Cloud Config Client 默认客户端实现:
SpringBoot 项目不需要改动任何代码,加入一个启动配置文件指明使用 ConfigServer 上哪个配置文件即可。
创建工程microservice-config-server-eureka
pom.xml
microservice-config-server-eureka/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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.itmuch.cloud</groupId>
<artifactId>microservice-spring-cloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>microservice-config-server-eureka</artifactId>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 8080
spring:
cloud:
config:
server:
git:
uri: https://git.oschina.net/it-much/config-repo-51cto-video
username:
password:
application:
name: microservice-config-server-eureka
eureka:
client:
serviceUrl:
defaultZone: http://user:password123@localhost:8761/eureka
instance:
prefer-ip-address: true
说明:
1)通过账号密码的链接到远程配置;
ConfigServerApplication
com.itmuch.cloud.ConfigServerApplication
package com.itmuch.cloud;
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;
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
说明:
1)在启动类上添加注解@EnableConfigServer;
2)spring boot 启动类,注意spring boot 的启动类需要放到某一个包下,不能直接在 src 目录下;
测试
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
说明:
1){application} 匹配客户端的 spring.application.name
2){profile} 匹配客户端的 spring.active.profiles
3){label} 如果是git,则匹配的是分支,如果是svn匹配 trunk/branchs等.
http://localhost:8080/abc-default.properties
http://localhost:8080/master/abc-default.properties
相关文章
1)https://blog.csdn.net/xiaojianhx/article/details/73695421
2)spring cloud config 用svn做配置文件仓库
3)springcloud-config配置中心的安全配置
4)https://blog.csdn.net/Yoara/article/details/54926407
==============================
QQ群:143522604
群里有相关资源
欢迎和大家一起学习、交流、提升!
==============================