Spring Cloud 配置中心高可用搭建

简介: 本文通过config server连接git仓库来实现配置中心,除了git还可以使用svn或者系统本地目录都行。引入依赖 org.springframework.cloud spring-cloud-config-server org.springframework.cloud spring-cloud-starter-eureka spring-cloud-config-server这个就是配置中心server的依赖。

本文通过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 最强技术教程

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

image
相关文章
|
3月前
|
Java Nacos Maven
从零搭建微服务架构:Spring Boot与Nacos完美整合
从零搭建微服务架构:Spring Boot与Nacos完美整合
193 0
|
9月前
|
Java Spring
Spring Cloud:使用Eureka集群搭建高可用服务注册中心
Spring Cloud:使用Eureka集群搭建高可用服务注册中心
|
11月前
|
存储 前端开发 Java
《微服务实战》 第十一章 Spring Cloud Alibaba nacos配置中心
《微服务实战》 第十一章 Spring Cloud Alibaba nacos配置中心
168 0
|
关系型数据库 MySQL Java
Spring Cloud Alibaba 微服务工具集之Nacos解决服务注册中心和统一配置中心
🍅程序员小王的博客:程序员小王的博客 🍅 欢迎点赞 👍 收藏 ⭐留言 📝 🍅 如有编辑错误联系作者,如果有比较好的文章欢迎分享给我,我会取其精华去其糟粕 🍅java自学的学习路线:java自学的学习路线
316 0
Spring Cloud Alibaba 微服务工具集之Nacos解决服务注册中心和统一配置中心
|
Java Nacos 微服务
分布式组件:Spring Cloud Alibaba——Nacos配置中心
分布式组件:Spring Cloud Alibaba——Nacos配置中心
280 0
|
存储 前端开发 Java
3.5 Spring Cloud 使用 Nacos 作为微服务统一配置中心| 学习笔记
快速学习 3.5 Spring Cloud 使用 Nacos 作为微服务统一配置中心。
310 0
3.5 Spring Cloud 使用 Nacos 作为微服务统一配置中心| 学习笔记
|
负载均衡 Java 测试技术
3.4 Spring Cloud 客户端 Feigh 集成 Nacos 中心| 学习笔记
快速学习 3.4 Spring Cloud 客户端 Feigh 集成 Nacos 中心。
105 0
|
存储 Java 文件存储
微服务学习笔记九 Spring Cloud Config本地配置中心
微服务学习笔记九 Spring Cloud Config本地配置中心
450 0
|
前端开发 Java 测试技术
Spring Cloud - 分布式配置中心客户端
Spring Cloud - 分布式配置中心客户端
|
Java 微服务 Spring
Spring Cloud构建微服务架构(六)高可用服务注册中心
Spring Cloud构建微服务架构(六)高可用服务注册中心
119 0
Spring Cloud构建微服务架构(六)高可用服务注册中心