简介
所谓的分布式配置中心,即将项目的配置文件交由git管理,然后项目能够读取git上的配置文件。
准备工作
先上传三个配置文件到git,怎么上传文件到git可参考这里
搭建配置中心server
在原有基础上新建模块
pom文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.klose</groupId>
<artifactId>springcloud-study</artifactId>
<version>0.0.1-SNAPSHOT</version> <!-- lookup parent from repository -->
</parent>
<groupId>com.klose</groupId>
<artifactId>a-config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>a-config-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置文件:
#项目部署的端口
server:
port: 8001
spring:
#当前项目的名字
application:
name: spring-cloud-config-server
#配置中心的相关配置
cloud:
config:
server:
git:
# 配置git仓库的URL地址
uri: https://github.com/kloseS/testConfig
# git仓库地址下的文件夹,可以配置多个,用“,”分割。
search-paths: /
#公共仓库可不用配置帐号密码
username:
password:
启动类:
使用@EnableConfigServer开启配置中心。
package com.klose.aconfigserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class AConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(AConfigServerApplication.class, args);
}
}
启动配置中心:
访问http://localhost:8001/application/dev
访问http://localhost:8001/application-dev.properties直接获取配置文件内容
git上配置文件内容 :
搜集的资料:SpringBoot的核心思想就是约定优于配置,所以仓库中的配置文件会被转换成web接口,访问可以参照以下的规则:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
搭建Client端
client的作用好比在项目中读取
pom文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.klose</groupId>
<artifactId>springcloud-study</artifactId>
<version>0.0.1-SNAPSHOT</version><!-- lookup parent from repository -->
</parent>
<groupId>com.klose</groupId>
<artifactId>a-config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>a-config-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置文件:
application.yml
spring:
application:
name: spring-cloud-config-client
server:
port: 8002
bootstrap.yml
spring:
cloud:
config:
name: application
profile: dev
uri: http://localhost:8001/
label: master
启动类:
@SpringBootApplication
public class AConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(AConfigClientApplication.class, args);
}
}
测试获取配置信息接口:
@RestController
public class testController {
@Value("${name}")
private String name;
@RequestMapping("/getName")
public String getName(){
return name;
}
}
测试:
访问http://localhost:8002/getName
经测试修改git的配置 不重启服务发现并不能读取到更新的值。
高可用集群搭建
基本思路:使用注册中心,启动多个配置中心服务,客户端读取配置信息,达到负载均衡的效果。
搭建注册中心
pom文件:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
其他配置与第一节无区别。
改造配置中心
就是在上一节基础上将服务注册到注册中心
pom文件:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
</dependencies>
配置文件:
#项目部署的端口
server:
port: 19011
#配置注册中心的位置,并把自己注册进去
eureka:
client:
serviceUrl:
defaultZone: http://localhost:18011/eureka/
spring:
#当前项目的名字
application:
name: spring-cloud-config-server
#配置中心的相关配置
cloud:
config:
server:
git:
# 配置git仓库的URL地址
uri: https://github.com/kloseS/testConfig
# git仓库地址下的文件夹,可以配置多个,用“,”分割。
search-paths: /
#公共仓库可不用配置帐号密码
username:
password:
启动类:
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class EurekaServerConfigApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerConfigApplication.class, args);
}
}
改造客户端
与改造配置中心同理
pom文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.klose</groupId>
<artifactId>springcloud-study</artifactId>
<version>0.0.1-SNAPSHOT</version> <!-- lookup parent from repository -->
</parent>
<groupId>com.klose</groupId>
<artifactId>eureka-client-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-client-config</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置文件:
application.yml
spring:
application:
name: spring-cloud-config-client
server:
port: 19013
bootstrap.yml
spring:
cloud:
config:
name: application
profile: dev
label: master
discovery:
enabled: true
service-id: spring-cloud-config-server
eureka:
client:
serviceUrl:
defaultZone: http://localhost:18011/eureka/
启动类:
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientConfigApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientConfigApplication.class, args);
}
}
。。。。。。
源码下载地址:
https://github.com/kloseS/springcloudstudy