配置中心简介
Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持。配置服务器为各应用的所有环境提供了一个中心化的外部配置。它实现了对服务端和客户端对Spring Environment和PropertySource抽象的映射,所以它除了适用于Spring构建的应用程序,也可以在任何其他语言运行的应用程序中使用。作为一个应用可以通过部署管道来进行测试或者投入生产,我们可以分别为这些环境创建配置,并且在需要迁移环境的时候获取对应环境的配置来运行
配置中心原理
注册用户:
进入 https://github.com 注册用户。
创建项目:
创建成功,点击进入
添加文件:
启用配置中心
Main类:
@SpringBootApplication
public class ConfigMain {
public static void main(String[] args) {
SpringApplication.run(ConfigMain.class, args);
}
}
文件 引入依赖:
网址:https://cloud.spring.io/spring-cloud-static/Edgware.SR5/single/spring-cloud.html#_spring_cloud_config
依赖:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
配置:
@SpringBootApplication
@EnableConfigServer
public class ConfigServer {
public static void main(String[] args) {
SpringApplication.run(ConfigServer.class, args);
}
}
配置存储库: application。
server:
port: 1111
spring:
cloud:
config:
server:
git:
uri: https://github.com/346913/yudu
测试:
浏览器格式:{[/{name}-{profiles}.properties],methods=[GET]}"
浏览器查询:http://localhost:1111/userservice-dev.properties (用properties 取得properties 格式显示)
{[/{name}-{profiles}.yml || /{name}-{profiles}.yaml],methods=[GET]}
http://localhost:1111/userservice-dev.yml (用yml 取得yml 格式显示)
bootstrap.yml (以后所有配置中都不会有application.yml文件了,都用bootstrap.yml代替。)
spring:
application:
name: userservice
profiles:
active: dev
#配置中心的位置ip和端口
#http://localhost:1111/userservice-dev.properties
cloud:
config:
uri: http://localhost:1111
默认不会自动调用,要加入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
运行:localhost:1111/userservice/dev