Config概述
Config 快速入门
config server:
- 使用gitee创建远程仓库,上传配置文件 (config-dev.yml)
- 搭建 config server 模块
- 导入 config-server 依赖
- 编写配置,设置 gitee 远程仓库地址
- 测试访问远程配置文件
配置文件(config-dev.yml):
pom文件:
<?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>config-parent</artifactId>
<groupId>com.itheima</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config-server</artifactId>
<dependencies>
<!-- config-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
</project>
配置文件application.yml:
server:
port: 9527
spring:
application:
name: config-server
# spring cloud config
cloud:
config:
server:
# git 的 远程仓库地址
git:
# 公有的不需要密码
uri: https://gitee.com/xiong_guoliang/spring-cloud-config.git
# username
# password
label: master # 分支配置
启动类:
package com.itheima.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer // 启用config server功能
public class ConfigServerApp {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApp.class,args);
}
}
测试访问:http://localhost:9527/master/config-dev.yml
config client:
- 导入 starter-config依赖
- 配置config server 地址,读取配置文件名称等信息
- 获取配置值
- 启动测试
starter-config依赖:
<!--config client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
配置文件bootstrap.yml(作用同application.yml,但优先级更高):
# 配置config-server地址
# 配置获得配置文件的名称等信息
spring:
cloud:
config:
# 配置config-server地址
uri: http://localhost:9527
# 配置获得配置文件的名称等信息
name: config # 文件名
profile: dev # profile指定, 由name和profile自动寻找config-dev.yml
label: master # 分支
获取配置值:
@Value("${itheima}")
Config 客户端刷新
Config 集成 Eureka
# 配置config-server地址
# 配置获得配置文件的名称等信息
spring:
cloud:
config:
# 配置config-server地址
# uri: http://localhost:9527
# 配置获得配置文件的名称等信息
name: config # 文件名
profile: dev # profile指定, 由name和profile自动寻找config-dev.yml
label: master # 分支
# 从注册中心寻找config-server地址
discovery:
enabled: true
service-id: CONFIG-SERVER
# 暴露端点
management:
endpoints:
web:
exposure:
include: 'refresh'
server:
port: 9527
spring:
application:
name: config-server
# spring cloud config
cloud:
config:
server:
# git 的 远程仓库地址
git:
#共有的不需要密码
uri: https://gitee.com/xiong_guoliang/spring-cloud-config.git
# username
# password
label: master # 分支配置
# 将自己注册到eureka中
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka