前言
本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3
本文基于前面的文章eureka-server的实现。
参考
概述
在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,所以需要分布式配置中心组件。Spring Cloud Config为分布式系统中的外部化配置提供服务器和客户端支持。
本篇涉及项目的结构为一个Config Server单机模式和适用于链接Git仓库,一个Config Client通过server来展示配置文件数据。 同时使用两个bus来模拟配置文件刷新。
创建config-server工程
Config-server功能
- 用于外部配置的http,基于资源的api
- 加密和解密属性
- 使用可嵌入spring boot的应用程序
1.1 创建配置中心server:config-server
1.2 添加config-server的pom.xml相关依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
1.3 添加config-server的application添加配置信息
#加载本地文件配置
spring:
application:
name: config-server
profiles:
active: native #加载本地配置
cloud:
config:
server:
native: #加载本地目录文件
search-locations: /Users/xxxx/config-server
server:
port: 13081
eureka:
instance:
hostname: eureka1.client.com
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 10
client:
service-url:
defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/
1.4 启动类ConfigServerApplication增加注解
package spring.cloud.demo.configserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
至此,一个单机本地config-server就搭建完成
1.5 创建配置中心client:config-client
1.6 添加config-client的pom相关依赖
<dependency>
<groupId>