Spring Cloud 之 Config 分布式配置中心

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:

  1. 导入 starter-config依赖
  2. 配置config server 地址,读取配置文件名称等信息
  3. 获取配置值
  4. 启动测试

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值