SpringCloud配置中心集群搭建

时间:2022-05-08
本文章向大家介绍SpringCloud配置中心集群搭建,主要内容包括一、简介、二、构建config-server、三、构建config-client、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

一、简介

为了方便配置文件统一管理,需要分布式配置中心组件,在springcloud中,使用配置中心。

可以放在本地,也可以放在远程git或者svn。

服务数量多的情况下,可以创建配置集群。

二、构建config-server

pom就不粘贴了,可在网上随便一搜。

配置文件:

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/unclehh/springcloudConfig.git
          searchPaths: config-1,config-2,config-3
#          username: username
#          password: password
server:
  port: 2001
security:
  user:
    name: hjb
    password: 'hjb123'
eureka:
  instance:
    prefer-ip-address: true
  client:
    serviceUrl:
      defaultZone: http://peer1:1111/eureka/

如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写

然后加上注解

@EnableConfigServer
@SpringBootApplication
@EnableEurekaClient
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

三、构建config-client

pom类似,主要是配置文件bootstrap.properties

spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
server.port=2002

加上注解

@SpringBootApplication
@EnableEurekaClient

这样子就可以根据serviceId配置多个配置中心。

最后启动,可以在注册中心看到配置中心集群。