SpringCloud配置中心高可用搭建

Easter79
• 阅读 487

本文通过config server连接git仓库来实现配置中心,除了git还可以使用svn或者系统本地目录都行。

引入依赖

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

spring-cloud-config-server这个就是配置中心server的依赖。

配置中心做到高可用本身也需要向注册中心注册自己的实例,所以需求引用spring-cloud-starter-eureka依赖。

添加启动类,开启Config Server功能

@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class ConfigApplication {

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

}

@EnableConfigServer:即开启配置服务器的功能。

@EnableDiscoveryClient:开启自动注册客户端,默认情况下,ServiceRegistry实现将自动注册正在运行的服务。如注册中心使用是Eureka,这里也可以使用的@EnableEurekaClient注解。

添加Config配置

spring: 
  application:
    name: config-center
  profiles:
    active: config-center1
  cloud: 
    config:
      server:
        git:
          uri: ${git.uri}
          searchPaths: ${git.searchPaths}
          username: ${git.username}
          password: ${git.password}
          basedir: ${git.basedir}
          clone-on-start: true
          force-pull: true
          
eureka:
  instance: 
    prefer-ip-address: true  
    instance-id: ${spring.cloud.client.ipAddress}:${server.port}
    lease-expiration-duration-in-seconds: ${lease-expiration-duration-in-seconds}
    lease-renewal-interval-in-seconds: ${lease-renewal-interval-in-seconds}
  client:
    serviceUrl:
      defaultZone: ${register-center.urls}
    
---
spring:
  profiles: config-center1
  
server: 
  port: ${config-center1.server.port}

---
spring: 
  profiles: config-center2
  
server: 
  port: ${config-center2.server.port}

这里配置了两台Config Server,都注册到了两台注册中心上。

Maven filter配置

#git
git.uri=http://gitlab.example.com/test/config.git
git.username=root
git.password=root
git.searchPaths=config-center
git.basedir=f:/config/config-center/git

Spring Cloud Git配置详解

**spring.cloud.config.server.git.uri**:git仓库地址。

**spring.cloud.config.server.git.searchPaths**:git仓库搜索目录。

**spring.cloud.config.server.git.username**:连接git的用户名。

**spring.cloud.config.server.git.password**:连接git的用户名密码。

**spring.cloud.config.server.git.basedir**:配置中心在本地缓存配置的目录。

**spring.cloud.config.server.git.clone-on-start**:配置为true表示启动时就克隆配置缓存到本地。

**spring.cloud.config.server.git.force-pull**:配置为true表示如果本地副本是脏的,将使Spring Cloud Config Server强制从远程存储库拉取配置。

启动配置中心

分别启动以下配置中心,使用不同的Profile指定端口。

spring-boot:run -Drun.profiles=config-center1 -P dev
spring-boot:run -Drun.profiles=config-center2 -P dev

推荐:Spring Boot & Cloud 最强技术教程

扫描关注我们的微信公众号,干货每天更新。

SpringCloud配置中心高可用搭建

点赞
收藏
评论区
推荐文章
Easter79 Easter79
2年前
spring集成kafka
1、引入依赖jar包<dependency<groupIdorg.springframework.kafka</groupId<artifactIdspringkafka</artifactId</dependency2、配置kafka信息
Stella981 Stella981
2年前
Spring Cloud Config
nvironmentRepository的默认实现使用Git后端,这对于管理升级和物理环境以及审核更改非常方便。要更改存储库的位置,可以在ConfigServer中设置“spring.cloud.config.server.git.uri”配置属性(例如application.yml)。如果您使用file:前缀进行设置,则应从本地存储库中工作,
Wesley13 Wesley13
2年前
GIT命令大全
Git命令大全Git最小配置某账号下所有的Git仓库都有效gitconfigglobaluser.name'您的名称'gitconfigglobaluser.email'您的Email'只对当前Git仓库有效gitconf
Stella981 Stella981
2年前
Consul Config 使用Git做版本控制的实现
SpringCloudConfig原理!image(http://b.pigx.top/springcloudconfig.png)我们通过git把配置文件推送到远程仓库做版本控制,当版本发生变化的时候,远程仓库通过webhook机制推送消息给ConfigServer,ConfigS
Stella981 Stella981
2年前
Git 手册
gitinit                                                 初始化本地git仓库(创建新仓库)gitconfigglobaluser.name"xxx"                      配置用户名gitconfigglobaluser.emai
Stella981 Stella981
2年前
Spring cloud学习总结
1注册中心学习 maven配置<dependency<groupIdorg.springframework.cloud</groupId<artifactIdspringcloudstarternetflixeurekaclient</artifactI
Stella981 Stella981
2年前
Spring cloud nacos config 配置中心例子
Springcloud集成nacosconfig配置中心的例子,自定义dataIdgroupIdnamespace 使用的依赖<dependencies<dependency<groupIdorg.springframework.boot</gr
Easter79 Easter79
2年前
SpringCloud配置中心——ConfigServer搭建
_在实际应用中,一个系统的每个微服务都会有相同的配置,如数据库配置信息等等。为了将每个微服务的公共配置可以抽取出来。SpringCloud提供了Config配置中心的配置,作为配置中心(ConfigServer),提供给微服务(ConfigClient)读取并且加载配置。使用git仓库存放配置文件,SpringCloudConfig读取到之后会自
Easter79 Easter79
2年前
SpringCloud Config(配置中心)实现配置自动刷新(十六)
一、实现原理1、ConfigServer(配置中心服务端)从远端git拉取配置文件并在本地git一份,ConfigClient(微服务)从ConfigServer端获取自己对应配置文件;2、当远端git仓库配置文件发生改变,ConfigServer如何通知到ConfigClient端,即ConfigClient如何感知到配置发生更新?S
Easter79 Easter79
2年前
SpringCloud动态刷新配置信息
有时候在配置中心有些参数是需要修改的,这时候如何不重启而达到实时生效的效果呢?添加依赖<dependencies...<dependency<groupIdorg.springframework.boot</groupId<artifactIdspringboot
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k