SpringCloud学习十(SpringCloud微服务之Config分布式配置中心)

本文详细介绍了SpringCloud Config的使用,包括Config Server的搭建,通过Git仓库管理配置,Config Client的配置,以及如何开启手动刷新配置功能。读者将学习到如何创建Git仓库,配置ConfigServer和ConfigClient,以及实现实时更新配置。
摘要由CSDN通过智能技术生成

一、SpringCloud Config简介

Spring Cloud Config为分布式系统中的配置管理提供服务器和客户端支持。它包含两部分:

  • Config Server:是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置,默认使用Git存储配置文件内容,也可以使用SVN存储,或者是本地文件存储。
  • Config Client:是Config Server的客户端,用于操作存储在Config Server中的配置内容。微服务在启动时Config Client会请求Config Server以获取配置文件的内容,请求到后再启动容器并应用配置。
    在这里插入图片描述

二、统一配置管理实现

1、创建git仓库(gitee)

在这里插入图片描述
创建一个文件夹
作为Git仓库,并添加一个配置文件,可以直接复制user-service工程中的配置文件
在这里插入图片描述

使用命令推送到git仓库

git init
git add .
git commit -m "初始化配置文件"
git remote add origin 仓库地址
git push -u origin master

2、搭建ConfigServer工程

(1)、引入依赖
 <!--eureka依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--config-server依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
(2)、创建启动类并添加注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class, args);
    }
}
(3)、添加配置文件(application.yml文件中)
server:
  port: 12580
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/Rrx-w/cloud-config.git  # git仓库地址
#          username:   # 如果有用户名和密码需要加上
#          password:
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka  # eureka地址
(4)、测试

访问:http://localhost:12580/userservice-dev.yml
访问规范

/{application}/{profile}[/{label}]
/{application}-{profile}.yml   # 没写lable 默认就是master分支
/{label}/{application}-{profile}.yml

在这里插入图片描述

3、配置ConfigClient

引入依赖(在客户端)
在user-service服务中引入config依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

添加配置文件
SpringBoot中提供了一个名为bootstrap.yml的文件,加载顺序比application.yml优先,重命名application.yml文件为application.yml.bak使其失效
创建bootstrap.yml文件

eureka:
  client:
    service-url:
      #      defaultZone: http://localhost:10086/eureka,http://localhost:10087/eureka
      defaultZone: http://localhost:10086/eureka
spring:
  application:
    name: user-service
  cloud:
    config:
      name: userservice  # 配置文件的应用名称
      label: master   # 配置文件的分支名称
      profile: dev   # 环境名称
      discovery:
        enabled: true  # 开启从注册中心获取配置中心地址
        service-id: config-server # 配置中心的服务名称

定义属性读取类

@Data
@Component
@ConfigurationProperties(prefix = "test.redis")
public class RedisProperties {
    private String host;
    private int port;
}

测试:定义一个controller类

@RestController
@RequestMapping("hello")
public class HelloController {

    @Autowired
    private RedisProperties properties;

    @GetMapping("prop")
    public RedisProperties getProperties(){
        return properties;
    }
}

集成测试
修改git仓库中,master分支的userservice-dev.yml文件

test:
  redis:
    host: localhost
    port: 6380

访问:http://localhost:8081/hello/prop
在这里插入图片描述

4、开启手动刷新功能

user-service微服务中引入依赖

<!-- springboot提供的监控依赖,可以查看当前springboot的一些信息
/health  /info  /up   /refresh:刷新配置  -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

user_service的 bootstrap.yml 文件中添加配置,暴露接口

management:
  endpoints:
    web:
      exposure:
        include: "*" # 暴露refresh接口

测试

  1. 重启user-service测试

  2. 修改git中的配置文件
    在这里插入图片描述

  3. 先访问http://localhost:8081/actuator/refresh,在刷新页面, 端口号变为7777
    在这里插入图片描述
    在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值