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

spring cloud config 简介

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

spring cloud config 分布式部署

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

  •     将分布式系统的配置文件集中管理到文件系统上
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上获取。spring

   目录config-repo用于存放全部的配置文件bootstrap

  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.propertiesapp

其中,经过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文件中须要添加以下配置this

<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;
    }

}
相关文章
相关标签/搜索