spring cloud config-server 高可用配置中心

spring cloud config 简介

Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持。配置服务器为各应用的所有环境提供了一个中心化的外部配置。

spring cloud config 分布式部署

    部署分布式配置管理分为以下4部

  •     将分布式系统的配置文件集中管理到文件系统上
spring.application.name=config-server
# config server port
server.port=8887

# use file system backend
spring.profiles.active=native
# url of file system where configuration files stored
spring.cloud.config.server.native.searchLocations=file:root/config-repo

    Spring Cloud Config提供本地存储配置的方式。我们只需要设置属性spring.profiles.active=native,Config Server会默认从应用的src/main/resource目录下检索配置文件。也可以通过spring.cloud.config.server.native.searchLocations=file:root/config-repo性来指定配置文件的位置,这样的话就可以将所有的配置文件统一管理。当然也可以从git上获取。

   目录config-repo用于存放所有的配置文件

  1. 存放配置文件的目录结构为{label}\{application}-{profile}.properties
  2. {label}可以用于对应prod、test、uat、dev、release等等
  3. {application}为各个模块的名字,如log、order等
  4. {profile}为profile的名字
  •     对集中配置管理服务器进行配置(由于需要高可用那么只需要将config-server也注册为服务,这样所有客户端就能以服务的方式进行访问,然后只需要启动多个指向同一文件位置的config-server就可以)
spring.application.name=config-server
# config server port
server.port=8887

# use file system backend
spring.profiles.active=native
# url of file system where configuration files stored
spring.cloud.config.server.native.searchLocations=file:root/config-repo

# 配置服务注册中心
eureka.client.serviceUrl.defaultZone=http://test:111@localhost:8761/eureka/,http://test:111@1ocalhost:8762/eureka/
# 在eurake注册的名称是IP地址+端口
eureka.instance.preferIpAddress=true
eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}

将config-servefr注册服务该怎么做呢?很简单在对应的pom文件中添加如下配置:

<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>
  •     对各环境(客户端)进行配置(bootstrap.properties)
spring.application.name=zuul
spring.cloud.config.profile=configInfo
spring.cloud.config.label=devs

eureka.client.serviceUrl.defaultZone=http://test:111@localhost:8761/eureka/,http://test:111@localhost:8762/eureka/

# 在eurake注册的名称是IP地址+端口
eureka.instance.preferIpAddress=true
eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}

spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
  • spring.application.name:对应前配置文件中的{application}部分
  • spring.cloud.config.profile:对应前配置文件中的{profile}部分
  • spring.cloud.config.label:对应前配置文件的config-repo下的目录

这里需要格外注意:上面这些属性必须配置在bootstrap.properties中,config部分内容才能被正确加载。因为config的相关配置会先于application.properties,而bootstrap.properties的加载也是先于application.properties

其中,通过eureka.client.serviceUrl.defaultZone参数指定服务注册中心,用于服务的注册与发现,再将spring.cloud.config.discovery.enabled参数设置为true,开启通过服务来访问Config Server的功能,最后利用spring.cloud.config.discovery.serviceId参数来指定Config Server注册的服务名。这里的spring.application.namespring.cloud.config.profile用来定位config-server的资源。

客户端对应的pom文件中需要添加如下配置

<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-config</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-eureka</artifactId>
	</dependency>
</dependencies>
  •     客户端取得配置文件并使用
spring.datasource.url=jdbc\:db2\://localhost\:50000/hrxtdev
spring.datasource.username=
spring.datasource.password=
spring.datasource.driver-class-name=com.ibm.db2.jcc.DB2Driver\
config.server.url=http://localhost:8080
server.port =8180
server.context-path =/log


@Component
@ConfigurationProperties(prefix = "config.server")
public class ConfigServerProperties{
    private String url;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

}

    

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值