Spring Cloud配置中心高可用搭建

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

本文通过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
相关文章
|
10月前
|
JSON Java Nacos
SpringCloud 应用 Nacos 配置中心注解
在 Spring Cloud 应用中可以非常低成本地集成 Nacos 实现配置动态刷新,在应用程序代码中通过 Spring 官方的注解 @Value 和 @ConfigurationProperties,引用 Spring enviroment 上下文中的属性值,这种用法的最大优点是无代码层面侵入性,但也存在诸多限制,为了解决问题,提升应用接入 Nacos 配置中心的易用性,Spring Cloud Alibaba 发布一套全新的 Nacos 配置中心的注解。
918 153
|
8月前
|
Cloud Native Java Nacos
springcloud/springboot集成NACOS 做注册和配置中心以及nacos源码分析
通过本文,我们详细介绍了如何在 Spring Cloud 和 Spring Boot 中集成 Nacos 进行服务注册和配置管理,并对 Nacos 的源码进行了初步分析。Nacos 作为一个强大的服务注册和配置管理平台,为微服务架构提供
3052 14
|
NoSQL Java Nacos
SpringCloud集成Seata并使用Nacos做注册中心与配置中心
SpringCloud集成Seata并使用Nacos做注册中心与配置中心
822 3
|
负载均衡 Java Nacos
SpringCloud基础2——Nacos配置、Feign、Gateway
nacos配置管理、Feign远程调用、Gateway服务网关
SpringCloud基础2——Nacos配置、Feign、Gateway
|
Java 微服务 Spring
Spring Cloud全解析:配置中心之解决configserver单点问题
但是如果该configserver挂掉了,那就无法获取最新的配置了,微服务就出现了configserver的单点问题,那么如何避免configserver单点呢?
187 1
|
12月前
|
负载均衡 Java API
【Spring Cloud生态】Spring Cloud Gateway基本配置
【Spring Cloud生态】Spring Cloud Gateway基本配置
858 0
|
运维 Java Nacos
Spring Cloud应用框架:Nacos作为服务注册中心和配置中心
Spring Cloud应用框架:Nacos作为服务注册中心和配置中心
|
Java Spring
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
344 3
|
Java 数据库连接 Nacos
SpringCloud微服务配置管理、配置热更新
SpringCloud微服务配置管理、配置热更新
511 0
|
消息中间件 Java Nacos
通用快照方案问题之通过Spring Cloud实现配置的自动更新如何解决
通用快照方案问题之通过Spring Cloud实现配置的自动更新如何解决
169 0