SpringCloud 进阶之分布式配置中心(SpringCloud Config)
1. SpringCloud Config
- SpringCLoud Config 为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用
的所有环境提供了一个中心化的外部配置; - SpringCloud Config 分为服务端和客户端:
- 服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,
加密/解密信息等访问接口; - 客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和
加载配置信息;
- 服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,
- 配置服务器默认采用Git来存储配置信息;
1.1 SpringCloud Config服务端配置
GitHub 准备工作
// GitHub 新建一个名为 microservicecloudd-config 的新Repository
// 获得SSH协议的git地址: git@github.com:Noodlescn/microservicecloud-config.git
// 本地硬盘目录上,新建Git仓库,并clone
git clone git@github.com:Noodlescn/microservicecloud-config.git
// 本地Git仓库,microservicecloud-config 新建 application.yml
spring:
profiles:
active:
- dev
---
spring:
profiles: dev # 开发环境
application:
name: microservicecloud-config-noodles-dev
---
spring:
profiles: test # 测试环境
application:
name: microservicecloud-config-noodles-test
# 文件保存为 UTF-8 格式
// 将application.yml推送到github上
新建 microservicecloud-config-3344(Cloud的配置中心模块)
// pom.xml
<!-- springCloud Config -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- 避免Config的Git插件报错 -->
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>4.10.0.201712302008-r</version>
</dependency>
// application.yml
server:
port: 3344
spring:
application:
name: microservicecloud-config
cloud:
config:
server:
git:
uri: git@github.com:Noodlescn/microservicecloud-config.git # GitHub上面的git仓库名字
// 主启动类
@SpringBootApplication
@EnableConfigServer
public class Config_3344_StartSpringCloudApp {
public static void main(String[] args) {
SpringApplication.run(Config_3344_StartSpringCloudApp.class, args);
}
}
// 修改hosts文件,增加映射
// 127.0.0.1 config-3344.com
// 测试启动微服务 3344
// http://config-3344.com:3344/application-dev.yml
// http://config-3344.com:3344/application-test.yml
1.2 SpringCloud Config客户端配置
GitHub 准备工作
// 本地 microservicecloud-config 路径下新建文件 microservicecloud-config-client.yml
spring:
profiles:
active
- dev
---
server:
port: 8201
spring:
profiles: dev
application:
name: microservicecloud-config-client
eureka:
client:
service-url:
defaultZone: http://eureka-dev.com:7001/eureka/
---
server:
port: 8202
spring:
profiles: test
application:
name: microservicecloud-config-client
eureka:
client:
service-url:
defaultZone: http://eureka-test.com:7001/eureka/
// 提交到GitHub
新建 microservicecloud-config-client-3355
// pom.xml
<!-- springCloud Config 客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
// bootstrap.yml(是系统级配置文件,优先级最高)
spring:
cloud:
config:
name: microservicecloud-config-client # 需要从github上读取的资源名称,注意没有yml后缀名
profile: dev # 本次访问的配置项
label: master
uri: http://config-3344.com:3344
# 本微服务启动后,先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址
// application.yml(是用户级的资源配置项)
spring:
application:
name: microservicecloud-config-client
// 修改hosts文件: 127.0.0.1 client-config.com
// 新建rest类,验证是否能从GitHub上读取配置
@RestController
public class ConfigClientRest {
@Value("${spring.application.name}$")
private String applicationName;
@Value("${eureka.client.service-url.defaultZone}$")
private String eurekaServers;
@Value("${server.port}$")
private String port;
@RequestMapping("/config")
private String getConfig() {
String str = "applicationName:"+applicationName+"\t eurekaServers:"+eurekaServers+"\t port:"+port;
System.out.println("********str:" + str);
return str;
}
}
// 主启动类ConfigClient_3355_StartSpringCloudApp
@SpringBootApplication
public class ConfigClient_3355_StartSpringCloudApp {
public static void main(String[] args) {
SpringApplication.run(ConfigClient_3355_StartSpringCloudApp.class, args);
}
}
// 测试访问:
// http://client-config.com:8201/config
2. SpringCloud Config 配置实战
GitHub 准备工作
// 本地microservicecloud-config 下新建:
// microservicecloud-config-eureka-client.yml
spring:
profiles:
active:
- dev
---
server:
port: 7001
spring:
profiles: dev
application:
name: microservicecloud-config-eureka-client
eureka:
instance:
hostname: eureka7001.com
client:
register-with-eureka: false # 当前的eureka-server自己不注册进服务列表中
fetch-registry: false # 不通过eureka获取注册信息
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
---
server:
port: 7001 # 注册中心占用7001端口
spring:
profiles: test
application:
name: microservicecloud-config-eureka-client
eureka:
instance:
hostname: eureka7001.com
client:
register-with-eureka: false # 当前的eureka-server自己不注册进服务列表中
fetch-registry: false # 不通过eureka获取注册信息
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
// 本地microservicecloud-config 下新建:
// microservicecloud-config-dept-client.yml
spring:
profiles:
active:
- dev
---
server:
port: 8001
spring:
profiles: dev
application:
name: microservicecloud-config-dept-client
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/cloudDB01
username: root
password: root
dbcp2:
min-idle: 5
initial-size: 5
max-total: 5
max-wait-millis: 200
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml
type-aliases-package: com.noodles.springcloud.entities
mapper-locations:
- classpath:mybatis/mapper/**/*.xml
eureka:
client: # 客户端注册进eureka服务列表内
service-url:
defaultZone: http://eureka7001.com:7001/eureka
instance:
instance-id: dept-8001.com
prefer-ip-address: true
info:
app.name: noodles-microservicecloud-springcloudconfig01
company.name: www.google.com
build.artifactId: $project.artifactId$
build.version: $project.version$
---
server:
port: 8001
spring:
profiles: test
application:
name: microservicecloud-config-dept-client
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/cloudDB02
username: root
password: root
dbcp2:
min-idle: 5
initial-size: 5
max-total: 5
max-wait-millis: 200
mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml
type-aliases-package: com.noodles.springcloud.entities
mapper-locations:
- classpath:mybatis/mapper/**/*.xml
eureka:
client: # 客户端注册进eureka服务列表内
service-url:
defaultZone: http://eureka7001.com:7001/eureka
instance:
instance-id: dept-8001.com
prefer-ip-address: true
info:
app.name: noodles-microservicecloud-springcloudconfig02
company.name: www.google.com
build.artifactId: $project.artifactId$
build.version: $project.version$
// 上传GitHub
新建 microservicecloud-config-eureka-client-7001 工程 (Config版的eureka服务端)
// pom.xml(参照microservicecloud-eureka-7001)
<!-- springCloud Config -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
// bootstrap.yml
spring:
cloud:
config:
name: microservicecloud-config-eureka-client # 需要从GitHub上读取的资源名称,没有yml后缀名
profile: dev
label: master
uri: http://config-3344.com:3344 # SpringCloudConfig 获取的服务地址
// application.yml
spring:
application:
name: microservicecloud-config-eureka-client
// 主启动类
@SpringBootApplication
@EnableEurekaServer // EurekaServer 服务器端启动类,接收其他微服务注册进来
public class Config_Git_EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(Config_Git_EurekaServerApplication.class, args);
}
}
//测试
// 启动 microservicecloud-config-3344
// 启动 microservicecloud-config-eureka-client-7001
// 访问 http://eureka7001.com:7001/
新建 microservicecloud-config-dept-client-8001(Config版的dept微服务)
// pom.xml(参照8001)
<!-- SpringCloud 配置 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
// bootstrap.yml
spring:
cloud:
config:
name: microservicecloud-config-dept-client # 需要从GitHub上读取的资源名称
# profile 配置什么就读取什么,配置 dev 或 test
profile: test
label: master
uri: http://config-3344.com:3344 # SpringCloudConfig 获取的服务地址
// application.yml
spring:
application:
name: microservicecloud-config-dept-client
// 主启动类
@SpringBootApplication
@EnableEurekaClient // 本服务启动后,自动注册进eureka服务中
@EnableDiscoveryClient // 服务发现
public class DeptProvider8001_App {
public static void main(String[] args) {
SpringApplication.run(DeptProvider8001_App.class, args);
}
}
// 测试
// 默认访问 test 配置: http://localhost:8001/dept/list
// 更换 dev 配置: http://localhost:8001/dept/list
参考资料: