Spring Cloud学习:06高可用分布式配置中心(Config Cluster)
配置中心从远程git仓库读取配置信息,配置客户端从配置中心读取信息,当服务实例数变多时,有必要进行集群部署,将配置中心也作为一个微服务,注册到服务注册中心,从而达到高可用的目的。
1 创建注册中心(Eureka Server)1.1基于之前工程,创建新模块eureka-server,用作服务注册中心,选择Spring Initializr->Cloud Discovery-> Eureka Server,并添加以下依赖:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
1.2 添加服务注册中心配置:
server:
port: 8889
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false # 是否注册到eureka
fetch-registry: false # 是否从eureka获取注册信息
serviceUrl:
# eureka服务器地址(注意:地址最后面的 /eureka/ 是固定值)
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
1.3 启动类添加@EnableEurekaServer注解
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
2 改造配置中心(Config Server)
2.1 修改config-server模块,pom文件添加spring-cloud-starter-eureka依赖:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
2.2 配置文件添加服务注册中心地址:http://localhost:8889/eureka/
server:
port: 8888
spring:
application:
name: config-server
# 配置中心
cloud:
config:
server:
git:
uri: https://github.com/laravelshao/spring-cloud-config-repo # cloud配置仓库地址
search-paths: spring-cloud-learning # 配置仓库路径
username: # 公开仓库可不填写
password: # 公开仓库可不填写
label: master # 配置仓库分支
eureka:
client:
service-url:
defaultZone: http://localhost:8889/eureka/ # 配置eureka服务器地址
2.3 启动类添加@EnableEurekaClient注解
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
3 改造配置客户端(Config Client)
3.1 修改config-client模块,pom文件添加spring-cloud-starter-eureka依赖:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
3.2 配置文件bootstrap.yml中添加以下配置:
spring.cloud.config.discovery.enabled:配置是否从配置中心读取文件 spring.cloud.config.discovery.serviceId:配置中心服务id eureka.client.serviceUrl.defaultZone:配置eureka服务器地址server:
port: 8881
spring:
application:
name: config-client
cloud:
config:
uri: http://localhost:8888/ # 配置服务中心地址
label: master # 远程仓库分支
profile: dev # 指定环境
discovery:
enabled: true # 从配置中心读取文件
service-id: config-server # 配置中心服务id
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8889/eureka/ # 配置eureka服务器地址
3.3 依次启动eureka-server、config-server、config-client模块,访问http://localhost:8889/
访问http://localhost:8881/hi,结果:
说明从配置中心获取到配置数据。
本文源码下载地址:
https://github.com/laravelshao/spring-cloud-learning/tree/master/setion06-config-cluster
4 参考资料Spring Cloud Config
史上最简单的SpringCloud教程
文章来源:
Author:LaravelShao
link:https://my.oschina.net/LaravelShao/blog/1583369