spring cloud 2.x版本 Config配置中心教程

本文详细介绍了如何搭建Spring Cloud Config的配置中心,包括创建config-server和config-client,配置git仓库,动态刷新配置等步骤。文章还提到了通过Webhooks和Spring Cloud Bus实现配置的自动更新,并提供了代码示例。
摘要由CSDN通过智能技术生成

前言

本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3

本文基于前面的文章eureka-server的实现。

参考

- eureka-server

概述

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,所以需要分布式配置中心组件。Spring Cloud Config为分布式系统中的外部化配置提供服务器和客户端支持。

本篇涉及项目的结构为一个Config Server单机模式和适用于链接Git仓库,一个Config Client通过server来展示配置文件数据。 同时使用两个bus来模拟配置文件刷新。

创建config-server工程

Config-server功能

  • 用于外部配置的http,基于资源的api
  • 加密和解密属性
  • 使用可嵌入spring boot的应用程序

1.1 创建配置中心server:config-server

1.2 添加config-server的pom.xml相关依赖

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

1.3 添加config-server的application添加配置信息

#加载本地文件配置
spring:
  application:
    name: config-server
  profiles:
    active: native #加载本地配置
  cloud:
    config:
      server:
        native: #加载本地目录文件
          search-locations: /Users/xxxx/config-server

server:
  port: 13081

eureka:
  instance:
    hostname: eureka1.client.com
    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 10
  client:
    service-url:
      defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/

1.4 启动类ConfigServerApplication增加注解

package spring.cloud.demo.configserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }

}

至此,一个单机本地config-server就搭建完成

1.5 创建配置中心client:config-client

1.6 添加config-client的pom相关依赖

<dependency>
  <groupId>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值