Spring Cloud Zookeeper 分布式服务注册中心搭建可参考:
33 Spring Boot 2.2 集成 Spring Cloud Zookeeper - 分布式服务注册中心 — 2020-02-23
Spring Cloud Zookeeper 分布式服务调用可参考:
34 Spring Boot 2.2 集成 Spring Cloud Zookeeper - Ribbon 分布式服务消费者 — 2020-02-25
35 Spring Boot 2.2 集成 Spring Cloud Zookeeper - Feign 分布式服务消费者 — 2020-02-25
本文将介绍 Spring Cloud Zookeeper Config 分布式配置中心的搭建教程
Spring Cloud Config 分为 服务端和客户端,服务端作为配置中心,客户端远程调用获取服务端配置信息
./cloud-zookeeper-config-server/pom.xml
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-zookeeper-allartifactId>
<exclusions>
<exclusion>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>${zookeeper.version}version>
<exclusions>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-config-serverartifactId>
dependency>
其中 ${zookeeper.version}
的版本为 3.4.12
(不要随意改版本号,会有兼容性问题)
注意: SpringBoot 的版本需要在 2.2及以上
bootstrap.yml
./cloud-zookeeper-config-server/src/main/resources/bootstrap.yml
## Application bootstrap config
## spring config
spring:
cloud:
zookeeper:
connect-string: 172.16.140.10:2181
config:
server:
git:
uri: https://gitee.com/flying9001/springCloudConfigDemo
search-paths: config
username:
password:
default-label: master
其中 spring.cloud.config.server.git.uri
为 Git 仓库地址
spring.cloud.config.server.git.search-paths
为仓库中配置文件的目录
spring.cloud.config.server.git.username
为 Git 仓库用户名
spring.cloud.config.server.git.password
为 Git 仓库密码
若仓库权限为公开的(public
),则不需要用户名和密码
default-label
: 默认读取 Git 仓库的分支
application.yml
./cloud-zookeeper-config-server/src/main/resources/application.yml
## Application config
## Server
server:
port: 8103
## Spring config
spring:
application:
name: cloud-zookeeper-config-server
SpringBoot 启动类
./cloud-zookeeper-config-server/src/main/java/com/ljq/demo/springboot/cloud/zookeeper/config/server/CloudZookeeperConfigServerApplication.java
package com.ljq.demo.springboot.cloud.zookeeper.config.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
/**
* @author junqiang.lu
*/
@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class CloudZookeeperConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(CloudZookeeperConfigServerApplication.class, args);
}
}
@EnableConfigServer
表明启用 Spring Cloud Config 服务端
@EnableDiscoveryClient
用于向 Spring Cloud 注册中心注册服务
访问地址与配置资源的关系如下:
{application}
: 表示配置文件名
{profile}
: 表示文件的后缀(非拓展名),类似 Spring Boot 环境配置,application-dev.yml
,其中 dev
即 profile
{label}
: 表示 Git 的分支
可以访问的方式:
/{application}/{profile}(/{label})
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.yml
/{label}/{application}-{profile}.properties
请求地址:
GET http://127.0.0.1:8103/config/application.yml
返回参数:
{
"name": "config",
"profiles": [
"application.yml"
],
"label": null,
"version": "a111f963aec3a4be465e9d0e3d53de2a554cbcb7",
"state": null,
"propertySources": [
{
"name": "https://gitee.com/flying9001/springCloudConfigDemo/config/application.yml",
"source": {
"applicationName": "spring-cloud-config-demo-dev"
}
}
]
}
application.yml
为主配置文件,返回的结果中包含文件的名称、路径以及配置内容等信息
请求参数:
GET http://127.0.0.1:8103/application-dev.yml
返回参数:
address:
area: Pudong
city: Shanghai
country: China
detail: 外滩 18 号
province: Shanghai
applicationName: spring-cloud-config-demo-dev
fileName: '"application-dev.properties"'
注意: 如果有同名的 properties
和 yml
配置文件存在,则只能访问到 properties
中的配置信息
请求参数:
GET http://127.0.0.1:8103/application-test.properties
返回参数:
address.area: Pudong
address.city: Beijing
address.country: China
address.detail: 外滩 20 号
address.province: Beijing
applicationName: spring-cloud-config-demo-dev
fileName: "application-test.properties"
注意: 无论配置中心的源配置文件的后缀为 properties
还是 yml
查询结果展示形式以请求地址中的后缀为准
即: 源配置文件为 application-test.yml
,但是访问地址中为 application-test.ptoperties
,则查询到的结果会以 properties
的形式展示,数据还是原来的配置,只是展示效果不同, Spring Cloud Config 在这个过程中自动进行格式转换
请求参数:
GET http://127.0.0.1:8103/master/application-dev.yml
返回参数:
address:
area: Pudong
city: Shanghai
country: China
detail: 外滩 18 号
province: Shanghai
applicationName: spring-cloud-config-demo-dev
fileName: '"application-dev.properties"'
如果 Git 库中只有一个 master
分支,且在 Spring 配置文件中指定默认分支,则可以在 URL 中省略分支选项
./cloud-zookeeper-config-client/pom.xml
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-zookeeper-allartifactId>
<exclusions>
<exclusion>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>${zookeeper.version}version>
<exclusions>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-config-clientartifactId>
dependency>
其中 ${zookeeper.version}
的版本为 3.4.12
(不要随意改版本号,会有兼容性问题)
注意: SpringBoot 的版本需要在 2.2及以上
bootstrap.yml
./cloud-zookeeper-config-client/src/main/resources/bootstrap.yml
## Application bootstrap config
## spring config
spring:
main:
allow-bean-definition-overriding: true
application:
name: cloud-zookeeper-config-client
cloud:
zookeeper:
connect-string: 172.16.140.10:2181
## 将当前应用注册到 Zookeeper 中,其默认值为 true,由于配置了 'spring.cloud.config' 属性,因此需要手动设置为 true
discovery:
register: true
config:
## 指定配置中心文件的{profile}
profile: dev
## 指定配置中心文件的{label}
label: master
discovery:
## 发现 Cloud Config 服务
enabled: true
## 指定配置服务的服务 id
service-id: cloud-zookeeper-config-server
application.yml
./cloud-zookeeper-config-client/src/main/resources/application.yml
## Application config
## Server
server:
port: 8104
./cloud-zookeeper-config-client/src/main/java/com/ljq/demo/springboot/cloud/zookeeper/config/client/controller/CloudZookeeperConfigClientController.java
package com.ljq.demo.springboot.cloud.zookeeper.config.client.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
/**
* @Description: Spring Cloud Zookeeper 分布式配置客户端
* @Author: junqiang.lu
* @Date: 2020/2/26
*/
@RestController
@RequestMapping("/api/cloud/zookeeper/config")
public class CloudZookeeperConfigClientController {
@Value("${spring.application.name: springCloudZookeeperConfigClientDefault}")
private String springApplicationName;
@Value("${applicationName: applicationNameDefault}")
private String applicationName;
@Value("${server.port: 8080}")
private String serverPort;
@Value("${address.detail: ShanghaiDefault}")
private String detailAddress;
@Value("${fileName: fileNameDefault}")
private String fileName;
@RequestMapping(value = "/test", method = {RequestMethod.GET, RequestMethod.POST},
produces = {MediaType.APPLICATION_JSON_VALUE})
public String readConfig() {
StringBuilder configBuilder = new StringBuilder();
configBuilder.append("springApplicationName: ").append(this.springApplicationName).append(",\n");
configBuilder.append("applicationName: ").append(this.applicationName).append(",\n");
configBuilder.append("serverPort: ").append(this.serverPort).append(",\n");
configBuilder.append("detailAddress: ").append(this.detailAddress).append(",\n");
configBuilder.append("fileName:").append(fileName).append("\n");
System.out.println(new Date() + "-" + configBuilder.toString());
return configBuilder.toString();
}
}
Spring Boot 提供通过 @Value
注解来读取参数,也可以使用 @Configuration
注解专门写一个配置类
./cloud-zookeeper-config-client/src/main/java/com/ljq/demo/springboot/cloud/zookeeper/config/client/CloudZookeeperConfigClientApplication.java
package com.ljq.demo.springboot.cloud.zookeeper.config.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author junqiang.lu
*/
@EnableDiscoveryClient
@SpringBootApplication
public class CloudZookeeperConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(CloudZookeeperConfigClientApplication.class, args);
}
}
@EnableDiscoveryClient
: 用于发现 Spring Cloud 注册中心的服务
请求地址:
GET http://127.0.0.1:8104/api/cloud/zookeeper/config/test
返回参数:
springApplicationName: cloud-zookeeper-config-client,
applicationName: spring-cloud-config-demo-dev,
serverPort: 8104,
detailAddress: 外滩 18 号,
fileName: "application-dev.properties"
至此,一套简易的 Spring Cloud Config 分布式配置中心已经搭建完成。
(PS: 预知 Spring Cloud Zookeeper 项目搭建过程中容易遇到的常见问题,且听下回分解)
官方文档 Spring Cloud Zookeeper
Zookeeper 完整系列教程 Spring-Cloud-Zookeeper-Based-Demo
Gtihub 源码地址 : https://github.com/Flying9001/springBootDemo
个人公众号:404Code,分享半个互联网人的技术与思考,感兴趣的可以关注.